Skip to content

Commit 69fe132

Browse files
authored
[cleanup] introduce filegen package, and move functions that generate files into it (#1189)
## Summary This PR introduces a `filegen` package. It will contain functions related to files that devbox generates in `.devbox/gen`: - to enable `devbox.PrintEnv`, via `filegen.GenerateForPrintEnv`. - to enable `devbox.RunScript` via `filegen.WriteScriptsToFile`. My motivation was that I wanted to pull logic out of impl and plansdk into a single package (and internalize parts of it so it is easier to reason about or follow). Currently, we have logic for generating code in .devbox/gen that is spread in different .go files in both impl and plansdk. No logic change. This is mostly consolidating code that has spread over `impl/devbox.go` into a smaller package. ## How was it tested? `devbox shell` worked. - [x] testscripts.
1 parent ceb5ae4 commit 69fe132

18 files changed

+162
-127
lines changed

internal/boxcli/generate/devcontainer_util.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
package generate
55

6+
// TODO move this to package filegen at impl/filegen
7+
// no need to be in `boxcli`.
8+
69
import (
710
"context"
811
"embed"

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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 impl
4+
package filegen
55

66
import (
77
"bytes"
@@ -17,26 +17,31 @@ import (
1717
"text/template"
1818

1919
"github.com/pkg/errors"
20-
2120
"go.jetpack.io/devbox/internal/cuecfg"
2221
"go.jetpack.io/devbox/internal/debug"
2322
"go.jetpack.io/devbox/internal/planner/plansdk"
2423
)
2524

25+
// TmplFS docblock to satisfy silly linter.
26+
// TODO savil: move package generate in boxcli into this filegen package, and then make `TmplFS` internal
27+
//
2628
//go:embed tmpl/*
27-
var tmplFS embed.FS
29+
var TmplFS embed.FS
2830

2931
var shellFiles = []string{"shell.nix"}
3032

31-
func (d *Devbox) generateShellFiles(ctx context.Context) error {
33+
// GenerateForPrintEnv will create all the files necessary for processing
34+
// devbox.PrintEnv, which is the core function from which devbox shell/run/direnv
35+
// functionality is derived.
36+
func GenerateForPrintEnv(ctx context.Context, devbox devboxer) error {
3237
defer trace.StartRegion(ctx, "generateShellFiles").End()
3338

34-
plan, err := d.ShellPlan(ctx)
39+
plan, err := devbox.FlakePlan(ctx)
3540
if err != nil {
3641
return err
3742
}
3843

39-
outPath := filepath.Join(d.projectDir, ".devbox/gen")
44+
outPath := filepath.Join(devbox.ProjectDir(), ".devbox/gen")
4045

4146
for _, file := range shellFiles {
4247
err := writeFromTemplate(outPath, plan, file)
@@ -46,7 +51,7 @@ func (d *Devbox) generateShellFiles(ctx context.Context) error {
4651
}
4752

4853
// Gitignore file is added to the .devbox directory
49-
err = writeFromTemplate(filepath.Join(d.projectDir, ".devbox"), plan, ".gitignore")
54+
err = writeFromTemplate(filepath.Join(devbox.ProjectDir(), ".devbox"), plan, ".gitignore")
5055
if err != nil {
5156
return errors.WithStack(err)
5257
}
@@ -56,7 +61,7 @@ func (d *Devbox) generateShellFiles(ctx context.Context) error {
5661
return errors.WithStack(err)
5762
}
5863

59-
return d.writeScriptsToFiles()
64+
return WriteScriptsToFiles(devbox)
6065
}
6166

6267
// Cache and buffers for generating templated files.
@@ -76,7 +81,7 @@ func writeFromTemplate(path string, plan any, tmplName string) error {
7681
tmpl.Funcs(templateFuncs)
7782

7883
var err error
79-
tmpl, err = tmpl.ParseFS(tmplFS, "tmpl/"+tmplKey)
84+
tmpl, err = tmpl.ParseFS(TmplFS, "tmpl/"+tmplKey)
8085
if err != nil {
8186
return errors.WithStack(err)
8287
}

internal/impl/generate_test.go renamed to internal/filegen/generate_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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 impl
4+
package filegen
55

66
import (
7+
"flag"
78
"os"
89
"path/filepath"
910
"strings"
@@ -13,6 +14,9 @@ import (
1314
"go.jetpack.io/devbox/internal/planner/plansdk"
1415
)
1516

17+
// update overwrites golden files with the new test results.
18+
var update = flag.Bool("update", false, "update the golden files with the test results")
19+
1620
func TestWriteFromTemplate(t *testing.T) {
1721
dir := filepath.Join(t.TempDir(), "makeme")
1822
outPath := filepath.Join(dir, "flake.nix")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)