From 477296ae4cd0da88b3e03f4d7b180dd0170f1e63 Mon Sep 17 00:00:00 2001 From: "ke.xiao" Date: Fri, 14 Apr 2023 18:13:38 +0800 Subject: [PATCH] 1. add private file --- package.json | 11 +++++++ src/cm/commands/add/add.ts | 33 +++++++++++++++++++ src/cm/commands/add/addParser.ts | 38 ++++++++++++++++++++++ src/cm/commands/index.ts | 1 + src/commands/addPrivateFile.ts | 55 ++++++++++++++++++++++++++++++++ src/commands/index.ts | 1 + src/plasticScm.ts | 3 +- 7 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 src/cm/commands/add/add.ts create mode 100644 src/cm/commands/add/addParser.ts create mode 100644 src/commands/addPrivateFile.ts diff --git a/package.json b/package.json index f6130cb..9670578 100644 --- a/package.json +++ b/package.json @@ -94,6 +94,12 @@ "category": "Plastic SCM", "title": "Open File", "icon": "$(go-to-file)" + }, + { + "command": "plastic-scm.addPrivateFile", + "category": "Plastic SCM", + "title": "Add Private File", + "icon": "$(add)" } ], "menus": { @@ -114,6 +120,11 @@ "command": "plastic-scm.openFile", "when": "scmProvider == plastic-scm && scmResourceGroup == status", "group": "inline@1" + }, + { + "command": "plastic-scm.addPrivateFile", + "when": "scmProvider == plastic-scm && scmResourceGroup == status", + "group": "inline@1" } ] } diff --git a/src/cm/commands/add/add.ts b/src/cm/commands/add/add.ts new file mode 100644 index 0000000..b4239f3 --- /dev/null +++ b/src/cm/commands/add/add.ts @@ -0,0 +1,33 @@ + +import { ICmParser, ICmResult, ICmShell } from "../../shell"; +import { AddParser } from "./addParser"; + +export class Add { + + public static async run( + shell: ICmShell, + paths: string[] | undefined + ): Promise { + + if (!paths) { + return; + } + + const args: string[] = [ + ...paths, + `--format='ADD {0}'`, + `--errorformat='ERR {0}'`, + ]; + + const parser: ICmParser = new AddParser(); + try { + const result: ICmResult = await shell.exec("add", args, parser); + if (!result.success || result.error) { + throw result.error; + } + return undefined; + } catch(e) { + console.log(e) + } + } +} \ No newline at end of file diff --git a/src/cm/commands/add/addParser.ts b/src/cm/commands/add/addParser.ts new file mode 100644 index 0000000..1a8e3aa --- /dev/null +++ b/src/cm/commands/add/addParser.ts @@ -0,0 +1,38 @@ + +import * as os from "os"; +import { ICmParser } from "../../shell"; + +export class AddParser implements ICmParser { + private readonly mOutputBuffer: string[] = []; + private readonly mErrorBuffer: string[] = []; + + readLineOut(line: string): void{ + this.mOutputBuffer.push(line); + } + readLineErr(line: string): void{ + this.mErrorBuffer.push(line); + } + + parse(): Promise{ + return new Promise((resolve, reject) => { + return Promise.all(this.mOutputBuffer); + }); + } + getError(): Error | undefined{ + var errMsg = this.mOutputBuffer.filter((line) => line.startsWith("ERR")); + + if(this.mErrorBuffer.length > 0){ + return new Error(this.mErrorBuffer.concat(errMsg).join(os.EOL)) + } + + if(errMsg.length > 0){ + return new Error(errMsg.join(os.EOL)) + } + + return undefined; + } + + getOutputLines(): string[]{ + return this.mOutputBuffer.concat(this.mErrorBuffer); + } +} \ No newline at end of file diff --git a/src/cm/commands/index.ts b/src/cm/commands/index.ts index faa7bb3..d58cc63 100644 --- a/src/cm/commands/index.ts +++ b/src/cm/commands/index.ts @@ -2,3 +2,4 @@ export { GetFile } from "./getFile/getFile"; export { GetWorkspaceFromPath } from "./getWorkspaceFromPath/getWorkspaceFromPath"; export { Status } from "./status/status"; export { Checkin } from "./checkin/checkin"; +export { Add } from "./add/add"; diff --git a/src/commands/addPrivateFile.ts b/src/commands/addPrivateFile.ts new file mode 100644 index 0000000..e2c8c57 --- /dev/null +++ b/src/commands/addPrivateFile.ts @@ -0,0 +1,55 @@ +import { commands, Disposable} from "vscode"; +import { + Add as CmAddCommand, +} from "../cm/commands"; +import { PlasticScm } from "../plasticScm"; +import { PlasticScmResource } from "../plasticScmResource"; +import { Workspace } from "../workspace"; +import { WorkspaceOperation } from "../workspaceOperations"; +import { ChangeType } from "../models"; + +export class AddPrivateFileCommand implements Disposable { + private readonly mPlasticScm: PlasticScm; + private readonly mDisposable?: Disposable; + + public constructor(plasticScm: PlasticScm) { + this.mPlasticScm = plasticScm; + this.mDisposable = commands.registerCommand( + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + "plastic-scm.addPrivateFile", args => execute(args, plasticScm)); + } + + public dispose(): void { + if (this.mDisposable) { + this.mDisposable.dispose(); + } + } +} + +async function execute(arg: any, mPlasticScm: PlasticScm) { + const workspace: Workspace | undefined = arg instanceof Workspace ? + arg as Workspace : + await mPlasticScm.promptUserToPickWorkspace(); + + if (!workspace) { + return; + } + + if (workspace.operations.isRunning(WorkspaceOperation.Checkin)) { + return; + } + + let resources: PlasticScmResource[]; + if (!(arg instanceof PlasticScmResource)) { + resources = arg.resourceStates; + }else { + resources = [arg]; + } + + const paths = resources.filter(r => r.type === ChangeType.Private).map(r => r.resourceUri.fsPath); + + if (paths.length === 0) { + return; + } + await CmAddCommand.run(workspace.shell, paths); +} \ No newline at end of file diff --git a/src/commands/index.ts b/src/commands/index.ts index 4df7dfc..b2f4f03 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -1 +1,2 @@ export * from "./checkin"; +export * from "./addPrivateFile"; diff --git a/src/plasticScm.ts b/src/plasticScm.ts index 2ce3dfe..4c4bf07 100644 --- a/src/plasticScm.ts +++ b/src/plasticScm.ts @@ -7,7 +7,7 @@ import { window as VsCodeWindow, workspace as VsCodeWorkspace, } from "vscode"; -import { CheckinCommand } from "./commands"; +import { CheckinCommand, AddPrivateFileCommand } from "./commands"; import { GetWorkspaceFromPath } from "./cm/commands"; import { IConfig } from "./config"; import { IWorkspaceInfo } from "./models"; @@ -90,6 +90,7 @@ export class PlasticScm implements Disposable { this.mDisposables.push(new RefreshCommand(this)); this.mDisposables.push(new OpenFileCommand(this)); this.mDisposables.push(new PlasticScmDecorations(this)); + this.mDisposables.push(new AddPrivateFileCommand(this)); } }