-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathgenerator_nodemodules.go
More file actions
124 lines (107 loc) · 3.48 KB
/
generator_nodemodules.go
File metadata and controls
124 lines (107 loc) · 3.48 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
120
121
122
123
124
package dalec
import (
"context"
"path/filepath"
"github.com/goccy/go-yaml/ast"
"github.com/moby/buildkit/client/llb"
"github.com/pkg/errors"
)
func (s *Source) isNodeMod() bool {
for _, gen := range s.Generate {
if gen.NodeMod != nil {
return true
}
}
return false
}
// HasNodeMods returns true if any of the sources in the spec are node modules.
func (s *Spec) HasNodeMods() bool {
for _, src := range s.Sources {
if src.isNodeMod() {
return true
}
}
return false
}
func withNodeMod(g *SourceGenerator, worker llb.State, name string, opts ...llb.ConstraintsOpt) llb.StateOption {
return func(in llb.State) llb.State {
workDir := "/work/src"
joinedWorkDir := filepath.Join(workDir, name, g.Subpath)
const installCmd = "npm install"
const installBasePath = "/work/download"
paths := g.NodeMod.Paths
if g.NodeMod.Paths == nil {
paths = []string{"."}
}
states := make([]llb.State, 0, len(paths))
for _, path := range paths {
// For each path, create an empty mount to store the downloaded packages
// The final result with add a "node_modules" directory at the given path
// To accomplish this, npm pip to download the packages to a similar
// subpath so that we can just take the contents of the mount directly
// without having to do an additional copy to move the files around.
installPath := filepath.Join(installBasePath, name, g.Subpath, path)
installCmd := installCmd + " --prefix " + installPath
st := worker.Run(
ShArgs(installCmd),
llb.Dir(filepath.Join(joinedWorkDir, path)),
WithConstraints(opts...),
llb.AddMount(workDir, in, llb.Readonly),
llb.IgnoreCache,
g.NodeMod._sourceMap.GetLocation(in),
).AddMount(installBasePath, in)
states = append(states, st)
}
return MergeAtPath(llb.Scratch(), append(states, in), "/", opts...)
}
}
func (s *Spec) nodeModSources() map[string]Source {
sources := map[string]Source{}
for name, src := range s.Sources {
if src.isNodeMod() {
sources[name] = src
}
}
return sources
}
// NodeModDeps returns a map[string]llb.State containing all the node module dependencies for the spec
// for any sources that have a node module generator specified.
// If there are no sources with a node module generator, this will return nil.
// The returned states have node_modules installed for each relevant source, using sources as input.
func (s *Spec) NodeModDeps(sOpt SourceOpts, worker llb.State, opts ...llb.ConstraintsOpt) map[string]llb.State {
sources := s.nodeModSources()
if len(sources) == 0 {
return nil
}
// Get the patched sources for the node modules
patched := s.getPatchedSources(sOpt, worker, func(name string) bool {
_, ok := sources[name]
return ok
}, opts...)
result := make(map[string]llb.State)
sorted := SortMapKeys(patched)
opts = append(opts, ProgressGroup("Fetch node module dependencies for sources"))
for _, key := range sorted {
src := s.Sources[key]
merged := patched[key]
for _, gen := range src.Generate {
if gen.NodeMod == nil {
continue
}
merged = merged.With(withNodeMod(gen, worker, key, opts...))
}
result[key] = merged
}
return result
}
func (gen *GeneratorNodeMod) UnmarshalYAML(ctx context.Context, node ast.Node) error {
type internal GeneratorNodeMod
var i internal
dec := getDecoder(ctx)
if err := dec.DecodeFromNodeContext(ctx, node, &i); err != nil {
return errors.Wrap(err, "failed to decode node module generator")
}
*gen = GeneratorNodeMod(i)
gen._sourceMap = newSourceMap(ctx, node)
return nil
}