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+ }
0 commit comments