@@ -62,58 +62,64 @@ export function getRawPackageJson(): any {
6262 return rawPackageJson ;
6363}
6464
65- export function getRawTasksJson ( ) : Promise < any > {
66- return new Promise < any > ( ( resolve , reject ) => {
67- const path : string | undefined = getTasksJsonPath ( ) ;
68- if ( ! path ) {
69- return resolve ( { } ) ;
70- }
71- fs . exists ( path , exists => {
72- if ( ! exists ) {
73- return resolve ( { } ) ;
74- }
75- const fileContents : string = fs . readFileSync ( path ) . toString ( ) ;
76- let rawTasks : any = { } ;
77- try {
78- rawTasks = jsonc . parse ( fileContents ) ;
79- } catch ( error ) {
80- return reject ( new Error ( failedToParseTasksJson ) ) ;
81- }
82- resolve ( rawTasks ) ;
83- } ) ;
84- } ) ;
65+ export async function getRawTasksJson ( ) : Promise < any > {
66+ const path : string | undefined = getTasksJsonPath ( ) ;
67+ if ( ! path ) {
68+ return { } ;
69+ }
70+ const fileExists : boolean = await checkFileExists ( path ) ;
71+ if ( ! fileExists ) {
72+ return { } ;
73+ }
74+
75+ const fileContents : string = await readFileText ( path ) ;
76+ let rawTasks : any = { } ;
77+ try {
78+ rawTasks = jsonc . parse ( fileContents ) ;
79+ } catch ( error ) {
80+ throw new Error ( failedToParseTasksJson ) ;
81+ }
82+ return rawTasks ;
8583}
8684
87- export async function ensureBuildTaskExists ( taskName : string ) : Promise < void > {
85+ export async function ensureBuildTaskExists ( taskLabel : string ) : Promise < void > {
8886 const rawTasksJson : any = await getRawTasksJson ( ) ;
8987
9088 // Ensure that the task exists in the user's task.json. Task will not be found otherwise.
9189 if ( ! rawTasksJson . tasks ) {
9290 rawTasksJson . tasks = new Array ( ) ;
9391 }
9492 // Find or create the task which should be created based on the selected "debug configuration".
95- let selectedTask : vscode . Task | undefined = rawTasksJson . tasks . find ( ( task : any ) => task . label && task . label === task ) ;
93+ let selectedTask : vscode . Task | undefined = rawTasksJson . tasks . find ( ( task : any ) => task . label && task . label === taskLabel ) ;
9694 if ( selectedTask ) {
9795 return ;
9896 }
9997
10098 const buildTasks : vscode . Task [ ] = await getBuildTasks ( false , true ) ;
101- selectedTask = buildTasks . find ( task => task . name === taskName ) ;
99+ selectedTask = buildTasks . find ( task => task . name === taskLabel ) ;
102100 console . assert ( selectedTask ) ;
103101 if ( ! selectedTask ) {
104102 throw new Error ( "Failed to get selectedTask in ensureBuildTaskExists()" ) ;
105103 }
106104
107105 rawTasksJson . version = "2.0.0" ;
108106
109- const selectedTask2 : vscode . Task = selectedTask ;
110- if ( ! rawTasksJson . tasks . find ( ( task : any ) => task . label === selectedTask2 . definition . label ) ) {
111- const task : any = {
112- ...selectedTask2 . definition ,
113- problemMatcher : selectedTask2 . problemMatchers ,
107+ // Modify the current default task
108+ rawTasksJson . tasks . forEach ( ( task : any ) => {
109+ if ( task . label === selectedTask ?. definition . label ) {
110+ task . group = { kind : "build" , "isDefault" : true } ;
111+ } else if ( task . group . kind && task . group . kind === "build" && task . group . isDefault && task . group . isDefault === true ) {
112+ task . group = "build" ;
113+ }
114+ } ) ;
115+
116+ if ( ! rawTasksJson . tasks . find ( ( task : any ) => task . label === selectedTask ?. definition . label ) ) {
117+ const newTask : any = {
118+ ...selectedTask . definition ,
119+ problemMatcher : selectedTask . problemMatchers ,
114120 group : { kind : "build" , "isDefault" : true }
115121 } ;
116- rawTasksJson . tasks . push ( task ) ;
122+ rawTasksJson . tasks . push ( newTask ) ;
117123 }
118124
119125 const settings : OtherSettings = new OtherSettings ( ) ;
@@ -632,6 +638,18 @@ export function readFileText(filePath: string, encoding: string = "utf8"): Promi
632638
633639/** Writes content to a text file */
634640export function writeFileText ( filePath : string , content : string , encoding : string = "utf8" ) : Promise < void > {
641+ const folders : string [ ] = filePath . split ( path . sep ) . slice ( 0 , - 1 ) ;
642+ if ( folders . length ) {
643+ // create folder path if it doesn't exist
644+ folders . reduce ( ( last , folder ) => {
645+ const folderPath : string = last ? last + path . sep + folder : folder ;
646+ if ( ! fs . existsSync ( folderPath ) ) {
647+ fs . mkdirSync ( folderPath ) ;
648+ }
649+ return folderPath ;
650+ } ) ;
651+ }
652+
635653 return new Promise < void > ( ( resolve , reject ) => {
636654 fs . writeFile ( filePath , content , { encoding } , ( err ) => {
637655 if ( err ) {
0 commit comments