forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_buildpack.go
117 lines (91 loc) · 2.22 KB
/
new_buildpack.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
package pack
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/style"
)
var (
bashBinBuild = `#!/usr/bin/env bash
set -euo pipefail
layers_dir="$1"
env_dir="$2/env"
plan_path="$3"
exit 0
`
bashBinDetect = `#!/usr/bin/env bash
exit 0
`
)
type NewBuildpackOptions struct {
// api compat version of the output buildpack artifact.
API string
// The base directory to generate assets
Path string
// The ID of the output buildpack artifact.
ID string
// version of the output buildpack artifact.
Version string
// The stacks this buildpack will work with
Stacks []dist.Stack
}
func (c *Client) NewBuildpack(ctx context.Context, opts NewBuildpackOptions) error {
api, err := api.NewVersion(opts.API)
if err != nil {
return err
}
buildpackTOML := dist.BuildpackDescriptor{
API: api,
Stacks: opts.Stacks,
Info: dist.BuildpackInfo{
ID: opts.ID,
Version: opts.Version,
},
}
if err := os.MkdirAll(opts.Path, 0755); err != nil {
return err
}
buildpackTOMLPath := filepath.Join(opts.Path, "buildpack.toml")
_, err = os.Stat(buildpackTOMLPath)
if os.IsNotExist(err) {
f, err := os.Create(buildpackTOMLPath)
if err != nil {
return err
}
if err := toml.NewEncoder(f).Encode(buildpackTOML); err != nil {
return err
}
defer f.Close()
c.logger.Infof(" %s buildpack.toml", style.Symbol("create"))
}
return createBashBuildpack(opts.Path, c)
}
func createBashBuildpack(path string, c *Client) error {
if err := createBinScript(path, "build", bashBinBuild, c); err != nil {
return err
}
if err := createBinScript(path, "detect", bashBinDetect, c); err != nil {
return err
}
return nil
}
func createBinScript(path, name, contents string, c *Client) error {
binDir := filepath.Join(path, "bin")
binFile := filepath.Join(binDir, name)
_, err := os.Stat(binFile)
if os.IsNotExist(err) {
if err := os.MkdirAll(binDir, 0755); err != nil {
return err
}
err = ioutil.WriteFile(binFile, []byte(contents), 0755)
if err != nil {
return err
}
c.logger.Infof(" %s bin/%s", style.Symbol("create"), name)
}
return nil
}