From 59331f2c41f182f2ca7bfb353f35d79a95c8673e Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Fri, 22 Sep 2023 11:49:49 -0700 Subject: [PATCH 1/2] [plugins] Fix init hooks for custom plugins --- .../plugins/local/my-plugin/my-plugin.json | 5 +++++ examples/plugins/local/test.sh | 7 +++++++ internal/plugin/hooks.go | 18 ++++++++++++++++-- internal/shellgen/scripts.go | 5 ++++- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/examples/plugins/local/my-plugin/my-plugin.json b/examples/plugins/local/my-plugin/my-plugin.json index 11d4bb85e12..866a7c8d158 100644 --- a/examples/plugins/local/my-plugin/my-plugin.json +++ b/examples/plugins/local/my-plugin/my-plugin.json @@ -10,5 +10,10 @@ "{{ .Virtenv }}/some-file": "some-file.txt", "{{ .DevboxDir }}/some-file.txt": "some-file.txt", "{{ .Virtenv }}/process-compose.yaml": "process-compose.yaml" + }, + "shell": { + "init_hook": [ + "export MY_INIT_HOOK_VAR=BAR" + ] } } diff --git a/examples/plugins/local/test.sh b/examples/plugins/local/test.sh index d66497eee85..e7744b64ca6 100755 --- a/examples/plugins/local/test.sh +++ b/examples/plugins/local/test.sh @@ -6,3 +6,10 @@ if [ -z "$MY_FOO_VAR" ]; then else echo "MY_FOO_VAR is set to '$MY_FOO_VAR'" fi + +if [ -z "$MY_INIT_HOOK_VAR" ]; then + echo "MY_INIT_HOOK_VAR environment variable is not set." + exit 1 +else + echo "MY_INIT_HOOK_VAR is set to '$MY_INIT_HOOK_VAR'" +fi diff --git a/internal/plugin/hooks.go b/internal/plugin/hooks.go index bbf4c9ab39f..c8bd5db5a78 100644 --- a/internal/plugin/hooks.go +++ b/internal/plugin/hooks.go @@ -7,10 +7,24 @@ import ( "go.jetpack.io/devbox/internal/devpkg" ) -func InitHooks(pkgs []*devpkg.Package, projectDir string) ([]string, error) { +func (m *Manager) InitHooks( + pkgs []*devpkg.Package, + includes []string, +) ([]string, error) { hooks := []string{} + allPkgs := []Includable{} for _, pkg := range pkgs { - c, err := getConfigIfAny(pkg, projectDir) + allPkgs = append(allPkgs, pkg) + } + for _, include := range includes { + name, err := m.ParseInclude(include) + if err != nil { + return nil, err + } + allPkgs = append(allPkgs, name) + } + for _, pkg := range allPkgs { + c, err := getConfigIfAny(pkg, m.ProjectDir()) if err != nil { return nil, err } diff --git a/internal/shellgen/scripts.go b/internal/shellgen/scripts.go index 698190d4ea7..77bf44c07d9 100644 --- a/internal/shellgen/scripts.go +++ b/internal/shellgen/scripts.go @@ -46,7 +46,10 @@ func WriteScriptsToFiles(devbox devboxer) error { // Write all hooks to a file. written := map[string]struct{}{} // set semantics; value is irrelevant - pluginHooks, err := plugin.InitHooks(devbox.InstallablePackages(), devbox.ProjectDir()) + pluginHooks, err := devbox.PluginManager().InitHooks( + devbox.InstallablePackages(), + devbox.Config().Include, + ) if err != nil { return errors.WithStack(err) } From cf3ab263b1243b79d51957e7dba761e330211dd9 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Fri, 22 Sep 2023 12:05:43 -0700 Subject: [PATCH 2/2] Fix indent --- examples/plugins/local/test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/plugins/local/test.sh b/examples/plugins/local/test.sh index e7744b64ca6..b02a6474b88 100755 --- a/examples/plugins/local/test.sh +++ b/examples/plugins/local/test.sh @@ -4,12 +4,12 @@ if [ -z "$MY_FOO_VAR" ]; then echo "MY_FOO_VAR environment variable is not set." exit 1 else - echo "MY_FOO_VAR is set to '$MY_FOO_VAR'" + echo "MY_FOO_VAR is set to '$MY_FOO_VAR'" fi if [ -z "$MY_INIT_HOOK_VAR" ]; then echo "MY_INIT_HOOK_VAR environment variable is not set." exit 1 else - echo "MY_INIT_HOOK_VAR is set to '$MY_INIT_HOOK_VAR'" + echo "MY_INIT_HOOK_VAR is set to '$MY_INIT_HOOK_VAR'" fi