-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathdetectDockerfile.ts
More file actions
66 lines (59 loc) · 3.23 KB
/
detectDockerfile.ts
File metadata and controls
66 lines (59 loc) · 3.23 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { AzExtFsExtra } from '@microsoft/vscode-azext-utils';
import * as vscode from 'vscode';
import { dockerfileGlobPattern, hostFileName } from '../../../constants';
import { getAzureContainerAppsApi } from '../../../getExtensionApi';
import { localize } from '../../../localize';
import { type ICreateFunctionAppContext } from '../../../tree/SubscriptionTreeItem';
import { findFiles } from '../../../utils/workspace';
import { isFunctionProject } from '../../createNewProject/verifyIsProject';
import path = require('path');
export async function detectDockerfile(context: ICreateFunctionAppContext): Promise<void> {
if (vscode.workspace.workspaceFolders) {
context.workspaceFolder = vscode.workspace.workspaceFolders[0];
const workspacePath = context.workspaceFolder.uri.fsPath;
let hostPath: string = workspacePath
context.rootPath = workspacePath;
//check for host.json location
if (await isFunctionProject(workspacePath)) {
const files = (await findFiles(context.workspaceFolder, `*/${hostFileName}`));
if (files.length === 0) {
throw new Error(localize('noHostJson', 'No host.json file found in the current workspace.'));
}
hostPath = path.dirname(files[0].fsPath);
}
// check if dockerfile exists in the same folder as the host.json
if ((await findFiles(hostPath, dockerfileGlobPattern)).length > 0) {
context.dockerfilePath = (await findFiles(hostPath, dockerfileGlobPattern))[0].fsPath;
} else {
context.dockerfilePath = undefined;
}
// prompt user to proceed with containerized function app creation
if (context.dockerfilePath) {
const placeHolder: string = localize('detectedDockerfile', 'Dockerfile detected. What would you like to deploy?');
const containerImageButton: vscode.MessageItem = { title: localize('containerImage', 'Container Image') };
const codeButton: vscode.MessageItem = { title: localize('code', 'Code') };
const buttons: vscode.MessageItem[] = [containerImageButton, codeButton];
const result: vscode.MessageItem = await context.ui.showWarningMessage(placeHolder, { modal: true }, ...buttons);
// if yes, ensure container apps extension is installed before proceeding
if (result === containerImageButton) {
await getAzureContainerAppsApi(context);
} else if (result === codeButton) {
context.dockerfilePath = undefined;
}
}
}
}
export async function detectFunctionsDockerfile(file: string): Promise<boolean> {
const content = await AzExtFsExtra.readFile(file);
const lines: string[] = content.split('\n');
for (const line of lines) {
if (line.includes('mcr.microsoft.com/azure-functions')) {
return true;
}
}
return false
}