5
5
"os"
6
6
"path/filepath"
7
7
"strings"
8
+ "text/template"
9
+
10
+ _ "embed"
8
11
9
12
"github.com/pkg/errors"
10
13
"go.jetpack.io/devbox/internal/boxcli/featureflag"
@@ -15,6 +18,9 @@ import (
15
18
"go.jetpack.io/devbox/internal/plugin"
16
19
)
17
20
21
+ //go:embed tmpl/init-hook.tmpl
22
+ var initHookTmpl string
23
+
18
24
const scriptsDir = ".devbox/gen/scripts"
19
25
20
26
// HooksFilename is the name of the file that contains the project's init-hooks and plugin hooks
@@ -27,6 +33,7 @@ type devboxer interface {
27
33
InstallablePackages () []* devpkg.Package
28
34
PluginManager () * plugin.Manager
29
35
ProjectDir () string
36
+ ProjectDirHash () string
30
37
}
31
38
32
39
// WriteScriptsToFiles writes scripts defined in devbox.json into files inside .devbox/gen/scripts.
@@ -55,7 +62,7 @@ func WriteScriptsToFiles(devbox devboxer) error {
55
62
}
56
63
hooks := strings .Join (append (pluginHooks , devbox .Config ().InitHook ().String ()), "\n \n " )
57
64
// always write it, even if there are no hooks, because scripts will source it.
58
- err = writeHookFile (devbox , hooks )
65
+ err = writeInitHookFile (devbox , hooks )
59
66
if err != nil {
60
67
return errors .WithStack (err )
61
68
}
@@ -84,15 +91,25 @@ func WriteScriptsToFiles(devbox devboxer) error {
84
91
return nil
85
92
}
86
93
87
- func writeHookFile (devbox devboxer , body string ) (err error ) {
94
+ func writeInitHookFile (devbox devboxer , body string ) (err error ) {
88
95
script , err := createScriptFile (devbox , HooksFilename )
89
96
if err != nil {
90
97
return errors .WithStack (err )
91
98
}
92
99
defer script .Close () // best effort: close file
93
100
94
- _ , err = script .WriteString (body )
95
- return errors .WithStack (err )
101
+ t , err := template .New ("init-hook-template" ).Parse (initHookTmpl )
102
+ if err != nil {
103
+ return errors .WithStack (err )
104
+ }
105
+
106
+ return t .Execute (script , map [string ]any {
107
+ "Body" : body ,
108
+ "InitHookHash" : "__DEVBOX_INIT_HOOK_" + devbox .ProjectDirHash (),
109
+ // TODO put IsFish() in common place so we can call here and in devbox package
110
+ // without adding more stuff to interface
111
+ "IsFish" : filepath .Base (os .Getenv ("SHELL" )) == "fish" ,
112
+ })
96
113
}
97
114
98
115
func WriteScriptFile (devbox devboxer , name , body string ) (err error ) {
0 commit comments