Skip to content

Commit c26b0e2

Browse files
authored
[naming] rename filegen to shellgen; and move devcontainer_util.go to /impl/generate (#1198)
1 parent d323c8e commit c26b0e2

21 files changed

+37
-24
lines changed

internal/impl/devbox.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import (
1919

2020
"github.com/pkg/errors"
2121
"github.com/samber/lo"
22-
"go.jetpack.io/devbox/internal/filegen"
22+
"go.jetpack.io/devbox/internal/impl/generate"
23+
"go.jetpack.io/devbox/internal/shellgen"
2324
"golang.org/x/exp/slices"
2425

2526
"go.jetpack.io/devbox/internal/boxcli/usererr"
@@ -141,7 +142,7 @@ func (d *Devbox) Generate(ctx context.Context) error {
141142
ctx, task := trace.NewTask(ctx, "devboxGenerate")
142143
defer task.End()
143144

144-
return errors.WithStack(filegen.GenerateForPrintEnv(ctx, d))
145+
return errors.WithStack(shellgen.GenerateForPrintEnv(ctx, d))
145146
}
146147

147148
func (d *Devbox) Shell(ctx context.Context) error {
@@ -175,7 +176,7 @@ func (d *Devbox) Shell(ctx context.Context) error {
175176
)
176177

177178
opts := []ShellOption{
178-
WithHooksFilePath(filegen.ScriptPath(d.ProjectDir(), filegen.HooksFilename)),
179+
WithHooksFilePath(shellgen.ScriptPath(d.ProjectDir(), shellgen.HooksFilename)),
179180
WithProfile(profileDir),
180181
WithHistoryFile(filepath.Join(d.projectDir, shellHistoryFile)),
181182
WithProjectDir(d.projectDir),
@@ -199,7 +200,7 @@ func (d *Devbox) RunScript(ctx context.Context, cmdName string, cmdArgs []string
199200
return err
200201
}
201202

202-
if err := filegen.WriteScriptsToFiles(d); err != nil {
203+
if err := shellgen.WriteScriptsToFiles(d); err != nil {
203204
return err
204205
}
205206

@@ -224,18 +225,18 @@ func (d *Devbox) RunScript(ctx context.Context, cmdName string, cmdArgs []string
224225
var cmdWithArgs []string
225226
if _, ok := d.cfg.Scripts()[cmdName]; ok {
226227
// it's a script, so replace the command with the script file's path.
227-
cmdWithArgs = append([]string{filegen.ScriptPath(d.ProjectDir(), cmdName)}, cmdArgs...)
228+
cmdWithArgs = append([]string{shellgen.ScriptPath(d.ProjectDir(), cmdName)}, cmdArgs...)
228229
} else {
229230
// Arbitrary commands should also run the hooks, so we write them to a file as well. However, if the
230231
// command args include env variable evaluations, then they'll be evaluated _before_ the hooks run,
231232
// which we don't want. So, one solution is to write the entire command and its arguments into the
232233
// file itself, but that may not be great if the variables contain sensitive information. Instead,
233234
// we save the entire command (with args) into the DEVBOX_RUN_CMD var, and then the script evals it.
234-
err := filegen.WriteScriptFile(d, arbitraryCmdFilename, filegen.ScriptBody(d, "eval $DEVBOX_RUN_CMD\n"))
235+
err := shellgen.WriteScriptFile(d, arbitraryCmdFilename, shellgen.ScriptBody(d, "eval $DEVBOX_RUN_CMD\n"))
235236
if err != nil {
236237
return err
237238
}
238-
cmdWithArgs = []string{filegen.ScriptPath(d.ProjectDir(), arbitraryCmdFilename)}
239+
cmdWithArgs = []string{shellgen.ScriptPath(d.ProjectDir(), arbitraryCmdFilename)}
239240
env["DEVBOX_RUN_CMD"] = strings.Join(append([]string{cmdName}, cmdArgs...), " ")
240241
}
241242

@@ -282,7 +283,7 @@ func (d *Devbox) PrintEnv(ctx context.Context, includeHooks bool) (string, error
282283
envStr := exportify(envs)
283284

284285
if includeHooks {
285-
hooksStr := ". " + filegen.ScriptPath(d.ProjectDir(), filegen.HooksFilename)
286+
hooksStr := ". " + shellgen.ScriptPath(d.ProjectDir(), shellgen.HooksFilename)
286287
envStr = fmt.Sprintf("%s\n%s;\n", envStr, hooksStr)
287288
}
288289

@@ -372,14 +373,14 @@ func (d *Devbox) GenerateDevcontainer(ctx context.Context, force bool) error {
372373
redact.Safe(filepath.Base(devContainerPath)), err)
373374
}
374375
// generate dockerfile
375-
err = filegen.CreateDockerfile(ctx,
376+
err = generate.CreateDockerfile(ctx,
376377
devContainerPath, d.getLocalFlakesDirs(), true /* isDevcontainer */)
377378
if err != nil {
378379
return redact.Errorf("error generating dev container Dockerfile in <project>/%s: %w",
379380
redact.Safe(filepath.Base(devContainerPath)), err)
380381
}
381382
// generate devcontainer.json
382-
err = filegen.CreateDevcontainer(ctx, devContainerPath, d.Packages())
383+
err = generate.CreateDevcontainer(ctx, devContainerPath, d.Packages())
383384
if err != nil {
384385
return redact.Errorf("error generating devcontainer.json in <project>/%s: %w",
385386
redact.Safe(filepath.Base(devContainerPath)), err)
@@ -404,12 +405,12 @@ func (d *Devbox) GenerateDockerfile(ctx context.Context, force bool) error {
404405

405406
// generate dockerfile
406407
return errors.WithStack(
407-
filegen.CreateDockerfile(ctx,
408+
generate.CreateDockerfile(ctx,
408409
d.projectDir, d.getLocalFlakesDirs(), false /* isDevcontainer */))
409410
}
410411

411412
func PrintEnvrcContent(w io.Writer) error {
412-
return filegen.EnvrcContent(w)
413+
return generate.EnvrcContent(w)
413414
}
414415

415416
// GenerateEnvrcFile generates a .envrc file that makes direnv integration convenient
@@ -438,7 +439,7 @@ func (d *Devbox) GenerateEnvrcFile(ctx context.Context, force bool) error {
438439
}
439440

440441
// .envrc file creation
441-
err := filegen.CreateEnvrc(ctx, d.projectDir)
442+
err := generate.CreateEnvrc(ctx, d.projectDir)
442443
if err != nil {
443444
return errors.WithStack(err)
444445
}

internal/filegen/devcontainer_util.go renamed to internal/impl/generate/devcontainer_util.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved.
22
// Use of this source code is governed by the license in the LICENSE file.
33

4-
package filegen
4+
package generate
5+
6+
// package generate has functionality to implement the `devbox generate` command
57

68
import (
79
"context"
10+
"embed"
811
"encoding/json"
912
"html/template"
1013
"io"
@@ -17,6 +20,9 @@ import (
1720
"go.jetpack.io/devbox/internal/debug"
1821
)
1922

23+
//go:embed tmpl/*
24+
var tmplFS embed.FS
25+
2026
type devcontainerObject struct {
2127
Name string `json:"name"`
2228
Build *build `json:"build"`
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

internal/impl/packages.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
"github.com/pkg/errors"
1717
"github.com/samber/lo"
18-
"go.jetpack.io/devbox/internal/filegen"
18+
"go.jetpack.io/devbox/internal/shellgen"
1919
"golang.org/x/exp/slices"
2020

2121
"go.jetpack.io/devbox/internal/boxcli/usererr"
@@ -177,7 +177,7 @@ func (d *Devbox) ensurePackagesAreInstalled(ctx context.Context, mode installMod
177177
return nil
178178
}
179179

180-
if err := filegen.GenerateForPrintEnv(ctx, d); err != nil {
180+
if err := shellgen.GenerateForPrintEnv(ctx, d); err != nil {
181181
return err
182182
}
183183
if mode == ensure {

internal/impl/shell_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"testing"
1414

1515
"github.com/google/go-cmp/cmp"
16-
"go.jetpack.io/devbox/internal/filegen"
16+
"go.jetpack.io/devbox/internal/shellgen"
1717
)
1818

1919
// update overwrites golden files with the new test results.
@@ -48,7 +48,7 @@ func testWriteDevboxShellrc(t *testing.T, testdirs []string) {
4848
test.env = pairsToMap(strings.Split(string(b), "\n"))
4949
}
5050

51-
test.hooksFilePath = filegen.ScriptPath(projectDir, filegen.HooksFilename)
51+
test.hooksFilePath = shellgen.ScriptPath(projectDir, shellgen.HooksFilename)
5252

5353
test.shellrcPath = filepath.Join(path, "shellrc")
5454
if _, err := os.Stat(test.shellrcPath); errors.Is(err, fs.ErrNotExist) {

internal/shellgen/doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package shellgen
2+
3+
// shellgen package is responsible for printing files in the .devbox/gen directory that
4+
// are needed to create the "devbox shell environment".
5+
//
6+
// This is flake.nix, and the init-hooks and scripts from the devbox.json.

internal/filegen/flake_input.go renamed to internal/shellgen/flake_input.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package filegen
1+
package shellgen
22

33
import (
44
"context"

0 commit comments

Comments
 (0)