Skip to content

Commit 1ccf03f

Browse files
Prompt user to add 'build' task to tasks.json
1 parent 1f8e772 commit 1ccf03f

File tree

4 files changed

+388
-1
lines changed

4 files changed

+388
-1
lines changed

src/omnisharpMain.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {StdioOmnisharpServer} from './omnisharpServer';
2323
import forwardChanges from './features/changeForwarding';
2424
import reportStatus from './features/omnisharpStatus';
2525
import {installCoreClrDebug} from './coreclr-debug';
26+
import {promptToAddBuildTaskIfNecessary} from './tasks';
2627
import * as vscode from 'vscode';
2728

2829
export function activate(context: vscode.ExtensionContext): any {
@@ -76,9 +77,12 @@ export function activate(context: vscode.ExtensionContext): any {
7677
server.stop();
7778
}));
7879

80+
// Check to see if there is a tasks.json with a "build" task and prompt the user to add it if missing.
81+
promptToAddBuildTaskIfNecessary();
82+
7983
// install coreclr-debug
8084
installCoreClrDebug(context);
81-
85+
8286
context.subscriptions.push(...disposables);
8387
}
8488

src/tasks.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
'use strict';
7+
8+
import * as fs from 'fs-extra-promise';
9+
import * as path from 'path';
10+
import * as vscode from 'vscode';
11+
import * as tasks from 'vscode-tasks';
12+
13+
function promptToAddBuildTask() {
14+
return new Promise<boolean>((resolve, reject) => {
15+
const item = { title: 'Yes' }
16+
17+
vscode.window.showInformationMessage('Would you like to add a build task for your project?', item).then(selection => {
18+
return selection
19+
? resolve(true)
20+
: resolve(false);
21+
});
22+
});
23+
}
24+
25+
function createBuildTaskDescription(): tasks.TaskDescription {
26+
return {
27+
taskName: "build",
28+
args: [],
29+
isBuildCommand: true,
30+
problemMatcher: "$msCompile"
31+
};
32+
}
33+
34+
function createTasksConfiguration(): tasks.TaskConfiguration {
35+
return {
36+
version: "0.1.0",
37+
command: "dotnet",
38+
isShellCommand: true,
39+
args: [],
40+
tasks: [ createBuildTaskDescription() ]
41+
};
42+
}
43+
44+
function writeTasksJson(tasksJsonPath: string, tasksConfig: tasks.TaskConfiguration) {
45+
const tasksJsonText = JSON.stringify(tasksConfig, null, ' ');
46+
fs.writeFileSync(tasksJsonPath, tasksJsonText);
47+
}
48+
49+
export function promptToAddBuildTaskIfNecessary() {
50+
if (!vscode.workspace.rootPath) {
51+
return;
52+
}
53+
54+
// If there is no project.json, we won't bother to prompt the user for tasks.json.
55+
const projectJsonPath = path.join(vscode.workspace.rootPath, 'project.json');
56+
if (!fs.existsSync(projectJsonPath)) {
57+
return;
58+
}
59+
60+
const vscodeFolder = path.join(vscode.workspace.rootPath, '.vscode');
61+
const tasksJsonPath = path.join(vscodeFolder, 'tasks.json');
62+
63+
fs.ensureDirAsync(vscodeFolder).then(() => {
64+
fs.existsAsync(tasksJsonPath).then(exists => {
65+
if (exists) {
66+
fs.readFileAsync(tasksJsonPath).then(text => {
67+
const fileText = text.toString();
68+
let tasksConfig: tasks.TaskConfiguration = JSON.parse(fileText);
69+
let buildTask = tasksConfig.tasks.find(td => td.taskName === 'build');
70+
if (!buildTask) {
71+
promptToAddBuildTask().then(shouldAdd => {
72+
buildTask = createBuildTaskDescription();
73+
tasksConfig.tasks.push(buildTask);
74+
writeTasksJson(tasksJsonPath, tasksConfig);
75+
});
76+
}
77+
});
78+
}
79+
else {
80+
promptToAddBuildTask().then(shouldAdd => {
81+
const tasksConfig = createTasksConfiguration();
82+
writeTasksJson(tasksJsonPath, tasksConfig);
83+
});
84+
}
85+
});
86+
});
87+
}

src/typings/vscode-extension-telemetry.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
16
declare module 'vscode-extension-telemetry' {
27
export default class TelemetryReporter {
38
constructor(extensionId: string,extensionVersion: string, key: string);

0 commit comments

Comments
 (0)