-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.go
100 lines (81 loc) · 2.8 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
package hugobuildpack
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/paketo-buildpacks/packit"
"github.com/paketo-buildpacks/packit/pexec"
"github.com/paketo-buildpacks/packit/postal"
"github.com/paketo-buildpacks/packit/scribe"
)
//go:generate faux --interface EntryResolver --output fakes/entry_resolver.go
type EntryResolver interface {
Resolve(name string, entries []packit.BuildpackPlanEntry, priorities []interface{}) (packit.BuildpackPlanEntry, []packit.BuildpackPlanEntry)
MergeLayerTypes(name string, entries []packit.BuildpackPlanEntry) (bool, 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
}
//go:generate faux --interface Executable --output fakes/executable.go
type Executable interface {
Execute(pexec.Execution) error
}
func Build(entryResolver EntryResolver, dependencyManager DependencyManager, hugo Executable, logs scribe.Emitter) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logs.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
entry, _ := entryResolver.Resolve("hugo", context.Plan.Entries, nil)
entryVersion, _ := entry.Metadata["version"].(string)
dependency, err := dependencyManager.Resolve(
filepath.Join(context.CNBPath, "buildpack.toml"),
entry.Name,
entryVersion,
context.Stack)
if err != nil {
return packit.BuildResult{}, err
}
// install hugo
hugoLayer, err := context.Layers.Get("hugo")
if err != nil {
return packit.BuildResult{}, err
}
hugoLayer, err = hugoLayer.Reset()
if err != nil {
return packit.BuildResult{}, err
}
launch, build := entryResolver.MergeLayerTypes("hugo", context.Plan.Entries)
hugoLayer.Build = build
hugoLayer.Cache = build
hugoLayer.Launch = launch
logs.Process("Installing Hugo")
err = dependencyManager.Deliver(dependency, context.CNBPath, hugoLayer.Path, context.Platform.Path)
if err != nil {
return packit.BuildResult{}, err
}
logs.Break()
// build static assets
logs.Process("Executing build process")
buffer := bytes.NewBuffer(nil)
args := []string{
"--destination", "public",
}
logs.Subprocess("Running 'hugo %s'", strings.Join(args, " "))
err = hugo.Execute(pexec.Execution{
Args: args,
Dir: context.WorkingDir,
Stdout: buffer,
Stderr: buffer,
Env: append(os.Environ(), fmt.Sprintf("PATH=%s:%s", filepath.Join(hugoLayer.Path, "bin"), os.Getenv("PATH"))),
})
if err != nil {
return packit.BuildResult{}, err
}
return packit.BuildResult{
Plan: packit.BuildpackPlan{},
Layers: []packit.Layer{hugoLayer},
}, nil
}
}