@@ -10,6 +10,8 @@ import (
10
10
11
11
"github.com/gruntwork-io/go-commons/collections"
12
12
13
+ "github.com/gruntwork-io/boilerplate/manifest"
14
+
13
15
"github.com/gruntwork-io/boilerplate/config"
14
16
"github.com/gruntwork-io/boilerplate/errors"
15
17
getter_helper "github.com/gruntwork-io/boilerplate/getter-helper"
@@ -22,12 +24,23 @@ import (
22
24
// The name of the variable that contains the current value of the loop in each iteration of for_each
23
25
const eachVarName = "__each__"
24
26
27
+ // Maintaining original function name and signature for backwards compatability
28
+ func ProcessTemplate (options , rootOpts * options.BoilerplateOptions , thisDep variables.Dependency ) error {
29
+ _ , err := processTemplateInternal (options , rootOpts , thisDep , false )
30
+ return err
31
+ }
32
+
33
+ // ProcessTemplateWithManifest works like ProcessTemplate but also returns the manifest of generated files
34
+ func ProcessTemplateWithManifest (options , rootOpts * options.BoilerplateOptions , thisDep variables.Dependency ) (* manifest.Manifest , error ) {
35
+ return processTemplateInternal (options , rootOpts , thisDep , true )
36
+ }
37
+
25
38
// Process the boilerplate template specified in the given options and use the existing variables. This function will
26
39
// download remote templates to a temporary working directory, which is cleaned up at the end of the function. This
27
40
// function will load any missing variables (either from command line options or by prompting the user), execute all the
28
41
// dependent boilerplate templates, and then execute this template. Note that we pass in rootOptions so that template
29
42
// dependencies can inspect properties of the root template.
30
- func ProcessTemplate (options , rootOpts * options.BoilerplateOptions , thisDep variables.Dependency ) error {
43
+ func processTemplateInternal (options , rootOpts * options.BoilerplateOptions , thisDep variables.Dependency , returnManifest bool ) ( * manifest. Manifest , error ) {
31
44
// If TemplateFolder is already set, use that directly as it is a local template. Otherwise, download to a temporary
32
45
// working directory.
33
46
if options .TemplateFolder == "" {
@@ -37,7 +50,7 @@ func ProcessTemplate(options, rootOpts *options.BoilerplateOptions, thisDep vari
37
50
os .RemoveAll (workingDir )
38
51
}()
39
52
if err != nil {
40
- return err
53
+ return nil , err
41
54
}
42
55
43
56
// Set the TemplateFolder of the options to the download dir
@@ -46,56 +59,64 @@ func ProcessTemplate(options, rootOpts *options.BoilerplateOptions, thisDep vari
46
59
47
60
rootBoilerplateConfig , err := config .LoadBoilerplateConfig (rootOpts )
48
61
if err != nil {
49
- return err
62
+ return nil , err
50
63
}
51
64
if err := config .EnforceRequiredVersion (rootBoilerplateConfig ); err != nil {
52
- return err
65
+ return nil , err
53
66
}
54
67
55
68
boilerplateConfig , err := config .LoadBoilerplateConfig (options )
56
69
if err != nil {
57
- return err
70
+ return nil , err
58
71
}
59
72
if err := config .EnforceRequiredVersion (boilerplateConfig ); err != nil {
60
- return err
73
+ return nil , err
61
74
}
62
75
63
76
vars , err := config .GetVariables (options , boilerplateConfig , rootBoilerplateConfig , thisDep )
64
77
if err != nil {
65
- return err
78
+ return nil , err
66
79
}
67
80
68
81
err = os .MkdirAll (options .OutputFolder , 0777 )
69
82
if err != nil {
70
- return errors .WithStackTrace (err )
83
+ return nil , errors .WithStackTrace (err )
71
84
}
72
85
73
86
err = processHooks (boilerplateConfig .Hooks .BeforeHooks , options , vars )
74
87
if err != nil {
75
- return err
88
+ return nil , err
76
89
}
77
90
78
91
err = processDependencies (boilerplateConfig .Dependencies , options , boilerplateConfig .GetVariablesMap (), vars )
79
92
if err != nil {
80
- return err
93
+ return nil , err
81
94
}
82
95
83
96
partials , err := processPartials (boilerplateConfig .Partials , options , vars )
84
97
if err != nil {
85
- return err
98
+ return nil , err
86
99
}
87
100
88
- err = processTemplateFolder (boilerplateConfig , options , vars , partials )
101
+ var fileManifest * manifest.Manifest
102
+ if returnManifest {
103
+ fileManifest = manifest .NewManifest (options .OutputFolder )
104
+ }
105
+
106
+ err = processTemplateFolder (boilerplateConfig , options , vars , partials , fileManifest )
89
107
if err != nil {
90
- return err
108
+ return nil , err
91
109
}
92
110
93
111
err = processHooks (boilerplateConfig .Hooks .AfterHooks , options , vars )
94
112
if err != nil {
95
- return err
113
+ return nil , err
96
114
}
97
115
98
- return nil
116
+ if returnManifest {
117
+ return fileManifest , nil
118
+ }
119
+ return nil , nil
99
120
}
100
121
101
122
func processPartials (partials []string , opts * options.BoilerplateOptions , vars map [string ]interface {}) ([]string , error ) {
@@ -455,6 +476,7 @@ func processTemplateFolder(
455
476
opts * options.BoilerplateOptions ,
456
477
variables map [string ]interface {},
457
478
partials []string ,
479
+ fileManifest * manifest.Manifest ,
458
480
) error {
459
481
util .Logger .Printf ("Processing templates in %s and outputting generated files to %s" , opts .TemplateFolder , opts .OutputFolder )
460
482
@@ -477,7 +499,7 @@ func processTemplateFolder(
477
499
return createOutputDir (path , opts , variables )
478
500
} else {
479
501
engine := determineTemplateEngine (processedEngines , path )
480
- return processFile (path , opts , variables , partials , engine )
502
+ return processFile (path , opts , variables , partials , engine , fileManifest )
481
503
}
482
504
})
483
505
}
@@ -490,16 +512,17 @@ func processFile(
490
512
variables map [string ]interface {},
491
513
partials []string ,
492
514
engine variables.TemplateEngineType ,
515
+ fileManifest * manifest.Manifest ,
493
516
) error {
494
517
isText , err := util .IsTextFile (path )
495
518
if err != nil {
496
519
return err
497
520
}
498
521
499
522
if isText {
500
- return processTemplate (path , opts , variables , partials , engine )
523
+ return processTemplate (path , opts , variables , partials , engine , fileManifest )
501
524
} else {
502
- return copyFile (path , opts , variables )
525
+ return copyFile (path , opts , variables , fileManifest )
503
526
}
504
527
}
505
528
@@ -548,14 +571,24 @@ func outPath(file string, opts *options.BoilerplateOptions, variables map[string
548
571
}
549
572
550
573
// Copy the given file, which is in options.TemplateFolder, to options.OutputFolder
551
- func copyFile (file string , opts * options.BoilerplateOptions , variables map [string ]interface {}) error {
574
+ func copyFile (file string , opts * options.BoilerplateOptions , variables map [string ]interface {}, fileManifest * manifest. Manifest ) error {
552
575
destination , err := outPath (file , opts , variables )
553
576
if err != nil {
554
577
return err
555
578
}
556
579
557
580
util .Logger .Printf ("Copying %s to %s" , file , destination )
558
- return util .CopyFile (file , destination )
581
+ if err := util .CopyFile (file , destination ); err != nil {
582
+ return err
583
+ }
584
+
585
+ if fileManifest != nil {
586
+ if err := fileManifest .AddFile (destination ); err != nil {
587
+ return err
588
+ }
589
+ }
590
+
591
+ return nil
559
592
}
560
593
561
594
// Run the template at templatePath, which is in templateFolder, through the Go template engine with the given
@@ -566,6 +599,7 @@ func processTemplate(
566
599
vars map [string ]interface {},
567
600
partials []string ,
568
601
engine variables.TemplateEngineType ,
602
+ fileManifest * manifest.Manifest ,
569
603
) error {
570
604
destination , err := outPath (templatePath , opts , vars )
571
605
if err != nil {
@@ -588,7 +622,17 @@ func processTemplate(
588
622
destination = strings .TrimSuffix (destination , ".jsonnet" )
589
623
}
590
624
591
- return util .WriteFileWithSamePermissions (templatePath , destination , []byte (out ))
625
+ if err := util .WriteFileWithSamePermissions (templatePath , destination , []byte (out )); err != nil {
626
+ return err
627
+ }
628
+
629
+ if fileManifest != nil {
630
+ if err := fileManifest .AddFile (destination ); err != nil {
631
+ return err
632
+ }
633
+ }
634
+
635
+ return nil
592
636
}
593
637
594
638
// Return true if this is a path that should not be copied
0 commit comments