This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.go
180 lines (145 loc) · 6.02 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package dotnetcoreruntime
import (
"fmt"
"path/filepath"
"regexp"
"time"
"github.com/Masterminds/semver"
"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(string, []packit.BuildpackPlanEntry, []interface{}) (packit.BuildpackPlanEntry, []packit.BuildpackPlanEntry)
MergeLayerTypes(string, []packit.BuildpackPlanEntry) (launch, build bool)
}
//go:generate faux --interface DependencyManager --output fakes/dependency_manager.go
type DependencyManager interface {
Deliver(dependency postal.Dependency, cnbPath, layerPath, platformPath string) error
GenerateBillOfMaterials(dependencies ...postal.Dependency) []packit.BOMEntry
}
//go:generate faux --interface DotnetSymlinker --output fakes/dotnet_symlinker.go
type DotnetSymlinker interface {
Link(workingDir, layerPath string) (Err error)
}
//go:generate faux --interface VersionResolver --output fakes/version_resolver.go
type VersionResolver interface {
Resolve(path string, entry packit.BuildpackPlanEntry, stack string) (postal.Dependency, error)
}
//go:generate faux --interface SBOMGenerator --output fakes/sbom_generator.go
type SBOMGenerator interface {
GenerateFromDependency(dependency postal.Dependency, dir string) (sbom.SBOM, error)
}
func Build(
entries EntryResolver,
dependencies DependencyManager,
dotnetSymlinker DotnetSymlinker,
versionResolver VersionResolver,
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 Runtime version")
priorities := []interface{}{
"BP_DOTNET_FRAMEWORK_VERSION",
"buildpack.yml",
regexp.MustCompile(`.*\.(cs)|(fs)|(vb)proj`),
"runtimeconfig.json",
}
entry, sortedEntries := entries.Resolve("dotnet-runtime", context.Plan.Entries, priorities)
logger.Candidates(sortedEntries)
source, _ := entry.Metadata["version-source"].(string)
if source == "buildpack.yml" {
nextMajorVersion := semver.MustParse(context.BuildpackInfo.Version).IncMajor()
logger.Subprocess("WARNING: Setting the .NET Framework version through buildpack.yml will be deprecated soon in .NET Core Runtime Buildpack v%s.", nextMajorVersion.String())
logger.Subprocess("Please specify the version through the $BP_DOTNET_FRAMEWORK_VERSION environment variable instead. See docs for more information.")
logger.Break()
}
dependency, err := versionResolver.Resolve(filepath.Join(context.CNBPath, "buildpack.toml"), entry, context.Stack)
if err != nil {
return packit.BuildResult{}, err
}
logger.SelectedDependency(entry, dependency, clock.Now())
dotnetCoreRuntimeLayer, err := context.Layers.Get("dotnet-core-runtime")
if err != nil {
return packit.BuildResult{}, err
}
bom := dependencies.GenerateBillOfMaterials(dependency)
launch, build := entries.MergeLayerTypes("dotnet-runtime", context.Plan.Entries)
var buildMetadata packit.BuildMetadata
if build {
buildMetadata.BOM = bom
}
var launchMetadata packit.LaunchMetadata
if launch {
launchMetadata.BOM = bom
}
cachedDependencySHA, ok := dotnetCoreRuntimeLayer.Metadata["dependency-sha"]
if ok && cachedDependencySHA == dependency.SHA256 {
logger.Process(fmt.Sprintf("Reusing cached layer %s", dotnetCoreRuntimeLayer.Path))
logger.Break()
err = dotnetSymlinker.Link(context.WorkingDir, dotnetCoreRuntimeLayer.Path)
if err != nil {
return packit.BuildResult{}, err
}
dotnetCoreRuntimeLayer.Launch, dotnetCoreRuntimeLayer.Build, dotnetCoreRuntimeLayer.Cache = launch, build, build
return packit.BuildResult{
Layers: []packit.Layer{dotnetCoreRuntimeLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
logger.Process("Executing build process")
dotnetCoreRuntimeLayer, err = dotnetCoreRuntimeLayer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
dotnetCoreRuntimeLayer.Launch, dotnetCoreRuntimeLayer.Build, dotnetCoreRuntimeLayer.Cache = launch, build, build
logger.Subprocess("Installing .NET Core Runtime %s", dependency.Version)
duration, err := clock.Measure(func() error {
return dependencies.Deliver(dependency, context.CNBPath, dotnetCoreRuntimeLayer.Path, context.Platform.Path)
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
dotnetCoreRuntimeLayer.Metadata = map[string]interface{}{
"dependency-sha": dependency.SHA256,
}
err = dotnetSymlinker.Link(context.WorkingDir, dotnetCoreRuntimeLayer.Path)
if err != nil {
return packit.BuildResult{}, err
}
// Set DOTNET_ROOT to the symlink directory in the working directory, instead of setting it to the layer path itself.
dotnetCoreRuntimeLayer.SharedEnv.Override("DOTNET_ROOT", filepath.Join(context.WorkingDir, ".dotnet_root"))
dotnetCoreRuntimeLayer.BuildEnv.Override("RUNTIME_VERSION", dependency.Version)
logger.EnvironmentVariables(dotnetCoreRuntimeLayer)
logger.GeneratingSBOM(dotnetCoreRuntimeLayer.Path)
var sbomContent sbom.SBOM
duration, err = clock.Measure(func() error {
sbomContent, err = sbomGenerator.GenerateFromDependency(dependency, dotnetCoreRuntimeLayer.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...)
dotnetCoreRuntimeLayer.SBOM, err = sbomContent.InFormats(context.BuildpackInfo.SBOMFormats...)
if err != nil {
return packit.BuildResult{}, err
}
return packit.BuildResult{
Layers: []packit.Layer{dotnetCoreRuntimeLayer},
Build: buildMetadata,
Launch: launchMetadata,
}, nil
}
}