Skip to content

Commit ae1d0f7

Browse files
Using ext bundle instead of hard-coded v2 templates (#3391) (#3467)
* Using ext bundle instead of hard-coded v2 templates * The blueprint append fix. * minor fix * Removed an unused variable. * We don't have fall back templates. Naren confirmed. Co-authored-by: Khuram Khan <[email protected]>
1 parent c247ea1 commit ae1d0f7

File tree

5 files changed

+99
-21
lines changed

5 files changed

+99
-21
lines changed

build/BuildSteps.cs

+11
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,17 @@ public static void AddTemplatesJson()
286286
File.Copy(templatesJsonPath, Path.Combine(Settings.OutputDir, runtime, "templates", "templates.json"));
287287
}
288288
}
289+
290+
string templatesv2JsonPath = Path.Combine(tempDirectoryPath, "templates-v2", "templates.json");
291+
string userPromptsv2JsonPath = Path.Combine(tempDirectoryPath, "bindings-v2", "userPrompts.json");
292+
if (File.Exists(templatesv2JsonPath) && File.Exists(userPromptsv2JsonPath))
293+
{
294+
foreach (var runtime in Settings.TargetRuntimes)
295+
{
296+
File.Copy(templatesv2JsonPath, Path.Combine(Settings.OutputDir, runtime, "templates-v2", "templates.json"));
297+
File.Copy(userPromptsv2JsonPath, Path.Combine(Settings.OutputDir, runtime, "templates-v2", "userPrompts.json"));
298+
}
299+
}
289300
}
290301

291302
public static void Test()

src/Azure.Functions.Cli/Actions/LocalActions/CreateFunctionAction.cs

+13-7
Original file line numberDiff line numberDiff line change
@@ -157,31 +157,37 @@ public async override Task RunAsync()
157157
}
158158
}
159159

160-
var variables = new Dictionary<string, string>();
160+
var providedInputs = new Dictionary<string, string>() { { GetFunctionNameParamId, FunctionName } };
161161
var jobName = "appendToFile";
162162
if (FileName != PySteinFunctionAppPy)
163163
{
164164
var filePath = Path.Combine(Environment.CurrentDirectory, FileName);
165-
jobName = !FileUtility.FileExists(filePath) ? "CreateNewBlueprint" : "AppendToBlueprint";
166-
variables["$(BLUEPRINT_FILENAME)"] = FileName;
167-
FileName = FileName[..^Path.GetExtension(FileName).Length];
165+
if (FileUtility.FileExists(filePath))
166+
{
167+
jobName = "AppendToBlueprint";
168+
providedInputs[GetBluePrintExistingFileNameParamId] = FileName;
169+
}
170+
else
171+
{
172+
jobName = "CreateNewBlueprint";
173+
providedInputs[GetBluePrintFileNameParamId] = FileName;
174+
}
168175
}
169176
else
170177
{
171-
variables["$(SELECTED_FILEPATH)"] = FileName;
178+
providedInputs[GetFileNameParamId] = FileName;
172179
}
173180

174181
var template = _newTemplates.Value.FirstOrDefault(t => string.Equals(t.Name, TemplateName, StringComparison.CurrentCultureIgnoreCase) && string.Equals(t.Language, Language, StringComparison.CurrentCultureIgnoreCase));
175182
var templateJob = template.Jobs.Single(x => x.Type.Equals(jobName, StringComparison.OrdinalIgnoreCase));
176-
var providedInputs = new Dictionary<string, string>() { { GetFunctionNameParamId, FunctionName } };
177183

184+
var variables = new Dictionary<string, string>();
178185
_userInputHandler.RunUserInputActions(providedInputs, templateJob.Inputs, variables);
179186

180187
if (string.IsNullOrEmpty(FunctionName))
181188
{
182189
FunctionName = providedInputs[GetFunctionNameParamId];
183190
}
184-
185191

186192
await _templatesManager.Deploy(templateJob, template, variables);
187193
}

src/Azure.Functions.Cli/Actions/UserInputHandler.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public void RunUserInputActions(IDictionary<string, string> providedValues, ILis
8686
}
8787

8888
var variableName = theInput.AssignTo;
89-
variables.Add(variableName, response);
89+
90+
variables[variableName] = response;
9091
}
9192
}
9293

src/Azure.Functions.Cli/Common/Constants.cs

+4
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ internal static class Constants
7171
public const string OldCoreToolsVersionMessage = "You are using an old Core Tools version. Please upgrade to the latest version.";
7272
public const string GetFunctionNameParamId = "trigger-functionName";
7373
public const string GetFileNameParamId = "app-selectedFileName";
74+
public const string GetBluePrintFileNameParamId = "blueprint-fileName";
75+
public const string GetBluePrintExistingFileNameParamId = "blueprint-existingFileName";
7476
public const string UserPromptBooleanType = "boolean";
7577
public const string UserPromptEnumType = "enum";
7678
public const string UserInputActionType = "UserInput";
79+
public const string UserPromptExtensionBundleFileName = "userPrompts.json";
80+
public const string TemplatesExtensionBundleFileName = "templates.json";
7781
public const string ShowMarkdownPreviewActionType = "ShowMarkdownPreview";
7882
public const string FunctionBodyTargetFileName = "FUNCTION_BODY_TARGET_FILE_NAME";
7983
public const string PythonProgrammingModelFunctionBodyFileKey = "function_body.py";

src/Azure.Functions.Cli/Common/TemplatesManager.cs

+69-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using Microsoft.AspNetCore.Routing.Constraints;
1313
using Microsoft.CodeAnalysis;
1414
using System.IO.Abstractions;
15+
using Microsoft.Azure.WebJobs.Script;
16+
using static Azure.Functions.Cli.Common.OutputTheme;
1517

1618
namespace Azure.Functions.Cli.Common
1719
{
@@ -32,6 +34,22 @@ public Task<IEnumerable<Template>> Templates
3234
}
3335
}
3436

37+
public async Task<string> GetExtensionBundleFileContent(string path)
38+
{
39+
var extensionBundleManager = ExtensionBundleHelper.GetExtensionBundleManager();
40+
var bundlePath = await extensionBundleManager.GetExtensionBundlePath();
41+
string contentFilePath = Path.Combine(bundlePath, path);
42+
43+
if (FileSystemHelpers.FileExists(contentFilePath))
44+
{
45+
return await FileSystemHelpers.ReadAllTextFromFileAsync(contentFilePath);
46+
}
47+
else
48+
{
49+
return null;
50+
}
51+
}
52+
3553
private static async Task<IEnumerable<Template>> GetTemplates()
3654
{
3755
var extensionBundleManager = ExtensionBundleHelper.GetExtensionBundleManager();
@@ -64,6 +82,28 @@ private static string GetTemplatesJson()
6482
return FileSystemHelpers.ReadAllTextFromFile(templatesLocation);
6583
}
6684

85+
private static async Task<string> GetV2TemplatesJson()
86+
{
87+
var templatesLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "templates-v2", "templates.json");
88+
if (!FileSystemHelpers.FileExists(templatesLocation))
89+
{
90+
throw new CliException($"Can't find v2 templates location. Looked at '{templatesLocation}'");
91+
}
92+
93+
return await FileSystemHelpers.ReadAllTextFromFileAsync(templatesLocation);
94+
}
95+
96+
private static async Task<string> GetV2UserPromptsJson()
97+
{
98+
var userPrompotsLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "templates-v2", "userPrompts.json");
99+
if (!FileSystemHelpers.FileExists(userPrompotsLocation))
100+
{
101+
throw new CliException($"Can't find v2 user prompts location. Looked at '{userPrompotsLocation}'");
102+
}
103+
104+
return await FileSystemHelpers.ReadAllTextFromFileAsync(userPrompotsLocation);
105+
}
106+
67107
private static async Task<IEnumerable<Template>> GetNodeV4TemplatesJson()
68108
{
69109
var staticTemplateJson = await StaticResources.GetValue($"node-v4-templates.json");
@@ -221,34 +261,50 @@ public Task<IEnumerable<NewTemplate>> NewTemplates
221261
{
222262
get
223263
{
224-
return GetStaticV2Templates();
264+
return GetV2Templates();
225265
}
226266
}
227267

228-
private static async Task<IEnumerable<NewTemplate>> GetStaticV2Templates()
268+
public async Task<IEnumerable<NewTemplate>> GetV2Templates()
229269
{
230-
var staticTemplateJson = await StaticResources.GetValue($"templatesv2.json");
231-
return JsonConvert.DeserializeObject<IEnumerable<NewTemplate>>(staticTemplateJson);
270+
271+
var extensionBundleManager = ExtensionBundleHelper.GetExtensionBundleManager();
272+
string templateJson;
273+
if (extensionBundleManager.IsExtensionBundleConfigured())
274+
{
275+
templateJson = await GetExtensionBundleFileContent(Path.Combine("StaticContent", "v2", "templates", Constants.TemplatesExtensionBundleFileName));
276+
}
277+
else
278+
{
279+
templateJson = await GetV2TemplatesJson();
280+
}
281+
282+
return JsonConvert.DeserializeObject<IEnumerable<NewTemplate>>(templateJson);
232283
}
233284

234285
public Task<IEnumerable<UserPrompt>> UserPrompts
235286
{
236287
get
237288
{
238-
return GetUserPrompts();
289+
return GetV2UserPrompts();
239290
}
240291
}
241292

242-
public async Task<IEnumerable<UserPrompt>> GetUserPrompts()
293+
public async Task<IEnumerable<UserPrompt>> GetV2UserPrompts()
243294
{
244-
return await GetNewTemplateUserPrompts();
245-
}
295+
var extensionBundleManager = ExtensionBundleHelper.GetExtensionBundleManager();
246296

247-
private static async Task<IEnumerable<UserPrompt>> GetNewTemplateUserPrompts()
248-
{
249-
var userPromptStr = await StaticResources.GetValue(Constants.UserPromptFileName);
250-
var userPromptList = JsonConvert.DeserializeObject<UserPrompt[]>(userPromptStr);
251-
return userPromptList;
297+
string userPromptJson;
298+
if (extensionBundleManager.IsExtensionBundleConfigured())
299+
{
300+
userPromptJson = await GetExtensionBundleFileContent(Path.Combine("StaticContent", "v2", "bindings", Constants.UserPromptExtensionBundleFileName));
301+
}
302+
else
303+
{
304+
userPromptJson = await GetV2UserPromptsJson();
305+
}
306+
307+
return JsonConvert.DeserializeObject<UserPrompt[]>(userPromptJson);
252308
}
253309

254310
private async Task RunTemplateActionAction(NewTemplate template, TemplateAction action, IDictionary<string, string> variables)

0 commit comments

Comments
 (0)