-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathDotnetTemplateProvider.ts
More file actions
176 lines (152 loc) · 9.63 KB
/
DotnetTemplateProvider.ts
File metadata and controls
176 lines (152 loc) · 9.63 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { AzExtFsExtra, type IActionContext } from '@microsoft/vscode-azext-utils';
import * as path from 'path';
import { RelativePattern, workspace } from 'vscode';
import { FuncVersion, getMajorVersion } from '../../FuncVersion';
import { ProjectLanguage } from '../../constants';
import { ext } from '../../extensionVariables';
import { localize } from '../../localize';
import { cliFeedUtils } from '../../utils/cliFeedUtils';
import { dotnetUtils } from '../../utils/dotnetUtils';
import { nonNullValue } from '../../utils/nonNull';
import { parseJson } from '../../utils/parseJson';
import { requestUtils } from '../../utils/requestUtils';
import { type IBindingTemplate } from '../IBindingTemplate';
import { type IFunctionTemplate } from '../IFunctionTemplate';
import { type ITemplates } from '../ITemplates';
import { TemplateProviderBase, TemplateSchemaVersion, TemplateType } from '../TemplateProviderBase';
import { executeDotnetTemplateCommand, getDotnetItemTemplatePath, getDotnetProjectTemplatePath, getDotnetTemplateDir, validateDotnetInstalled } from './executeDotnetTemplateCommand';
import { parseDotnetTemplates } from './parseDotnetTemplates';
export class DotnetTemplateProvider extends TemplateProviderBase {
public templateType: TemplateType = TemplateType.Dotnet;
public templateSchemaVersion: TemplateSchemaVersion = TemplateSchemaVersion.v1;
public constructor(version: FuncVersion, projectPath: string | undefined, language: ProjectLanguage, projectTemplateKey: string | undefined) {
super(version, projectPath, language, projectTemplateKey);
if (projectPath) {
const projGlob = language === ProjectLanguage.FSharp ? '*.fsproj' : '*.csproj';
const watcher = workspace.createFileSystemWatcher(new RelativePattern(projectPath, projGlob));
this._disposables.push(watcher);
this._disposables.push(watcher.onDidChange(() => { this.projKeyMayHaveChanged(); }));
this._disposables.push(watcher.onDidDelete(() => { this.projKeyMayHaveChanged(); }));
this._disposables.push(watcher.onDidCreate(() => { this.projKeyMayHaveChanged(); }));
}
}
protected get backupSubpath(): string {
return path.join('dotnet', this.version);
}
private _rawTemplates: object[];
public async refreshProjKey(context: IActionContext): Promise<string> {
return await dotnetUtils.getTemplateKeyFromProjFile(context, this.projectPath, this.version, this.language);
}
public async getCachedTemplates(context: IActionContext): Promise<ITemplates | undefined> {
const projKey = await this.getProjKey(context);
const projectFilePath: string = getDotnetProjectTemplatePath(context, this.version, projKey);
const itemFilePath: string = getDotnetItemTemplatePath(context, this.version, projKey);
if (!await AzExtFsExtra.pathExists(projectFilePath) || !await AzExtFsExtra.pathExists(itemFilePath)) {
return undefined;
}
const cachedDotnetTemplates: object[] | undefined = await this.getCachedValue(projKey);
if (cachedDotnetTemplates) {
return await parseDotnetTemplates(cachedDotnetTemplates, this.version);
} else {
return undefined;
}
}
public async getLatestTemplateVersion(context: IActionContext): Promise<string> {
const projKey = await this.getProjKey(context);
const templateVersion = await cliFeedUtils.getLatestVersion(context, this.version);
let netRelease = await this.getNetRelease(context, projKey, templateVersion);
if (netRelease) {
return templateVersion;
} else {
const templateVersions = await cliFeedUtils.getSortedVersions(context, this.version);
for (const newTemplateVersion of templateVersions) {
try {
netRelease = await this.getNetRelease(context, projKey, newTemplateVersion);
if (netRelease) {
const latestFuncRelease = await cliFeedUtils.getRelease(context, templateVersion);
const latestProjKeys = Object.values(latestFuncRelease.workerRuntimes.dotnet).map(r => dotnetUtils.getTemplateKeyFromFeedEntry(r));
const warning = localize('oldProjKeyWarning', 'WARNING: "{0}" does not support the latest templates. Use "{1}" for the latest.', projKey, latestProjKeys.join('", "'));
ext.outputChannel.appendLog(warning);
return newTemplateVersion;
}
} catch {
// ignore and try next version
}
}
for (const newFuncVersion of Object.values(FuncVersion)) {
try {
if (newFuncVersion !== this.version) {
const newTemplateVersion = await cliFeedUtils.getLatestVersion(context, newFuncVersion);
netRelease = await this.getNetRelease(context, projKey, newTemplateVersion);
if (netRelease) {
context.telemetry.properties.effectiveProjectRuntime = newFuncVersion;
const oldMajorVersion = getMajorVersion(this.version);
const newMajorVersion = getMajorVersion(newFuncVersion);
const warning = localize('mismatchProjKeyWarning', 'WARNING: "{0}" is not supported on Azure Functions v{1}. Using templates from v{2} instead.', projKey, oldMajorVersion, newMajorVersion);
ext.outputChannel.appendLog(warning);
return newTemplateVersion;
}
}
} catch {
// ignore and try next version
}
}
throw new Error(localize('projKeyError', '"{0}" is not supported for Azure Functions version "{1}".', projKey, this.version));
}
}
public async getLatestTemplates(context: IActionContext, latestTemplateVersion: string): Promise<ITemplates> {
await validateDotnetInstalled(context);
const projKey = await this.getProjKey(context);
const projectFilePath: string = getDotnetProjectTemplatePath(context, this.version, projKey);
const itemFilePath: string = getDotnetItemTemplatePath(context, this.version, projKey);
const netRelease = nonNullValue(await this.getNetRelease(context, projKey, latestTemplateVersion), 'netRelease');
await Promise.all([
requestUtils.downloadFile(context, netRelease.projectTemplates, projectFilePath),
requestUtils.downloadFile(context, netRelease.itemTemplates, itemFilePath)
]);
return await this.parseTemplates(context, projKey);
}
private async getNetRelease(context: IActionContext, projKey: string, templateVersion: string): Promise<cliFeedUtils.IWorkerRuntime | undefined> {
const funcRelease: cliFeedUtils.IRelease = await cliFeedUtils.getRelease(context, templateVersion);
return Object.values(funcRelease.workerRuntimes.dotnet).find(r => projKey === dotnetUtils.getTemplateKeyFromFeedEntry(r));
}
public async getBackupTemplates(context: IActionContext): Promise<ITemplates> {
const projKey = await this.getProjKey(context);
const files: string[] = [getDotnetProjectTemplatePath(context, this.version, projKey), getDotnetItemTemplatePath(context, this.version, projKey)];
for (const file of files) {
await AzExtFsExtra.copy(this.convertToBackupFilePath(projKey, file), file, { overwrite: true });
}
return await this.parseTemplates(context, projKey);
}
public async updateBackupTemplates(context: IActionContext): Promise<void> {
const projKey = await this.getProjKey(context);
const files: string[] = [getDotnetProjectTemplatePath(context, this.version, projKey), getDotnetItemTemplatePath(context, this.version, projKey)];
for (const file of files) {
await AzExtFsExtra.copy(file, this.convertToBackupFilePath(projKey, file), { overwrite: true });
}
}
private convertToBackupFilePath(projKey: string, file: string): string {
return path.join(this.getBackupPath(), projKey, path.basename(file));
}
public async cacheTemplates(context: IActionContext): Promise<void> {
const projKey = await this.getProjKey(context);
await this.updateCachedValue(projKey, this._rawTemplates);
}
public async clearCachedTemplates(context: IActionContext): Promise<void> {
const projKey = await this.getProjKey(context);
await this.deleteCachedValue(projKey);
await AzExtFsExtra.deleteResource(getDotnetTemplateDir(context, this.version, projKey), { recursive: true });
}
private async parseTemplates(context: IActionContext, projKey: string): Promise<ITemplates> {
this._rawTemplates = parseJson(await executeDotnetTemplateCommand(context, this.version, projKey));
return parseDotnetTemplates(this._rawTemplates, this.version);
}
public includeTemplate(template: IFunctionTemplate | IBindingTemplate): boolean {
const isIsolated = (v: string) => v.toLowerCase().includes('isolated');
return !('id' in template) || !this._sessionProjKey || isIsolated(template.id) === isIsolated(this._sessionProjKey);
}
}