Add a "Quickstart" style command

This commit is contained in:
vector-of-bool 2016-04-16 22:48:17 -06:00
Родитель 0c9b9b2436
Коммит 18fb5c34cb
4 изменённых файлов: 105 добавлений и 2 удалений

Просмотреть файл

@ -15,4 +15,17 @@ Issues? Questions? Feature requests? Create an issue on
- Set CMake configuration values in ``settings.json``.
- Provides diagnostics for CMake scripts.
- Quickly jump the CMakeCache.txt
- Run CTest.
- Run CTest.
## Command Listing:
- CMake: Configure
- CMake: Build
- CMake: Build a target [Builds a target by name]
- CMake: Set build type [Change the build type, ie "Debug", "Release", etc.]
- CMake: Delete cached build settings and reconfigure
- CMake: Clean
- CMake: Clean rebuild
- CMake: Edit the cache fiel
- CTest: Run tests
- CMake: Quickstart [Quickly generate a very simple CMakeLists.txt]

Просмотреть файл

@ -39,7 +39,8 @@
"onCommand:cmake.clean",
"onCommand:cmake.cleanRebuild",
"onCommand:cmake.ctest",
"onCommand:cmake.jumpToCacheFile"
"onCommand:cmake.jumpToCacheFile",
"onCommand:cmake.quickStart"
],
"main": "./out/src/extension",
"contributes": {
@ -79,6 +80,10 @@
{
"command": "cmake.jumpToCacheFile",
"title": "CMake: Edit the CMake Cache"
},
{
"command": "cmake.quickStart",
"title": "CMake: Quick Start"
}
],
"configuration": {

Просмотреть файл

@ -30,6 +30,12 @@ export class CMakeTools {
return vscode.workspace.rootPath;
}
public get mainListFile() : string {
return path.join(this.sourceDir, 'CMakeLists.txt');
}
public get binaryDir(): string {
const build_dir = this.config<string>('buildDirectory');
return build_dir.replace('${workspaceRoot}', vscode.workspace.rootPath);
@ -285,4 +291,82 @@ export class CMakeTools {
const _this: CMakeTools = this;
return await _this.execute(['-E', 'chdir', _this.binaryDir, 'ctest', '-j8', '--output-on-failure']);
}
public quickStart = async function(): Promise<Number> {
const _this: CMakeTools = this;
if (await doAsync<Boolean>(fs.exists, _this.mainListFile)) {
vscode.window.showErrorMessage('This workspace already contains a CMakeLists.txt!');
return -1;
}
const project_name = await vscode.window.showInputBox({
prompt: 'Enter a name for the new project',
validateInput: (value: string): string => {
if (!value.length)
return 'A project name is required';
return null;
},
});
if (!project_name)
return -1;
const target_type = (await vscode.window.showQuickPick([{
label: 'Library',
description: 'Create a library',
}, {
label: 'Executable',
description: 'Create an executable'
}]));
if (!target_type)
return -1;
const type = target_type.label;
const init = [
'cmake_minimum_required(VERSION 3.0.0)',
`project(${project_name} VERSION 0.0.0)`,
'',
{
Library: `add_library(${project_name} ${project_name}.cpp)`,
Executable: `add_executable(${project_name} main.cpp)`,
}[type],
'',
].join('\n');
if (type === 'Library') {
if (!(await doAsync(fs.exists, path.join(_this.sourceDir, project_name + '.cpp')))) {
await doAsync(
fs.writeFile,
path.join(_this.sourceDir, project_name + '.cpp'),
[
'#include <iostream>',
'',
`void say_hello(){ std::cout << "Hello, from ${project_name}!\\n"; }`,
'',
].join('\n')
);
}
} else {
if (!(await doAsync(fs.exists, path.join(_this.sourceDir, 'main.cpp')))) {
await doAsync(
fs.writeFile,
path.join(_this.sourceDir, 'main.cpp'),
[
'#include <iostream>',
'',
'int main(int, char**)',
'{',
' std::cout << "Hello, world!\\n";',
'}',
'',
].join('\n')
);
}
}
await doAsync(fs.writeFile, _this.mainListFile, init);
const doc = await vscode.workspace.openTextDocument(_this.mainListFile);
await vscode.window.showTextDocument(doc);
await _this.configure();
}
}

Просмотреть файл

@ -25,6 +25,7 @@ export function activate(context: vscode.ExtensionContext) {
'buildWithTarget',
'setBuildType',
'ctest',
'quickStart',
]) {
context.subscriptions.push(register('cmake.' + key, cmake[key]));
}