-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathexecuteDotnetTemplateCommand.ts
More file actions
99 lines (84 loc) · 4.53 KB
/
executeDotnetTemplateCommand.ts
File metadata and controls
99 lines (84 loc) · 4.53 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IActionContext } from '@microsoft/vscode-azext-utils';
import * as path from 'path';
import { SemVer, coerce as semVerCoerce } from 'semver';
import { FuncVersion } from '../../FuncVersion';
import { ext } from "../../extensionVariables";
import { localize } from '../../localize';
import { cpUtils } from "../../utils/cpUtils";
export async function executeDotnetTemplateCommand(context: IActionContext, version: FuncVersion, projTemplateKey: string, workingDirectory: string | undefined, operation: 'list' | 'create', ...args: string[]): Promise<string> {
const framework: string = await getFramework(context, workingDirectory);
const jsonDllPath: string = ext.context.asAbsolutePath(path.join('resources', 'dotnetJsonCli', framework, 'Microsoft.TemplateEngine.JsonCli.dll'));
return await cpUtils.executeCommand(
undefined,
workingDirectory,
'dotnet',
cpUtils.wrapArgInQuotes(jsonDllPath),
'--templateDir',
cpUtils.wrapArgInQuotes(getDotnetTemplateDir(context, version, projTemplateKey)),
'--operation',
operation,
...args);
}
export function getDotnetItemTemplatePath(context: IActionContext, version: FuncVersion, projTemplateKey: string): string {
return path.join(getDotnetTemplateDir(context, version, projTemplateKey), 'item.nupkg');
}
export function getDotnetProjectTemplatePath(context: IActionContext, version: FuncVersion, projTemplateKey: string): string {
return path.join(getDotnetTemplateDir(context, version, projTemplateKey), 'project.nupkg');
}
export function getDotnetTemplateDir(context: IActionContext, version: FuncVersion, projTemplateKey: string): string {
const templateProvider = ext.templateProvider.get(context);
return path.join(ext.context.globalStoragePath, templateProvider.templateSource || '', version, projTemplateKey);
}
export async function validateDotnetInstalled(context: IActionContext): Promise<void> {
// NOTE: Doesn't feel obvious that `getFramework` would validate dotnet is installed, hence creating a separate function named `validateDotnetInstalled` to export from this file
await getFramework(context, undefined);
}
let cachedFramework: string | undefined;
async function getFramework(context: IActionContext, workingDirectory: string | undefined): Promise<string> {
if (!cachedFramework) {
let versions: string = '';
try {
versions += await cpUtils.executeCommand(undefined, workingDirectory, 'dotnet', '--version');
} catch {
// ignore
}
try {
versions += await cpUtils.executeCommand(undefined, workingDirectory, 'dotnet', '--list-sdks');
} catch {
// ignore
}
// Prioritize "LTS", then "Current", then "Preview"
const netVersions: string[] = ['6.0', '7.0', '8.0'];
const semVersions: SemVer[] = netVersions.map(v => semVerCoerce(v) as SemVer);
let pickedVersion: SemVer | undefined;
// Try to get a GA version first (i.e. "1.0.0")
for (const semVersion of semVersions) {
const regExp: RegExp = new RegExp(`^\\s*${semVersion.major}\\.${semVersion.minor}\\.[0-9]+(\\s|$)`, 'm');
if (regExp.test(versions)) {
pickedVersion = semVersion;
break;
}
}
// Otherwise allow a preview version (i.e. "1.0.0-alpha")
if (!pickedVersion) {
for (const semVersion of semVersions) {
const regExp: RegExp = new RegExp(`^\\s*${semVersion.major}\\.${semVersion.minor}\\.`, 'm');
if (regExp.test(versions)) {
pickedVersion = semVersion;
break;
}
}
}
if (!pickedVersion) {
context.errorHandling.suppressReportIssue = true;
throw new Error(localize('noMatchingFramework', 'You must have the [.NET Core SDK](https://aka.ms/AA4ac70) installed to perform this operation. See [here](https://aka.ms/AA1tpij) for supported versions.'));
} else {
cachedFramework = `${pickedVersion.major < 4 ? 'netcoreapp' : 'net'}${pickedVersion.major}.${pickedVersion.minor}`;
}
}
return cachedFramework;
}