forked from microsoft/vscode-azurefunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseScriptTemplatesV2.ts
More file actions
168 lines (149 loc) · 6.18 KB
/
parseScriptTemplatesV2.ts
File metadata and controls
168 lines (149 loc) · 6.18 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { nonNullValue } from '@microsoft/vscode-azext-utils';
import { type ActionType, type ProjectLanguage } from '../../constants';
import { localize } from '../../localize';
import { type ResourceType } from '../IBindingTemplate';
import { type FunctionV2Template } from '../IFunctionTemplate';
import { TemplateSchemaVersion } from '../TemplateProviderBase';
import { getResourceValue } from './parseScriptTemplates';
/**
* Describes script template resources to be used for parsing
*/
export interface Resources {
lang?: { [key: string]: string | undefined };
// Every Resources.json file also contains the english strings
en: { [key: string]: string | undefined };
}
export interface RawTemplateV2 {
actions: ParsedAction[];
author?: string;
name: string;
id: string;
description: string;
programmingModel: string;
language: ProjectLanguage;
jobs: RawJob[]
files: { [filename: string]: string };
}
export interface ParsedAction {
name: string;
type: ActionType;
assignTo?: string;
filePath?: string;
continueOnError?: boolean;
errorText?: string;
source?: string;
createIfNotExists?: boolean;
replaceTokens?: boolean;
}
interface RawJob {
actions: string[];
condition: { name: string, expectedValue: string }
inputs: RawInput[];
name: string;
type: JobType;
}
export enum JobType {
CreateNewApp = 'CreateNewApp',
CreateNewBlueprint = 'CreateNewBlueprint',
AppendToBlueprint = 'AppendToBlueprint',
GetTemplateFileContent = 'GetTemplateFileContent',
WriteToFile = 'WriteToFile',
AppendToFile = 'AppendToFile',
ShowMarkdownPreview = 'ShowMarkdownPreview'
}
export interface RawInput {
assignTo: string;
defaultValue: string;
paramId: string;
required: boolean;
}
interface RawUserPrompt {
id: string;
name: string;
label: string;
help?: string;
validators?: UserPromptValidator[];
value: 'string' | 'enum' | 'boolean';
enum?: {
value: string;
display: string;
}[];
resource?: ResourceType,
placeHolder?: string;
}
type UserPromptValidator = {
expression: string;
errorText: string;
};
export interface ParsedInput extends RawUserPrompt, RawInput {
}
export interface ParsedJob extends RawJob {
parsedInputs: ParsedInput[];
parsedActions: ParsedAction[];
}
export function parseScriptTemplates(rawTemplates: RawTemplateV2[], rawBindings: object[], resources: Resources): FunctionV2Template[] {
const userPrompts: RawUserPrompt[] = parseUserPrompts(rawBindings, resources);
const templates: FunctionV2Template[] = [];
for (const templateV2 of rawTemplates) {
const parsedJobs: ParsedJob[] = [];
for (const job of templateV2.jobs) {
const parsedInputs: ParsedInput[] = [];
job.inputs.forEach(input => {
const userPrompt = userPrompts.find(up => up.id.toLocaleLowerCase() === input.paramId.toLocaleLowerCase());
parsedInputs.push(Object.assign(input, userPrompt));
});
const parsedActions: ParsedAction[] = [];
for (const action of job.actions) {
const parsedAction = templateV2.actions.find(a => a.name.toLowerCase() === action.toLowerCase());
if (parsedAction) {
parsedActions.push(parsedAction);
}
}
parsedJobs.push(Object.assign(job, { parsedInputs, parsedActions }));
}
const isHttpTrigger = !!templateV2.id?.toLowerCase().includes('httptrigger-');
const isTimerTrigger = !!templateV2.id?.toLowerCase().includes('timertrigger-');
// MCP trigger variants use multiple IDs across templates/providers.
const isMcpTrigger = !!templateV2.id?.toLowerCase().includes('mcptooltrigger') ||
templateV2.id?.toLowerCase().includes('mcptrigger') ||
templateV2.id?.toLowerCase().includes('mcpresourcetrigger');
templates.push(Object.assign(templateV2, {
wizards: parsedJobs,
id: nonNullValue(templateV2.id),
isHttpTrigger,
isTimerTrigger,
isMcpTrigger,
templateSchemaVersion: TemplateSchemaVersion.v2
}));
}
return templates;
}
export function parseUserPrompts(rawUserPrompts: object[], resources: Resources): RawUserPrompt[] {
const userPrompts: RawUserPrompt[] = [];
for (const rawUserPrompt of rawUserPrompts) {
const userPrompt: RawUserPrompt = rawUserPrompt as RawUserPrompt;
for (const key of Object.keys(rawUserPrompt)) {
// all of the properties in the rawResources are in the format of "param_name" but the keys in the rawUserPrompt are in the format of "param-name"
const paramName = userPrompt[key] as unknown;
if (typeof paramName === 'string' && paramName.startsWith('$')) {
userPrompt[key] = getResourceValue(resources, paramName, true) || paramName;
} else if (key === 'validators' && Array.isArray(rawUserPrompt[key])) {
const validators: UserPromptValidator[] = rawUserPrompt[key] as UserPromptValidator[];
for (const validator of validators) {
// there are a few edge cases with tokens where the format is [variables('param_name')] instead of $param_name
const matches: RegExpMatchArray | null = validator.errorText.match(/\[variables\(\'(.*)\'\)\]/);
// but in the resources key is $variables_paramName
validator.errorText = getResourceValue(resources, matches ? '$variables_' + matches[1] : validator.errorText, true)
|| localize('validatorError', 'Invalid input');
}
userPrompt[key] = validators;
}
}
userPrompts.push(userPrompt);
}
return userPrompts;
}