-
Notifications
You must be signed in to change notification settings - Fork 0
/
workspace.go
230 lines (200 loc) · 4.85 KB
/
workspace.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
package main
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/pgavlin/dawn"
"github.com/pgavlin/dawn/label"
starlark_os "github.com/pgavlin/dawn/lib/os"
starlark_sh "github.com/pgavlin/dawn/lib/sh"
"github.com/spf13/cobra"
starlark_json "go.starlark.net/lib/json"
"go.starlark.net/repl"
"go.starlark.net/starlark"
)
type workspace struct {
root string
package_ string
reindex bool
project *dawn.Project
graph graph
renderer renderer
}
func (w *workspace) init() error {
wd, err := os.Getwd()
if err != nil {
return err
}
rootDir := wd
for {
if _, err := os.Stat(filepath.Join(rootDir, ".dawnconfig")); err == nil {
break
}
if rootDir == "/" || rootDir == "." {
return errors.New("could not find .dawnconfig")
}
rootDir = filepath.Dir(rootDir)
}
w.root = rootDir
pkg, err := filepath.Rel(w.root, wd)
if err != nil {
return err
}
if pkg == "." {
pkg = pkg[1:]
}
w.package_ = "//" + pkg
return nil
}
func (w *workspace) labelArg(args []string) (*label.Label, []string, error) {
rawlabel := ":default"
switch len(args) {
case 0:
// OK
default:
if !strings.HasPrefix(args[0], "--") {
rawlabel, args = args[0], args[1:]
// Unless it is obviously a label, see if the arg is interpretable as a path to a source file.
if !strings.HasPrefix(rawlabel, "//") && !strings.ContainsRune(rawlabel, ':') {
p, err := filepath.Abs(rawlabel)
if err != nil {
return nil, nil, fmt.Errorf("computing path: %w", err)
}
stat, err := os.Lstat(p)
if !os.IsNotExist(err) {
if err != nil {
return nil, nil, fmt.Errorf("stating file: %w", err)
}
if !stat.IsDir() {
modulePath, sourceFile := path.Split(filepath.ToSlash(rawlabel))
rawlabel = fmt.Sprintf("source:%v:%v", modulePath, sourceFile)
}
}
}
}
}
label, err := label.Parse(rawlabel)
if err != nil {
return nil, nil, err
}
label, err = label.RelativeTo(w.package_)
if err != nil {
return nil, nil, err
}
return label, args, nil
}
func (w *workspace) validLabels(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
targets, err := dawn.Targets(w.root)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
labels := make([]string, 0, len(targets))
for _, t := range targets {
if dawn.IsTarget(t.Label) {
labels = append(labels, fmt.Sprintf("%v\t%s", t.Label, t.Summary))
}
}
return labels, cobra.ShellCompDirectiveDefault
}
func (w *workspace) loadProject(args []string, index, quiet bool) error {
rendered := make(chan bool)
firstLoad := true
renderer, err := newRenderer(func() {
// We only care about the first onload event.
if firstLoad {
close(rendered)
firstLoad = false
}
})
if err != nil {
return err
}
w.renderer = renderer
events := dawn.Events(w.renderer)
if quiet {
close(rendered)
events = dawn.DiscardEvents
}
options := &dawn.LoadOptions{
Args: args,
Events: events,
Builtins: starlark.StringDict{
"json": starlark_json.Module,
"os": starlark_os.Module,
"sh": starlark_sh.Module,
},
PreferIndex: !w.reindex && index,
}
project, err := dawn.Load(w.root, options)
if err != nil {
renderer.Close()
return err
}
w.project = project
w.graph = buildGraph(w.project)
<-rendered
return nil
}
func (w *workspace) target(label *label.Label) (dawn.Target, error) {
return w.project.Target(label)
}
func (w *workspace) flag(name string) (*dawn.Flag, error) {
return w.project.Flag(name)
}
func (w *workspace) flags() []*dawn.Flag {
return w.project.Flags()
}
func (w *workspace) depends(label *label.Label) ([]string, error) {
t, err := w.target(label)
if err != nil {
return nil, err
}
return w.graph.depends(t)
}
func (w *workspace) whatDepends(label *label.Label) ([]string, error) {
t, err := w.target(label)
if err != nil {
return nil, err
}
return w.graph.whatDepends(t)
}
func (w *workspace) sources(label *label.Label) ([]string, error) {
t, err := w.target(label)
if err != nil {
return nil, err
}
return w.graph.sources(t, w.root)
}
func (w *workspace) repl(label *label.Label) error {
thread, globals := w.project.REPLEnv(os.Stdout, label)
globals["depends"] = w.newBuiltin_depends()
globals["what_depends"] = w.newBuiltin_whatDepends()
repl.REPL(thread, globals)
return nil
}
func (w *workspace) labelOrNearestDefault(l *label.Label) *label.Label {
original := l
if l.Name != "default" {
return l
}
for l.Package != "//" {
if _, err := w.project.Target(l); err == nil {
return l
}
l.Package = label.Parent(l.Package)
}
return original
}
func (w *workspace) run(label *label.Label, opts dawn.RunOptions) error {
err := w.project.Run(w.labelOrNearestDefault(label), &opts)
w.renderer.Close()
return err
}
func (w *workspace) watch(label *label.Label) error {
err := w.project.Watch(w.labelOrNearestDefault(label))
w.renderer.Close()
return err
}