This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.go
141 lines (114 loc) · 4.03 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
134
135
136
137
138
139
140
141
package dep
import (
"path/filepath"
"time"
"github.com/paketo-buildpacks/packit/v2"
"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 bool, 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,
logger scribe.Emitter,
) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
logger.Break()
entry, _ := entryResolver.Resolve(Dep, context.Plan.Entries, nil)
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
}
bom := dependencyManager.GenerateBillOfMaterials(dependency)
launch, build := entryResolver.MergeLayerTypes(Dep, context.Plan.Entries)
depLayer, err := context.Layers.Get(Dep)
if err != nil {
return packit.BuildResult{}, err
}
var buildMetadata = packit.BuildMetadata{}
var launchMetadata = packit.LaunchMetadata{}
if build {
buildMetadata = packit.BuildMetadata{BOM: bom}
}
if launch {
launchMetadata = packit.LaunchMetadata{BOM: bom}
}
cachedSHA, ok := depLayer.Metadata[DependencyCacheKey].(string)
if ok && cachedSHA == dependency.SHA256 {
logger.Process("Reusing cached layer %s", depLayer.Path)
logger.Break()
depLayer.Launch, depLayer.Build, depLayer.Cache = launch, build, build
return packit.BuildResult{
Layers: []packit.Layer{depLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
logger.Process("Executing build process")
depLayer, err = depLayer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
depLayer.Launch, depLayer.Build, depLayer.Cache = launch, build, build
logger.Subprocess("Installing Dep")
duration, err := clock.Measure(func() error {
return dependencyManager.Deliver(dependency, context.CNBPath, depLayer.Path, context.Platform.Path)
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
logger.GeneratingSBOM(depLayer.Path)
var sbomContent sbom.SBOM
duration, err = clock.Measure(func() error {
sbomContent, err = sbomGenerator.GenerateFromDependency(dependency, depLayer.Path)
return err
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
logger.FormattingSBOM(context.BuildpackInfo.SBOMFormats...)
depLayer.SBOM, err = sbomContent.InFormats(context.BuildpackInfo.SBOMFormats...)
if err != nil {
return packit.BuildResult{}, err
}
depLayer.Metadata = map[string]interface{}{
DependencyCacheKey: dependency.SHA256,
}
return packit.BuildResult{
Layers: []packit.Layer{depLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
}