-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathcmdUtil.ts
More file actions
193 lines (160 loc) · 7.66 KB
/
cmdUtil.ts
File metadata and controls
193 lines (160 loc) · 7.66 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { IActionContext, IAzureQuickPickItem, UserCancelledError } from '@microsoft/vscode-azext-utils';
import { composeArgs, withArg, withNamedArg } from '@microsoft/vscode-processutils';
import { createHash } from 'crypto';
import * as path from 'path';
import * as vscode from 'vscode';
import { createAzureDevCli } from '../utils/azureDevCli';
import { execAsync } from '../utils/execAsync';
import { fileExists } from '../utils/fileUtils';
import { isAzureDevCliModel, isTreeViewModel, TreeViewModel } from '../utils/isTreeViewModel';
const AzureYamlGlobPattern: vscode.GlobPattern = '**/[aA][zZ][uU][rR][eE].{[yY][aA][mM][lL],[yY][mM][lL]}';
/**
* Validates that a URI has a valid fsPath for file system operations.
* Virtual file systems or certain VS Code contexts may not provide a valid fsPath.
* @param context The action context
* @param selectedFile The URI to validate
* @param selectedItem The original selected item (for error message context)
* @param commandName The name of the command being executed (for error message)
* @throws Error if the URI doesn't have a valid fsPath
*/
export function validateFileSystemUri(
context: IActionContext,
selectedFile: vscode.Uri | undefined,
selectedItem: vscode.Uri | TreeViewModel | undefined,
commandName: string
): void {
if (selectedFile && selectedFile.fsPath === undefined) {
context.errorHandling.suppressReportIssue = true;
const itemType = isTreeViewModel(selectedItem) ? 'TreeViewModel' :
isAzureDevCliModel(selectedItem) ? 'AzureDevCliModel' :
selectedItem ? 'vscode.Uri' : 'undefined';
throw new Error(vscode.l10n.t(
"Unable to determine working folder for {0} command. The selected file has an unsupported URI scheme '{1}' (selectedItem type: {2}). " +
"Azure Developer CLI commands are not supported in virtual file systems. Please open a local folder or clone the repository locally.",
commandName,
selectedFile.scheme,
itemType
));
}
}
// If the command was invoked with a specific file context, use the file context as the working directory for running Azure developer CLI commands.
// Otherwise search the workspace for "azure.yaml" or "azure.yml" files. If only one is found, use it (i.e. its folder). If more than one is found, ask the user which one to use.
// If at this point we still do not have a working directory, prompt the user to select one.
export async function getWorkingFolder(context: IActionContext, selectedFile?: vscode.Uri): Promise<string> {
let folderPath = selectedFile ? path.dirname(selectedFile.fsPath) : undefined;
if (!folderPath) {
const azureYamlFile = await pickAzureYamlFile(context);
if (azureYamlFile) {
folderPath = path.dirname(azureYamlFile.fsPath);
}
}
if (!folderPath) {
const localFolderUris = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
title: vscode.l10n.t('Select application folder')
});
if (!localFolderUris || localFolderUris.length === 0) {
throw new UserCancelledError();
}
const folderUri = localFolderUris[0];
const azureYamlUri = vscode.Uri.joinPath(folderUri, 'azure.yaml');
const azureYmlUri = vscode.Uri.joinPath(folderUri, 'azure.yml');
if (!await fileExists(azureYamlUri) && !await fileExists(azureYmlUri)) {
context.errorHandling.suppressReportIssue = true;
throw new Error(vscode.l10n.t("The selected folder does not contain 'azure.yaml' or 'azure.yml' file and cannot be used to run Azure Developer CLI commands"));
}
folderPath = folderUri.fsPath;
}
return folderPath;
}
export async function pickAzureYamlFile(context: IActionContext): Promise<vscode.Uri | undefined> {
let filePath: vscode.Uri | undefined = undefined;
const azureYamlFileUris = await vscode.workspace.findFiles(AzureYamlGlobPattern);
if (azureYamlFileUris && azureYamlFileUris.length > 0) {
if (azureYamlFileUris.length > 1) {
const choices: IAzureQuickPickItem<vscode.Uri>[] = azureYamlFileUris.map(u => { return {
label: u.fsPath,
data: u
};});
const chosenFile = await context.ui.showQuickPick(choices, {
canPickMany: false,
suppressPersistence: true,
placeHolder: vscode.l10n.t("Select configuration file ('azure.yaml' or 'azure.yml') to use for running Azure developer CLI commands")
});
filePath = chosenFile.data;
} else {
filePath = azureYamlFileUris[0];
}
}
return filePath;
}
export function getAzDevTerminalTitle(): string {
return vscode.l10n.t('az dev');
}
const UseCustomTemplate: string = 'azure-dev:/template/custom';
export async function selectApplicationTemplate(context: IActionContext): Promise<string> {
let templateUrl: string = '';
const azureCli = await createAzureDevCli(context);
const args = composeArgs(
withArg('template', 'list'),
withNamedArg('--output', 'json'),
)();
const { stdout } = await execAsync(azureCli.invocation, args, azureCli.spawnOptions());
const templates = JSON.parse(stdout) as { name: string, description: string, repositoryPath: string }[];
const choices = templates.map(t => { return { label: t.name, detail: t.description, data: t.repositoryPath } as IAzureQuickPickItem<string>; });
choices.unshift({ label: vscode.l10n.t('Use another template...'), data: '', id: UseCustomTemplate });
const template = await context.ui.showQuickPick(choices, {
canPickMany: false,
title: vscode.l10n.t('Select application template')
});
if (template.id === UseCustomTemplate) {
templateUrl = await context.ui.showInputBox({
prompt: vscode.l10n.t("Enter application template repository name ('{org or user}/{repo}')")
});
} else {
templateUrl = template.data;
}
context.telemetry.properties.templateUrlHash = sha256(templateUrl.toLowerCase());
return templateUrl;
}
export type EnvironmentInfo = {
Name: string,
IsDefault: boolean,
HasLocal?: boolean,
HasRemote?: boolean,
DotEnvPath: string,
};
export async function getEnvironments(context: IActionContext, cwd: string): Promise<EnvironmentInfo[]> {
const azureCli = await createAzureDevCli(context);
const args = composeArgs(
withArg('env', 'list', '--no-prompt'),
withNamedArg('--output', 'json'),
)();
const { stdout } = await execAsync(azureCli.invocation, args, azureCli.spawnOptions(cwd));
const envInfo = JSON.parse(stdout) as EnvironmentInfo[];
context.telemetry.properties.environmentCount = envInfo.length.toString();
return envInfo;
}
function sha256(s: string): string {
const hash = createHash('sha256');
const retval = hash.update(s).digest('hex');
return retval;
}
export async function showReadmeFile(folder: vscode.Uri | undefined): Promise<void> {
// The whole action is "best effort" -- if folder/file do not exist, just do nothing.
if (!folder) {
return;
}
const candidates: string[] = ["README.md", "README.MD", "readme.md"];
for (const fname of candidates) {
const fullPath = vscode.Uri.joinPath(folder, fname);
if (await fileExists(fullPath)) {
void vscode.commands.executeCommand('markdown.showPreview', fullPath, { 'sideBySide': false });
return;
}
}
}