Skip to content

Commit

Permalink
deprecate devbox build + hide devbox plan (jetify-com#336)
Browse files Browse the repository at this point in the history
## Summary
Title says it.

## How was it tested?
- compile
- try ./devbox build
- try ./devbox plan
  • Loading branch information
mohsenari authored Dec 2, 2022
1 parent a1af823 commit 2ac95d2
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 175 deletions.
30 changes: 6 additions & 24 deletions boxcli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
package boxcli

import (
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/devbox"
"go.jetpack.io/devbox/docker"
)

Expand All @@ -21,12 +17,13 @@ func BuildCmd() *cobra.Command {
flags := buildCmdFlags{}

command := &cobra.Command{
Use: "build",
Short: "Build an OCI image that can run as a container",
Long: "Builds your current source directory and devbox configuration as a Docker container. Devbox will create a plan for your container based on your source code, and then apply the packages and stage overrides in your devbox.json. \n To learn more about how to configure your builds, see the [configuration reference](/docs/configuration_reference)",
Args: cobra.MaximumNArgs(1),
Use: "build",
Deprecated: "Please follow devbox documentation on how to build a container image around your devbox project.",
Short: "(deprecated) Build an OCI image that can run as a container",
Long: "(deprecated) Builds your current source directory and devbox configuration as a Docker container. Devbox will create a plan for your container based on your source code, and then apply the packages and stage overrides in your devbox.json. \n To learn more about how to configure your builds, see the [configuration reference](/docs/configuration_reference)",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return buildCmdFunc(cmd, args, flags)
return nil
},
}

Expand All @@ -42,18 +39,3 @@ func BuildCmd() *cobra.Command {

return command
}

func buildCmdFunc(_ *cobra.Command, args []string, flags buildCmdFlags) error {
path, err := configPathFromUser(args, &flags.config)
if err != nil {
return err
}

// Check the directory exists.
box, err := devbox.Open(path, os.Stdout)
if err != nil {
return errors.WithStack(err)
}

return box.Build(&flags.docker)
}
18 changes: 5 additions & 13 deletions boxcli/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ func PlanCmd() *cobra.Command {
flags := planCmdFlags{}

command := &cobra.Command{
Use: "plan",
Short: "Preview the plan used to build your environment",
Args: cobra.MaximumNArgs(1),
Use: "plan",
Hidden: true,
Short: "Preview the plan used to build your environment",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runPlanCmd(cmd, args, flags)
},
Expand Down Expand Up @@ -52,14 +53,5 @@ func runPlanCmd(_ *cobra.Command, args []string, flags planCmdFlags) error {
return err
}

err = enc.Encode(shellPlan)
if err != nil {
return errors.WithStack(err)
}

buildPlan, err := box.BuildPlan()
if err != nil {
return errors.WithStack(err)
}
return errors.WithStack(enc.Encode(buildPlan))
return errors.WithStack(enc.Encode(shellPlan))
}
31 changes: 0 additions & 31 deletions devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"go.jetpack.io/devbox/boxcli/featureflag"
"go.jetpack.io/devbox/cuecfg"
"go.jetpack.io/devbox/debug"
"go.jetpack.io/devbox/docker"
"go.jetpack.io/devbox/nix"
"go.jetpack.io/devbox/pkgcfg"
"go.jetpack.io/devbox/planner"
Expand Down Expand Up @@ -163,21 +162,6 @@ func (d *Devbox) Remove(pkgs ...string) error {
return d.printPackageUpdateMessage(uninstall, uninstalledPackages)
}

// Build creates a Docker image containing a shell with the devbox environment.
func (d *Devbox) Build(flags *docker.BuildFlags) error {
defaultFlags := &docker.BuildFlags{
Name: flags.Name,
DockerfilePath: filepath.Join(d.configDir, ".devbox/gen", "Dockerfile"),
}
opts := append([]docker.BuildOptions{docker.WithFlags(defaultFlags)}, docker.WithFlags(flags))

err := d.generateBuildFiles()
if err != nil {
return errors.WithStack(err)
}
return docker.Build(d.configDir, opts...)
}

// ShellPlan creates a plan of the actions that devbox will take to generate its
// shell environment.
func (d *Devbox) ShellPlan() (*plansdk.ShellPlan, error) {
Expand Down Expand Up @@ -221,9 +205,6 @@ func (d *Devbox) Generate() error {
if err := d.generateShellFiles(); err != nil {
return errors.WithStack(err)
}
if err := d.generateBuildFiles(); err != nil {
return errors.WithStack(err)
}
return nil
}

Expand Down Expand Up @@ -406,18 +387,6 @@ func (d *Devbox) generateShellFiles() error {
return generateForShell(d.configDir, plan)
}

func (d *Devbox) generateBuildFiles() error {
// BuildPlan() will return error if plan is invalid.
buildPlan, err := d.BuildPlan()
if err != nil {
return errors.WithStack(err)
}
if buildPlan.Warning() != nil {
fmt.Printf("[WARNING]: %s\n", buildPlan.Warning().Error())
}
return generateForBuild(d.configDir, buildPlan)
}

func (d *Devbox) profileDir() (string, error) {
absPath := filepath.Join(d.configDir, nix.ProfilePath)
if err := os.MkdirAll(filepath.Dir(absPath), 0755); err != nil {
Expand Down
93 changes: 0 additions & 93 deletions devbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func TestDevbox(t *testing.T) {

for _, testPath := range testPaths {
testShell(t, testPath)
testBuild(t, testPath)
}
}

Expand Down Expand Up @@ -72,106 +71,14 @@ func testShell(t *testing.T, testPath string) {
})
}

func testBuild(t *testing.T, testPath string) {

currentDir, err := os.Getwd()
require.New(t).NoError(err)

baseDir := filepath.Dir(testPath)
testName := fmt.Sprintf("%s_build_plan", baseDir)
t.Run(testName, func(t *testing.T) {
assert := assert.New(t)
buildPlanFile := filepath.Join(baseDir, "build_plan.json")
hasBuildPlanFile := fileExists(buildPlanFile)

box, err := Open(baseDir, os.Stdout)
assert.NoErrorf(err, "%s should be a valid devbox project", baseDir)

// Just for tests, we make configDir be a relative path so that the paths in plan.json
// of various test cases have relative paths. Absolute paths are a no-go because they'd
// be of the form `/Users/savil/...`, which are not generalized and cannot be checked in.
box.configDir, err = filepath.Rel(currentDir, box.configDir)
assert.NoErrorf(err, "expect to construct relative path from %s relative to base %s", box.configDir, currentDir)

buildPlan, err := box.BuildPlan()
buildErrorExpectedFile := filepath.Join(baseDir, "build_error_expected")
hasBuildErrorExpectedFile := fileExists(buildErrorExpectedFile)
if hasBuildErrorExpectedFile {
assert.NotNil(err)
// Since build error is expected, skip the rest of the test
return
}
assert.NoError(err, "devbox plan should not fail")

err = box.generateBuildFiles()
assert.NoError(err, "devbox generate should not fail")

if !hasBuildPlanFile {
assert.NotEmpty(buildPlan.DevPackages, "the plan should have dev packages")
return
}

data, err := os.ReadFile(buildPlanFile)
assert.NoError(err, "plan.json should be readable")

expected := &plansdk.BuildPlan{}
err = json.Unmarshal(data, &expected)
assert.NoError(err, "plan.json should parse correctly")
assertBuildPlansMatch(t, expected, buildPlan)
})
}

func assertShellPlansMatch(t *testing.T, expected *plansdk.ShellPlan, actual *plansdk.ShellPlan) {
assert := assert.New(t)

assert.ElementsMatch(expected.DevPackages, actual.DevPackages, "DevPackages should match")
assert.ElementsMatch(expected.NixOverlays, actual.NixOverlays, "NixOverlays should match")
}

func assertBuildPlansMatch(t *testing.T, expected *plansdk.BuildPlan, actual *plansdk.BuildPlan) {
assert := assert.New(t)

assert.ElementsMatch(expected.DevPackages, actual.DevPackages, "DevPackages should match")
assert.ElementsMatch(expected.RuntimePackages, actual.RuntimePackages, "RuntimePackages should match")
assert.Equal(expected.InstallStage.GetCommand(), actual.InstallStage.GetCommand(), "Install stage should match")
assert.Equal(expected.BuildStage.GetCommand(), actual.BuildStage.GetCommand(), "Build stage should match")
assert.Equal(expected.StartStage.GetCommand(), actual.StartStage.GetCommand(), "Start stage should match")
// Check that input files are the same for all stages.
// Depending on where the test command is invoked, the input file paths can be different.
// We will compare the file name only.
assert.ElementsMatch(
expected.InstallStage.GetInputFiles(),
getFileNames(actual.InstallStage.GetInputFiles()),
"InstallStage.InputFiles should match",
)
assert.ElementsMatch(
expected.BuildStage.GetInputFiles(),
getFileNames(actual.BuildStage.GetInputFiles()),
"BuildStage.InputFiles should match",
)
assert.ElementsMatch(
expected.StartStage.GetInputFiles(),
actual.StartStage.GetInputFiles(),
"StartStage.InputFiles should match",
)

assert.ElementsMatch(expected.Definitions, actual.Definitions, "Definitions should match")
}

func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}

func getFileNames(paths []string) []string {
names := []string{}
for _, path := range paths {
if path == "." {
names = append(names, path)
} else {
names = append(names, filepath.Base(path))
}
}

return names
}
14 changes: 0 additions & 14 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
var tmplFS embed.FS

var shellFiles = []string{"development.nix", "shell.nix"}
var buildFiles = []string{"development.nix", "runtime.nix", "Dockerfile", "Dockerfile.dockerignore"}

func generateForShell(rootPath string, plan *plansdk.ShellPlan) error {
outPath := filepath.Join(rootPath, ".devbox/gen")
Expand Down Expand Up @@ -61,19 +60,6 @@ func generateForShell(rootPath string, plan *plansdk.ShellPlan) error {
return nil
}

func generateForBuild(rootPath string, plan *plansdk.BuildPlan) error {
outPath := filepath.Join(rootPath, ".devbox/gen")

for _, file := range buildFiles {
err := writeFromTemplate(outPath, plan, file)
if err != nil {
return errors.WithStack(err)
}
}

return nil
}

func writeFromTemplate(path string, plan interface{}, tmplName string) error {
embeddedPath := fmt.Sprintf("tmpl/%s.tmpl", tmplName)

Expand Down

0 comments on commit 2ac95d2

Please sign in to comment.