From e1dc3dfde249762c2f433c9d128ae55a0934a988 Mon Sep 17 00:00:00 2001 From: Nathan Date: Mon, 7 Oct 2024 15:24:52 -0700 Subject: [PATCH] For v2 templates, use the index.json for template versioning and construct the url ourselves (#4297) --- .../script/PysteinTemplateProvider.ts | 4 +--- .../script/ScriptBundleTemplateProvider.ts | 2 +- src/utils/bundleFeedUtils.ts | 24 +++++++++++-------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/templates/script/PysteinTemplateProvider.ts b/src/templates/script/PysteinTemplateProvider.ts index e75ab914f..d0679921e 100644 --- a/src/templates/script/PysteinTemplateProvider.ts +++ b/src/templates/script/PysteinTemplateProvider.ts @@ -6,7 +6,6 @@ import { AzExtFsExtra, type IActionContext } from '@microsoft/vscode-azext-utils'; import * as path from 'path'; import { ProjectLanguage } from '../../constants'; -import { type IBundleMetadata } from '../../funcConfig/host'; import { bundleFeedUtils } from '../../utils/bundleFeedUtils'; import { feedUtils } from '../../utils/feedUtils'; import { verifyTemplateIsV2 } from '../../utils/templateVersionUtils'; @@ -33,8 +32,7 @@ export class PysteinTemplateProvider extends ScriptBundleTemplateProvider { protected _language: string; public async getLatestTemplates(context: IActionContext, latestTemplateVersion: string): Promise { - const bundleMetadata: IBundleMetadata | undefined = await this.getBundleInfo(); - const release: bundleFeedUtils.ITemplatesReleaseV2 = await bundleFeedUtils.getReleaseV2(context, bundleMetadata, latestTemplateVersion); + const release: bundleFeedUtils.ITemplatesReleaseV2 = await bundleFeedUtils.getReleaseV2(latestTemplateVersion); const language = this.getResourcesLanguage(); const resourcesUrl: string = release.resources.replace('{locale}', language); const urls: string[] = [release.userPrompts ?? release.bindings, resourcesUrl, release.functions]; diff --git a/src/templates/script/ScriptBundleTemplateProvider.ts b/src/templates/script/ScriptBundleTemplateProvider.ts index e982b0580..8c4b9b168 100644 --- a/src/templates/script/ScriptBundleTemplateProvider.ts +++ b/src/templates/script/ScriptBundleTemplateProvider.ts @@ -27,7 +27,7 @@ export class ScriptBundleTemplateProvider extends ScriptTemplateProvider { public async getLatestTemplateVersion(context: IActionContext): Promise { const bundleMetadata: IBundleMetadata | undefined = await this.getBundleInfo(); - return await bundleFeedUtils.getLatestTemplateVersion(context, bundleMetadata, this.templateSchemaVersion); + return await bundleFeedUtils.getLatestTemplateVersion(context, bundleMetadata); } public async getLatestTemplates(context: IActionContext, latestTemplateVersion: string): Promise { diff --git a/src/utils/bundleFeedUtils.ts b/src/utils/bundleFeedUtils.ts index 9b3d2450f..07f7c4b95 100644 --- a/src/utils/bundleFeedUtils.ts +++ b/src/utils/bundleFeedUtils.ts @@ -10,7 +10,6 @@ import { type IBundleMetadata, type IHostJsonV2 } from '../funcConfig/host'; import { localize } from '../localize'; import { type IBindingTemplate } from '../templates/IBindingTemplate'; import { type FunctionTemplateBase, type IFunctionTemplate } from '../templates/IFunctionTemplate'; -import { type TemplateSchemaVersion } from '../templates/TemplateProviderBase'; import { feedUtils } from './feedUtils'; import { nugetUtils } from './nugetUtils'; @@ -46,20 +45,19 @@ export namespace bundleFeedUtils { export interface ITemplatesReleaseV2 extends ITemplatesReleaseBase { userPrompts: string; - // it is not supposed to exist in the v2 schema, but sometimes userPrompts accidentally gets replaced with bindings + // for v3 runtimes, it still uses bindings for user prompts bindings?: string; } - export async function getLatestTemplateVersion(context: IActionContext, bundleMetadata: IBundleMetadata | undefined, templateSchemaVersion: TemplateSchemaVersion): Promise { + export async function getLatestTemplateVersion(context: IActionContext, bundleMetadata: IBundleMetadata | undefined): Promise { bundleMetadata = bundleMetadata || {}; - - const feed: IBundleFeed = await getBundleFeed(context, bundleMetadata); - const validVersions: string[] = Object.keys(feed.templates[templateSchemaVersion]).filter((v: string) => !!semver.valid(v)); + const versionArray: string[] = await feedUtils.getJsonFeed(context, 'https://aka.ms/azFuncBundleVersions'); + const validVersions: string[] = versionArray.filter((v: string) => !!semver.valid(v)); const bundleVersion: string | undefined = nugetUtils.tryGetMaxInRange(bundleMetadata.version || await getLatestVersionRange(context), validVersions); if (!bundleVersion) { throw new Error(localize('failedToFindBundleVersion', 'Failed to find bundle version satisfying range "{0}".', bundleMetadata.version)); } else { - return feed.bundleVersions[bundleVersion].templates; + return bundleVersion; } } @@ -68,9 +66,15 @@ export namespace bundleFeedUtils { return feed.templates.v1[templateVersion]; } - export async function getReleaseV2(context: IActionContext, bundleMetadata: IBundleMetadata | undefined, templateVersion: string): Promise { - const feed: IBundleFeed = await getBundleFeed(context, bundleMetadata); - return feed.templates.v2[templateVersion]; + export async function getReleaseV2(templateVersion: string): Promise { + // build the url ourselves because the index-v2.json file is no longer publishing version updates for v2 templates + const functionsCdn: string = 'https://functionscdn.azureedge.net/public/ExtensionBundles/Microsoft.Azure.Functions.ExtensionBundle/'; + return { + functions: `${functionsCdn}${templateVersion}/StaticContent/v2/templates/templates.json`, + bindings: `${functionsCdn}${templateVersion}/StaticContent/v2/bindings/userPrompts.json`, + userPrompts: `${functionsCdn}${templateVersion}/StaticContent/v2/bindings/userPrompts.json`, + resources: `${functionsCdn}${templateVersion}/StaticContent/v2/resources/Resources.{locale}.json`, + } } export function isBundleTemplate(template: FunctionTemplateBase | IBindingTemplate): boolean {