Use SQL input / output template instead (#18768)

* use sql input / output template instead

* workaround for storage wizard that is opened
This commit is contained in:
Vasu Bhog
2022-03-18 17:31:19 -07:00
committed by GitHub
parent 7914fcf101
commit 1a1556ca75
3 changed files with 31 additions and 65 deletions

View File

@@ -2,7 +2,6 @@
* Copyright (c) Microsoft Corporation. All rights reserved. * Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information. * Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import * as os from 'os';
import * as fs from 'fs'; import * as fs from 'fs';
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as path from 'path'; import * as path from 'path';
@@ -133,32 +132,6 @@ export async function getAzureFunctionsExtensionApi(): Promise<AzureFunctionsExt
} }
} }
/**
* TODO REMOVE defaultSqlBindingTextLines
* Overwrites the Azure function methods body to work with the binding
* @param filePath is the path for the function file (.cs for C# functions)
*/
export function overwriteAzureFunctionMethodBody(filePath: string): void {
let defaultBindedFunctionText = fs.readFileSync(filePath, 'utf-8');
// Replace default binding text
let newValueLines = defaultBindedFunctionText.split(os.EOL);
const defaultFunctionTextToSkip = new Set(constants.defaultSqlBindingTextLines);
let replacedValueLines = [];
for (let defaultLine of newValueLines) {
// Skipped lines
if (defaultFunctionTextToSkip.has(defaultLine.trimStart())) {
continue;
} else if (defaultLine.trimStart() === constants.defaultBindingResult) { // Result change
replacedValueLines.push(defaultLine.replace(constants.defaultBindingResult, constants.sqlBindingResult));
} else {
// Normal lines to be included
replacedValueLines.push(defaultLine);
}
}
defaultBindedFunctionText = replacedValueLines.join(os.EOL);
fs.writeFileSync(filePath, defaultBindedFunctionText, 'utf-8');
}
/** /**
* Gets the azure function project for the user to choose from a list of projects files * Gets the azure function project for the user to choose from a list of projects files
* If only one project is found that project is used to add the binding to * If only one project is found that project is used to add the binding to
@@ -276,7 +249,7 @@ export async function addNugetReferenceToProjectFile(selectedProjectFile: string
export async function addConnectionStringToConfig(connectionString: string, projectFile: string): Promise<void> { export async function addConnectionStringToConfig(connectionString: string, projectFile: string): Promise<void> {
const settingsFile = await getSettingsFile(projectFile); const settingsFile = await getSettingsFile(projectFile);
if (settingsFile) { if (settingsFile) {
await setLocalAppSetting(path.dirname(settingsFile), constants.sqlConnectionString, connectionString); await setLocalAppSetting(path.dirname(settingsFile), constants.sqlConnectionStringSetting, connectionString);
} }
} }

View File

@@ -10,21 +10,14 @@ const localize = nls.loadMessageBundle();
// Azure Functions // Azure Functions
export const azureFunctionsExtensionName = 'ms-azuretools.vscode-azurefunctions'; export const azureFunctionsExtensionName = 'ms-azuretools.vscode-azurefunctions';
export const sqlConnectionString = 'SqlConnectionString';
export const linkToAzureFunctionExtension = 'https://docs.microsoft.com/azure/azure-functions/functions-develop-vs-code'; export const linkToAzureFunctionExtension = 'https://docs.microsoft.com/azure/azure-functions/functions-develop-vs-code';
export const sqlBindingsDoc = 'https://aka.ms/sqlbindings'; export const sqlBindingsDoc = 'https://aka.ms/sqlbindings';
export const defaultSqlBindingTextLines = export const sqlConnectionStringSetting = 'SqlConnectionString';
[ export const azureWebJobsStorageSetting = 'AzureWebJobsStorage';
'log.LogInformation(\"C# HTTP trigger function processed a request.\");', export const azureWebJobsStoragePlaceholder = 'Enter your Azure Web Jobs storage connection string';
'string name = req.Query[\"name\"];',
'string requestBody = await new StreamReader(req.Body).ReadToEndAsync();',
'dynamic data = JsonConvert.DeserializeObject(requestBody);',
'name = name ?? data?.name;',
'string responseMessage = string.IsNullOrEmpty(name) ? \"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.\" : $\"Hello, {name}. This HTTP triggered function executed successfully.\";'
];
export const defaultBindingResult = 'return new OkObjectResult(responseMessage);';
export const sqlBindingResult = `return new OkObjectResult(result);`;
export const sqlExtensionPackageName = 'Microsoft.Azure.WebJobs.Extensions.Sql'; export const sqlExtensionPackageName = 'Microsoft.Azure.WebJobs.Extensions.Sql';
export const inputTemplateID = 'SqlInputBinding';
export const outputTemplateID = 'SqlOutputBinding';
export const functionNameTitle = localize('functionNameTitle', 'Function Name'); export const functionNameTitle = localize('functionNameTitle', 'Function Name');
export const selectProject = localize('selectProject', 'Select the Azure Function project for the SQL Binding'); export const selectProject = localize('selectProject', 'Select the Azure Function project for the SQL Binding');
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?'); 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?');
@@ -65,7 +58,6 @@ export const noAzureFunctionsProjectsInWorkspace = localize('noAzureFunctionsPro
export const addPackage = localize('addPackage', "Add Package"); export const addPackage = localize('addPackage', "Add Package");
export const createNewLocalAppSetting = localize('createNewLocalAppSetting', 'Create new local app setting'); export const createNewLocalAppSetting = localize('createNewLocalAppSetting', 'Create new local app setting');
export const createNewLocalAppSettingWithIcon = `$(add) ${createNewLocalAppSetting}`; export const createNewLocalAppSettingWithIcon = `$(add) ${createNewLocalAppSetting}`;
export const sqlConnectionStringSetting = 'SqlConnectionString';
export const valueMustNotBeEmpty = localize('valueMustNotBeEmpty', "Value must not be empty"); export const valueMustNotBeEmpty = localize('valueMustNotBeEmpty', "Value must not be empty");
export const enterConnectionStringSettingName = localize('enterConnectionStringSettingName', "Enter connection string setting name"); export const enterConnectionStringSettingName = localize('enterConnectionStringSettingName', "Enter connection string setting name");
export const enterConnectionString = localize('enterConnectionString', "Enter connection string"); export const enterConnectionString = localize('enterConnectionString', "Enter connection string");

View File

@@ -55,7 +55,6 @@ export async function createAzureFunction(connectionString: string, schema: stri
// because of an AF extension API issue, we have to get the newly created file by adding a watcher // because of an AF extension API issue, we have to get the newly created file by adding a watcher
// issue: https://github.com/microsoft/vscode-azurefunctions/issues/2908 // issue: https://github.com/microsoft/vscode-azurefunctions/issues/2908
const newFunctionFileObject = azureFunctionsUtils.waitForNewFunctionFile(projectFile); const newFunctionFileObject = azureFunctionsUtils.waitForNewFunctionFile(projectFile);
let functionFile: string;
let functionName: string; let functionName: string;
try { try {
@@ -71,41 +70,43 @@ export async function createAzureFunction(connectionString: string, schema: stri
return; return;
} }
// create C# HttpTrigger // select input or output binding
const selectedBinding = await azureFunctionsUtils.promptForBindingType();
if (!selectedBinding) {
return;
}
// set the templateId based on the selected binding type
let templateId: string = selectedBinding.type === BindingType.input ? constants.inputTemplateID : constants.outputTemplateID;
let objectName = utils.generateQuotedFullName(schema, table);
// We need to set the azureWebJobsStorage to a placeholder
// to suppress the warning for opening the wizard
// issue https://github.com/microsoft/azuredatastudio/issues/18780
await azureFunctionsUtils.setLocalAppSetting(path.dirname(projectFile), constants.azureWebJobsStorageSetting, constants.azureWebJobsStoragePlaceholder);
// create C# Azure Function with SQL Binding
await azureFunctionApi.createFunction({ await azureFunctionApi.createFunction({
language: 'C#', language: 'C#',
templateId: 'HttpTrigger', templateId: templateId,
functionName: functionName, functionName: functionName,
functionSettings: {
connectionStringSetting: constants.sqlConnectionStringSetting,
...(selectedBinding.type === BindingType.input && { object: objectName }),
...(selectedBinding.type === BindingType.output && { table: objectName })
},
folderPath: projectFile folderPath: projectFile
}); });
// check for the new function file to be created and dispose of the file system watcher // check for the new function file to be created and dispose of the file system watcher
const timeoutForFunctionFile = utils.timeoutPromise(constants.timeoutAzureFunctionFileError); const timeoutForFunctionFile = utils.timeoutPromise(constants.timeoutAzureFunctionFileError);
functionFile = await Promise.race([newFunctionFileObject.filePromise, timeoutForFunctionFile]); await Promise.race([newFunctionFileObject.filePromise, timeoutForFunctionFile]);
} finally { } finally {
newFunctionFileObject.watcherDisposable.dispose(); newFunctionFileObject.watcherDisposable.dispose();
} }
// select input or output binding
const selectedBinding = await azureFunctionsUtils.promptForBindingType();
if (!selectedBinding) {
return;
}
await azureFunctionsUtils.addNugetReferenceToProjectFile(projectFile);
await azureFunctionsUtils.addConnectionStringToConfig(connectionString, projectFile); await azureFunctionsUtils.addConnectionStringToConfig(connectionString, projectFile);
let objectName = utils.generateQuotedFullName(schema, table);
await addSqlBinding(
selectedBinding.type,
functionFile,
functionName,
objectName,
constants.sqlConnectionString
);
azureFunctionsUtils.overwriteAzureFunctionMethodBody(functionFile);
} }
} }