-
Notifications
You must be signed in to change notification settings - Fork 679
/
assets.ts
736 lines (626 loc) · 26.6 KB
/
assets.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as fs from 'fs-extra';
import * as path from 'path';
import * as protocol from './omnisharp/protocol';
import * as serverUtils from './omnisharp/utils';
import * as tasks from 'vscode-tasks';
import * as util from './common';
import * as vscode from 'vscode';
import { OmniSharpServer } from './omnisharp/server';
import { tolerantParse } from './json';
export class AssetGenerator {
public workspaceFolder: vscode.WorkspaceFolder;
public vscodeFolder: string;
public tasksJsonPath: string;
public launchJsonPath: string;
private executeableProjects: protocol.MSBuildProject[];
private startupProject: protocol.MSBuildProject | undefined;
private fallbackBuildProject: protocol.MSBuildProject;
public constructor(workspaceInfo: protocol.WorkspaceInformationResponse, workspaceFolder: vscode.WorkspaceFolder = undefined) {
if (workspaceFolder) {
this.workspaceFolder = workspaceFolder;
}
else {
let resourcePath: string = undefined;
if (!resourcePath && workspaceInfo.Cake) {
resourcePath = workspaceInfo.Cake.Path;
}
if (!resourcePath && workspaceInfo.ScriptCs) {
resourcePath = workspaceInfo.ScriptCs.Path;
}
if (!resourcePath && workspaceInfo.DotNet && workspaceInfo.DotNet.Projects.length > 0) {
resourcePath = workspaceInfo.DotNet.Projects[0].Path;
}
if (!resourcePath && workspaceInfo.MsBuild) {
resourcePath = workspaceInfo.MsBuild.SolutionPath;
}
this.workspaceFolder = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(resourcePath));
}
this.vscodeFolder = path.join(this.workspaceFolder.uri.fsPath, '.vscode');
this.tasksJsonPath = path.join(this.vscodeFolder, 'tasks.json');
this.launchJsonPath = path.join(this.vscodeFolder, 'launch.json');
this.startupProject = undefined;
this.fallbackBuildProject = undefined;
if (workspaceInfo.MsBuild && workspaceInfo.MsBuild.Projects.length > 0) {
this.executeableProjects = protocol.findExecutableMSBuildProjects(workspaceInfo.MsBuild.Projects);
if (this.executeableProjects.length === 0) {
this.fallbackBuildProject = workspaceInfo.MsBuild.Projects[0];
}
} else {
this.executeableProjects = [];
}
}
public hasExecutableProjects(): boolean {
return this.executeableProjects.length > 0;
}
public isStartupProjectSelected(): boolean {
if (this.startupProject) {
return true;
} else {
return false;
}
}
public async selectStartupProject(selectedIndex?: number): Promise<boolean> {
if (!this.hasExecutableProjects()) {
throw new Error("No executable projects");
}
if (this.executeableProjects.length === 1) {
this.startupProject = this.executeableProjects[0];
return true;
} else {
const mapItemNameToProject: { [key: string]: protocol.MSBuildProject } = {};
const itemNames: string[] = [];
this.executeableProjects.forEach(project => {
const itemName = `${path.basename(project.Path, ".csproj")} (${project.Path})`;
itemNames.push(itemName);
mapItemNameToProject[itemName] = project;
});
let selectedItem: string;
if (selectedIndex != null) {
selectedItem = itemNames[selectedIndex];
}
else {
selectedItem = await vscode.window.showQuickPick(itemNames, {
matchOnDescription: true,
placeHolder: "Select the project to launch"
});
}
if (!selectedItem || !mapItemNameToProject[selectedItem]) {
return false;
}
this.startupProject = mapItemNameToProject[selectedItem];
return true;
}
}
// This method is used by the unit tests instead of selectStartupProject
public setStartupProject(index: number): void {
if (index >= this.executeableProjects.length) {
throw new Error("Invalid project index");
}
this.startupProject = this.executeableProjects[index];
}
public hasWebServerDependency(): boolean {
if (!this.startupProject) {
throw new Error("Startup project not set");
}
return this.startupProject.IsWebProject;
}
public computeProgramLaunchType(): ProgramLaunchType {
if (!this.startupProject) {
throw new Error("Startup project not set");
}
if (this.startupProject.IsBlazorWebAssemblyStandalone) {
return ProgramLaunchType.BlazorWebAssemblyStandalone;
}
if (this.startupProject.IsBlazorWebAssemblyHosted) {
return ProgramLaunchType.BlazorWebAssemblyHosted;
}
if (this.startupProject.IsWebProject) {
return ProgramLaunchType.Web;
}
return ProgramLaunchType.Console;
}
private computeProgramPath() {
if (!this.startupProject) {
throw new Error("Startup project not set");
}
const startupProjectDir = path.dirname(this.startupProject.Path);
const relativeProjectDir = path.join('${workspaceFolder}', path.relative(this.workspaceFolder.uri.fsPath, startupProjectDir));
const configurationName = 'Debug';
const targetFramework = protocol.findNetCoreAppTargetFramework(this.startupProject) ?? protocol.findNet5TargetFramework(this.startupProject);
const result = path.join(relativeProjectDir, `bin/${configurationName}/${targetFramework.ShortName}/${this.startupProject.AssemblyName}.dll`);
return result;
}
private computeWorkingDirectory(): string {
if (!this.startupProject) {
throw new Error("Startup project not set");
}
const startupProjectDir = path.dirname(this.startupProject.Path);
return path.join('${workspaceFolder}', path.relative(this.workspaceFolder.uri.fsPath, startupProjectDir));
}
public createLaunchJsonConfigurations(programLaunchType: ProgramLaunchType): string {
switch (programLaunchType) {
case ProgramLaunchType.Console: {
const launchConfigurationsMassaged: string = indentJsonString(createLaunchConfiguration(this.computeProgramPath(), this.computeWorkingDirectory()));
const attachConfigurationsMassaged: string = indentJsonString(createAttachConfiguration());
return `
[
${launchConfigurationsMassaged},
${attachConfigurationsMassaged}
]`;
}
case ProgramLaunchType.Web: {
const webLaunchConfigurationsMassaged: string = indentJsonString(createWebLaunchConfiguration(this.computeProgramPath(), this.computeWorkingDirectory()));
const attachConfigurationsMassaged: string = indentJsonString(createAttachConfiguration());
return `
[
${webLaunchConfigurationsMassaged},
${attachConfigurationsMassaged}
]`;
}
case ProgramLaunchType.BlazorWebAssemblyHosted: {
const hostedLaunchConfigMassaged: string = indentJsonString(createBlazorWebAssemblyHostedLaunchConfiguration(this.computeProgramPath(), this.computeWorkingDirectory()));
return `
[
${hostedLaunchConfigMassaged}
]`;
}
case ProgramLaunchType.BlazorWebAssemblyStandalone: {
const standaloneLaunchConfigMassaged: string = indentJsonString(createBlazorWebAssemblyStandaloneLaunchConfiguration(this.computeWorkingDirectory()));
return `
[
${standaloneLaunchConfigMassaged}
]`;
}
}
}
private createBuildTaskDescription(): tasks.TaskDescription {
let commandArgs = ['build'];
this.AddAdditionalCommandArgs(commandArgs);
return {
label: 'build',
command: 'dotnet',
type: 'process',
args: commandArgs,
problemMatcher: '$msCompile'
};
}
private createPublishTaskDescription(): tasks.TaskDescription {
let commandArgs = ['publish'];
this.AddAdditionalCommandArgs(commandArgs);
return {
label: 'publish',
command: 'dotnet',
type: 'process',
args: commandArgs,
problemMatcher: '$msCompile'
};
}
private createWatchTaskDescription(): tasks.TaskDescription {
let commandArgs = ['watch', 'run'];
this.AddAdditionalCommandArgs(commandArgs);
return {
label: 'watch',
command: 'dotnet',
type: 'process',
args: commandArgs,
problemMatcher: '$msCompile'
};
}
private AddAdditionalCommandArgs(commandArgs: string[]) {
let buildProject = this.startupProject;
if (!buildProject) {
buildProject = this.fallbackBuildProject;
}
if (buildProject) {
const buildPath = path.join('${workspaceFolder}', path.relative(this.workspaceFolder.uri.fsPath, buildProject.Path));
commandArgs.push(util.convertNativePathToPosix(buildPath));
}
commandArgs.push("/property:GenerateFullPaths=true");
commandArgs.push("/consoleloggerparameters:NoSummary");
}
public createTasksConfiguration(): tasks.TaskConfiguration {
return {
version: "2.0.0",
tasks: [this.createBuildTaskDescription(), this.createPublishTaskDescription(), this.createWatchTaskDescription()]
};
}
}
export enum ProgramLaunchType {
Console,
Web,
BlazorWebAssemblyHosted,
BlazorWebAssemblyStandalone,
}
export function createWebLaunchConfiguration(programPath: string, workingDirectory: string): string {
return `
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${util.convertNativePathToPosix(programPath)}",
"args": [],
"cwd": "${util.convertNativePathToPosix(workingDirectory)}",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\\\bNow listening on:\\\\s+(https?://\\\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "\${workspaceFolder}/Views"
}
}`;
}
export function createBlazorWebAssemblyHostedLaunchConfiguration(programPath: string, workingDirectory: string): string {
return `
{
"name": "Launch and Debug Hosted Blazor WebAssembly App",
"type": "blazorwasm",
"request": "launch",
"hosted": true,
// If you have changed target frameworks, make sure to update the program path.
"program": "${util.convertNativePathToPosix(programPath)}",
"cwd": "${util.convertNativePathToPosix(workingDirectory)}"
}`;
}
export function createBlazorWebAssemblyStandaloneLaunchConfiguration(workingDirectory: string): string {
return `
{
"name": "Launch and Debug Standalone Blazor WebAssembly App",
"type": "blazorwasm",
"request": "launch",
"cwd": "${util.convertNativePathToPosix(workingDirectory)}"
}`;
}
export function createLaunchConfiguration(programPath: string, workingDirectory: string): string {
return `
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${util.convertNativePathToPosix(programPath)}",
"args": [],
"cwd": "${util.convertNativePathToPosix(workingDirectory)}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
}`;
}
// DebugConfiguration written to launch.json when the extension fails to generate a good configuration
export function createFallbackLaunchConfiguration(): vscode.DebugConfiguration {
return {
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"WARNING01": "*********************************************************************************",
"WARNING02": "The C# extension was unable to automatically decode projects in the current",
"WARNING03": "workspace to create a runnable launch.json file. A template launch.json file has",
"WARNING04": "been created as a placeholder.",
"WARNING05": "",
"WARNING06": "If OmniSharp is currently unable to load your project, you can attempt to resolve",
"WARNING07": "this by restoring any missing project dependencies (example: run 'dotnet restore')",
"WARNING08": "and by fixing any reported errors from building the projects in your workspace.",
"WARNING09": "If this allows OmniSharp to now load your project then --",
"WARNING10": " * Delete this file",
"WARNING11": " * Open the Visual Studio Code command palette (View->Command Palette)",
"WARNING12": " * run the command: '.NET: Generate Assets for Build and Debug'.",
"WARNING13": "",
"WARNING14": "If your project requires a more complex launch configuration, you may wish to delete",
"WARNING15": "this configuration and pick a different template using the 'Add Configuration...'",
"WARNING16": "button at the bottom of this file.",
"WARNING17": "*********************************************************************************",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false
};
}
// AttachConfiguration
export function createAttachConfiguration(): string {
return `
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "\${command:pickProcess}"
}`;
}
export interface AssetOperations {
addTasksJson?: boolean;
updateTasksJson?: boolean;
addLaunchJson?: boolean;
}
function hasAddOperations(operations: AssetOperations) {
return operations.addTasksJson || operations.addLaunchJson;
}
async function getOperations(generator: AssetGenerator): Promise<AssetOperations> {
return getBuildOperations(generator).then(async operations =>
getLaunchOperations(generator, operations));
}
/**
* Finds a build task if there is one. Only handles new format.
*/
function getBuildTasks(tasksConfiguration: tasks.TaskConfiguration): tasks.TaskDescription[] {
let result: tasks.TaskDescription[] = [];
function findBuildTask(version: string, tasksDescriptions: tasks.TaskDescription[]) {
let buildTask = undefined;
if (tasksDescriptions) {
buildTask = tasksDescriptions.find(td => td.group === 'build');
}
if (buildTask !== undefined) {
result.push(buildTask);
}
}
findBuildTask(tasksConfiguration.version, tasksConfiguration.tasks);
if (tasksConfiguration.windows) {
findBuildTask(tasksConfiguration.version, tasksConfiguration.windows.tasks);
}
if (tasksConfiguration.osx) {
findBuildTask(tasksConfiguration.version, tasksConfiguration.osx.tasks);
}
if (tasksConfiguration.linux) {
findBuildTask(tasksConfiguration.version, tasksConfiguration.linux.tasks);
}
return result;
}
export async function getBuildOperations(generator: AssetGenerator): Promise<AssetOperations> {
return new Promise<AssetOperations>((resolve, reject) => {
fs.exists(generator.tasksJsonPath, exists => {
if (exists) {
fs.readFile(generator.tasksJsonPath, (err, buffer) => {
if (err) {
return reject(err);
}
const text = buffer.toString();
let tasksConfiguration: tasks.TaskConfiguration;
try {
tasksConfiguration = tolerantParse(text);
}
catch (error) {
vscode.window.showErrorMessage(`Failed to parse tasks.json file`);
return resolve({ updateTasksJson: false });
}
if (!tasksConfiguration.version || !tasksConfiguration.version.startsWith("2.0.")) {
// We don't have code to update the older tasks format, so don't try to update it
return resolve({ updateTasksJson: false });
}
let buildTasks = getBuildTasks(tasksConfiguration);
resolve({ updateTasksJson: buildTasks.length === 0 });
});
}
else {
resolve({ addTasksJson: true });
}
});
});
}
async function getLaunchOperations(generator: AssetGenerator, operations: AssetOperations): Promise<AssetOperations> {
if (!generator.hasExecutableProjects()) {
return Promise.resolve(operations);
}
return new Promise<AssetOperations>((resolve, reject) => {
return fs.exists(generator.launchJsonPath, exists => {
if (exists) {
resolve(operations);
}
else {
operations.addLaunchJson = true;
resolve(operations);
}
});
});
}
enum PromptResult {
Yes,
No,
Disable
}
interface PromptItem extends vscode.MessageItem {
result: PromptResult;
}
async function promptToAddAssets(workspaceFolder: vscode.WorkspaceFolder) {
return new Promise<PromptResult>((resolve, reject) => {
const yesItem: PromptItem = { title: 'Yes', result: PromptResult.Yes };
const noItem: PromptItem = { title: 'Not Now', result: PromptResult.No, isCloseAffordance: true };
const disableItem: PromptItem = { title: "Don't Ask Again", result: PromptResult.Disable };
const projectName = path.basename(workspaceFolder.uri.fsPath);
let csharpConfig = vscode.workspace.getConfiguration('csharp');
if (!csharpConfig.get<boolean>('supressBuildAssetsNotification')) {
vscode.window.showWarningMessage(
`Required assets to build and debug are missing from '${projectName}'. Add them?`, disableItem, noItem, yesItem)
.then(selection => resolve(selection?.result ?? PromptResult.No));
}
});
}
export async function addTasksJsonIfNecessary(generator: AssetGenerator, operations: AssetOperations) {
return new Promise<void>((resolve, reject) => {
if (!operations.addTasksJson && !operations.updateTasksJson) {
return resolve();
}
const tasksJson = generator.createTasksConfiguration();
// NOTE: We only want to do this when we are supposed to update the task configuration. Otherwise,
// in the case of the 'generateAssets' command, even though we already deleted the tasks.json file
// this will still return the old tasks.json content
if (operations.updateTasksJson) {
const tasksConfigs = vscode.workspace.getConfiguration('tasks');
let existingTaskConfigs = tasksConfigs.get<Array<tasks.TaskDescription>>('tasks');
if (existingTaskConfigs) {
tasksJson['tasks'] = tasksJson['tasks'].concat(existingTaskConfigs);
}
}
const tasksJsonText = JSON.stringify(tasksJson, null, ' ');
fs.writeFile(generator.tasksJsonPath, tasksJsonText, err => {
if (err) {
return reject(err);
}
resolve();
});
});
}
function indentJsonString(json: string, numSpaces: number = 4): string {
return json.split('\n').map(line => ' '.repeat(numSpaces) + line).join('\n').trim();
}
async function addLaunchJsonIfNecessary(generator: AssetGenerator, operations: AssetOperations) {
return new Promise<void>((resolve, reject) => {
if (!operations.addLaunchJson) {
return resolve();
}
// NOTE: We will NOT attempt to merge in the existing launch.json configurations
// because in the startup prompt case, we will not attempt to create a launch.json if it
// already exists, and in the command case, we delete the launch.json file, but the VS
// Code API will return old configurations anyway, which we do NOT want.
const programLaunchType = generator.computeProgramLaunchType();
const launchJsonConfigurations: string = generator.createLaunchJsonConfigurations(programLaunchType);
const configurationsMassaged: string = indentJsonString(launchJsonConfigurations);
const launchJsonText = `
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": ${configurationsMassaged}
}`;
fs.writeFile(generator.launchJsonPath, launchJsonText.trim(), err => {
if (err) {
return reject(err);
}
resolve();
});
});
}
async function addAssets(generator: AssetGenerator, operations: AssetOperations) {
if (generator.hasExecutableProjects() && !generator.isStartupProjectSelected()) {
if (!await generator.selectStartupProject()) {
return;
}
}
const promises = [
addTasksJsonIfNecessary(generator, operations),
addLaunchJsonIfNecessary(generator, operations)
];
return Promise.all(promises);
}
export enum AddAssetResult {
NotApplicable,
Done,
Disable,
Cancelled
}
export async function addAssetsIfNecessary(server: OmniSharpServer): Promise<AddAssetResult> {
return new Promise<AddAssetResult>((resolve, reject) => {
if (!vscode.workspace.workspaceFolders) {
return resolve(AddAssetResult.NotApplicable);
}
serverUtils.requestWorkspaceInformation(server).then(async info => {
const generator = new AssetGenerator(info);
// If there aren't executable projects, we will not prompt
if (generator.hasExecutableProjects()) {
return getOperations(generator).then(operations => {
if (!hasAddOperations(operations)) {
return resolve(AddAssetResult.NotApplicable);
}
promptToAddAssets(generator.workspaceFolder).then(result => {
if (result === PromptResult.Disable) {
return resolve(AddAssetResult.Disable);
}
if (result !== PromptResult.Yes) {
return resolve(AddAssetResult.Cancelled);
}
fs.ensureDir(generator.vscodeFolder, err => {
addAssets(generator, operations).then(() =>
resolve(AddAssetResult.Done));
});
});
});
}
}).catch(err =>
reject(err));
});
}
async function doesAnyAssetExist(generator: AssetGenerator) {
return new Promise<boolean>((resolve, reject) => {
fs.exists(generator.launchJsonPath, exists => {
if (exists) {
resolve(true);
}
else {
fs.exists(generator.tasksJsonPath, exists => {
resolve(exists);
});
}
});
});
}
async function deleteAssets(generator: AssetGenerator) {
return Promise.all([
util.deleteIfExists(generator.launchJsonPath),
util.deleteIfExists(generator.tasksJsonPath)
]);
}
async function shouldGenerateAssets(generator: AssetGenerator): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
doesAnyAssetExist(generator).then(res => {
if (res) {
const yesItem = { title: 'Yes' };
const cancelItem = { title: 'Cancel', isCloseAffordance: true };
vscode.window.showWarningMessage('Replace existing build and debug assets?', cancelItem, yesItem)
.then(selection => {
if (selection === yesItem) {
resolve(true);
}
else {
// The user clicked cancel
resolve(false);
}
});
}
else {
// The assets don't exist, so we're good to go.
resolve(true);
}
});
});
}
export async function generateAssets(server: OmniSharpServer, selectedIndex?: number): Promise<void> {
try {
let workspaceInformation = await serverUtils.requestWorkspaceInformation(server);
if (workspaceInformation.MsBuild && workspaceInformation.MsBuild.Projects.length > 0) {
const generator = new AssetGenerator(workspaceInformation);
let doGenerateAssets = await shouldGenerateAssets(generator);
if (!doGenerateAssets) {
return; // user cancelled
}
const operations: AssetOperations = {
addLaunchJson: generator.hasExecutableProjects(),
addTasksJson: true
};
if (operations.addLaunchJson) {
if (!await generator.selectStartupProject(selectedIndex)) {
return; // user cancelled
}
}
await deleteAssets(generator);
await fs.ensureDir(generator.vscodeFolder);
await addAssets(generator, operations);
}
else {
await vscode.window.showErrorMessage("Could not locate .NET Core project. Assets were not generated.");
}
}
catch (err) {
await vscode.window.showErrorMessage(`Unable to generate assets to build and debug. ${err}`);
}
}