-
Notifications
You must be signed in to change notification settings - Fork 2
/
runner.go
178 lines (162 loc) · 4.12 KB
/
runner.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 sol
import (
"context"
"github.com/apigear-io/cli/pkg/cfg"
"github.com/apigear-io/cli/pkg/gen"
"github.com/apigear-io/cli/pkg/helper"
"github.com/apigear-io/cli/pkg/model"
"github.com/apigear-io/cli/pkg/spec"
"github.com/apigear-io/cli/pkg/tasks"
)
type Runner struct {
tm *tasks.TaskManager
// tasks map[string]*task
}
func NewRunner() *Runner {
return &Runner{
// tasks: make(map[string]*task),
tm: tasks.NewTaskManager(),
}
}
func (r *Runner) HasTask(name string) bool {
return r.tm.Has(name)
}
func (r *Runner) TaskFiles() []string {
return r.tm.Names()
}
func (r *Runner) OnTask(fn func(*tasks.TaskEvent)) {
r.tm.On(fn)
}
func (r *Runner) RunSource(ctx context.Context, source string, force bool) error {
task := func(ctx context.Context) error {
return r.runSolutionFromSource(ctx, source, force)
}
meta := map[string]interface{}{
"solution": source,
}
r.tm.Register(source, meta, task)
return r.tm.Run(ctx, source)
}
// RunDoc runs the given file task once.
// It should not act on a cached value.
func (r *Runner) RunDoc(ctx context.Context, file string, doc *spec.SolutionDoc) error {
task := func(ctx context.Context) error {
return runSolution(doc)
}
meta := map[string]interface{}{
"solution": file,
}
r.tm.Register(file, meta, task)
return r.tm.Run(ctx, file)
}
func (r *Runner) WatchSource(ctx context.Context, source string, force bool) error {
doc, err := ReadSolutionDoc(source)
if err != nil {
return err
}
deps := doc.AggregateDependencies()
deps = append(deps, source)
task := func(ctx context.Context) error {
return r.runSolutionFromSource(ctx, source, force)
}
meta := map[string]interface{}{
"solution": source,
}
r.tm.Register(source, meta, task)
return r.tm.Watch(ctx, source, deps...)
}
// WatchDoc starts the watch of the given file task.
func (r *Runner) WatchDoc(ctx context.Context, file string, doc *spec.SolutionDoc) error {
if err := doc.Validate(); err != nil {
return err
}
deps := doc.AggregateDependencies()
deps = append(deps, file)
task := func(ctx context.Context) error {
return runSolution(doc)
}
meta := map[string]interface{}{
"solution": file,
}
r.tm.Register(file, meta, task)
return r.tm.Watch(ctx, file, deps...)
}
// StopWatch stops the watch of the given file task.
func (r *Runner) StopWatch(file string) {
err := r.tm.Cancel(file)
if err != nil {
log.Warn().Err(err).Msgf("stop watch %s", file)
}
}
func (r *Runner) Clear() {
r.tm.CancelAll()
}
func (r *Runner) runSolutionFromSource(ctx context.Context, source string, force bool) error {
doc, err := ReadSolutionDoc(source)
if err != nil {
return err
}
if force {
for _, target := range doc.Targets {
target.Force = true
}
}
return runSolution(doc)
}
func runSolution(doc *spec.SolutionDoc) error {
log.Debug().Msgf("run solution %s", doc.RootDir)
if err := doc.Validate(); err != nil {
return err
}
rootDir := doc.RootDir
for _, target := range doc.Targets {
name := target.Name
outDir := target.GetOutputDir(rootDir)
if name == "" {
name = helper.BaseName(outDir)
}
system := model.NewSystem(name)
if err := parseInputs(system, target.ExpandedInputs()); err != nil {
return err
}
if err := helper.MakeDir(outDir); err != nil {
return err
}
// TODO: clean up this code
if doc.Meta == nil {
doc.Meta = make(map[string]interface{})
}
doc.Meta["Layer"] = target
doc.Meta["App"] = cfg.GetBuildInfo("cli")
opts := gen.Options{
OutputDir: outDir,
TemplatesDir: target.TemplatesDir,
System: system,
Features: target.Features,
Force: target.Force,
Meta: doc.Meta,
}
g, err := gen.New(opts)
if err != nil {
return err
}
doc, err := gen.ReadRulesDoc(target.RulesFile)
if err != nil {
return err
}
bi := cfg.GetBuildInfo("cli")
ok, errs := doc.CheckEngines(bi.Version)
if !ok {
// a warning should be enough
log.Warn().Msgf("template requires cli version %s. Only found %s", doc.Engines.Cli, bi.Version)
for _, err := range errs {
log.Warn().Err(err).Msg("cli version check error")
}
}
err = g.ProcessRules(doc)
if err != nil {
return err
}
}
return nil
}