@@ -23,6 +23,7 @@ namespace chocolatey.infrastructure.app.services
23
23
using System . Reflection ;
24
24
using System . Text ;
25
25
using configuration ;
26
+ using infrastructure . services ;
26
27
using logging ;
27
28
using templates ;
28
29
using tokens ;
@@ -35,6 +36,7 @@ public class TemplateService : ITemplateService
35
36
private readonly UTF8Encoding utf8WithoutBOM = new UTF8Encoding ( false ) ;
36
37
private readonly IFileSystem _fileSystem ;
37
38
private readonly ILogger _nugetLogger ;
39
+ private readonly IXmlService _xmlService ;
38
40
39
41
private readonly IList < string > _templateBinaryExtensions = new List < string > {
40
42
".exe" , ".msi" , ".msu" , ".msp" , ".mst" ,
@@ -45,10 +47,12 @@ public class TemplateService : ITemplateService
45
47
46
48
private readonly string _builtInTemplateOverrideName = "default" ;
47
49
private readonly string _builtInTemplateName = "built-in" ;
50
+ private readonly string _templateParameterCacheFilename = ".parameters" ;
48
51
49
- public TemplateService ( IFileSystem fileSystem )
52
+ public TemplateService ( IFileSystem fileSystem , IXmlService xmlService )
50
53
{
51
54
_fileSystem = fileSystem ;
55
+ _xmlService = xmlService ;
52
56
}
53
57
54
58
public void generate_noop ( ChocolateyConfiguration configuration )
@@ -148,6 +152,7 @@ public void generate(ChocolateyConfiguration configuration)
148
152
configuration . NewCommand . TemplateName = string . IsNullOrWhiteSpace ( configuration . NewCommand . TemplateName ) ? "default" : configuration . NewCommand . TemplateName ;
149
153
150
154
var templatePath = _fileSystem . combine_paths ( ApplicationParameters . TemplatesLocation , configuration . NewCommand . TemplateName ) ;
155
+ var templateParameterCachePath = _fileSystem . combine_paths ( templatePath , _templateParameterCacheFilename ) ;
151
156
if ( ! _fileSystem . directory_exists ( templatePath ) ) throw new ApplicationException ( "Unable to find path to requested template '{0}'. Path should be '{1}'" . format_with ( configuration . NewCommand . TemplateName , templatePath ) ) ;
152
157
153
158
this . Log ( ) . Info ( configuration . QuietOutput ? logger : ChocolateyLoggers . Important , "Generating package from custom template at '{0}'." . format_with ( templatePath ) ) ;
@@ -174,6 +179,10 @@ public void generate(ChocolateyConfiguration configuration)
174
179
this . Log ( ) . Debug ( " Treating template file ('{0}') as binary instead of replacing templated values." . format_with ( _fileSystem . get_file_name ( file ) ) ) ;
175
180
_fileSystem . copy_file ( file , packageFileLocation , overwriteExisting : true ) ;
176
181
}
182
+ else if ( templateParameterCachePath . is_equal_to ( file ) )
183
+ {
184
+ this . Log ( ) . Debug ( "{0} is the parameter cache file, ignoring" . format_with ( file ) ) ;
185
+ }
177
186
else
178
187
{
179
188
generate_file_from_template ( configuration , tokens , _fileSystem . read_file ( file ) , packageFileLocation , Encoding . UTF8 ) ;
@@ -280,6 +289,7 @@ protected void list_custom_template_info(ChocolateyConfiguration configuration,
280
289
. combine_paths ( ApplicationParameters . TemplatesLocation , configuration . TemplateCommand . Name ) , "*" , SearchOption . AllDirectories ) ) ) ;
281
290
var isOverridingBuiltIn = configuration . TemplateCommand . Name == _builtInTemplateOverrideName ;
282
291
var isDefault = string . IsNullOrWhiteSpace ( configuration . DefaultTemplateName ) ? isOverridingBuiltIn : ( configuration . DefaultTemplateName == configuration . TemplateCommand . Name ) ;
292
+ var templateParams = " {0}" . format_with ( string . Join ( "{0} " . format_with ( Environment . NewLine ) , get_template_parameters ( configuration , templateInstalledViaPackage ) ) ) ;
283
293
284
294
if ( configuration . RegularOutput )
285
295
{
@@ -292,14 +302,17 @@ protected void list_custom_template_info(ChocolateyConfiguration configuration,
292
302
{5}{6}
293
303
List of files:
294
304
{7}
305
+ List of Parameters:
306
+ {8}
295
307
" . format_with ( configuration . TemplateCommand . Name ,
296
308
pkgVersion ,
297
309
isDefault ,
298
310
isOverridingBuiltIn ? "This template is overriding the built in template{0}" . format_with ( Environment . NewLine ) : string . Empty ,
299
311
pkgTitle ,
300
312
string . IsNullOrEmpty ( pkgSummary ) ? "Template not installed as a package" : "Summary: {0}" . format_with ( pkgSummary ) ,
301
313
string . IsNullOrEmpty ( pkgDescription ) ? string . Empty : "{0}Description:{0} {1}" . format_with ( Environment . NewLine , pkgDescription ) ,
302
- pkgFiles ) ) ;
314
+ pkgFiles ,
315
+ templateParams ) ) ;
303
316
}
304
317
else
305
318
{
@@ -345,5 +358,51 @@ protected void list_built_in_template_info(ChocolateyConfiguration configuration
345
358
}
346
359
}
347
360
}
361
+
362
+ protected IEnumerable < string > get_template_parameters ( ChocolateyConfiguration configuration , bool templateInstalledViaPackage )
363
+ {
364
+ // If the template was installed via package, the cache file gets removed on upgrade, so the cache file would be up to date if it exists
365
+ if ( templateInstalledViaPackage )
366
+ {
367
+ var templateDirectory = _fileSystem . combine_paths ( ApplicationParameters . TemplatesLocation , configuration . TemplateCommand . Name ) ;
368
+ var cacheFilePath = _fileSystem . combine_paths ( templateDirectory , _templateParameterCacheFilename ) ;
369
+
370
+ if ( ! _fileSystem . file_exists ( cacheFilePath ) )
371
+ {
372
+ _xmlService . serialize ( get_template_parameters_from_files ( configuration ) . ToList ( ) , cacheFilePath ) ;
373
+ }
374
+
375
+ return _xmlService . deserialize < List < string > > ( cacheFilePath ) ;
376
+ }
377
+ // If the template is not installed via a package, always read the parameters directly as the template may have been updated manually
378
+
379
+ return get_template_parameters_from_files ( configuration ) . ToList ( ) ;
380
+ }
381
+
382
+ protected HashSet < string > get_template_parameters_from_files ( ChocolateyConfiguration configuration )
383
+ {
384
+ var filesList = _fileSystem . get_files ( _fileSystem . combine_paths ( ApplicationParameters . TemplatesLocation , configuration . TemplateCommand . Name ) , "*" , SearchOption . AllDirectories ) ;
385
+ var parametersList = new HashSet < string > ( ) ;
386
+
387
+ foreach ( var filePath in filesList )
388
+ {
389
+ if ( _templateBinaryExtensions . Contains ( _fileSystem . get_file_extension ( filePath ) ) )
390
+ {
391
+ this . Log ( ) . Debug ( "{0} is a binary file, not reading parameters" . format_with ( filePath ) ) ;
392
+ continue ;
393
+ }
394
+
395
+ if ( _fileSystem . get_file_name ( filePath ) == _templateParameterCacheFilename )
396
+ {
397
+ this . Log ( ) . Debug ( "{0} is the parameter cache file, not reading parameters" . format_with ( filePath ) ) ;
398
+ continue ;
399
+ }
400
+
401
+ var fileContents = _fileSystem . read_file ( filePath ) ;
402
+ parametersList . UnionWith ( TokenReplacer . get_tokens ( fileContents , "[[" , "]]" ) ) ;
403
+ }
404
+
405
+ return parametersList ;
406
+ }
348
407
}
349
408
}
0 commit comments