-
Notifications
You must be signed in to change notification settings - Fork 9
/
genall.go
93 lines (83 loc) · 3.1 KB
/
genall.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
// 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 genpackage
import (
"context"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/languages"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/execution"
"namespacelabs.dev/foundation/std/pkggraph"
)
// ForNodeLocations generates protos for Extensions and Services. Locations in `locs` are sorted in a topological order.
func ForLocationsGenProto(ctx context.Context, out pkggraph.MutableModule, env cfg.Context, locs []fnfs.Location, onError func(fnerrors.CodegenError)) error {
pl := parsing.NewPackageLoader(env)
g := execution.NewEmptyPlan()
for _, loc := range locs {
pkg, err := pl.LoadByName(ctx, loc.AsPackageName())
if err != nil {
onError(fnerrors.CodegenError{PackageName: loc.AsPackageName().String(), What: "loading schema", Err: err})
continue
}
if n := pkg.Node(); n != nil {
defs, err := ProtosForNode(pkg)
if err != nil {
onError(fnerrors.CodegenError{PackageName: loc.AsPackageName().String(), What: "generate node", Err: err})
} else {
g.Add(defs...)
}
}
if err := execution.Execute(ctx, env, "workspace.generate.phase.node", g, nil,
pkggraph.MutableModuleInjection.With(out),
pkggraph.PackageLoaderInjection.With(pl.Seal()),
); err != nil {
return err
}
}
return nil
}
// ForLocationsGenCode generates code for all packages in `locs`. At this stage we assume protos are already generated.
func ForLocationsGenCode(ctx context.Context, out pkggraph.MutableModule, env cfg.Context, locs []fnfs.Location, onError func(fnerrors.CodegenError)) error {
pl := parsing.NewPackageLoader(env)
g := execution.NewEmptyPlan()
for _, loc := range locs {
sealed, err := parsing.Seal(ctx, pl, loc.AsPackageName(), nil)
if err != nil {
onError(fnerrors.CodegenError{PackageName: loc.AsPackageName().String(), What: "loading schema", Err: err})
continue
}
if srv := sealed.Proto.Server; srv != nil {
defs, err := languages.IntegrationFor(srv.Framework).GenerateServer(sealed.ParsedPackage, sealed.Proto.Node)
if err != nil {
onError(fnerrors.CodegenError{PackageName: loc.AsPackageName().String(), What: "generate server", Err: err})
} else {
g.Add(defs...)
}
} else {
var pkg *pkggraph.Package
for _, dep := range sealed.Deps {
if dep.PackageName() == loc.AsPackageName() {
pkg = dep
break
}
}
if pkg == nil || pkg.Node() == nil {
continue
}
defs, err := ForNodeForLanguage(pkg, sealed.Proto.Node)
if err != nil {
onError(fnerrors.CodegenError{PackageName: loc.AsPackageName().String(), What: "generate node", Err: err})
return err
} else {
g.Add(defs...)
}
}
}
return execution.Execute(ctx, env, "workspace.generate.phase.code", g, nil,
pkggraph.MutableModuleInjection.With(out),
pkggraph.PackageLoaderInjection.With(pl.Seal()),
)
}