Load settings from local.settings.json for add sql bindings quickpick (#16957)

* load settings from local.settings.json for add sql bindings quickpick

* cleanup

* addressing comments
This commit is contained in:
Kim Santiago
2021-09-02 17:25:13 -07:00
committed by GitHub
parent 0bc2a50d78
commit a47276f708
7 changed files with 167 additions and 46 deletions

View File

@@ -2,10 +2,9 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as fse from 'fs-extra';
import * as path from 'path';
import * as vscode from 'vscode';
import * as utils from '../common/utils';
import * as azureFunctionsUtils from '../common/azureFunctionsUtils';
import * as constants from '../common/constants';
import { DotNetCommandOptions, NetCoreTool } from './netcoreTool';
@@ -57,48 +56,19 @@ export class PackageHelper {
*/
public async addPackageToAFProjectContainingFile(filePath: string, packageName: string, packageVersion?: string): Promise<void> {
try {
const project = await this.getAFProjectContainingFile(filePath);
const project = await azureFunctionsUtils.getAFProjectContainingFile(filePath);
// if no AF projects were found, an error gets thrown from getAFProjectContainingFile(). This check is temporary until
// multiple AF projects in the workspace is handled. That scenario returns undefined and shows an info message telling the
// user to make sure their project has the package reference
if (project) {
await this.addPackage(project, packageName, packageVersion);
} else {
void vscode.window.showInformationMessage(`To use SQL bindings, ensure your Azure Functions project has a reference to ${constants.sqlExtensionPackageName}`);
}
} catch (e) {
void vscode.window.showErrorMessage(e.message);
}
}
/**
* Gets the Azure Functions project that contains the given file
* @param filePath file that the containing project needs to be found for
* @returns filepath of project or undefined if project couldn't be found
*/
public async getAFProjectContainingFile(filePath: string): Promise<string | undefined> {
// get functions csprojs in the workspace
const projectPromises = vscode.workspace.workspaceFolders?.map(f => utils.getAllProjectsInFolder(f.uri, '.csproj')) ?? [];
const functionsProjects = (await Promise.all(projectPromises)).reduce((prev, curr) => prev.concat(curr), []).filter(p => this.isFunctionProject(path.dirname(p.fsPath)));
// look for project folder containing file if there's more than one
if (functionsProjects.length > 1) {
// TODO: figure out which project contains the file
// the new style csproj doesn't list all the files in the project anymore, unless the file isn't in the same folder
// so we can't rely on using that to check
void vscode.window.showInformationMessage(`To use SQL bindings, ensure your Azure Functions project has a reference to ${constants.sqlExtensionPackageName}`);
console.error('need to find which project contains the file ' + filePath);
return undefined;
} else if (functionsProjects.length === 0) {
throw new Error(constants.noAzureFunctionsProjectsInWorkspace);
} else {
return functionsProjects[0].fsPath;
}
}
// Use 'host.json' as an indicator that this is a functions project
// copied from verifyIsproject.ts in vscode-azurefunctions extension
async isFunctionProject(folderPath: string): Promise<boolean> {
return await fse.pathExists(path.join(folderPath, constants.hostFileName));
}
}