-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.go
160 lines (129 loc) · 5.04 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package dotnetcoresdk
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/Masterminds/semver"
"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,
logger scribe.Emitter,
clock chronos.Clock,
) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
logger.Process("Resolving .NET Core SDK version")
planEntry, entries := entryResolver.Resolve(DotnetDependency, context.Plan.Entries, Priorities)
logger.Candidates(entries)
version, _ := planEntry.Metadata["version"].(string)
versionSource, _ := planEntry.Metadata["version-source"].(string)
sdkDependency, err := dependencyManager.Resolve(filepath.Join(context.CNBPath, "buildpack.toml"), planEntry.Name, version, context.Stack)
if err != nil {
return packit.BuildResult{}, err
}
logger.SelectedDependency(planEntry, sdkDependency, clock.Now())
if versionSource == "buildpack.yml" {
nextMajorVersion := semver.MustParse(context.BuildpackInfo.Version).IncMajor()
logger.Break()
logger.Subprocess("WARNING: Setting the .NET Core SDK version through buildpack.yml will be deprecated soon in .NET Core SDK Buildpack v%s.", nextMajorVersion.String())
}
sdkLayer, err := context.Layers.Get("dotnet-core-sdk")
if err != nil {
return packit.BuildResult{}, err
}
bom := dependencyManager.GenerateBillOfMaterials(sdkDependency)
launch, build := entryResolver.MergeLayerTypes(DotnetDependency, context.Plan.Entries)
var buildMetadata packit.BuildMetadata
if build {
buildMetadata.BOM = bom
}
var launchMetadata packit.LaunchMetadata
if launch {
launchMetadata.BOM = bom
}
dependencyChecksum := sdkDependency.Checksum
//nolint Ignore SA1019, informed usage of deprecated field
if sdkDependency.SHA256 != "" {
dependencyChecksum = sdkDependency.SHA256
}
cachedChecksum, ok := sdkLayer.Metadata["dependency-checksum"].(string)
if ok && cargo.Checksum(dependencyChecksum).MatchString(cachedChecksum) {
logger.Process(fmt.Sprintf("Reusing cached layer %s", sdkLayer.Path))
logger.Break()
sdkLayer.Build, sdkLayer.Launch, sdkLayer.Cache = build, launch, build || launch
return packit.BuildResult{
Layers: []packit.Layer{
sdkLayer,
},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
logger.Process("Executing build process")
sdkLayer, err = sdkLayer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
logger.Subprocess("Installing %s %s", ".NET Core SDK", sdkDependency.Version)
duration, err := clock.Measure(func() error {
return dependencyManager.Deliver(sdkDependency, context.CNBPath, sdkLayer.Path, context.Platform.Path)
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
sdkLayer.Metadata = map[string]interface{}{
"dependency-checksum": dependencyChecksum,
}
sdkLayer.Build, sdkLayer.Launch, sdkLayer.Cache = build, launch, build || launch
sdkLayer.BuildEnv.Prepend("PATH", sdkLayer.Path, string(os.PathListSeparator))
logger.EnvironmentVariables(sdkLayer)
logger.GeneratingSBOM(sdkLayer.Path)
var sbomContent sbom.SBOM
duration, err = clock.Measure(func() error {
sbomContent, err = sbomGenerator.GenerateFromDependency(sdkDependency, sdkLayer.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...)
sdkLayer.SBOM, err = sbomContent.InFormats(context.BuildpackInfo.SBOMFormats...)
if err != nil {
return packit.BuildResult{}, err
}
return packit.BuildResult{
Layers: []packit.Layer{
sdkLayer,
},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
}