-
Notifications
You must be signed in to change notification settings - Fork 9
/
frontend.go
226 lines (190 loc) · 6.67 KB
/
frontend.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.
// This is about parsing Fn-specific dialect of Cue.
package cuefrontend
import (
"context"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs/memfs"
"namespacelabs.dev/foundation/internal/frontend/fncue"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
type impl struct {
loader parsing.EarlyPackageLoader
env *schema.Environment
evalctx *fncue.EvalCtx
newSyntaxParser NewSyntaxParser
}
type NewSyntaxParser interface {
ParsePackage(ctx context.Context, partial *fncue.Partial, loc pkggraph.Location) (*pkggraph.Package, error)
}
type cueInjectedScope struct {
// Injecting schema.Environment as $env so the user can use it without importing.
// It is temporarily optional since not all commands (that should) accept the --env flag.
Env *cueEnv `json:"$env"`
}
// Variables that always available for the user in CUE files, without explicit importing.
func InjectedScope(env *schema.Environment) interface{} {
return &cueInjectedScope{
Env: &cueEnv{
Name: env.Name,
Runtime: env.Runtime,
Purpose: env.Purpose.String(),
Ephemeral: env.Ephemeral,
},
}
}
func NewFrontend(pl parsing.EarlyPackageLoader, opaqueParser NewSyntaxParser, env *schema.Environment) parsing.Frontend {
return impl{
loader: pl,
env: env,
evalctx: fncue.NewEvalCtx(WorkspaceLoader{pl}, InjectedScope(env)),
newSyntaxParser: opaqueParser,
}
}
func (ft impl) ParsePackage(ctx context.Context, loc pkggraph.Location) (*pkggraph.Package, error) {
partial, err := parsePackage(ctx, ft.evalctx, ft.loader, loc)
if err != nil {
return nil, err
}
// Packages in the new syntax don't rely as much on cue features. They're
// streamlined data definitions without the constraints of json.
if isNewSyntax(partial) {
return ft.newSyntaxParser.ParsePackage(ctx, partial, loc)
}
v := &partial.CueV
parsed := &pkggraph.Package{
Location: loc,
PackageSources: partial.Package.Snapshot,
Parsed: phase1plan{owner: loc.PackageName, partial: partial, Value: v, Left: partial.Left},
}
var count int
if extension := v.LookupPath("extension"); extension.Exists() {
if err := parseCueNode(ctx, ft.env, ft.loader, loc, schema.Node_EXTENSION, v, extension, parsed); err != nil {
return nil, fnerrors.NewWithLocation(loc, "parsing extension: %w", err)
}
count++
}
if service := v.LookupPath("service"); service.Exists() {
if err := parseCueNode(ctx, ft.env, ft.loader, loc, schema.Node_SERVICE, v, service, parsed); err != nil {
return nil, fnerrors.NewWithLocation(loc, "parsing service: %w", err)
}
count++
}
if server := v.LookupPath("server"); server.Exists() {
parsedSrv, binaries, err := parseCueServer(ctx, ft.loader, loc, v, server)
if err != nil {
return nil, fnerrors.NewWithLocation(loc, "parsing server: %w", err)
}
parsed.Server = parsedSrv
parsed.Binaries = append(parsed.Binaries, binaries...)
count++
}
// Binaries should really be called "OCI Images".
if binary := v.LookupPath("binary"); binary.Exists() {
parsedBinary, err := parseCueBinary(ctx, loc, v, binary)
if err != nil {
return nil, fnerrors.NewWithLocation(loc, "parsing binary: %w", err)
}
parsed.Binaries = append(parsed.Binaries, parsedBinary)
count++
}
if test := v.LookupPath("test"); test.Exists() {
parsedTest, err := parseCueTest(ctx, loc, v, test)
if err != nil {
return nil, fnerrors.NewWithLocation(loc, "parsing test: %w", err)
}
parsed.Tests = append(parsed.Tests, parsedTest)
count++
}
if function := v.LookupPath("function"); function.Exists() {
parsedFunction, err := parseCueFunction(ctx, loc, v, function)
if err != nil {
return nil, fnerrors.NewWithLocation(loc, "parsing function: %w", err)
}
parsed.ExperimentalFunction = parsedFunction
count++
}
if count > 1 {
return nil, fnerrors.New("package must only define one of: server, service, extension, binary or test")
}
return parsed, nil
}
func isNewSyntax(partial *fncue.Partial) bool {
if len(partial.CueImports) > 1 {
// There is at least one import: the file itself.
return false
}
// Detecting the simplified syntax to define opaque servers.
for _, path := range []string{"server", "resources", "resourceClasses", "providers", "volumes", "secrets", "tests"} {
if partial.CueV.LookupPath(path).Exists() {
return true
}
}
return false
}
func (ft impl) GuessPackageType(ctx context.Context, pkg schema.PackageName) (parsing.PackageType, error) {
firstPass, err := ft.evalctx.EvalPackage(ctx, pkg.String())
if err != nil {
return parsing.PackageType_None, err
}
topLevels := map[string]parsing.PackageType{
"service": parsing.PackageType_Service,
"server": parsing.PackageType_Server, // TODO This can be old or new syntax. In new syntax, there can be other primitives in the same package.
"extension": parsing.PackageType_Extension,
"test": parsing.PackageType_Test,
"binary": parsing.PackageType_Binary,
// TODO consider refining.
"secrets": parsing.PackageType_NewSyntax,
"resources": parsing.PackageType_NewSyntax,
"resourceClasses": parsing.PackageType_NewSyntax,
"providers": parsing.PackageType_NewSyntax,
"volumes": parsing.PackageType_NewSyntax,
"tests": parsing.PackageType_NewSyntax,
}
for k, v := range topLevels {
if firstPass.LookupPath(k).Exists() {
return v, nil
}
}
return parsing.PackageType_None, nil
}
func (ft impl) HasNodePackage(ctx context.Context, pkg schema.PackageName) (bool, error) {
firstPass, err := ft.evalctx.EvalPackage(ctx, pkg.String())
if err != nil {
return false, err
}
var topLevels = []string{"service", "extension"}
for _, topLevel := range topLevels {
if firstPass.LookupPath(topLevel).Exists() {
return true, nil
}
}
return false, nil
}
type WorkspaceLoader struct {
PackageLoader parsing.EarlyPackageLoader
}
func (wl WorkspaceLoader) SnapshotDir(ctx context.Context, pkgname schema.PackageName, opts memfs.SnapshotOpts) (*fncue.PackageContents, error) {
loc, err := wl.PackageLoader.Resolve(ctx, pkgname)
if err != nil {
return nil, err
}
w, err := wl.PackageLoader.WorkspaceOf(ctx, loc.Module)
if err != nil {
return nil, err
}
fsys, err := memfs.SnapshotDir(w, loc.Rel(), opts)
if err != nil {
return nil, err
}
return &fncue.PackageContents{
ModuleName: loc.Module.ModuleName(),
RelPath: loc.Rel(),
Snapshot: fsys,
AbsPath: loc.Abs(),
}, nil
}