-
Notifications
You must be signed in to change notification settings - Fork 3
/
oyafile.go
158 lines (139 loc) · 3.97 KB
/
oyafile.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
package oyafile
import (
"fmt"
"io"
"path"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
"github.com/tooploox/oya/pkg/errors"
"github.com/tooploox/oya/pkg/raw"
"github.com/tooploox/oya/pkg/semver"
"github.com/tooploox/oya/pkg/task"
"github.com/tooploox/oya/pkg/template"
"github.com/tooploox/oya/pkg/types"
)
type PackReference struct {
ImportPath types.ImportPath
Version semver.Version
// ReplacementPath is a path relative to the root directory, when the replacement for the pack can be found, based on the Replace: directive.
ReplacementPath string
}
type PackReplacements map[types.ImportPath]string
type Oyafile struct {
Dir string
Path string
RootDir string
Shell string
Imports map[types.Alias]types.ImportPath
Tasks task.Table
Values template.Scope
Project string // Project is set for root Oyafile.
Ignore []string // Ignore contains directory exclusion rules.
Requires []PackReference
// Replacements map packs to local paths relative to project root directory for development based on the Replace: directive.
Replacements PackReplacements
IsBuilt bool
relPath string
}
func New(oyafilePath string, rootDir string) (*Oyafile, error) {
relPath, err := filepath.Rel(rootDir, oyafilePath)
log.Debug("Oyafile at ", oyafilePath, " root dir: ", rootDir)
if err != nil {
return nil, err
}
dir := path.Dir(oyafilePath)
return &Oyafile{
Dir: filepath.Clean(dir),
Path: filepath.Clean(oyafilePath),
RootDir: filepath.Clean(rootDir),
Shell: "/bin/bash",
Imports: make(map[types.Alias]types.ImportPath),
Tasks: task.NewTable(),
Values: template.Scope{},
relPath: relPath,
Replacements: make(PackReplacements),
}, nil
}
func Load(oyafilePath, rootDir string) (*Oyafile, bool, error) {
raw, found, err := raw.Load(oyafilePath, rootDir)
if err != nil || !found {
return nil, found, err
}
oyafile, err := Parse(raw)
if err != nil {
return nil, false, wrapLoadErr(err, oyafilePath)
}
return oyafile, true, nil
}
func LoadFromDir(dirPath, rootDir string) (*Oyafile, bool, error) {
raw, found, err := raw.LoadFromDir(dirPath, rootDir)
if err != nil || !found {
return nil, found, err
}
oyafile, err := Parse(raw)
if err != nil {
return nil, false, wrapLoadErr(err, raw.Path)
}
return oyafile, true, nil
}
func (oyafile Oyafile) RunTask(taskName task.Name, args []string, scope template.Scope, stdout, stderr io.Writer) (bool, error) {
if !oyafile.IsBuilt {
return false, errors.Errorf("Internal error: Oyafile has not been built")
}
task, ok := oyafile.Tasks.LookupTask(taskName)
if !ok {
return false, nil
}
err := task.Exec(oyafile.Dir, args, scope, stdout, stderr)
if err != nil {
err = oyafile.enrichError(err, taskName, args)
return true, errors.Wrap(
err,
ErrTaskFail{
TaskName: taskName,
Args: args,
},
errors.Location{
VerboseName: fmt.Sprintf("in file %q", oyafile.Path),
Name: oyafile.Path,
},
)
}
return true, nil
}
func (oyafile Oyafile) enrichError(err error, taskName task.Name, args []string) error {
alias, name := taskName.Split()
if alias.IsEmpty() {
return err
}
importPath, ok := oyafile.Imports[alias]
if !ok {
return err
}
return errors.Wrap(
err,
ErrTaskFail{
TaskName: task.Name(name),
Args: args,
},
errors.Location{
Name: importPath.String(),
VerboseName: fmt.Sprintf("in %v imported as %q", importPath, alias),
},
)
}
func (oyafile Oyafile) Equals(other Oyafile) bool {
// TODO: Far from perfect, we should ensure relative vs absolute paths work.
// The simplest thing is probably to ensure oyafile.Dir is always absolute.
return filepath.Clean(oyafile.Dir) == filepath.Clean(other.Dir)
}
func wrapLoadErr(err error, oyafilePath string) error {
return errors.Wrapf(err, "error loading Oyafile %v", oyafilePath)
}
func (o *Oyafile) Ignores() string {
return strings.Join(o.Ignore, "\n")
}
func (o *Oyafile) RelPath() string {
return o.relPath
}