Skip to content

Commit

Permalink
use lifecycle hook
Browse files Browse the repository at this point in the history
  • Loading branch information
savil committed Nov 1, 2024
1 parent 3b0a9c9 commit 3c02e75
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 24 deletions.
2 changes: 2 additions & 0 deletions devbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"go": "latest",
"runx:golangci/golangci-lint": "latest",
"runx:mvdan/gofumpt": "latest",

"hello": "latest",
},
"env": {
"GOENV": "off",
Expand Down
48 changes: 48 additions & 0 deletions devbox.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,54 @@
}
}
},
"hello@latest": {
"last_modified": "2024-10-13T23:44:06Z",
"resolved": "github:NixOS/nixpkgs/d4f247e89f6e10120f911e2e2d2254a050d0f732#hello",
"source": "devbox-search",
"version": "2.12.1",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/8fpvkfwr8fm91xlzznsgh3g1fcw0hfnh-hello-2.12.1",
"default": true
}
],
"store_path": "/nix/store/8fpvkfwr8fm91xlzznsgh3g1fcw0hfnh-hello-2.12.1"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/9i2ay9whhyswa2cc30azxiyqrjb795xj-hello-2.12.1",
"default": true
}
],
"store_path": "/nix/store/9i2ay9whhyswa2cc30azxiyqrjb795xj-hello-2.12.1"
},
"x86_64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/h6lx6hmccksq64nsa4h3c8p9gl1vzqcf-hello-2.12.1",
"default": true
}
],
"store_path": "/nix/store/h6lx6hmccksq64nsa4h3c8p9gl1vzqcf-hello-2.12.1"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/26xbg1ndr7hbcncrlf9nhx5is2b25d13-hello-2.12.1",
"default": true
}
],
"store_path": "/nix/store/26xbg1ndr7hbcncrlf9nhx5is2b25d13-hello-2.12.1"
}
}
},
"runx:golangci/golangci-lint@latest": {
"resolved": "golangci/[email protected]",
"version": "v1.60.2"
Expand Down
20 changes: 15 additions & 5 deletions internal/boxcli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"go.jetpack.io/devbox/internal/devbox"
"go.jetpack.io/devbox/internal/devbox/devopt"
"go.jetpack.io/devbox/internal/redact"
"go.jetpack.io/devbox/internal/ux"
)

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

onStaleState := func() {
ux.FHidableWarning(
ctx,
cmd.ErrOrStderr(),
devbox.StateOutOfDateMessage,
"with --recompute=true",
)
}

envOpts := devopt.EnvOptions{
OmitNixEnv: flags.omitNixEnv,
Pure: flags.pure,
RecomputeEnv: &devopt.RecomputeEnvOpts{
Disabled: !flags.recomputeEnv,
StateOutOfDateMessage: fmt.Sprintf(devbox.StateOutOfDateMessage, "with --recompute=true"),
Hooks: devopt.EnvLifecycleHooks{
OnSkipRecomputeDespiteStaleState: onStaleState,
},
OmitNixEnv: flags.omitNixEnv,
Pure: flags.pure,
SkipRecomputeEnv: !flags.recomputeEnv,
}
if err := box.RunScript(ctx, envOpts, script, scriptArgs); err != nil {
return redact.Errorf("error running script %q in Devbox: %w", script, err)
Expand Down
20 changes: 15 additions & 5 deletions internal/boxcli/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.jetpack.io/devbox/internal/devbox"
"go.jetpack.io/devbox/internal/devbox/devopt"
"go.jetpack.io/devbox/internal/envir"
"go.jetpack.io/devbox/internal/ux"
)

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

onStaleState := func() {
ux.FHidableWarning(
ctx,
cmd.ErrOrStderr(),
devbox.StateOutOfDateMessage,
"with --recompute=true",
)
}

return box.Shell(ctx, devopt.EnvOptions{
OmitNixEnv: flags.omitNixEnv,
Pure: flags.pure,
RecomputeEnv: &devopt.RecomputeEnvOpts{
Disabled: !flags.recomputeEnv,
StateOutOfDateMessage: fmt.Sprintf(devbox.StateOutOfDateMessage, "with --recompute=true"),
Hooks: devopt.EnvLifecycleHooks{
OnSkipRecomputeDespiteStaleState: onStaleState,
},
OmitNixEnv: flags.omitNixEnv,
Pure: flags.pure,
SkipRecomputeEnv: !flags.recomputeEnv,
})
}

Expand Down
17 changes: 13 additions & 4 deletions internal/boxcli/shellenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,24 @@ func shellEnvFunc(
}
}

onStaleState := func() {
ux.FHidableWarning(
ctx,
cmd.ErrOrStderr(),
devbox.StateOutOfDateMessage,
"devbox install",
)
}

envStr, err := box.EnvExports(ctx, devopt.EnvExportsOpts{
EnvOptions: devopt.EnvOptions{
Hooks: devopt.EnvLifecycleHooks{
OnSkipRecomputeDespiteStaleState: onStaleState,
},
OmitNixEnv: flags.omitNixEnv,
PreservePathStack: flags.preservePathStack,
Pure: flags.pure,
RecomputeEnv: &devopt.RecomputeEnvOpts{
Disabled: !flags.recomputeEnv,
StateOutOfDateMessage: fmt.Sprintf(devbox.StateOutOfDateMessage, box.RefreshAliasOrCommand()),
},
SkipRecomputeEnv: !flags.recomputeEnv,
},
NoRefreshAlias: flags.noRefreshAlias,
RunHooks: flags.runInitHook,
Expand Down
10 changes: 4 additions & 6 deletions internal/devbox/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,14 +804,12 @@ func (d *Devbox) ensureStateIsUpToDateAndComputeEnv(
) (map[string]string, error) {
defer debug.FunctionTimer().End()

if envOpts.RecomputeEnv.Disabled {
if envOpts.SkipRecomputeEnv {
upToDate, _ := d.lockfile.IsUpToDateAndInstalled(isFishShell())
if !upToDate {
ux.FHidableWarning(
ctx,
d.stderr,
envOpts.RecomputeEnv.StateOutOfDateMessage, //nolint:govet
)
if envOpts.Hooks.OnSkipRecomputeDespiteStaleState != nil {
envOpts.Hooks.OnSkipRecomputeDespiteStaleState()
}
}
} else {
// When ensureStateIsUpToDate is called with ensure=true, it always
Expand Down
9 changes: 5 additions & 4 deletions internal/devbox/devopt/devboxopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ type EnvExportsOpts struct {
// like `shellenv`, `shell` and `run`.
// - The struct is designed for the "common case" to be zero-initialized as `EnvOptions{}`.
type EnvOptions struct {
Hooks EnvLifecycleHooks
OmitNixEnv bool
PreservePathStack bool
Pure bool
RecomputeEnv *RecomputeEnvOpts
SkipRecomputeEnv bool
}

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

0 comments on commit 3c02e75

Please sign in to comment.