-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject.go
363 lines (332 loc) · 8.43 KB
/
project.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package project
import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"github.com/missena-corp/gally/repo"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
)
const configFileName = ".gally.yml"
var envVars map[string]string
type Project struct {
// BaseDir is the directory where the configuration file is located
BaseDir string `mapstructure:"-"`
// command to build the project
BuildScript string `mapstructure:"build"`
// where the config file is located
ConfigFile string `mapstructure:"-"`
// list of dependency folders
DependsOn []string `mapstructure:"depends_on"`
Dependencies Dependencies
// Dir is the directory where the work happen
Dir string `mapstructure:"workdir"`
Disable bool
Env []struct {
Name string
Value string
}
Ignore []string
IsLibrary bool `mapstructure:"is_library"`
Name string
RootDir string
Scripts map[string]string
Strategies Strategies
Tag bool `mapstructure:"tag"`
VersionScript string `mapstructure:"version"`
// cache
bumped *bool `mapstructure:"-"`
updated *bool `mapstructure:"-"`
version *string `mapstructure:"-"`
}
type Projects map[string]*Project
func BuildTag(name *string, tag string, rootDir string) error {
sp := strings.Split(tag, "@")
if len(sp) != 2 {
return fmt.Errorf("%q is not a valid tag", tag)
}
p := Find(sp[0], rootDir)
if p == nil {
return fmt.Errorf("project %q not found", sp[0])
} else if p != nil && name != nil && p.Name != *name {
return fmt.Errorf("project %s and tag %q do not match", *name, sp[0])
}
version := p.Version()
// this allow to have project without `version` defined
if version != "" && version != sp[1] {
return fmt.Errorf("versions mismatch %q≠%q", sp[1], version)
}
return p.runBuild(version)
}
func BuildForceWithoutTag(name *string, rootDir string, noDep bool) error {
projects := Projects{}
if name == nil {
allProjects := FindAllUpdated(rootDir, noDep)
for _, p := range allProjects {
if p.Tag {
projects[p.Name] = p
}
}
} else {
p := Find(*name, rootDir)
if p == nil {
return fmt.Errorf("project %q not found", *name)
}
if p.Tag {
projects[*name] = p
}
}
for _, p := range projects {
version := p.Version()
if version == "" {
return fmt.Errorf("could not find version for %s", p.Name)
}
if err := p.runBuild(p.Version()); err != nil {
return err
}
}
return nil
}
func BuildWithoutTag(name *string, rootDir string, noDep bool) error {
projects := Projects{}
if name == nil {
projects = FindAllUpdated(rootDir, noDep)
} else {
p := Find(*name, rootDir)
if p == nil {
return fmt.Errorf("project %q not found", *name)
}
projects[*name] = p
}
for _, p := range projects {
if !p.Tag {
if err := p.runBuild(p.Version()); err != nil {
return err
}
}
}
return nil
}
func (p *Project) exec(str string, env Env) ([]byte, error) {
cmd := exec.Command("sh", "-c", str)
cmd.Dir = p.Dir
cmd.Env = append(os.Environ(), env.ToSlice()...)
return cmd.Output()
}
func Find(name string, rootDir string) *Project {
return FindAll(rootDir)[name]
}
func findPaths(rootDir string) (dirs []string, err error) {
path, _ := os.Getwd()
abs, _ := filepath.Abs(rootDir)
base, err := filepath.Rel(path, abs)
if err != nil {
base = "."
err = nil
}
cmd := exec.Command("git", "ls-files")
cmd.Dir = rootDir
output, _ := cmd.Output()
for _, v := range strings.Split(string(output), "\n") {
l := len(v) - len(configFileName)
if l >= 0 && v[l:] == configFileName {
if l == 0 {
dirs = append(dirs, base)
}
if l > 0 {
dirs = append(dirs, fmt.Sprintf("%s%s%s", base, "/", v[:l-1]))
}
}
}
return
}
func FindAll(rootDir string) Projects {
if rootDir == "" {
rootDir = repo.Root()
}
projects := make(Projects)
paths, _ := findPaths(rootDir)
for _, path := range paths {
p := New(path, rootDir)
if d, exists := projects[p.Name]; exists {
jww.FATAL.Fatalf("2 projects with name %q exist:\n- %q\n- %q\n", p.Name, d.BaseDir, p.BaseDir)
}
projects[p.Name] = p
}
return projects
}
func FindAllUpdated(rootDir string, noDep bool) Projects {
projects := make(Projects)
for name, project := range FindAll(rootDir) {
if project.WasUpdated(noDep) {
projects[name] = project
}
}
return projects
}
func FindByName(rootDir, name string) *Project {
projects := FindAll(rootDir)
return projects[name]
}
func (p *Project) ignored(file string) bool {
for _, pattern := range p.Ignore {
files, err := filepath.Glob(pattern)
if err != nil {
panic(err)
}
for _, f := range files {
if f == file {
return true
}
}
}
return false
}
// New reads current config in directory
// the function is expecting full path as argument
func New(dir, rootDir string, isDependency ...bool) (p *Project) {
v := viper.New()
if !path.IsAbs(dir) {
d, err := filepath.Abs(dir)
if err != nil {
jww.FATAL.Fatalf("unable to expand directory %q: %v", d, err)
}
dir = d
}
file := path.Join(dir, configFileName)
v.SetConfigFile(file)
if err := v.ReadInConfig(); err != nil && len(isDependency) == 0 {
jww.FATAL.Fatalf("could not read config file %q: %v", file, err)
}
if err := v.Unmarshal(&p); err != nil {
jww.FATAL.Fatalf("unable to decode file %s into struct: %v", file, err)
}
// init Name based on current directory if not set in config
if p.Name == "" {
p.Name = filepath.Base(dir)
}
// init BaseDir
p.BaseDir = dir
if p.Dir == "" {
p.Dir = dir
}
if !path.IsAbs(p.Dir) {
p.Dir = path.Clean(path.Join(dir, p.Dir))
}
if _, err := os.Stat(p.Dir); os.IsNotExist(err) {
jww.FATAL.Fatalf("workdir directory %q does not exist", p.Dir)
}
// init RootDir
p.RootDir = path.Clean(rootDir)
if !path.IsAbs(rootDir) {
d, err := filepath.Abs(rootDir)
if err != nil {
jww.FATAL.Fatalf("unable to expand directory %q: %v", d, err)
}
p.RootDir = d
}
// init Strategies, set default strategies if not set
if len(p.Strategies) == 0 {
p.Strategies = defaultStrategies
}
// init Dependencies
for _, dep := range p.DependsOn {
p.Dependencies = append(p.Dependencies, New(path.Join(p.BaseDir, dep), rootDir, true))
}
return p
}
func newFalse() *bool { b := false; return &b }
func newTrue() *bool { b := true; return &b }
// Run a command by its name
func (p *Project) Run(s string) error {
script, ok := p.Scripts[s]
if !ok {
return fmt.Errorf("script %q not available", s)
}
return p.run(script, p.env(generateVersion()))
}
// run a command script for a project
func (p *Project) run(script string, env Env) error {
if p.Disable {
jww.WARN.Printf("project %q disabled", p.BaseDir)
return nil
}
for _, dep := range p.Dependencies {
// build dependencies if they are libraries
if dep.IsLibrary {
dep.runBuild(dep.Version())
}
}
cmd := exec.Command("sh", "-c", script)
cmd.Dir = p.Dir
cmd.Env = append(os.Environ(), env.ToSlice()...)
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
jww.INFO.Printf("running %q in directory %q\n", script, cmd.Dir)
if err := cmd.Start(); err != nil {
return err
}
return cmd.Wait()
}
func (p *Project) runBuild(version string) error {
jww.INFO.Printf("building %q version %q\n", p.Name, version)
return p.run(p.BuildScript, p.env(setVersion(version)))
}
func (projs Projects) ToSlice(noDep bool) []map[string]interface{} {
out := make([]map[string]interface{}, 0)
for _, p := range projs {
addition := map[string]interface{}{
"directory": p.BaseDir,
"environment": p.env(generateVersion()),
"name": p.Name,
"update": p.WasUpdated(noDep),
"version": p.Version(),
}
if len(p.DependsOn) != 0 {
addition["dependencies"] = p.DependsOn
}
out = append(out, addition)
}
return out
}
func (p *Project) Version() string {
if p.version != nil {
return *p.version
}
if p.VersionScript == "" {
version := repo.Version(p.Dir, p.Dependencies.paths(), p.Ignore)
p.version = &version
return version
}
v, _ := p.exec(p.VersionScript, p.env())
version := strings.TrimSpace(string(v))
p.version = &version
return version
}
func (p *Project) WasUpdated(noDep bool) bool {
// cache response
if p.updated != nil {
return *p.updated
}
for _, f := range p.Strategies.UpdatedFiles() {
if strings.HasPrefix(f, p.BaseDir) && !p.ignored(f) {
p.updated = newTrue()
return true
}
if noDep {
continue
}
for _, dep := range p.Dependencies {
if dep.WasUpdated(noDep) {
p.updated = newTrue()
return true
}
}
}
p.updated = newFalse()
return false
}