-
Notifications
You must be signed in to change notification settings - Fork 3
/
imports.go
82 lines (73 loc) · 2.07 KB
/
imports.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
package oyafile
import (
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/tooploox/oya/pkg/template"
"github.com/tooploox/oya/pkg/types"
)
type PackLoader interface {
Load(importPath types.ImportPath) (*Oyafile, bool, error)
}
func (oyafile *Oyafile) Build(loader PackLoader) error {
// Do not resolve imports when loading Oyafile. Sometimes, we need to load Oyafile before packs are ready to be imported.
if !oyafile.IsBuilt {
err := oyafile.resolveImports(loader)
if err != nil {
return err
}
oyafile.IsBuilt = true
}
return nil
}
func (oyafile *Oyafile) resolveImports(loader PackLoader) error {
for alias, importPath := range oyafile.Imports {
packOyafile, err := oyafile.loadPackOyafile(loader, importPath)
if err != nil {
return err
}
err = packOyafile.Build(loader)
if err != nil {
return err
}
err = oyafile.Values.UpdateScopeAt(string(alias),
func(scope template.Scope) template.Scope {
// Values in the main Oyafile overwrite values in the pack Oyafile.
merged := packOyafile.Values.Merge(scope)
merged = merged.Merge(aliasBuiltin(alias))
// Task is keeping a pointed to the scope.
packOyafile.Values.Replace(merged)
return merged
}, false)
if err != nil {
return errors.Wrapf(err, "error merging values for imported pack %v", alias)
}
oyafile.Tasks.ImportTasks(alias, packOyafile.Tasks)
}
return nil
}
func (oyafile *Oyafile) loadPackOyafile(loader PackLoader, importPath types.ImportPath) (*Oyafile, error) {
o, found, err := loader.Load(importPath)
if err != nil {
return nil, err
}
if found {
return o, nil
}
// Attempt to load the Oyafile using the local path.
fullImportPath := filepath.Join(oyafile.RootDir, string(importPath))
if isValidImportPath(fullImportPath) {
o, found, err := LoadFromDir(fullImportPath, oyafile.RootDir)
if err != nil {
return nil, err
}
if found {
return o, nil
}
}
return nil, errors.Errorf("missing pack %v", importPath)
}
func isValidImportPath(fullImportPath string) bool {
f, err := os.Stat(fullImportPath)
return err == nil && f.IsDir()
}