12
12
using Microsoft . AspNetCore . Routing . Constraints ;
13
13
using Microsoft . CodeAnalysis ;
14
14
using System . IO . Abstractions ;
15
+ using Microsoft . Azure . WebJobs . Script ;
16
+ using static Azure . Functions . Cli . Common . OutputTheme ;
15
17
16
18
namespace Azure . Functions . Cli . Common
17
19
{
@@ -32,6 +34,22 @@ public Task<IEnumerable<Template>> Templates
32
34
}
33
35
}
34
36
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
+
35
53
private static async Task < IEnumerable < Template > > GetTemplates ( )
36
54
{
37
55
var extensionBundleManager = ExtensionBundleHelper . GetExtensionBundleManager ( ) ;
@@ -64,6 +82,28 @@ private static string GetTemplatesJson()
64
82
return FileSystemHelpers . ReadAllTextFromFile ( templatesLocation ) ;
65
83
}
66
84
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
+
67
107
private static async Task < IEnumerable < Template > > GetNodeV4TemplatesJson ( )
68
108
{
69
109
var staticTemplateJson = await StaticResources . GetValue ( $ "node-v4-templates.json") ;
@@ -221,34 +261,50 @@ public Task<IEnumerable<NewTemplate>> NewTemplates
221
261
{
222
262
get
223
263
{
224
- return GetStaticV2Templates ( ) ;
264
+ return GetV2Templates ( ) ;
225
265
}
226
266
}
227
267
228
- private static async Task < IEnumerable < NewTemplate > > GetStaticV2Templates ( )
268
+ public async Task < IEnumerable < NewTemplate > > GetV2Templates ( )
229
269
{
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 ) ;
232
283
}
233
284
234
285
public Task < IEnumerable < UserPrompt > > UserPrompts
235
286
{
236
287
get
237
288
{
238
- return GetUserPrompts ( ) ;
289
+ return GetV2UserPrompts ( ) ;
239
290
}
240
291
}
241
292
242
- public async Task < IEnumerable < UserPrompt > > GetUserPrompts ( )
293
+ public async Task < IEnumerable < UserPrompt > > GetV2UserPrompts ( )
243
294
{
244
- return await GetNewTemplateUserPrompts ( ) ;
245
- }
295
+ var extensionBundleManager = ExtensionBundleHelper . GetExtensionBundleManager ( ) ;
246
296
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 ) ;
252
308
}
253
309
254
310
private async Task RunTemplateActionAction ( NewTemplate template , TemplateAction action , IDictionary < string , string > variables )
0 commit comments