-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathexecuteDotnetTemplateCommand.ts
More file actions
198 lines (169 loc) · 8.02 KB
/
executeDotnetTemplateCommand.ts
File metadata and controls
198 lines (169 loc) · 8.02 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
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { randomUtils, type IActionContext } from '@microsoft/vscode-azext-utils';
import { composeArgs, withArg, withNamedArg, withQuotedArg } from '@microsoft/vscode-processutils';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { coerce as semVerCoerce, type SemVer } from 'semver';
import { type FuncVersion } from '../../FuncVersion';
import { ext } from "../../extensionVariables";
import { localize } from '../../localize';
import { cpUtils } from "../../utils/cpUtils";
import { findShortNameByIdentity, parseTemplatesFromNupkg } from './parseNupkgTemplates';
const itemNupkgFileName = 'item.nupkg';
const projectNupkgFileName = 'project.nupkg';
/**
* Lists templates by parsing nupkg files directly (no longer uses the JsonCli DLL).
*/
export async function executeDotnetTemplateCommand(context: IActionContext, version: FuncVersion, projTemplateKey: string): Promise<string> {
const templateDir = getDotnetTemplateDir(context, version, projTemplateKey);
return await listDotnetTemplates(templateDir);
}
/**
* Lists all templates from the item.nupkg and project.nupkg in the template directory
* by parsing the `.template.config/template.json` files directly from the nupkg archives.
*/
async function listDotnetTemplates(templateDir: string): Promise<string> {
const itemNupkg = path.join(templateDir, itemNupkgFileName);
const projectNupkg = path.join(templateDir, projectNupkgFileName);
const templates: object[] = [];
for (const nupkgPath of [itemNupkg, projectNupkg]) {
try {
await fs.promises.access(nupkgPath);
templates.push(...await parseTemplatesFromNupkg(nupkgPath));
} catch {
// nupkg doesn't exist, skip
}
}
return JSON.stringify(templates);
}
/**
* Creates a function or project from a .NET template using native `dotnet new` commands.
* Uses an isolated DOTNET_CLI_HOME to avoid polluting the user's global template installation.
*/
export async function executeDotnetTemplateCreate(
context: IActionContext,
version: FuncVersion,
projTemplateKey: string,
workingDirectory: string | undefined,
identity: string,
templateArgs: Record<string, string>,
options?: { force?: boolean },
): Promise<void> {
const templateDir = getDotnetTemplateDir(context, version, projTemplateKey);
const itemNupkg = path.join(templateDir, itemNupkgFileName);
const projectNupkg = path.join(templateDir, projectNupkgFileName);
// Collect existing nupkg paths
const nupkgPaths: string[] = [];
for (const p of [itemNupkg, projectNupkg]) {
try {
await fs.promises.access(p);
nupkgPaths.push(p);
} catch {
// doesn't exist, skip
}
}
// Find the shortName for the given template identity
const shortName = await findShortNameByIdentity(nupkgPaths, identity);
// Use an isolated DOTNET_CLI_HOME so template installation doesn't affect the user's global state
// This is how the JSON CLI tool operated
const tempCliHome = path.join(os.tmpdir(), `azfunc-dotnet-home-${randomUtils.getRandomHexString()}`);
const prevDotnetCliHome = process.env.DOTNET_CLI_HOME;
try {
process.env.DOTNET_CLI_HOME = tempCliHome;
// Install template packages
for (const nupkgPath of nupkgPaths) {
await cpUtils.executeCommand(
undefined,
undefined,
'dotnet',
composeArgs(withArg('new', 'install'), withQuotedArg(nupkgPath))(),
);
}
// Build dotnet new args: dotnet new <shortName> --<param> <value> ...
const createArgs = composeArgs(
withArg('new', shortName),
...Object.entries(templateArgs)
.filter(([, value]) => value !== undefined && value !== '')
.map(([key, value]) => withNamedArg(`--${key}`, value, { shouldQuote: true })),
...(options?.force ? [withArg('--force')] : []),
)();
await cpUtils.executeCommand(
undefined,
workingDirectory,
'dotnet',
createArgs,
);
} finally {
// Restore DOTNET_CLI_HOME
if (prevDotnetCliHome !== undefined) {
process.env.DOTNET_CLI_HOME = prevDotnetCliHome;
} else {
delete process.env.DOTNET_CLI_HOME;
}
// Clean up isolated home directory
await fs.promises.rm(tempCliHome, { recursive: true, force: true }).catch(() => { /* best-effort cleanup */ });
}
}
export function getDotnetItemTemplatePath(context: IActionContext, version: FuncVersion, projTemplateKey: string): string {
return path.join(getDotnetTemplateDir(context, version, projTemplateKey), itemNupkgFileName);
}
export function getDotnetProjectTemplatePath(context: IActionContext, version: FuncVersion, projTemplateKey: string): string {
return path.join(getDotnetTemplateDir(context, version, projTemplateKey), projectNupkgFileName);
}
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', composeArgs(withArg('--version'))());
} catch {
// ignore
}
try {
versions += await cpUtils.executeCommand(undefined, workingDirectory, 'dotnet', composeArgs(withArg('--list-sdks'))());
} catch {
// ignore
}
// Prioritize "LTS", then "Current", then "Preview"
const netVersions: string[] = ['6.0', '7.0', '8.0', '9.0', '10.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;
}