Skip to content

Commit ec78396

Browse files
committed
use lifecycle hook
1 parent 3b0a9c9 commit ec78396

File tree

7 files changed

+102
-24
lines changed

7 files changed

+102
-24
lines changed

devbox.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"go": "latest",
66
"runx:golangci/golangci-lint": "latest",
77
"runx:mvdan/gofumpt": "latest",
8+
9+
"hello": "latest",
810
},
911
"env": {
1012
"GOENV": "off",

devbox.lock

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,54 @@
4949
}
5050
}
5151
},
52+
"hello@latest": {
53+
"last_modified": "2024-10-13T23:44:06Z",
54+
"resolved": "github:NixOS/nixpkgs/d4f247e89f6e10120f911e2e2d2254a050d0f732#hello",
55+
"source": "devbox-search",
56+
"version": "2.12.1",
57+
"systems": {
58+
"aarch64-darwin": {
59+
"outputs": [
60+
{
61+
"name": "out",
62+
"path": "/nix/store/8fpvkfwr8fm91xlzznsgh3g1fcw0hfnh-hello-2.12.1",
63+
"default": true
64+
}
65+
],
66+
"store_path": "/nix/store/8fpvkfwr8fm91xlzznsgh3g1fcw0hfnh-hello-2.12.1"
67+
},
68+
"aarch64-linux": {
69+
"outputs": [
70+
{
71+
"name": "out",
72+
"path": "/nix/store/9i2ay9whhyswa2cc30azxiyqrjb795xj-hello-2.12.1",
73+
"default": true
74+
}
75+
],
76+
"store_path": "/nix/store/9i2ay9whhyswa2cc30azxiyqrjb795xj-hello-2.12.1"
77+
},
78+
"x86_64-darwin": {
79+
"outputs": [
80+
{
81+
"name": "out",
82+
"path": "/nix/store/h6lx6hmccksq64nsa4h3c8p9gl1vzqcf-hello-2.12.1",
83+
"default": true
84+
}
85+
],
86+
"store_path": "/nix/store/h6lx6hmccksq64nsa4h3c8p9gl1vzqcf-hello-2.12.1"
87+
},
88+
"x86_64-linux": {
89+
"outputs": [
90+
{
91+
"name": "out",
92+
"path": "/nix/store/26xbg1ndr7hbcncrlf9nhx5is2b25d13-hello-2.12.1",
93+
"default": true
94+
}
95+
],
96+
"store_path": "/nix/store/26xbg1ndr7hbcncrlf9nhx5is2b25d13-hello-2.12.1"
97+
}
98+
}
99+
},
52100
"runx:golangci/golangci-lint@latest": {
53101
"resolved": "golangci/[email protected]",
54102
"version": "v1.60.2"

internal/boxcli/run.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"go.jetpack.io/devbox/internal/devbox"
1818
"go.jetpack.io/devbox/internal/devbox/devopt"
1919
"go.jetpack.io/devbox/internal/redact"
20+
"go.jetpack.io/devbox/internal/ux"
2021
)
2122

2223
type runCmdFlags struct {
@@ -122,13 +123,22 @@ func runScriptCmd(cmd *cobra.Command, args []string, flags runCmdFlags) error {
122123
return redact.Errorf("error reading devbox.json: %w", err)
123124
}
124125

126+
onStaleState := func() {
127+
ux.FHidableWarning(
128+
ctx,
129+
cmd.ErrOrStderr(),
130+
devbox.StateOutOfDateMessage,
131+
"with --recompute=true",
132+
)
133+
}
134+
125135
envOpts := devopt.EnvOptions{
126-
OmitNixEnv: flags.omitNixEnv,
127-
Pure: flags.pure,
128-
RecomputeEnv: &devopt.RecomputeEnvOpts{
129-
Disabled: !flags.recomputeEnv,
130-
StateOutOfDateMessage: fmt.Sprintf(devbox.StateOutOfDateMessage, "with --recompute=true"),
136+
Hooks: devopt.EnvLifecycleHooks{
137+
OnSkipRecomputeDespiteStaleState: onStaleState,
131138
},
139+
OmitNixEnv: flags.omitNixEnv,
140+
Pure: flags.pure,
141+
SkipRecomputeEnv: !flags.recomputeEnv,
132142
}
133143
if err := box.RunScript(ctx, envOpts, script, scriptArgs); err != nil {
134144
return redact.Errorf("error running script %q in Devbox: %w", script, err)

internal/boxcli/shell.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"go.jetpack.io/devbox/internal/devbox"
1414
"go.jetpack.io/devbox/internal/devbox/devopt"
1515
"go.jetpack.io/devbox/internal/envir"
16+
"go.jetpack.io/devbox/internal/ux"
1617
)
1718

1819
type shellCmdFlags struct {
@@ -95,13 +96,22 @@ func runShellCmd(cmd *cobra.Command, flags shellCmdFlags) error {
9596
return shellInceptionErrorMsg("devbox shell")
9697
}
9798

99+
onStaleState := func() {
100+
ux.FHidableWarning(
101+
ctx,
102+
cmd.ErrOrStderr(),
103+
devbox.StateOutOfDateMessage,
104+
"with --recompute=true",
105+
)
106+
}
107+
98108
return box.Shell(ctx, devopt.EnvOptions{
99-
OmitNixEnv: flags.omitNixEnv,
100-
Pure: flags.pure,
101-
RecomputeEnv: &devopt.RecomputeEnvOpts{
102-
Disabled: !flags.recomputeEnv,
103-
StateOutOfDateMessage: fmt.Sprintf(devbox.StateOutOfDateMessage, "with --recompute=true"),
109+
Hooks: devopt.EnvLifecycleHooks{
110+
OnSkipRecomputeDespiteStaleState: onStaleState,
104111
},
112+
OmitNixEnv: flags.omitNixEnv,
113+
Pure: flags.pure,
114+
SkipRecomputeEnv: !flags.recomputeEnv,
105115
})
106116
}
107117

internal/boxcli/shellenv.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,24 @@ func shellEnvFunc(
115115
}
116116
}
117117

118+
onStaleState := func() {
119+
ux.FHidableWarning(
120+
ctx,
121+
cmd.ErrOrStderr(),
122+
devbox.StateOutOfDateMessage,
123+
box.RefreshAliasOrCommand(),
124+
)
125+
}
126+
118127
envStr, err := box.EnvExports(ctx, devopt.EnvExportsOpts{
119128
EnvOptions: devopt.EnvOptions{
129+
Hooks: devopt.EnvLifecycleHooks{
130+
OnSkipRecomputeDespiteStaleState: onStaleState,
131+
},
120132
OmitNixEnv: flags.omitNixEnv,
121133
PreservePathStack: flags.preservePathStack,
122134
Pure: flags.pure,
123-
RecomputeEnv: &devopt.RecomputeEnvOpts{
124-
Disabled: !flags.recomputeEnv,
125-
StateOutOfDateMessage: fmt.Sprintf(devbox.StateOutOfDateMessage, box.RefreshAliasOrCommand()),
126-
},
135+
SkipRecomputeEnv: !flags.recomputeEnv,
127136
},
128137
NoRefreshAlias: flags.noRefreshAlias,
129138
RunHooks: flags.runInitHook,

internal/devbox/devbox.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -804,14 +804,12 @@ func (d *Devbox) ensureStateIsUpToDateAndComputeEnv(
804804
) (map[string]string, error) {
805805
defer debug.FunctionTimer().End()
806806

807-
if envOpts.RecomputeEnv.Disabled {
807+
if envOpts.SkipRecomputeEnv {
808808
upToDate, _ := d.lockfile.IsUpToDateAndInstalled(isFishShell())
809809
if !upToDate {
810-
ux.FHidableWarning(
811-
ctx,
812-
d.stderr,
813-
envOpts.RecomputeEnv.StateOutOfDateMessage, //nolint:govet
814-
)
810+
if envOpts.Hooks.OnSkipRecomputeDespiteStaleState != nil {
811+
envOpts.Hooks.OnSkipRecomputeDespiteStaleState()
812+
}
815813
}
816814
} else {
817815
// When ensureStateIsUpToDate is called with ensure=true, it always

internal/devbox/devopt/devboxopts.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,14 @@ type EnvExportsOpts struct {
7272
// like `shellenv`, `shell` and `run`.
7373
// - The struct is designed for the "common case" to be zero-initialized as `EnvOptions{}`.
7474
type EnvOptions struct {
75+
Hooks EnvLifecycleHooks
7576
OmitNixEnv bool
7677
PreservePathStack bool
7778
Pure bool
78-
RecomputeEnv *RecomputeEnvOpts
79+
SkipRecomputeEnv bool
7980
}
8081

81-
type RecomputeEnvOpts struct {
82-
Disabled bool // Disabled instead of Enabled, because zero-value is false
83-
StateOutOfDateMessage string
82+
type EnvLifecycleHooks struct {
83+
// OnSkipRecomputeDespiteStaleState is called when the Devbox state is out of date, AND it is not being recomputed.
84+
OnSkipRecomputeDespiteStaleState func()
8485
}

0 commit comments

Comments
 (0)