-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathgenerator_cargohome.go
More file actions
119 lines (100 loc) · 2.89 KB
/
generator_cargohome.go
File metadata and controls
119 lines (100 loc) · 2.89 KB
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
package dalec
import (
"context"
"path/filepath"
"github.com/goccy/go-yaml/ast"
"github.com/moby/buildkit/client/llb"
"github.com/pkg/errors"
)
const (
cargoHomeDir = "/cargo/registry"
)
func (s *Source) isCargohome() bool {
for _, gen := range s.Generate {
if gen.Cargohome != nil {
return true
}
}
return false
}
// HasCargohomes returns true if any of the sources in the spec are a Rust Cargo project.
func (s *Spec) HasCargohomes() bool {
for _, src := range s.Sources {
if src.isCargohome() {
return true
}
}
return false
}
func withCargohome(g *SourceGenerator, srcSt, worker llb.State, subPath string, opts ...llb.ConstraintsOpt) func(llb.State) llb.State {
return func(in llb.State) llb.State {
workDir := "/work/src"
joinedWorkDir := filepath.Join(workDir, subPath, g.Subpath)
srcMount := llb.AddMount(workDir, srcSt)
paths := g.Cargohome.Paths
if g.Cargohome.Paths == nil {
paths = []string{"."}
}
for _, path := range paths {
in = worker.Run(
ShArgs("cargo fetch"),
llb.AddEnv("CARGO_HOME", cargoHomeDir),
llb.Dir(filepath.Join(joinedWorkDir, path)),
srcMount,
WithConstraints(opts...),
g.Cargohome._sourceMap.GetLocation(in),
).AddMount(cargoHomeDir, in)
}
return in
}
}
func (s *Spec) cargohomeSources() map[string]Source {
sources := map[string]Source{}
for name, src := range s.Sources {
if src.isCargohome() {
sources[name] = src
}
}
return sources
}
// CargohomeDeps returns an [llb.State] containing all the Cargo dependencies for the spec
// for any sources that have a cargohome generator specified.
// If there are no sources with a cargohome generator, this will return a nil state.
func (s *Spec) CargohomeDeps(sOpt SourceOpts, worker llb.State, opts ...llb.ConstraintsOpt) *llb.State {
sources := s.cargohomeSources()
if len(sources) == 0 {
return nil
}
deps := llb.Scratch()
// Get the patched sources for the Cargo projects
// This is needed in case a patch includes changes to Cargo.toml or Cargo.lock
patched := s.getPatchedSources(sOpt, worker, func(name string) bool {
_, ok := sources[name]
return ok
}, opts...)
sorted := SortMapKeys(patched)
for _, key := range sorted {
src := s.Sources[key]
opts := append(opts, ProgressGroup("Fetch Cargo dependencies for source: "+key))
deps = deps.With(func(in llb.State) llb.State {
for _, gen := range src.Generate {
if gen.Cargohome != nil {
in = in.With(withCargohome(gen, patched[key], worker, key, opts...))
}
}
return in
})
}
return &deps
}
func (gen *GeneratorCargohome) UnmarshalYAML(ctx context.Context, node ast.Node) error {
type internal GeneratorCargohome
var i internal
dec := getDecoder(ctx)
if err := dec.DecodeFromNodeContext(ctx, node, &i); err != nil {
return errors.Wrap(err, "failed to decode cargohome generator")
}
*gen = GeneratorCargohome(i)
gen._sourceMap = newSourceMap(ctx, node)
return nil
}