Skip to content

Basic support for extension bundles #1204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extension.bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export * from './src/commands/initProjectForVSCode/initProjectForVSCode';
export * from './src/commands/deploy/verifyAppSettings';
export * from './src/constants';
export * from './src/extensionVariables';
export * from './src/FunctionConfig';
export * from './src/funcConfig/function';
export * from './src/vsCodeConfig/settings';
export * from './src/templates/IFunctionTemplate';
export * from './src/templates/ScriptTemplateRetriever';
Expand Down
2 changes: 1 addition & 1 deletion src/commands/addBinding/BindingCreateStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { Progress, Uri, window, workspace } from "vscode";
import { AzureWizardExecuteStep } from "vscode-azureextensionui";
import { IFunctionBinding, IFunctionJson } from "../../FunctionConfig";
import { IFunctionBinding, IFunctionJson } from "../../funcConfig/function";
import { IBindingTemplate } from "../../templates/IBindingTemplate";
import { confirmEditJsonFile } from '../../utils/fs';
import { nonNullProp } from "../../utils/nonNull";
Expand Down
2 changes: 1 addition & 1 deletion src/commands/addBinding/IBindingWizardContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { IFunctionBinding } from "../../FunctionConfig";
import { IFunctionBinding } from "../../funcConfig/function";
import { IBindingTemplate } from "../../templates/IBindingTemplate";
import { IFunctionWizardContext } from "../createFunction/IFunctionWizardContext";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { Progress } from 'vscode';
import { AzureWizardExecuteStep } from 'vscode-azureextensionui';
import { setLocalAppSetting } from '../../../LocalAppSettings';
import { setLocalAppSetting } from '../../../funcConfig/local.settings';
import { localize } from '../../../localize';
import { IFunctionSetting } from '../../../templates/IFunctionSetting';
import { nonNullProp } from '../../../utils/nonNull';
Expand Down
10 changes: 5 additions & 5 deletions src/commands/addBinding/settingSteps/BindingNameStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
*--------------------------------------------------------------------------------------------*/

import * as fse from 'fs-extra';
import { FunctionConfig } from '../../../FunctionConfig';
import { ParsedFunctionJson } from '../../../funcConfig/function';
import { localize } from '../../../localize';
import { IBindingWizardContext } from '../IBindingWizardContext';
import { StringPromptStep } from './StringPromptStep';

export class BindingNameStep extends StringPromptStep {
private _functionConfig: FunctionConfig | undefined;
private _functionJson: ParsedFunctionJson | undefined;

public async getDefaultValue(wizardContext: IBindingWizardContext): Promise<string | undefined> {
const defaultValue: string | undefined = await super.getDefaultValue(wizardContext);
Expand Down Expand Up @@ -44,11 +44,11 @@ export class BindingNameStep extends StringPromptStep {

private async bindingExists(wizardContext: IBindingWizardContext, val: string): Promise<boolean> {
try {
if (!this._functionConfig) {
this._functionConfig = new FunctionConfig(await fse.readJSON(wizardContext.functionJsonPath));
if (!this._functionJson) {
this._functionJson = new ParsedFunctionJson(await fse.readJSON(wizardContext.functionJsonPath));
}

return !!this._functionConfig.bindings.find(b => b.name === val);
return !!this._functionJson.bindings.find(b => b.name === val);
} catch {
// If we can't parse the function.json file, we will prompt to overwrite the file later and can assume the binding doesn't exist
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { Progress } from 'vscode';
import { AzureWizardExecuteStep } from 'vscode-azureextensionui';
import { localSettingsFileName } from '../../../constants';
import { setLocalAppSetting } from '../../../LocalAppSettings';
import { setLocalAppSetting } from '../../../funcConfig/local.settings';
import { localize } from '../../../localize';
import { nonNullProp } from '../../../utils/nonNull';
import { IBindingWizardContext } from '../IBindingWizardContext';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as path from 'path';
import { AzureWizardExecuteStep, AzureWizardPromptStep, IAzureQuickPickItem, ISubscriptionWizardContext, IWizardOptions, StorageAccountKind, StorageAccountListStep, StorageAccountPerformance, StorageAccountReplication } from 'vscode-azureextensionui';
import { localSettingsFileName } from '../../../constants';
import { ext } from '../../../extensionVariables';
import { getLocalAppSettings, ILocalAppSettings } from '../../../LocalAppSettings';
import { getLocalSettingsJson, ILocalSettingsJson } from '../../../funcConfig/local.settings';
import { localize } from '../../../localize';
import { IFunctionSetting, ResourceType } from '../../../templates/IFunctionSetting';
import { IBindingWizardContext } from '../IBindingWizardContext';
Expand All @@ -30,7 +30,7 @@ export class LocalAppSettingListStep extends AzureWizardPromptStep<IBindingWizar

public async prompt(wizardContext: IBindingWizardContext): Promise<void> {
const localSettingsPath: string = path.join(wizardContext.projectPath, localSettingsFileName);
const settings: ILocalAppSettings = await getLocalAppSettings(localSettingsPath);
const settings: ILocalSettingsJson = await getLocalSettingsJson(localSettingsPath);
const existingSettings: string[] = settings.Values ? Object.keys(settings.Values) : [];
let picks: IAzureQuickPickItem<string | undefined>[] = [{ label: localize('newAppSetting', '$(plus) Create new local app setting'), data: undefined }];
picks = picks.concat(existingSettings.map((s: string) => { return { data: s, label: s }; }));
Expand Down
6 changes: 3 additions & 3 deletions src/commands/appSettings/downloadAppSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as vscode from 'vscode';
import { AppSettingsTreeItem, SiteClient } from "vscode-azureappservice";
import { localSettingsFileName } from "../../constants";
import { ext } from "../../extensionVariables";
import { getLocalAppSettings, ILocalAppSettings } from "../../LocalAppSettings";
import { getLocalSettingsJson, ILocalSettingsJson } from "../../funcConfig/local.settings";
import { localize } from "../../localize";
import { confirmOverwriteSettings } from "./confirmOverwriteSettings";
import { decryptLocalSettings } from "./decryptLocalSettings";
Expand All @@ -30,12 +30,12 @@ export async function downloadAppSettings(node?: AppSettingsTreeItem): Promise<v
await node.runWithTemporaryDescription(localize('downloading', 'Downloading...'), async () => {
ext.outputChannel.show(true);
ext.outputChannel.appendLine(localize('downloadStart', 'Downloading settings from "{0}"...', client.fullName));
let localSettings: ILocalAppSettings = await getLocalAppSettings(localSettingsPath, true /* allowOverwrite */);
let localSettings: ILocalSettingsJson = await getLocalSettingsJson(localSettingsPath, true /* allowOverwrite */);

const isEncrypted: boolean | undefined = localSettings.IsEncrypted;
if (localSettings.IsEncrypted) {
await decryptLocalSettings(localSettingsUri);
localSettings = <ILocalAppSettings>await fse.readJson(localSettingsPath);
localSettings = <ILocalSettingsJson>await fse.readJson(localSettingsPath);
}

try {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/appSettings/uploadAppSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as vscode from 'vscode';
import { AppSettingsTreeItem, SiteClient } from "vscode-azureappservice";
import { localSettingsFileName } from "../../constants";
import { ext } from "../../extensionVariables";
import { ILocalAppSettings } from "../../LocalAppSettings";
import { ILocalSettingsJson } from "../../funcConfig/local.settings";
import { localize } from "../../localize";
import { confirmOverwriteSettings } from "./confirmOverwriteSettings";
import { decryptLocalSettings } from "./decryptLocalSettings";
Expand All @@ -30,11 +30,11 @@ export async function uploadAppSettings(node?: AppSettingsTreeItem, workspacePat
await node.runWithTemporaryDescription(localize('uploading', 'Uploading...'), async () => {
ext.outputChannel.show(true);
ext.outputChannel.appendLine(localize('uploadStart', 'Uploading settings to "{0}"...', client.fullName));
let localSettings: ILocalAppSettings = <ILocalAppSettings>await fse.readJson(localSettingsPath);
let localSettings: ILocalSettingsJson = <ILocalSettingsJson>await fse.readJson(localSettingsPath);
if (localSettings.IsEncrypted) {
await decryptLocalSettings(localSettingsUri);
try {
localSettings = <ILocalAppSettings>await fse.readJson(localSettingsPath);
localSettings = <ILocalSettingsJson>await fse.readJson(localSettingsPath);
} finally {
await encryptLocalSettings(localSettingsUri);
}
Expand Down
29 changes: 26 additions & 3 deletions src/commands/createFunction/FunctionCreateStepBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import * as fse from 'fs-extra';
import * as path from 'path';
import { Progress, Uri, window, workspace } from 'vscode';
import { AzureWizardExecuteStep, callWithTelemetryAndErrorHandling, IActionContext } from 'vscode-azureextensionui';
import { localSettingsFileName } from '../../constants';
import { AzureWizardExecuteStep, callWithTelemetryAndErrorHandling, IActionContext, parseError } from 'vscode-azureextensionui';
import { hostFileName, localSettingsFileName, ProjectRuntime } from '../../constants';
import { ext } from '../../extensionVariables';
import { validateAzureWebJobsStorage } from '../../LocalAppSettings';
import { IHostJson } from '../../funcConfig/host';
import { validateAzureWebJobsStorage } from '../../funcConfig/local.settings';
import { localize } from '../../localize';
import { IFunctionTemplate } from '../../templates/IFunctionTemplate';
import { writeFormattedJson } from '../../utils/fs';
import { nonNullProp } from '../../utils/nonNull';
import { getContainingWorkspace } from '../../utils/workspace';
import { IFunctionWizardContext } from './IFunctionWizardContext';
Expand Down Expand Up @@ -53,6 +55,10 @@ export abstract class FunctionCreateStepBase<T extends IFunctionWizardContext> e
progress.report({ message: localize('creatingFunction', 'Creating new {0}...', template.name) });

const newFilePath: string = await this.executeCore(wizardContext);
if (wizardContext.runtime !== ProjectRuntime.v1 && !template.isHttpTrigger && !template.isTimerTrigger) {
await this.verifyExtensionBundle(wizardContext);
}

const cachedFunc: ICachedFunction = { projectPath: wizardContext.projectPath, newFilePath, isHttpTrigger: template.isHttpTrigger };

if (wizardContext.openBehavior) {
Expand All @@ -65,6 +71,23 @@ export abstract class FunctionCreateStepBase<T extends IFunctionWizardContext> e
runPostFunctionCreateSteps(cachedFunc);
}

public async verifyExtensionBundle(wizardContext: T): Promise<void> {
const hostFilePath: string = path.join(wizardContext.projectPath, hostFileName);
try {
const hostJson: IHostJson = <IHostJson>await fse.readJSON(hostFilePath);
if (!hostJson.extensionBundle) {
// https://github.com/Microsoft/vscode-azurefunctions/issues/1202
hostJson.extensionBundle = {
id: 'Microsoft.Azure.Functions.ExtensionBundle',
version: '[1.*, 2.0.0)'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a line that needs to be changed often. Is there an API call you can make to reduce how much this needs to be maintained?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See linked issue a few lines up

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was writing in the context of #1202. If they bump up the version often, does it make sense to ask an API endpoint to poll for the supported versions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That issue is about getting it from an api. Specifically this one:
https://functionscdn.azureedge.net/public/cli-feed-v3.json

Copy link
Member

@nturinski nturinski Apr 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well ideally the cli-feed will provide this property anyway, right? We only write it manually if it didn't exist?

};
await writeFormattedJson(hostFilePath, hostJson);
}
} catch (error) {
throw new Error(localize('failedToParseHostJson', 'Failed to parse {0}: {1}', hostFileName, parseError(error).message));
}
}

public shouldExecute(wizardContext: T): boolean {
return !!wizardContext.functionTemplate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as fse from 'fs-extra';
import * as path from 'path';
import { functionJsonFileName, ProjectLanguage } from '../../../constants';
import { IFunctionBinding, IFunctionJson } from '../../../FunctionConfig';
import { IFunctionBinding, IFunctionJson } from '../../../funcConfig/function';
import { localize } from '../../../localize';
import { IScriptFunctionTemplate } from '../../../templates/parseScriptTemplates';
import * as fsUtil from '../../../utils/fs';
Expand Down Expand Up @@ -48,13 +48,13 @@ export class ScriptFunctionCreateStep extends FunctionCreateStepBase<IScriptFunc
await fse.writeFile(path.join(functionPath, f), template.templateFiles[f]);
}));

const inBinding: IFunctionBinding = nonNullProp(template.functionConfig, 'inBinding');
const triggerBinding: IFunctionBinding = nonNullProp(template.functionJson, 'triggerBinding');
for (const setting of template.userPromptedSettings) {
// tslint:disable-next-line: strict-boolean-expressions no-unsafe-any
inBinding[setting.name] = wizardContext[setting.name] || '';
triggerBinding[setting.name] = wizardContext[setting.name] || '';
}

const functionJson: IFunctionJson = template.functionConfig.functionJson;
const functionJson: IFunctionJson = template.functionJson.data;
if (this.editFunctionJson) {
await this.editFunctionJson(wizardContext, functionJson);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as fse from 'fs-extra';
import * as path from 'path';
import { tsConfigFileName, tsDefaultOutDir } from '../../../constants';
import { IFunctionJson } from '../../../FunctionConfig';
import { IFunctionJson } from '../../../funcConfig/function';
import { nonNullProp } from '../../../utils/nonNull';
import { IScriptFunctionWizardContext } from './IScriptFunctionWizardContext';
import { ScriptFunctionCreateStep } from './ScriptFunctionCreateStep';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import * as os from 'os';
import * as path from 'path';
import { Progress } from 'vscode';
import { gitignoreFileName, hostFileName, localSettingsFileName, ProjectRuntime, proxiesFileName } from '../../../constants';
import { ILocalAppSettings } from '../../../LocalAppSettings';
import { IHostJson } from '../../../funcConfig/host';
import { ILocalSettingsJson } from '../../../funcConfig/local.settings';
import { confirmOverwriteFile, writeFormattedJson } from "../../../utils/fs";
import { nonNullProp } from '../../../utils/nonNull';
import { getFunctionsWorkerRuntime } from '../../../vsCodeConfig/settings';
Expand All @@ -24,13 +25,13 @@ export class ScriptProjectCreateStep extends ProjectCreateStepBase {
const runtime: ProjectRuntime = nonNullProp(wizardContext, 'runtime');
const hostJsonPath: string = path.join(wizardContext.projectPath, hostFileName);
if (await confirmOverwriteFile(hostJsonPath)) {
const hostJson: object = this.getHostContent(runtime);
const hostJson: IHostJson = this.getHostContent(runtime);
await writeFormattedJson(hostJsonPath, hostJson);
}

const localSettingsJsonPath: string = path.join(wizardContext.projectPath, localSettingsFileName);
if (await confirmOverwriteFile(localSettingsJsonPath)) {
const localSettingsJson: ILocalAppSettings = {
const localSettingsJson: ILocalSettingsJson = {
IsEncrypted: false,
Values: {
AzureWebJobsStorage: ''
Expand Down Expand Up @@ -71,7 +72,7 @@ local.settings.json`));
}
}

private getHostContent(runtime: ProjectRuntime): object {
private getHostContent(runtime: ProjectRuntime): IHostJson {
if (runtime === ProjectRuntime.v2) {
if (this.supportsManagedDependencies) {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/deploy/notifyDeployComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { MessageItem, window } from 'vscode';
import { AzureTreeItem, callWithTelemetryAndErrorHandling, IActionContext } from 'vscode-azureextensionui';
import { ext } from '../../extensionVariables';
import { HttpAuthLevel } from '../../FunctionConfig';
import { HttpAuthLevel } from '../../funcConfig/function';
import { localize } from '../../localize';
import { FunctionsTreeItem } from '../../tree/FunctionsTreeItem';
import { FunctionTreeItem } from '../../tree/FunctionTreeItem';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export abstract class InitVSCodeStepBase extends AzureWizardExecuteStep<IProject

const vscodePath: string = path.join(wizardContext.workspacePath, '.vscode');
await fse.ensureDir(vscodePath);
await this.writeTasksJson(wizardContext, vscodePath, runtime);
await this.writeTasksJson(wizardContext, vscodePath);
await this.writeLaunchJson(wizardContext.workspaceFolder, vscodePath, runtime);
await this.writeSettingsJson(wizardContext.workspaceFolder, vscodePath, language, runtime);
await this.writeExtensionsJson(vscodePath, language);
Expand All @@ -55,7 +55,7 @@ export abstract class InitVSCodeStepBase extends AzureWizardExecuteStep<IProject
}

protected abstract executeCore(wizardContext: IProjectWizardContext): Promise<void>;
protected abstract getTasks(runtime: ProjectRuntime): TaskDefinition[];
protected abstract getTasks(): TaskDefinition[];
protected getDebugConfiguration?(runtime: ProjectRuntime): DebugConfiguration;
protected getRecommendedExtensions?(language: ProjectLanguage): string[];

Expand All @@ -71,8 +71,8 @@ export abstract class InitVSCodeStepBase extends AzureWizardExecuteStep<IProject
return path.posix.join(subDir, fsPath);
}

private async writeTasksJson(wizardContext: IProjectWizardContext, vscodePath: string, runtime: ProjectRuntime): Promise<void> {
const newTasks: TaskDefinition[] = this.getTasks(runtime);
private async writeTasksJson(wizardContext: IProjectWizardContext, vscodePath: string): Promise<void> {
const newTasks: TaskDefinition[] = this.getTasks();
for (const task of newTasks) {
// tslint:disable-next-line: strict-boolean-expressions no-unsafe-any
let cwd: string = (task.options && task.options.cwd) || '.';
Expand Down
Loading