-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.go
138 lines (119 loc) · 3.97 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
package shellgen
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/synopkg/devbox/internal/boxcli/featureflag"
"github.com/synopkg/devbox/internal/debug"
"github.com/synopkg/devbox/internal/devconfig"
"github.com/synopkg/devbox/internal/devpkg"
"github.com/synopkg/devbox/internal/lock"
"github.com/synopkg/devbox/internal/plugin"
)
const scriptsDir = ".devbox/gen/scripts"
// HooksFilename is the name of the file that contains the project's init-hooks and plugin hooks
const HooksFilename = ".hooks"
type devboxer interface {
Config() *devconfig.Config
Lockfile() *lock.File
AllInstallablePackages() ([]*devpkg.Package, error)
InstallablePackages() []*devpkg.Package
PluginManager() *plugin.Manager
ProjectDir() 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
pluginHooks, err := devbox.PluginManager().InitHooks(
devbox.InstallablePackages(),
devbox.Config().Include,
)
if err != nil {
return errors.WithStack(err)
}
hooks := strings.Join(append(pluginHooks, devbox.Config().InitHook().String()), "\n\n")
// always write it, even if there are no hooks, because scripts will source it.
err = writeHookFile(devbox, hooks)
if err != nil {
return errors.WithStack(err)
}
written[HooksFilename] = struct{}{}
// Write scripts to files.
for name, body := range devbox.Config().Scripts() {
err = WriteScriptFile(devbox, name, ScriptBody(devbox, body.String()))
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 writeHookFile(devbox devboxer, body string) (err error) {
script, err := createScriptFile(devbox, HooksFilename)
if err != nil {
return errors.WithStack(err)
}
defer script.Close() // best effort: close file
_, err = script.WriteString(body)
return errors.WithStack(err)
}
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 {
return fmt.Sprintf(". %s\n\n%s", ScriptPath(d.ProjectDir(), HooksFilename), body)
}