Skip to content

Commit e55ef34

Browse files
authored
[plugins] Fix init hooks for custom plugins (#1500)
## Summary Ensure custom plugin init hooks run. Previously they were only running for builtins. ## How was it tested? `devbox run run_test`
1 parent a734dc7 commit e55ef34

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

examples/plugins/local/my-plugin/my-plugin.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@
1010
"{{ .Virtenv }}/some-file": "some-file.txt",
1111
"{{ .DevboxDir }}/some-file.txt": "some-file.txt",
1212
"{{ .Virtenv }}/process-compose.yaml": "process-compose.yaml"
13+
},
14+
"shell": {
15+
"init_hook": [
16+
"export MY_INIT_HOOK_VAR=BAR"
17+
]
1318
}
1419
}

examples/plugins/local/test.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,12 @@ if [ -z "$MY_FOO_VAR" ]; then
44
echo "MY_FOO_VAR environment variable is not set."
55
exit 1
66
else
7-
echo "MY_FOO_VAR is set to '$MY_FOO_VAR'"
7+
echo "MY_FOO_VAR is set to '$MY_FOO_VAR'"
8+
fi
9+
10+
if [ -z "$MY_INIT_HOOK_VAR" ]; then
11+
echo "MY_INIT_HOOK_VAR environment variable is not set."
12+
exit 1
13+
else
14+
echo "MY_INIT_HOOK_VAR is set to '$MY_INIT_HOOK_VAR'"
815
fi

internal/plugin/hooks.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,24 @@ import (
77
"go.jetpack.io/devbox/internal/devpkg"
88
)
99

10-
func InitHooks(pkgs []*devpkg.Package, projectDir string) ([]string, error) {
10+
func (m *Manager) InitHooks(
11+
pkgs []*devpkg.Package,
12+
includes []string,
13+
) ([]string, error) {
1114
hooks := []string{}
15+
allPkgs := []Includable{}
1216
for _, pkg := range pkgs {
13-
c, err := getConfigIfAny(pkg, projectDir)
17+
allPkgs = append(allPkgs, pkg)
18+
}
19+
for _, include := range includes {
20+
name, err := m.ParseInclude(include)
21+
if err != nil {
22+
return nil, err
23+
}
24+
allPkgs = append(allPkgs, name)
25+
}
26+
for _, pkg := range allPkgs {
27+
c, err := getConfigIfAny(pkg, m.ProjectDir())
1428
if err != nil {
1529
return nil, err
1630
}

internal/shellgen/scripts.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ func WriteScriptsToFiles(devbox devboxer) error {
4646

4747
// Write all hooks to a file.
4848
written := map[string]struct{}{} // set semantics; value is irrelevant
49-
pluginHooks, err := plugin.InitHooks(devbox.InstallablePackages(), devbox.ProjectDir())
49+
pluginHooks, err := devbox.PluginManager().InitHooks(
50+
devbox.InstallablePackages(),
51+
devbox.Config().Include,
52+
)
5053
if err != nil {
5154
return errors.WithStack(err)
5255
}

0 commit comments

Comments
 (0)