Skip to content

Commit 5538efa

Browse files
authored
[devbox] Put generated files in hidden directory (#2495)
## Summary Write generated files into the `.devbox` hidden directory. The directory comes with a .gitignore so that users don't accidentally check in the generated files. I've updated `shell` and `build` to use the files in that directory. ## How was it tested? Compiled, ran on test project. ## Is this change backwards-compatible? Yes.
1 parent 37226cb commit 5538efa

File tree

11 files changed

+59
-43
lines changed

11 files changed

+59
-43
lines changed

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ go_library(
1111
"tmpl/default.nix.tmpl",
1212
"tmpl/shell.nix.tmpl",
1313
"tmpl/Dockerfile.tmpl",
14+
"tmpl/.gitignore.tmpl",
1415
],
1516
importpath = "go.jetpack.io/axiom/opensource/devbox",
1617
visibility = ["//visibility:public"],

boxcli/build.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ func BuildCmd() *cobra.Command {
1111
flags := &docker.BuildFlags{}
1212

1313
command := &cobra.Command{
14-
Use: "build [<dir>]",
15-
Args: cobra.MaximumNArgs(1),
16-
RunE: buildCmdFunc(flags),
14+
Use: "build [<dir>]",
15+
Args: cobra.MaximumNArgs(1),
16+
Hidden: true, // Hide until ready for release.
17+
RunE: buildCmdFunc(flags),
1718
}
1819

1920
command.Flags().BoolVar(

boxcli/generate.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import (
66
"go.jetpack.io/axiom/opensource/devbox"
77
)
88

9-
// TODO: this command is useful for debugging.
10-
// Decide whether we want to keep it for real – or it should be removed.
119
func GenerateCmd() *cobra.Command {
1210
command := &cobra.Command{
13-
Use: "generate [<dir>]",
14-
Args: cobra.MaximumNArgs(1),
15-
RunE: runGenerateCmd,
11+
Use: "generate [<dir>]",
12+
Args: cobra.MaximumNArgs(1),
13+
Hidden: true, // For debugging only
14+
RunE: runGenerateCmd,
1615
}
1716
return command
1817
}

boxcli/plan.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"go.jetpack.io/axiom/opensource/devbox"
99
)
1010

11-
// TODO: this command is useful for debugging.
12-
// Decide whether we want to keep it for real – or it should be removed.
1311
func PlanCmd() *cobra.Command {
1412
command := &cobra.Command{
1513
Use: "plan [<dir>]",

devbox.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func (d *Devbox) Remove(pkgs ...string) error {
6464

6565
func (d *Devbox) Build(opts ...docker.BuildOptions) error {
6666
defaultFlags := &docker.BuildFlags{
67-
Name: "devbox",
67+
Name: "devbox",
68+
DockerfilePath: filepath.Join(d.srcDir, ".devbox/gen", "Dockerfile"),
6869
}
6970
opts = append([]docker.BuildOptions{docker.WithFlags(defaultFlags)}, opts...)
7071

@@ -82,7 +83,6 @@ func (d *Devbox) Plan() *planner.BuildPlan {
8283
return planner.MergePlans(basePlan, planner.Plan(d.srcDir))
8384
}
8485

85-
// TODO: generate necessary files without modifying src directory.
8686
func (d *Devbox) Generate() error {
8787
plan := d.Plan()
8888
return generate(d.srcDir, plan)
@@ -93,7 +93,8 @@ func (d *Devbox) Shell() error {
9393
if err != nil {
9494
return errors.WithStack(err)
9595
}
96-
return nix.Shell(d.srcDir)
96+
nixDir := filepath.Join(d.srcDir, ".devbox/gen")
97+
return nix.Shell(nixDir)
9798
}
9899

99100
func (d *Devbox) saveCfg() error {

docker/args.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func ToArgs(args []string, flags *BuildFlags) []string {
1919
args = append(args, "-t", fmt.Sprintf("%s:%s", flags.Name, tag))
2020
}
2121
}
22-
22+
if flags.DockerfilePath != "" {
23+
args = append(args, "-f", flags.DockerfilePath)
24+
}
2325
if len(flags.Platforms) > 0 {
2426
args = append(args, fmt.Sprintf("--platform=%s", strings.Join(flags.Platforms, ",")))
2527
}

docker/docker.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import (
1919
// management.
2020

2121
type BuildFlags struct {
22-
Name string
23-
Tags []string
24-
Platforms []string
25-
NoCache bool
22+
Name string
23+
Tags []string
24+
Platforms []string
25+
NoCache bool
26+
DockerfilePath string
2627
}
2728

2829
type BuildOptions func(*BuildFlags)

generate.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,38 @@ import (
1212
"go.jetpack.io/axiom/opensource/devbox/planner"
1313
)
1414

15-
//go:embed tmpl
15+
//go:embed tmpl/* tmpl/.*
1616
var tmplFS embed.FS
1717

18-
func generate(path string, plan *planner.BuildPlan) error {
19-
err := writeFromTemplate(path, plan, "Dockerfile")
20-
if err != nil {
21-
return errors.WithStack(err)
22-
}
18+
func generate(rootPath string, plan *planner.BuildPlan) error {
19+
// TODO: we should also generate a .dockerignore file
20+
files := []string{".gitignore", "Dockerfile", "shell.nix", "default.nix"}
2321

24-
err = writeFromTemplate(path, plan, "shell.nix")
25-
if err != nil {
26-
return errors.WithStack(err)
27-
}
22+
outPath := filepath.Join(rootPath, ".devbox/gen")
2823

29-
err = writeFromTemplate(path, plan, "default.nix")
30-
if err != nil {
31-
return errors.WithStack(err)
24+
for _, file := range files {
25+
err := writeFromTemplate(outPath, plan, file)
26+
if err != nil {
27+
return errors.WithStack(err)
28+
}
3229
}
30+
3331
return nil
3432
}
3533

3634
func writeFromTemplate(path string, plan *planner.BuildPlan, tmplName string) error {
37-
tmplPath := fmt.Sprintf("tmpl/%s.tmpl", tmplName)
38-
t := template.Must(template.New(tmplName+".tmpl").Funcs(templateFuncs).ParseFS(tmplFS, tmplPath))
35+
embeddedPath := fmt.Sprintf("tmpl/%s.tmpl", tmplName)
36+
t := template.Must(template.New(tmplName+".tmpl").Funcs(templateFuncs).ParseFS(tmplFS, embeddedPath))
37+
38+
// Should we clear the directory so we start "fresh"?
39+
outPath := filepath.Join(path, tmplName)
40+
outDir := filepath.Dir(outPath)
41+
err := os.MkdirAll(outDir, 0755) // Ensure directory exists.
42+
if err != nil {
43+
return errors.WithStack(err)
44+
}
3945

40-
f, err := os.Create(filepath.Join(path, tmplName))
46+
f, err := os.Create(outPath)
4147
defer func() {
4248
_ = f.Close()
4349
}()

tmpl/.gitignore.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
.*

tmpl/Dockerfile.tmpl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# syntax=docker/dockerfile:1
22

3-
FROM nixos/nix as builder
3+
FROM nixos/nix:2.10.3 as base
4+
5+
FROM base as builder
46

57
# 1. SETUP PHASE
68
# Setup the container, and install nix packages.
@@ -11,25 +13,25 @@ SHELL [ "/bin/sh", "-eux", "-o", "pipefail", "-c"]
1113
WORKDIR /scratch
1214

1315
# Setup nix in its own layer so it can be cached.
14-
COPY --link ./default.nix ./
16+
COPY --link ./.devbox/gen/default.nix ./.devbox/gen/
1517
# Setup /nix/store as a cache directory.
1618
# Declaring it as a cache directory makes it empty directory, so we need to
1719
# specify, using from= and source=, that we want to re-copy the contents of
1820
# the directory from nixos/nix.
19-
RUN --mount=type=cache,target=/nix/store,from=nixos/nix,source=/nix/store \
20-
nix-env -if default.nix
21+
RUN --mount=type=cache,target=/nix/store,from=base,source=/nix/store \
22+
nix-env -if ./.devbox/gen/default.nix
2123

2224
# 2. INSTALL PHASE
2325
# Install libraries needed by the source code.
2426
# -----------------------------------------------
2527

2628
COPY --link . ./
27-
RUN --mount=type=cache,target=/nix/store,from=nixos/nix,source=/nix/store {{.InstallCommand}}
29+
RUN --mount=type=cache,target=/nix/store,from=base,source=/nix/store {{.InstallCommand}}
2830

2931
# 3. BUILD PHASE
3032
# Compile the source code into an executable.
3133
# ----------------------------------------------
32-
RUN --mount=type=cache,target=/nix/store,from=nixos/nix,source=/nix/store {{.BuildCommand}}
34+
RUN --mount=type=cache,target=/nix/store,from=base,source=/nix/store {{.BuildCommand}}
3335

3436
# 4. PACKAGING PHASE
3537
# Create a minimal image that contains the executable.

0 commit comments

Comments
 (0)