forked from paketo-buildpacks/go-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
146 lines (122 loc) · 3.93 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
package gobuild
import (
"fmt"
"path/filepath"
"strconv"
"time"
"github.com/paketo-buildpacks/packit"
"github.com/paketo-buildpacks/packit/chronos"
"github.com/paketo-buildpacks/packit/scribe"
)
//go:generate faux --interface BuildProcess --output fakes/build_process.go
type BuildProcess interface {
Execute(config GoBuildConfiguration) (binaries []string, err error)
}
//go:generate faux --interface ChecksumCalculator --output fakes/checksum_calculator.go
type ChecksumCalculator interface {
Sum(paths ...string) (string, error)
}
//go:generate faux --interface PathManager --output fakes/path_manager.go
type PathManager interface {
Setup(workspace, importPath string) (goPath, path string, err error)
Teardown(goPath string) error
}
//go:generate faux --interface SourceRemover --output fakes/source_remover.go
type SourceRemover interface {
Clear(path string) error
}
func Build(
parser ConfigurationParser,
buildProcess BuildProcess,
checksumCalculator ChecksumCalculator,
pathManager PathManager,
clock chronos.Clock,
logs scribe.Emitter,
sourceRemover SourceRemover,
) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logs.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
shouldReload, err := checkLiveReloadEnabled()
if err != nil {
return packit.BuildResult{}, err
}
if shouldReload && context.Stack == TinyStackName {
return packit.BuildResult{}, fmt.Errorf("cannot enable live reload on stack '%s': stack does not support watchexec", context.Stack)
}
targetsLayer, err := context.Layers.Get(TargetsLayerName)
if err != nil {
return packit.BuildResult{}, err
}
targetsLayer.Launch = true
goCacheLayer, err := context.Layers.Get(GoCacheLayerName)
if err != nil {
return packit.BuildResult{}, err
}
goCacheLayer.Cache = true
// Parse the BuildConfiguration from the environment again since a prior
// step may have augmented the configuration.
configuration, err := parser.Parse(context.BuildpackInfo.Version, context.WorkingDir)
if err != nil {
return packit.BuildResult{}, packit.Fail.WithMessage("failed to parse build configuration: %w", err)
}
goPath, path, err := pathManager.Setup(context.WorkingDir, configuration.ImportPath)
if err != nil {
return packit.BuildResult{}, err
}
binaries, err := buildProcess.Execute(GoBuildConfiguration{
Workspace: path,
Output: filepath.Join(targetsLayer.Path, "bin"),
GoPath: goPath,
GoCache: goCacheLayer.Path,
Flags: configuration.Flags,
Targets: configuration.Targets,
})
if err != nil {
return packit.BuildResult{}, err
}
sum, err := checksumCalculator.Sum(targetsLayer.Path)
if err != nil {
return packit.BuildResult{}, err
}
cachedSha, _ := targetsLayer.Metadata["cache_sha"].(string)
if cachedSha != sum {
targetsLayer.Metadata = map[string]interface{}{
"cache_sha": sum,
"built_at": clock.Now().Format(time.RFC3339Nano),
}
}
err = pathManager.Teardown(goPath)
if err != nil {
return packit.BuildResult{}, err
}
err = sourceRemover.Clear(context.WorkingDir)
if err != nil {
return packit.BuildResult{}, err
}
var processes []packit.Process
for index, binary := range binaries {
processes = append(processes, packit.Process{
Type: filepath.Base(binary),
Command: binary,
Direct: true,
Default: index == 0 && !shouldReload,
})
if shouldReload {
processes = append(processes, packit.Process{
Type: fmt.Sprintf("reload-%s", filepath.Base(binary)),
Command: "watchexec",
Args: []string{"--restart", "--watch", context.WorkingDir, "--watch", filepath.Dir(binary), strconv.Quote(binary)},
Direct: true,
Default: index == 0,
})
}
}
logs.LaunchProcesses(processes)
return packit.BuildResult{
Layers: []packit.Layer{targetsLayer, goCacheLayer},
Launch: packit.LaunchMetadata{
Processes: processes,
},
}, nil
}
}