-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
133 lines (107 loc) · 4.22 KB
/
build.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
package godist
import (
"path/filepath"
"time"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/cargo"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/postal"
"github.com/paketo-buildpacks/packit/v2/sbom"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:generate faux --interface EntryResolver --output fakes/entry_resolver.go
type EntryResolver interface {
Resolve(name string, entries []packit.BuildpackPlanEntry, priorites []interface{}) (packit.BuildpackPlanEntry, []packit.BuildpackPlanEntry)
MergeLayerTypes(name string, entries []packit.BuildpackPlanEntry) (launch, build bool)
}
//go:generate faux --interface DependencyManager --output fakes/dependency_manager.go
type DependencyManager interface {
Resolve(path, id, version, stack string) (postal.Dependency, error)
Deliver(dependency postal.Dependency, cnbPath, layerPath, platformPath string) error
GenerateBillOfMaterials(dependencies ...postal.Dependency) []packit.BOMEntry
}
//go:generate faux --interface SBOMGenerator --output fakes/sbom_generator.go
type SBOMGenerator interface {
GenerateFromDependency(dependency postal.Dependency, dir string) (sbom.SBOM, error)
}
func Build(entryResolver EntryResolver, dependencyManager DependencyManager, sbomGenerator SBOMGenerator, clock chronos.Clock, logs scribe.Emitter) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logs.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
logs.Process("Resolving Go version")
entry, entries := entryResolver.Resolve(GoDependency, context.Plan.Entries, Priorities)
logs.Candidates(entries)
version, ok := entry.Metadata["version"].(string)
if !ok {
version = "default"
}
dependency, err := dependencyManager.Resolve(filepath.Join(context.CNBPath, "buildpack.toml"), entry.Name, version, context.Stack)
if err != nil {
return packit.BuildResult{}, err
}
logs.SelectedDependency(entry, dependency, clock.Now())
bom := dependencyManager.GenerateBillOfMaterials(dependency)
goLayer, err := context.Layers.Get(GoLayerName)
if err != nil {
return packit.BuildResult{}, err
}
launch, build := entryResolver.MergeLayerTypes(GoDependency, context.Plan.Entries)
var buildMetadata = packit.BuildMetadata{}
var launchMetadata = packit.LaunchMetadata{}
if build {
buildMetadata = packit.BuildMetadata{BOM: bom}
}
if launch {
launchMetadata = packit.LaunchMetadata{BOM: bom}
}
cachedChecksum, ok := goLayer.Metadata["dependency-checksum"].(string)
if ok && cargo.Checksum(dependency.Checksum).MatchString(cachedChecksum) {
logs.Process("Reusing cached layer %s", goLayer.Path)
logs.Break()
goLayer.Launch, goLayer.Build, goLayer.Cache = launch, build, build
return packit.BuildResult{
Layers: []packit.Layer{goLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
logs.Process("Executing build process")
goLayer, err = goLayer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
goLayer.Launch, goLayer.Build, goLayer.Cache = launch, build, build
logs.Subprocess("Installing Go %s", dependency.Version)
duration, err := clock.Measure(func() error {
return dependencyManager.Deliver(dependency, context.CNBPath, goLayer.Path, context.Platform.Path)
})
if err != nil {
return packit.BuildResult{}, err
}
logs.Action("Completed in %s", duration.Round(time.Millisecond))
logs.Break()
logs.GeneratingSBOM(goLayer.Path)
var sbomContent sbom.SBOM
duration, err = clock.Measure(func() error {
sbomContent, err = sbomGenerator.GenerateFromDependency(dependency, goLayer.Path)
return err
})
if err != nil {
return packit.BuildResult{}, err
}
logs.Action("Completed in %s", duration.Round(time.Millisecond))
logs.Break()
logs.FormattingSBOM(context.BuildpackInfo.SBOMFormats...)
goLayer.SBOM, err = sbomContent.InFormats(context.BuildpackInfo.SBOMFormats...)
if err != nil {
return packit.BuildResult{}, err
}
goLayer.Metadata = map[string]interface{}{
"dependency-checksum": dependency.Checksum,
}
return packit.BuildResult{
Layers: []packit.Layer{goLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
}