55
66import * as fs from 'fs-extra' ;
77import * as path from 'path' ;
8- import * as vscode from 'vscode' ;
8+ import * as protocol from './omnisharp/protocol' ;
9+ import * as serverUtils from './omnisharp/utils' ;
910import * as tasks from 'vscode-tasks' ;
11+ import * as util from './common' ;
12+ import * as vscode from 'vscode' ;
13+
1014import { OmniSharpServer } from './omnisharp/server' ;
11- import * as serverUtils from './omnisharp/utils' ;
12- import * as protocol from './omnisharp/protocol' ;
1315import { tolerantParse } from './json' ;
14- import * as util from './common' ;
1516
1617export class AssetGenerator {
17- public rootPath : string ;
18+ public workspaceFolder : vscode . WorkspaceFolder ;
1819 public vscodeFolder : string ;
1920 public tasksJsonPath : string ;
2021 public launchJsonPath : string ;
@@ -26,13 +27,33 @@ export class AssetGenerator {
2627 private executableName : string ;
2728 private configurationName : string ;
2829
29- public constructor ( workspaceInfo : protocol . WorkspaceInformationResponse , rootPath : string = vscode . workspace . rootPath ) {
30- if ( rootPath === null || rootPath === undefined ) {
31- throw new Error ( 'rootPath must set.' ) ;
30+ public constructor ( workspaceInfo : protocol . WorkspaceInformationResponse , workspaceFolder : vscode . WorkspaceFolder = undefined ) {
31+ if ( workspaceFolder ) {
32+ this . workspaceFolder = workspaceFolder ;
3233 }
34+ else {
35+ let resourcePath : string = undefined ;
36+
37+ if ( ! resourcePath && workspaceInfo . Cake ) {
38+ resourcePath = workspaceInfo . Cake . Path ;
39+ }
3340
34- this . rootPath = rootPath ;
35- this . vscodeFolder = path . join ( this . rootPath , '.vscode' ) ;
41+ if ( ! resourcePath && workspaceInfo . ScriptCs ) {
42+ resourcePath = workspaceInfo . ScriptCs . Path ;
43+ }
44+
45+ if ( ! resourcePath && workspaceInfo . DotNet && workspaceInfo . DotNet . Projects . length > 0 ) {
46+ resourcePath = workspaceInfo . DotNet . Projects [ 0 ] . Path ;
47+ }
48+
49+ if ( ! resourcePath && workspaceInfo . MsBuild ) {
50+ resourcePath = workspaceInfo . MsBuild . SolutionPath ;
51+ }
52+
53+ this . workspaceFolder = vscode . workspace . getWorkspaceFolder ( vscode . Uri . file ( resourcePath ) ) ;
54+ }
55+
56+ this . vscodeFolder = path . join ( this . workspaceFolder . uri . fsPath , '.vscode' ) ;
3657 this . tasksJsonPath = path . join ( this . vscodeFolder , 'tasks.json' ) ;
3758 this . launchJsonPath = path . join ( this . vscodeFolder , 'launch.json' ) ;
3859
@@ -132,15 +153,15 @@ export class AssetGenerator {
132153 let result = '${workspaceFolder}' ;
133154
134155 if ( this . projectPath ) {
135- result = path . join ( result , path . relative ( this . rootPath , this . projectPath ) ) ;
156+ result = path . join ( result , path . relative ( this . workspaceFolder . uri . fsPath , this . projectPath ) ) ;
136157 }
137158
138159 result = path . join ( result , `bin/${ this . configurationName } /${ this . targetFramework } /${ this . executableName } ` ) ;
139160
140161 return result ;
141162 }
142163
143- private computeWorkingDirectory ( ) : string {
164+ private computeWorkingDirectory ( ) : string {
144165 if ( ! this . hasProject ) {
145166 // If there's no target project data, use a placeholder for the path.
146167 return '${workspaceFolder}' ;
@@ -149,7 +170,7 @@ export class AssetGenerator {
149170 let result = '${workspaceFolder}' ;
150171
151172 if ( this . projectPath ) {
152- result = path . join ( result , path . relative ( this . rootPath , this . projectPath ) ) ;
173+ result = path . join ( result , path . relative ( this . workspaceFolder . uri . fsPath , this . projectPath ) ) ;
153174 }
154175
155176 return result ;
@@ -179,7 +200,7 @@ export class AssetGenerator {
179200 private createBuildTaskDescription ( ) : tasks . TaskDescription {
180201 let buildPath = '' ;
181202 if ( this . hasProject ) {
182- buildPath = path . join ( '${workspaceFolder}' , path . relative ( this . rootPath , this . projectFilePath ) ) ;
203+ buildPath = path . join ( '${workspaceFolder}' , path . relative ( this . workspaceFolder . uri . fsPath , this . projectFilePath ) ) ;
183204 }
184205
185206 return {
@@ -235,7 +256,7 @@ export function createWebLaunchConfiguration(programPath: string, workingDirecto
235256}` ;
236257}
237258
238- export function createLaunchConfiguration ( programPath : string , workingDirectory : string ) : string {
259+ export function createLaunchConfiguration ( programPath : string , workingDirectory : string ) : string {
239260 return `
240261{
241262 "name": ".NET Core Launch (console)",
@@ -378,13 +399,13 @@ interface PromptItem extends vscode.MessageItem {
378399 result : PromptResult ;
379400}
380401
381- function promptToAddAssets ( ) {
402+ function promptToAddAssets ( workspaceFolder : vscode . WorkspaceFolder ) {
382403 return new Promise < PromptResult > ( ( resolve , reject ) => {
383404 const yesItem : PromptItem = { title : 'Yes' , result : PromptResult . Yes } ;
384405 const noItem : PromptItem = { title : 'Not Now' , result : PromptResult . No , isCloseAffordance : true } ;
385406 const disableItem : PromptItem = { title : "Don't Ask Again" , result : PromptResult . Disable } ;
386407
387- const projectName = path . basename ( vscode . workspace . rootPath ) ;
408+ const projectName = path . basename ( workspaceFolder . uri . fsPath ) ;
388409
389410 vscode . window . showWarningMessage (
390411 `Required assets to build and debug are missing from '${ projectName } '. Add them?` , disableItem , noItem , yesItem )
@@ -463,7 +484,7 @@ export enum AddAssetResult {
463484
464485export function addAssetsIfNecessary ( server : OmniSharpServer ) : Promise < AddAssetResult > {
465486 return new Promise < AddAssetResult > ( ( resolve , reject ) => {
466- if ( ! vscode . workspace . rootPath ) {
487+ if ( ! vscode . workspace . workspaceFolders ) {
467488 return resolve ( AddAssetResult . NotApplicable ) ;
468489 }
469490
@@ -476,7 +497,7 @@ export function addAssetsIfNecessary(server: OmniSharpServer): Promise<AddAssetR
476497 return resolve ( AddAssetResult . NotApplicable ) ;
477498 }
478499
479- promptToAddAssets ( ) . then ( result => {
500+ promptToAddAssets ( generator . workspaceFolder ) . then ( result => {
480501 if ( result === PromptResult . Disable ) {
481502 return resolve ( AddAssetResult . Disable ) ;
482503 }
@@ -546,24 +567,20 @@ function shouldGenerateAssets(generator: AssetGenerator) {
546567 } ) ;
547568}
548569
549- export function generateAssets ( server : OmniSharpServer ) {
550- serverUtils . requestWorkspaceInformation ( server ) . then ( info => {
551- if ( protocol . containsDotNetCoreProjects ( info ) ) {
552- const generator = new AssetGenerator ( info ) ;
553- getOperations ( generator ) . then ( operations => {
554- if ( hasAddOperations ( operations ) ) {
555- shouldGenerateAssets ( generator ) . then ( res => {
556- if ( res ) {
557- fs . ensureDir ( generator . vscodeFolder , err => {
558- addAssets ( generator , operations ) ;
559- } ) ;
560- }
561- } ) ;
562- }
563- } ) ;
564- }
565- else {
566- vscode . window . showErrorMessage ( "Could not locate .NET Core project. Assets were not generated." ) ;
570+ export async function generateAssets ( server : OmniSharpServer ) {
571+ let workspaceInformation = await serverUtils . requestWorkspaceInformation ( server ) ;
572+ if ( protocol . containsDotNetCoreProjects ( workspaceInformation ) ) {
573+ const generator = new AssetGenerator ( workspaceInformation ) ;
574+ let operations = await getOperations ( generator ) ;
575+ if ( hasAddOperations ( operations ) ) {
576+ let doGenerateAssets = await shouldGenerateAssets ( generator ) ;
577+ if ( doGenerateAssets ) {
578+ await fs . ensureDir ( generator . vscodeFolder ) ;
579+ await addAssets ( generator , operations ) ;
580+ }
567581 }
568- } ) ;
582+ }
583+ else {
584+ await vscode . window . showErrorMessage ( "Could not locate .NET Core project. Assets were not generated." ) ;
585+ }
569586}
0 commit comments