-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
163 lines (138 loc) · 3.79 KB
/
Copy pathmain.go
File metadata and controls
163 lines (138 loc) · 3.79 KB
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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/dibrinsofor/chase/src"
"github.com/dibrinsofor/chase/src/engine"
"github.com/dibrinsofor/chase/src/state"
)
// todo: add file watcher to detect chasefile in root directory and generate dependency graph
var filename = "Chasefile"
func runLint(env *src.ChaseEnv) {
cache, err := state.Load("")
if err != nil {
fmt.Println("No state cache found. Run a build first to trace dependencies.")
return
}
hasIssues := false
for _, dash := range env.Dashes() {
ts := cache.GetTarget(dash.Name())
if ts == nil {
fmt.Printf("%s:\n ⚠ no traced data (run build first)\n\n", dash.Name())
continue
}
declaredInputs := toSet(dash.Inputs())
declaredOutputs := toSet(dash.Outputs())
tracedInputs := toSet(ts.TracedInputs)
tracedOutputs := toSet(ts.TracedOutputs)
undeclaredInputs := diff(tracedInputs, declaredInputs)
undeclaredOutputs := diff(tracedOutputs, declaredOutputs)
if len(undeclaredInputs) == 0 && len(undeclaredOutputs) == 0 {
fmt.Printf("%s:\n ✓ all dependencies declared\n\n", dash.Name())
continue
}
hasIssues = true
fmt.Printf("%s:\n", dash.Name())
if len(undeclaredInputs) > 0 {
fmt.Println(" ⚠ undeclared inputs:")
for _, p := range undeclaredInputs {
fmt.Printf(" + %s\n", p)
}
}
if len(undeclaredOutputs) > 0 {
fmt.Println(" ⚠ undeclared outputs:")
for _, p := range undeclaredOutputs {
fmt.Printf(" + %s\n", p)
}
}
fmt.Println()
fmt.Println(" Suggested additions:")
if len(undeclaredInputs) > 0 {
fmt.Printf(" inputs: %v\n", undeclaredInputs)
}
if len(undeclaredOutputs) > 0 {
fmt.Printf(" outputs: %v\n", undeclaredOutputs)
}
fmt.Println()
}
if hasIssues {
os.Exit(1)
}
}
func toSet(slice []string) map[string]bool {
set := make(map[string]bool)
for _, s := range slice {
set[s] = true
}
return set
}
func diff(a, b map[string]bool) []string {
var result []string
for k := range a {
if !b[k] {
result = append(result, k)
}
}
return result
}
func main() {
// flags -l (list all sprints), -{sprint name}
l := flag.Bool("l", false, "list all dashes in the chasefile")
r := flag.String("r", "", "run specific dash")
j := flag.Int("j", 0, "number of parallel workers (default: number of CPUs)")
lint := flag.Bool("lint", false, "compare traced deps vs declared deps")
flag.Parse()
// check if chasefile exists
info, err := os.Stat(filename)
if os.IsNotExist(err) || info.IsDir() || err != nil {
panic(fmt.Errorf("chase: error opening chasefile: %w", err))
}
eng := engine.New(filename, *j)
if *l {
res := eng.Compute(context.Background(), engine.ComputeKey{Kind: engine.KeyMarshaled, Target: filename})
if res.Err != nil {
panic(res.Err)
}
chaseIR, ok := res.Value.(*src.ChaseEnv)
if !ok {
panic(fmt.Errorf("chase: invalid marshaled value type: %T", res.Value))
}
src.ListDashes(chaseIR)
return
}
if *lint {
res := eng.Compute(context.Background(), engine.ComputeKey{Kind: engine.KeyMarshaled, Target: filename})
if res.Err != nil {
panic(res.Err)
}
chaseIR, ok := res.Value.(*src.ChaseEnv)
if !ok {
panic(fmt.Errorf("chase: invalid marshaled value type: %T", res.Value))
}
runLint(chaseIR)
return
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigCh
cancel()
}()
res := eng.Compute(ctx, engine.ComputeKey{Kind: engine.KeyExecuted, Target: *r})
if res.Err != nil {
panic(res.Err)
}
summary, ok := res.Value.(*engine.ExecutionSummary)
if !ok {
panic(fmt.Errorf("chase: invalid execution value type: %T", res.Value))
}
for _, w := range summary.Warnings {
fmt.Fprintf(os.Stderr, "warning: %v\n", w)
}
}