Skip to content

Commit 38df1e2

Browse files
authored
Prompt if Azure Function Extension not installed (#17230)
* link to install azure function extension if not installed * race condition
1 parent 794c33e commit 38df1e2

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/azureFunction/azureFunctionUtils.ts

+42-1
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,49 @@ export async function setLocalAppSetting(projectFolder: string, key: string, val
7676
return true;
7777
}
7878

79+
/**
80+
* Gets the Azure Functions extension API if it is installed
81+
* if it is not installed, prompt the user to install directly, learn more, or do not install
82+
* @returns the Azure Functions extension API if it is installed, prompt if it is not installed
83+
*/
7984
export async function getAzureFunctionsExtensionApi(): Promise<AzureFunctionsExtensionApi | undefined> {
80-
const apiProvider = await vscode.extensions.getExtension(constants.azureFunctionsExtensionName)?.activate() as AzureExtensionApiProvider;
85+
let apiProvider = await vscode.extensions.getExtension(constants.azureFunctionsExtensionName)?.activate() as AzureExtensionApiProvider;
86+
if (!apiProvider) {
87+
const response = await vscode.window.showInformationMessage(constants.azureFunctionsExtensionNotFound,
88+
constants.installAzureFunction, constants.learnMore, constants.doNotInstall);
89+
if (response === constants.installAzureFunction) {
90+
const extensionInstalled = new Promise<void>((resolve, reject) => {
91+
const timeout = setTimeout(async () => {
92+
reject(new Error(constants.timeoutError));
93+
extensionChange.dispose();
94+
}, 10000);
95+
let extensionChange = vscode.extensions.onDidChange(async () => {
96+
if (vscode.extensions.getExtension(constants.azureFunctionsExtensionName)) {
97+
resolve();
98+
extensionChange.dispose();
99+
clearTimeout(timeout);
100+
}
101+
});
102+
});
103+
await vscode.window.withProgress(
104+
{
105+
location: vscode.ProgressLocation.Notification,
106+
title: constants.azureFunctionsExtensionName,
107+
cancellable: false
108+
}, async (_progress, _token) => {
109+
await vscode.commands.executeCommand('workbench.extensions.installExtension', constants.azureFunctionsExtensionName);
110+
}
111+
);
112+
// the extension has not been notified that the azure function extension is installed so wait till it is to then activate it
113+
await extensionInstalled;
114+
apiProvider = await vscode.extensions.getExtension(constants.azureFunctionsExtensionName)?.activate() as AzureExtensionApiProvider;
115+
} else if (response === constants.learnMore) {
116+
await vscode.env.openExternal(vscode.Uri.parse(constants.linkToAzureFunctionExtension));
117+
return undefined;
118+
} else {
119+
return undefined;
120+
}
121+
}
81122
const azureFunctionApi = apiProvider.getApi<AzureFunctionsExtensionApi>('*');
82123
if (azureFunctionApi) {
83124
return azureFunctionApi;

src/constants/constants.ts

+5
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export const unixResourceClientPath = 'SqlToolsResourceProviderService';
158158
// Azure Functions
159159
export const azureFunctionsExtensionName = 'ms-azuretools.vscode-azurefunctions';
160160
export const sqlConnectionString = 'SqlConnectionString';
161+
export const linkToAzureFunctionExtension = 'https://docs.microsoft.com/azure/azure-functions/functions-develop-vs-code';
161162
export const defaultSqlBindingTextLines =
162163
[
163164
'log.LogInformation(\"C# HTTP trigger function processed a request.\");',
@@ -182,3 +183,7 @@ export const yesString = localize('yesString', 'Yes');
182183
export const functionNameTitle = localize('functionNameTitle', 'Function Name');
183184
export const selectProject = localize('selectProject', 'Select the Azure Function project for the SQL Binding');
184185
export const timeoutError = localize('timeoutError', 'Timed out waiting for azure function file creation');
186+
export const azureFunctionsExtensionNotFound = localize('azureFunctionsExtensionNotFound', 'The Azure Functions extension is required to create a new Azure Function with SQL binding but is not installed, install it now?');
187+
export const installAzureFunction = localize('install', 'Install');
188+
export const learnMore = localize('learnMore', 'Learn more');
189+
export const doNotInstall = localize('doNotInstall', 'Do not install');

0 commit comments

Comments
 (0)