-
Notifications
You must be signed in to change notification settings - Fork 52
/
generate_modules.go
178 lines (149 loc) · 4.39 KB
/
generate_modules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package generate
import (
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"text/template"
"github.com/commitdev/zero/internal/config"
"github.com/commitdev/zero/internal/constants"
"github.com/commitdev/zero/internal/module"
"github.com/commitdev/zero/internal/util"
"github.com/commitdev/zero/pkg/util/exit"
"github.com/commitdev/zero/pkg/util/flog"
"github.com/k0kubun/pp"
"github.com/commitdev/zero/pkg/util/fs"
)
func GenerateModules(cfg *config.GeneratorConfig) {
var templateModules []*module.TemplateModule
// TODO: Refactor this since the module struct is changing
// Initiate all the modules defined in the config
// for _, moduleConfig := range cfg.Modules {
//mod, err := module.NewTemplateModule(moduleConfig)
// if err != nil {
// exit.Error("module failed to load: %s", err)
// }
// templateModules = append(templateModules, mod)
// }
// Prompt for module params and execute each of the generator modules
for _, mod := range templateModules {
// TODO: read zero-project.yml instead
err := Generate(mod, cfg)
if err != nil {
exit.Error("module %s: %s", mod.Source, err)
}
}
}
type TemplateParams struct {
Name string
Context map[string]string
Params map[string]string
}
func Generate(mod *module.TemplateModule, generatorCfg *config.GeneratorConfig) error {
moduleDir := path.Join(module.GetSourceDir(mod.Source), mod.Config.TemplateConfig.InputDir)
delimiters := mod.Config.TemplateConfig.Delimiters
overwrite := mod.Overwrite
outputDir := mod.Output
if outputDir == "" {
outputDir = mod.Config.TemplateConfig.OutputDir
}
templateData := TemplateParams{}
templateData.Name = generatorCfg.Name
templateData.Context = generatorCfg.Context
templateData.Params = mod.Params
fileTmplts := NewTemplates(moduleDir, outputDir, overwrite)
ExecuteTemplates(fileTmplts, templateData, delimiters)
return nil
}
type TemplateConfig struct {
source string
destination string
isTemplate bool
}
// NewTemplates walks the module directory to find all to be templated
func NewTemplates(moduleDir string, outputDir string, overwrite bool) []*TemplateConfig {
templates := []*TemplateConfig{}
paths, err := GetAllFilePathsInDirectory(moduleDir)
if err != nil {
panic(err)
}
for _, path := range paths {
ignoredPaths, _ := regexp.Compile(constants.IgnoredPaths)
if ignoredPaths.MatchString(path) {
continue
}
_, file := filepath.Split(path)
hasTmpltSuffix := strings.HasSuffix(file, constants.TemplateExtn)
if hasTmpltSuffix {
file = strings.Replace(file, constants.TemplateExtn, "", -1)
}
outputPath := fs.ReplacePath(path, moduleDir, outputDir)
if !overwrite {
if exists, _ := fs.FileExists(outputPath); exists {
flog.Warnf("%v already exists. skipping.", outputPath)
continue
}
}
templates = append(templates, &TemplateConfig{
source: path,
destination: outputPath,
isTemplate: hasTmpltSuffix,
})
}
return templates
}
// GetAllFilePathsInDirectory Recursively get all file paths in directory, including sub-directories.
func GetAllFilePathsInDirectory(moduleDir string) ([]string, error) {
var paths []string
err := filepath.Walk(moduleDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
paths = append(paths, path)
}
return nil
})
if err != nil {
return nil, err
}
return paths, nil
}
func ExecuteTemplates(templates []*TemplateConfig, data interface{}, delimiters []string) {
var wg sync.WaitGroup
leftDelim := delimiters[0]
rightDelim := delimiters[1]
if leftDelim == "" {
leftDelim = "{{"
}
if rightDelim == "" {
rightDelim = "}}"
}
flog.Infof("Templating params:")
pp.Println(data)
for _, tmpltConfig := range templates {
source := tmpltConfig.source
dest := tmpltConfig.destination
outputDirPath, _ := path.Split(dest)
err := fs.CreateDirs(outputDirPath)
if err != nil {
flog.Errorf("Error creating directory '%s': %v", source, err)
}
f, err := os.Create(dest)
if err != nil {
flog.Errorf("Error initializing file '%s'", err)
}
// @TODO if strict mode then only copy file
name := path.Base(source)
template, err := template.New(name).Delims(leftDelim, rightDelim).Funcs(util.FuncMap).ParseFiles(source)
err = template.Execute(f, data)
if err != nil {
flog.Errorf("Error templating '%s': %v", source, err)
} else {
flog.Successf("Finished templating : %s", dest)
}
}
wg.Wait()
}