generated from laraboot-io/seed
/
common.go
166 lines (132 loc) · 3.96 KB
/
common.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
package integration
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/cloudfoundry/dagger"
"github.com/paketo-buildpacks/occam"
. "github.com/onsi/gomega"
)
var (
sharedOfflineURI string
phpDistOfflineURI string
phpWebOfflineURI string
phpComposerOfflineURI string
smokeGunPackage struct {
Name string `json:"name"`
Version string `json:"version"`
}
buildpackInfo struct {
Buildpack struct {
ID string
Name string
}
}
)
func PreparePhpOfflineBps() {
var config struct {
PhpDistOffline string `json:"php-dist"`
}
file, err := os.Open("../integration.json")
Expect(err).ToNot(HaveOccurred())
defer file.Close()
Expect(json.NewDecoder(file).Decode(&config)).To(Succeed())
var cmdConfig struct {
SmokeGunPackage struct {
Name string `json:"name"`
Version string `json:"version"`
} `json:"smoke-gun"`
}
// test-case
file, err = os.Open("../integration/testdata/sandbox/shared.json")
defer file.Close()
Expect(json.NewDecoder(file).Decode(&cmdConfig)).To(Succeed())
smokeGunPackage = cmdConfig.SmokeGunPackage
bpRoot, err := filepath.Abs("./..")
Expect(err).ToNot(HaveOccurred())
sharedOfflineURI, err = Package(bpRoot, "1.2.3", true)
Expect(err).ToNot(HaveOccurred())
buildpackStore := occam.NewBuildpackStore()
phpDistOfflineURI, err = buildpackStore.Get.
WithVersion("1.2.3").
WithOfflineDependencies().
Execute(config.PhpDistOffline)
phpWebRepo, err := dagger.GetLatestUnpackagedCommunityBuildpack("paketo-buildpacks", "php-web")
Expect(err).NotTo(HaveOccurred())
phpWebOfflineURI, err = Package(phpWebRepo, "1.2.3", true)
Expect(err).ToNot(HaveOccurred())
phpComposerRepo, err := dagger.GetLatestUnpackagedCommunityBuildpack("paketo-buildpacks", "php-composer")
Expect(err).NotTo(HaveOccurred())
phpComposerOfflineURI, err = Package(phpComposerRepo, "1.2.3", true)
Expect(err).ToNot(HaveOccurred())
}
// PreparePhpBps builds the current buildpacks.
func PreparePhpBps() ([]string, error) {
var config struct {
PhpDist string `json:"php-dist"`
}
file, err := os.Open("../integration.json")
Expect(err).ToNot(HaveOccurred())
defer file.Close()
Expect(json.NewDecoder(file).Decode(&config)).To(Succeed())
bpRoot, err := filepath.Abs("./..")
if err != nil {
return []string{}, err
}
composerBp, err := Package(bpRoot, "1.2.3", false)
if err != nil {
return []string{}, err
}
buildpackStore := occam.NewBuildpackStore()
phpDistBp, err := buildpackStore.Get.Execute(config.PhpDist)
Expect(err).ToNot(HaveOccurred())
phpWebBp, err := dagger.GetLatestBuildpack("php-web-cnb")
Expect(err).NotTo(HaveOccurred())
return []string{phpDistBp, composerBp, phpWebBp}, nil
}
// MakeBuildEnv creates a build environment map
func MakeBuildEnv(debug bool) map[string]string {
env := make(map[string]string)
if debug {
env["BP_DEBUG"] = "true"
}
githubToken := os.Getenv("GIT_TOKEN")
if githubToken != "" {
env["COMPOSER_GITHUB_OAUTH_TOKEN"] = githubToken
}
return env
}
func DecodeBPToml() {
file, err := os.Open("../buildpack.toml")
Expect(err).NotTo(HaveOccurred())
defer file.Close()
_, err = toml.DecodeReader(file, &buildpackInfo)
Expect(err).NotTo(HaveOccurred())
}
func Package(root, version string, cached bool) (string, error) {
var cmd *exec.Cmd
dir, err := filepath.Abs("./..")
if err != nil {
return "", err
}
bpPath := filepath.Join(root, "artifact")
if cached {
cmd = exec.Command(filepath.Join(dir, ".bin", "packager"), "--archive", "--version", version, fmt.Sprintf("%s-cached", bpPath))
} else {
cmd = exec.Command(filepath.Join(dir, ".bin", "packager"), "--archive", "--uncached", "--version", version, bpPath)
}
cmd.Env = append(os.Environ(), fmt.Sprintf("PACKAGE_DIR=%s", bpPath))
cmd.Dir = root
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return "", err
}
if cached {
return fmt.Sprintf("%s-cached.tgz", bpPath), nil
}
return fmt.Sprintf("%s.tgz", bpPath), nil
}