|
4 | 4 | * ------------------------------------------------------------------------------------------ */
|
5 | 5 |
|
6 | 6 | import SqlToolsServiceClient from '../languageservice/serviceclient';
|
| 7 | +import * as vscode from 'vscode'; |
7 | 8 | import * as mssql from 'vscode-mssql';
|
8 | 9 | import * as azureFunctionsContracts from '../models/contracts/azureFunctions/azureFunctionsContracts';
|
| 10 | +import * as azureFunctionUtils from '../azureFunction/azureFunctionUtils'; |
| 11 | +import * as constants from '../constants/constants'; |
| 12 | +import { generateQuotedFullName } from '../utils/utils'; |
| 13 | +import * as LocalizedConstants from '../constants/localizedConstants'; |
9 | 14 |
|
10 | 15 | export const hostFileName: string = 'host.json';
|
11 | 16 |
|
@@ -55,4 +60,53 @@ export class AzureFunctionsService implements mssql.IAzureFunctionsService {
|
55 | 60 |
|
56 | 61 | return this._client.sendRequest(azureFunctionsContracts.GetAzureFunctionsRequest.type, params);
|
57 | 62 | }
|
| 63 | + |
| 64 | + public async createAzureFunction(connectionString: string, schema: string, table: string): Promise<void> { |
| 65 | + const azureFunctionApi = await azureFunctionUtils.getAzureFunctionsExtensionApi(); |
| 66 | + if (!azureFunctionApi) { |
| 67 | + return; |
| 68 | + } |
| 69 | + let projectFile = await azureFunctionUtils.getAzureFunctionProject(); |
| 70 | + if (!projectFile) { |
| 71 | + vscode.window.showErrorMessage(LocalizedConstants.azureFunctionsProjectMustBeOpened); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + // because of an AF extension API issue, we have to get the newly created file by adding |
| 76 | + // a watcher: https://github.com/microsoft/vscode-azurefunctions/issues/2908 |
| 77 | + const newFilePromise = azureFunctionUtils.waitForNewFunctionFile(projectFile); |
| 78 | + |
| 79 | + // get function name from user |
| 80 | + const functionName = await vscode.window.showInputBox({ |
| 81 | + title: constants.functionNameTitle, |
| 82 | + value: table, |
| 83 | + ignoreFocusOut: true |
| 84 | + }); |
| 85 | + if (!functionName) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + // create C# HttpTrigger |
| 90 | + await azureFunctionApi.createFunction({ |
| 91 | + language: 'C#', |
| 92 | + templateId: 'HttpTrigger', |
| 93 | + functionName: functionName, |
| 94 | + folderPath: projectFile |
| 95 | + }); |
| 96 | + |
| 97 | + await azureFunctionUtils.addNugetReferenceToProjectFile(projectFile); |
| 98 | + await azureFunctionUtils.addConnectionStringToConfig(connectionString, projectFile); |
| 99 | + const functionFile = await newFilePromise; |
| 100 | + |
| 101 | + let objectName = generateQuotedFullName(schema, table); |
| 102 | + await this.addSqlBinding( |
| 103 | + mssql.BindingType.input, |
| 104 | + functionFile, |
| 105 | + functionName, |
| 106 | + objectName, |
| 107 | + constants.sqlConnectionString |
| 108 | + ); |
| 109 | + |
| 110 | + azureFunctionUtils.overwriteAzureFunctionMethodBody(functionFile); |
| 111 | + } |
58 | 112 | }
|
0 commit comments