From 562889bf15816eecf5c1418cb9786102d63ee708 Mon Sep 17 00:00:00 2001 From: Gabriel Majeri Date: Mon, 23 Sep 2019 09:02:53 +0300 Subject: [PATCH] Add command for creating a new Cargo project --- package.json | 9 +++++- src/extension.ts | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 9c0e5856..d8c5f14f 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ ], "preview": true, "activationEvents": [ + "onCommand:cargo.new", "onLanguage:rust", "workspaceContains:Cargo.toml" ], @@ -83,6 +84,12 @@ } ], "commands": [ + { + "command": "cargo.new", + "title": "Create new Cargo project", + "description": "Use `cargo new` to create a new Rust project", + "category": "Rust" + }, { "command": "rls.update", "title": "Update the RLS", @@ -430,4 +437,4 @@ } } } -} \ No newline at end of file +} diff --git a/src/extension.ts b/src/extension.ts index d66a2912..938d509e 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -45,6 +45,81 @@ interface ProgressParams { export async function activate(context: ExtensionContext) { context.subscriptions.push(configureLanguage()); + const createNewProject = commands.registerCommand('cargo.new', async () => { + const projectName = await window.showInputBox({ + prompt: 'Enter the name of the new project', + placeHolder: 'Project name', + value: 'example', + validateInput: (value: string) => { + if (!value) { + return 'Crate name cannot be empty'; + } + + return; + }, + }); + if (!projectName) { + return; + } + + // Try to use the currently open folder if available. + let rootPath = process.cwd(); + if (workspace.workspaceFolders && workspace.workspaceFolders.length > 0) { + const rootUri = workspace.workspaceFolders[0].uri; + if (rootUri.scheme === 'file') { + rootPath = rootUri.fsPath; + } + } + + const projectPath = await window.showInputBox({ + prompt: 'Enter the path where to create the project directory', + value: path.join(rootPath, projectName), + }); + if (!projectPath) { + return; + } + + const crateType = await window.showQuickPick(['bin', 'lib'], { + placeHolder: 'Crate type', + }); + if (!crateType) { + return; + } + + const edition = await window.showQuickPick(['2018', '2015'], { + placeHolder: 'Rust edition', + }); + if (!edition) { + return; + } + + const vcs = await window.showQuickPick( + ['none', 'git', 'hg', 'pijul', 'fossil'], + { placeHolder: 'Initialize a Version Control System' }, + ); + if (!vcs) { + return; + } + + child_process.exec( + `cargo new --${crateType} --edition ${edition} --vcs ${vcs} --name ${projectName} ${projectPath}`, + {}, + (error, _stdout, _stderr) => { + if (error) { + window.showErrorMessage(`Failed to run Cargo:\n${error}`); + return; + } + + workspace.updateWorkspaceFolders( + workspace.workspaceFolders ? workspace.workspaceFolders.length : 0, + null, + { uri: Uri.file(projectPath) }, + ); + }, + ); + }); + context.subscriptions.push(createNewProject); + workspace.onDidOpenTextDocument(doc => whenOpeningTextDocument(doc, context)); workspace.textDocuments.forEach(doc => whenOpeningTextDocument(doc, context)); workspace.onDidChangeWorkspaceFolders(e =>