-
Notifications
You must be signed in to change notification settings - Fork 9
/
workspace.go
226 lines (183 loc) · 6.89 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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package parsing
import (
"bytes"
"context"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"github.com/protocolbuffers/txtpbfmt/parser"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"namespacelabs.dev/foundation/internal/findroot"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/protos"
"namespacelabs.dev/foundation/internal/versions"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
"namespacelabs.dev/foundation/std/tasks"
)
const (
originalWorkspaceFilename = "workspace.ns.textpb"
foundationModule = "namespacelabs.dev/foundation"
)
var ModuleLoader interface {
FindModuleRoot(string) (string, error)
ModuleAt(context.Context, string) (pkggraph.WorkspaceData, error)
}
func FindModuleRoot(dir string) (string, error) {
return ModuleLoader.FindModuleRoot(dir)
}
type ModuleAtArgs struct {
SkipAPIRequirements bool
}
// Loads and validates a module at a given path.
func ModuleAt(ctx context.Context, path string, args ModuleAtArgs) (pkggraph.WorkspaceData, error) {
ws, err := ModuleLoader.ModuleAt(ctx, path)
if err != nil {
return ws, err
}
if !args.SkipAPIRequirements {
if err := validateAPIRequirements(ws.ModuleName(), ws.Proto().Foundation); err != nil {
return ws, err
}
}
return ws, nil
}
func RawFindModuleRoot(dir string, names ...string) (string, error) {
return findroot.Find("workspace", dir, findroot.LookForFile(append(names, originalWorkspaceFilename)...))
}
// RawModuleAt returns a schema.WorkspaceData with a reference to the workspace filename tried, even when errors are returned.
func RawModuleAt(ctx context.Context, path string) (pkggraph.WorkspaceData, error) {
return tasks.Return(ctx, tasks.Action("workspace.load-workspace-textpb").Arg("dir", path), func(ctx context.Context) (pkggraph.WorkspaceData, error) {
data := rawWorkspaceData{absPath: path, definitionFile: originalWorkspaceFilename}
file := filepath.Join(path, originalWorkspaceFilename)
moduleBytes, err := os.ReadFile(file)
if err != nil {
return data, err
}
// So we do a first-pass at the module definition, with loose parsing on, to
// make sure that we meet the version requirements set by the module owners.
firstPass := &schema.Workspace{}
if err := (prototext.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}).Unmarshal(moduleBytes, firstPass); err != nil {
return data, fnerrors.Wrapf(nil, err, "failed to parse %s for validation", file)
}
w := &schema.Workspace{}
if err := prototext.Unmarshal(moduleBytes, w); err != nil {
return data, fnerrors.Wrapf(nil, err, "failed to parse %s", file)
}
data.data = moduleBytes
data.parsed = w
return data, nil
})
}
func validateAPIRequirements(moduleName string, w *schema.Workspace_FoundationRequirements) error {
if w.GetMinimumApi() > versions.APIVersion {
return fnerrors.DoesNotMeetVersionRequirements(moduleName, w.GetMinimumApi(), versions.APIVersion)
}
// Check that the foundation repo dep uses an API compatible with the current CLI.
if moduleName == foundationModule && w.GetMinimumApi() > 0 && w.GetMinimumApi() < versions.MinimumAPIVersion {
return fnerrors.UserError(nil, fmt.Sprintf(`Unfortunately, this version of Foundation is too recent to be used with the
current repository. If you're testing out an existing repository that uses
Foundation, try fetching a newer version of the repository. If this is your
own codebase, then you'll need to either revert to a previous version of
"ns", or update your dependency versions with "ns mod get %s".
This version check will be removed in future non-alpha versions of
Foundation, which establish a stable longer term supported API surface.`, foundationModule))
}
return nil
}
func writeTextMessage(w io.Writer, msg proto.Message) {
fmt.Fprint(w, prototext.MarshalOptions{Multiline: true}.Format(msg))
}
type rawWorkspaceData struct {
absPath, definitionFile string
data []byte
parsed *schema.Workspace
}
func (r rawWorkspaceData) ErrorLocation() string { return r.absPath }
func (r rawWorkspaceData) ModuleName() string { return r.parsed.ModuleName }
func (r rawWorkspaceData) Proto() *schema.Workspace { return r.parsed }
func (r rawWorkspaceData) AbsPath() string { return r.absPath }
func (r rawWorkspaceData) DefinitionFile() string { return r.definitionFile }
func (r rawWorkspaceData) RawData() []byte { return r.data }
func (r rawWorkspaceData) FormatTo(w io.Writer) error {
// We force a particular structure by controlling which messages are emited when.
var buf bytes.Buffer
ws := r.parsed
writeTextMessage(&buf, &schema.Workspace{ModuleName: ws.ModuleName, Foundation: ws.Foundation})
if len(ws.Dep) > 0 {
sort.Slice(ws.Dep, func(i, j int) bool {
return strings.Compare(ws.Dep[i].ModuleName, ws.Dep[j].ModuleName) < 0
})
fmt.Fprintln(&buf)
writeTextMessage(&buf, &schema.Workspace{Dep: ws.Dep})
}
if len(ws.Replace) > 0 {
fmt.Fprintln(&buf)
writeTextMessage(&buf, &schema.Workspace{Replace: ws.Replace})
}
if len(ws.EnvSpec) > 0 {
writeTextMessage(&buf, &schema.Workspace{EnvSpec: ws.EnvSpec})
}
if len(ws.PrebuiltBinary) > 0 {
sorted := slices.Clone(ws.PrebuiltBinary)
slices.SortFunc(sorted, func(a, b *schema.Workspace_BinaryDigest) bool {
return strings.Compare(a.PackageName, b.PackageName) < 0
})
fmt.Fprintln(&buf)
writeTextMessage(&buf, &schema.Workspace{PrebuiltBinary: sorted})
}
if ws.PrebuiltBaseRepository != "" {
writeTextMessage(&buf, &schema.Workspace{PrebuiltBaseRepository: ws.PrebuiltBaseRepository})
}
stableFmt, err := parser.Format(buf.Bytes())
if err != nil {
return err
}
fmt.Fprintf(w, "%s", stableFmt)
return nil
}
func (r rawWorkspaceData) WithSetDependency(deps ...*schema.Workspace_Dependency) pkggraph.WorkspaceData {
cloned := protos.Clone(r.parsed)
var mods, changes int
var newDeps []*schema.Workspace_Dependency
for _, dep := range deps {
for _, existing := range cloned.Dep {
if existing.ModuleName == dep.ModuleName {
mods++
if existing.Version != dep.Version {
existing.Version = dep.Version
changes++
}
}
}
}
if mods == 0 {
cloned.Dep = append(cloned.Dep, newDeps...)
copy := r
copy.parsed = cloned
return copy
}
return nil
}
func (r rawWorkspaceData) WithReplacedDependencies(deps []*schema.Workspace_Dependency) pkggraph.WorkspaceData {
cloned := protos.Clone(r.parsed)
cloned.Dep = deps
copy := r
copy.parsed = cloned
return copy
}
func (r rawWorkspaceData) LoadedFrom() *schema.Workspace_LoadedFrom {
return &schema.Workspace_LoadedFrom{
AbsPath: r.AbsPath(),
DefinitionFile: r.DefinitionFile(),
Contents: r.RawData(),
}
}