/
scripts.go
178 lines (153 loc) · 5.01 KB
/
scripts.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
package shellgen
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
_ "embed"
"github.com/pkg/errors"
"go.jetpack.io/devbox/internal/boxcli/featureflag"
"go.jetpack.io/devbox/internal/debug"
"go.jetpack.io/devbox/internal/devconfig"
"go.jetpack.io/devbox/internal/devpkg"
"go.jetpack.io/devbox/internal/lock"
"go.jetpack.io/devbox/internal/plugin"
)
//go:embed tmpl/script-wrapper.tmpl
var scriptWrapperTmplString string
var scriptWrapperTmpl = template.Must(template.New("script-wrapper").Parse(scriptWrapperTmplString))
//go:embed tmpl/init-hook-wrapper.tmpl
var initHookWrapperString string
var initHookWrapperTmpl = template.Must(template.New("init-hook-wrapper").Parse(initHookWrapperString))
const scriptsDir = ".devbox/gen/scripts"
// HooksFilename is the name of the file that contains a wrapper of the
// project's init-hooks and plugin hooks
const (
HooksFilename = ".hooks"
rawHooksFilename = ".raw-hooks"
)
type devboxer interface {
Config() *devconfig.Config
Lockfile() *lock.File
InstallablePackages() []*devpkg.Package
PluginManager() *plugin.Manager
ProjectDir() string
ProjectDirHash() string
}
// WriteScriptsToFiles writes scripts defined in devbox.json into files inside .devbox/gen/scripts.
// Scripts (and hooks) are persisted so that we can easily call them from devbox run (inside or outside shell).
func WriteScriptsToFiles(devbox devboxer) error {
defer debug.FunctionTimer().End()
err := os.MkdirAll(filepath.Join(devbox.ProjectDir(), scriptsDir), 0o755) // Ensure directory exists.
if err != nil {
return errors.WithStack(err)
}
// Read dir contents before writing, so we can clean up later.
entries, err := os.ReadDir(filepath.Join(devbox.ProjectDir(), scriptsDir))
if err != nil {
return errors.WithStack(err)
}
// Write all hooks to a file.
written := map[string]struct{}{} // set semantics; value is irrelevant
// always write it, even if there are no hooks, because scripts will source it.
err = writeRawInitHookFile(devbox, devbox.Config().InitHook().String())
if err != nil {
return errors.WithStack(err)
}
written[rawHooksFilename] = struct{}{}
err = writeInitHookWrapperFile(devbox)
if err != nil {
return errors.WithStack(err)
}
written[HooksFilename] = struct{}{}
// Write scripts to files.
for name, body := range devbox.Config().Scripts() {
scriptBody, err := ScriptBody(devbox, body.String())
if err != nil {
return errors.WithStack(err)
}
err = WriteScriptFile(devbox, name, scriptBody)
if err != nil {
return errors.WithStack(err)
}
written[name] = struct{}{}
}
// Delete any files that weren't written just now.
for _, entry := range entries {
scriptName := strings.TrimSuffix(entry.Name(), ".sh")
if _, ok := written[scriptName]; !ok && !entry.IsDir() {
err := os.Remove(ScriptPath(devbox.ProjectDir(), scriptName))
if err != nil {
debug.Log("failed to clean up script file %s, error = %s", entry.Name(), err) // no need to fail run
}
}
}
return nil
}
func writeRawInitHookFile(devbox devboxer, body string) (err error) {
script, err := createScriptFile(devbox, rawHooksFilename)
if err != nil {
return errors.WithStack(err)
}
defer script.Close() // best effort: close file
_, err = script.WriteString(body)
return errors.WithStack(err)
}
func writeInitHookWrapperFile(devbox devboxer) (err error) {
script, err := createScriptFile(devbox, HooksFilename)
if err != nil {
return errors.WithStack(err)
}
defer script.Close() // best effort: close file
return initHookWrapperTmpl.Execute(script, map[string]string{
"InitHookHash": "__DEVBOX_INIT_HOOK_" + devbox.ProjectDirHash(),
"RawHooksFile": ScriptPath(devbox.ProjectDir(), rawHooksFilename),
})
}
func WriteScriptFile(devbox devboxer, name, body string) (err error) {
script, err := createScriptFile(devbox, name)
if err != nil {
return errors.WithStack(err)
}
defer script.Close() // best effort: close file
if featureflag.ScriptExitOnError.Enabled() {
// NOTE: Devbox scripts run using `sh` for consistency.
body = fmt.Sprintf("set -e\n\n%s", body)
}
_, err = script.WriteString(body)
return errors.WithStack(err)
}
func createScriptFile(devbox devboxer, name string) (script *os.File, err error) {
script, err = os.Create(ScriptPath(devbox.ProjectDir(), name))
if err != nil {
return nil, errors.WithStack(err)
}
defer func() {
// best effort: close file if there was some subsequent error
if err != nil {
_ = script.Close()
}
}()
err = script.Chmod(0o755)
if err != nil {
return nil, errors.WithStack(err)
}
return script, nil
}
func ScriptPath(projectDir, scriptName string) string {
return filepath.Join(projectDir, scriptsDir, scriptName+".sh")
}
func ScriptBody(d devboxer, body string) (string, error) {
var buf bytes.Buffer
err := scriptWrapperTmpl.Execute(&buf, map[string]string{
"Body": body,
"InitHookHash": "__DEVBOX_INIT_HOOK_" + d.ProjectDirHash(),
"InitHookPath": ScriptPath(d.ProjectDir(), HooksFilename),
})
if err != nil {
return "", errors.WithStack(err)
}
return buf.String(), nil
}