forked from bufbuild/buf
/
builder.go
226 lines (213 loc) · 6.06 KB
/
builder.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 2020-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bufgraph
import (
"context"
"fmt"
"github.com/bufbuild/buf/private/bufpkg/bufanalysis"
"github.com/bufbuild/buf/private/bufpkg/bufimage"
"github.com/bufbuild/buf/private/bufpkg/bufimage/bufimagebuild"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref"
"github.com/bufbuild/buf/private/pkg/dag"
"go.uber.org/zap"
)
type builder struct {
logger *zap.Logger
moduleResolver bufmodule.ModuleResolver
moduleReader bufmodule.ModuleReader
imageBuilder bufimagebuild.Builder
}
func newBuilder(
logger *zap.Logger,
moduleResolver bufmodule.ModuleResolver,
moduleReader bufmodule.ModuleReader,
) *builder {
return &builder{
logger: logger,
moduleResolver: moduleResolver,
moduleReader: moduleReader,
imageBuilder: bufimagebuild.NewBuilder(
logger,
moduleReader,
),
}
}
func (b *builder) Build(
ctx context.Context,
modules []bufmodule.Module,
options ...BuildOption,
) (*dag.Graph[Node], []bufanalysis.FileAnnotation, error) {
buildOptions := newBuildOptions()
for _, option := range options {
option(buildOptions)
}
return b.build(
ctx,
modules,
buildOptions.workspace,
)
}
func (b *builder) build(
ctx context.Context,
modules []bufmodule.Module,
workspace bufmodule.Workspace,
) (*dag.Graph[Node], []bufanalysis.FileAnnotation, error) {
graph := dag.NewGraph[Node]()
alreadyProcessedNodes := make(map[Node]struct{})
for _, module := range modules {
fileAnnotations, err := b.buildForModule(
ctx,
module,
newNodeForModule(module),
workspace,
graph,
alreadyProcessedNodes,
)
if err != nil {
return nil, nil, err
}
if len(fileAnnotations) > 0 {
return nil, fileAnnotations, nil
}
}
return graph, nil, nil
}
func (b *builder) buildForModule(
ctx context.Context,
module bufmodule.Module,
node Node,
workspace bufmodule.Workspace,
graph *dag.Graph[Node],
alreadyProcessedNodes map[Node]struct{},
) ([]bufanalysis.FileAnnotation, error) {
// We can't rely on the existence of a node in the graph for this, as when we add an edge
// to the graph, the node is added, and we still need to process the node as a potential
// source node.
if _, ok := alreadyProcessedNodes[node]; ok {
return nil, nil
}
alreadyProcessedNodes[node] = struct{}{}
graph.AddNode(node)
image, fileAnnotations, err := b.imageBuilder.Build(
ctx,
module,
bufimagebuild.WithWorkspace(workspace),
bufimagebuild.WithExpectedDirectDependencies(module.DeclaredDirectDependencies()),
)
if err != nil {
return nil, err
}
if len(fileAnnotations) > 0 {
return fileAnnotations, nil
}
for _, imageModuleDependency := range bufimage.ImageModuleDependencies(image) {
dependencyNode := newNodeForImageModuleDependency(imageModuleDependency)
if imageModuleDependency.IsDirect() {
graph.AddEdge(node, dependencyNode)
}
dependencyModule, err := b.getModuleForImageModuleDependency(
ctx,
imageModuleDependency,
workspace,
)
if err != nil {
return nil, err
}
// TODO: deal with the case where there are differing commits for a given ModuleIdentity.
fileAnnotations, err := b.buildForModule(
ctx,
dependencyModule,
dependencyNode,
workspace,
graph,
alreadyProcessedNodes,
)
if err != nil {
return nil, err
}
if len(fileAnnotations) > 0 {
return fileAnnotations, nil
}
}
return nil, nil
}
func (b *builder) getModuleForImageModuleDependency(
ctx context.Context,
imageModuleDependency bufimage.ImageModuleDependency,
workspace bufmodule.Workspace,
) (bufmodule.Module, error) {
moduleIdentity := imageModuleDependency.ModuleIdentity()
commit := imageModuleDependency.Commit()
if workspace != nil {
module, ok := workspace.GetModule(moduleIdentity)
if ok {
return module, nil
}
}
if commit == "" {
// TODO: can we error here? The only
// case we should likely not have a commit is when we are using a workspace.
// There's no enforcement of this property, so erroring here is a bit weird,
// but it might be better to check our assumptions and figure out if there
// are exceptions after the fact, as opposed to resolving a ModulePin for
// main when we don't know if main is what we want.
return nil, fmt.Errorf("had ModuleIdentity %v with no associated commit, but did not have the module in a workspace", moduleIdentity)
}
moduleReference, err := bufmoduleref.NewModuleReference(
moduleIdentity.Remote(),
moduleIdentity.Owner(),
moduleIdentity.Repository(),
commit,
)
if err != nil {
return nil, err
}
modulePin, err := b.moduleResolver.GetModulePin(
ctx,
moduleReference,
)
if err != nil {
return nil, err
}
return b.moduleReader.GetModule(
ctx,
modulePin,
)
}
func newNodeForImageModuleDependency(imageModuleDependency bufimage.ImageModuleDependency) Node {
return Node{
Remote: imageModuleDependency.ModuleIdentity().Remote(),
Owner: imageModuleDependency.ModuleIdentity().Owner(),
Repository: imageModuleDependency.ModuleIdentity().Repository(),
Commit: imageModuleDependency.Commit(),
}
}
func newNodeForModule(module bufmodule.Module) Node {
// TODO: deal with unnamed Modules
var node Node
if moduleIdentity := module.ModuleIdentity(); moduleIdentity != nil {
node.Remote = moduleIdentity.Remote()
node.Owner = moduleIdentity.Owner()
node.Repository = moduleIdentity.Repository()
node.Commit = module.Commit()
}
return node
}
type buildOptions struct {
workspace bufmodule.Workspace
}
func newBuildOptions() *buildOptions {
return &buildOptions{}
}