Skip to content

Commit

Permalink
fix: implementation, tests and usage
Browse files Browse the repository at this point in the history
Signed-off-by: Thulio Ferraz Assis <thulio@aspect.dev>
  • Loading branch information
Thulio Ferraz Assis committed Dec 11, 2021
1 parent 15eb185 commit 6da6743
Show file tree
Hide file tree
Showing 29 changed files with 511 additions and 426 deletions.
1 change: 1 addition & 0 deletions cmd/aspect/build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
"//pkg/aspecterrors",
"//pkg/bazel",
"//pkg/ioutils",
"//pkg/pathutils",
"//pkg/plugin/system",
"//pkg/plugin/system/bep",
"@com_github_spf13_cobra//:cobra",
Expand Down
8 changes: 5 additions & 3 deletions cmd/aspect/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"aspect.build/cli/pkg/aspecterrors"
"aspect.build/cli/pkg/bazel"
"aspect.build/cli/pkg/ioutils"
"aspect.build/cli/pkg/pathutils"
"aspect.build/cli/pkg/plugin/system"
"aspect.build/cli/pkg/plugin/system/bep"
)
Expand All @@ -35,14 +36,14 @@ func NewDefaultBuildCmd(pluginSystem system.PluginSystem) *cobra.Command {
func NewBuildCmd(
streams ioutils.Streams,
pluginSystem system.PluginSystem,
bzl bazel.Spawner,
bzl bazel.Bazel,
) *cobra.Command {
cmd := &cobra.Command{
Use: "build",
Short: "Builds the specified targets, using the options.",
Long: "Invokes bazel build on the specified targets. " +
"See 'bazel help target-syntax' for details and examples on how to specify targets to build.",
RunE: func(cmd *cobra.Command, args []string) (exitErr error) {
RunE: pathutils.InvokeCmdInsideWorkspace(func(workspaceRoot string, cmd *cobra.Command, args []string) (exitErr error) {
isInteractiveMode, err := cmd.Root().PersistentFlags().GetBool(rootFlags.InteractiveFlagName)
if err != nil {
return err
Expand All @@ -62,11 +63,12 @@ func NewBuildCmd(
}
}()

bzl.SetWorkspaceRoot(workspaceRoot)
b := build.New(streams, bzl)
return pluginSystem.WithBESBackend(cmd.Context(), func(besBackend bep.BESBackend) error {
return b.Run(args, besBackend)
})
},
}),
}

return cmd
Expand Down
3 changes: 3 additions & 0 deletions cmd/aspect/clean/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/aspect/clean",
"//pkg/bazel",
"//pkg/ioutils",
"//pkg/pathutils",
"@com_github_mattn_go_isatty//:go-isatty",
"@com_github_spf13_cobra//:cobra",
],
Expand Down
27 changes: 21 additions & 6 deletions cmd/aspect/clean/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ import (
"github.com/spf13/cobra"

"aspect.build/cli/pkg/aspect/clean"
"aspect.build/cli/pkg/bazel"
"aspect.build/cli/pkg/ioutils"
"aspect.build/cli/pkg/pathutils"
)

// NewDefaultCleanCmd creates a new clean cobra command.
// NewDefaultCleanCmd creates a new default clean cobra command.
func NewDefaultCleanCmd() *cobra.Command {
isInteractive := isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
b := clean.NewDefault(isInteractive)
return NewCleanCmd(ioutils.DefaultStreams, bazel.New())
}

// NewCleanCmd creates a new clean cobra command.
func NewCleanCmd(streams ioutils.Streams, bzl bazel.Bazel) *cobra.Command {
var expunge bool
var expungeAsync bool

cmd := &cobra.Command{
Use: "clean",
Expand Down Expand Up @@ -53,16 +61,23 @@ Workaround inconistent state:
Such problems are fixable and these bugs are a high priority.
If you ever find an incorrect incremental build, please file a bug report,
and only use clean as a temporary workaround.`,
RunE: b.Run,
RunE: pathutils.InvokeCmdInsideWorkspace(func(workspaceRoot string, cmd *cobra.Command, args []string) (exitErr error) {
bzl.SetWorkspaceRoot(workspaceRoot)
isInteractive := isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())
c := clean.NewDefault(bzl, isInteractive)
c.Expunge = expunge
c.ExpungeAsync = expungeAsync
return c.Run(cmd, args)
}),
}

cmd.PersistentFlags().BoolVarP(&b.Expunge, "expunge", "", false, `Remove the entire output_base tree.
cmd.PersistentFlags().BoolVarP(&expunge, "expunge", "", false, `Remove the entire output_base tree.
This removes all build output, external repositories,
and temp files created by Bazel.
It also stops the Bazel server after the clean,
equivalent to the shutdown command.`)

cmd.PersistentFlags().BoolVarP(&b.ExpungeAsync, "expunge_async", "", false, `Expunge in the background.
cmd.PersistentFlags().BoolVarP(&expungeAsync, "expunge_async", "", false, `Expunge in the background.
It is safe to invoke a Bazel command in the same
workspace while the asynchronous expunge continues to run.
Note, however, that this may introduce IO contention.`)
Expand Down
1 change: 1 addition & 0 deletions cmd/aspect/info/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
deps = [
"//pkg/aspect/info",
"//pkg/ioutils",
"//pkg/pathutils",
"@com_github_spf13_cobra//:cobra",
],
)
3 changes: 2 additions & 1 deletion cmd/aspect/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"aspect.build/cli/pkg/aspect/info"
"aspect.build/cli/pkg/ioutils"
"aspect.build/cli/pkg/pathutils"
)

func NewDefaultInfoCmd() *cobra.Command {
Expand Down Expand Up @@ -42,7 +43,7 @@ the bazel User Manual, and can be programmatically obtained with
See also 'bazel version' for more detailed bazel version
information.`,
Args: cobra.MaximumNArgs(1),
RunE: v.Run,
RunE: pathutils.InvokeCmdInsideWorkspace(v.Run),
}

cmd.PersistentFlags().BoolVarP(&v.ShowMakeEnv, "show_make_env", "", false, `include the set of key/value pairs in the "Make" environment,
Expand Down
1 change: 1 addition & 0 deletions cmd/aspect/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_library(
"//pkg/aspect/test",
"//pkg/bazel",
"//pkg/ioutils",
"//pkg/pathutils",
"@com_github_spf13_cobra//:cobra",
],
)
11 changes: 7 additions & 4 deletions cmd/aspect/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ import (
"aspect.build/cli/pkg/aspect/test"
"aspect.build/cli/pkg/bazel"
"aspect.build/cli/pkg/ioutils"
"aspect.build/cli/pkg/pathutils"
)

func NewDefaultTestCmd() *cobra.Command {
return NewTestCmd(ioutils.DefaultStreams, bazel.New())
}

func NewTestCmd(streams ioutils.Streams, bzl bazel.Spawner) *cobra.Command {
v := test.New(streams, bzl)

func NewTestCmd(streams ioutils.Streams, bzl bazel.Bazel) *cobra.Command {
cmd := &cobra.Command{
Use: "test",
Short: "Builds the specified targets and runs all test targets among them.",
Expand All @@ -35,7 +34,11 @@ don't forget to pass all your 'build' options to 'test' too.
See 'bazel help target-syntax' for details and examples on how to
specify targets.
`,
RunE: v.Run,
RunE: pathutils.InvokeCmdInsideWorkspace(func(workspaceRoot string, cmd *cobra.Command, args []string) (exitErr error) {
bzl.SetWorkspaceRoot(workspaceRoot)
t := test.New(streams, bzl)
return t.Run(cmd, args)
}),
}

return cmd
Expand Down
4 changes: 2 additions & 2 deletions pkg/aspect/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (
// Build represents the aspect build command.
type Build struct {
ioutils.Streams
bzl bazel.Spawner
bzl bazel.Bazel
}

// New creates a Build command.
func New(
streams ioutils.Streams,
bzl bazel.Spawner,
bzl bazel.Bazel,
) *Build {
return &Build{
Streams: streams,
Expand Down
18 changes: 9 additions & 9 deletions pkg/aspect/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func TestBuild(t *testing.T) {
defer ctrl.Finish()

streams := ioutils.Streams{}
spawner := bazel_mock.NewMockSpawner(ctrl)
bzl := bazel_mock.NewMockBazel(ctrl)
expectErr := &aspecterrors.ExitError{
Err: fmt.Errorf("failed to run bazel build"),
ExitCode: 5,
}
spawner.
bzl.
EXPECT().
Spawn([]string{"build", "--bes_backend=grpc://127.0.0.1:12345", "//..."}).
Return(expectErr.ExitCode, expectErr.Err)
Expand All @@ -48,7 +48,7 @@ func TestBuild(t *testing.T) {
Errors().
Times(1)

b := build.New(streams, spawner)
b := build.New(streams, bzl)
err := b.Run([]string{"//..."}, besBackend)

g.Expect(err).To(MatchError(expectErr))
Expand All @@ -61,8 +61,8 @@ func TestBuild(t *testing.T) {

var stderr strings.Builder
streams := ioutils.Streams{Stderr: &stderr}
spawner := bazel_mock.NewMockSpawner(ctrl)
spawner.
bzl := bazel_mock.NewMockBazel(ctrl)
bzl.
EXPECT().
Spawn([]string{"build", "--bes_backend=grpc://127.0.0.1:12345", "//..."}).
Return(0, nil)
Expand All @@ -81,7 +81,7 @@ func TestBuild(t *testing.T) {
}).
Times(1)

b := build.New(streams, spawner)
b := build.New(streams, bzl)
err := b.Run([]string{"//..."}, besBackend)

g.Expect(err).To(MatchError(&aspecterrors.ExitError{ExitCode: 1}))
Expand All @@ -94,8 +94,8 @@ func TestBuild(t *testing.T) {
defer ctrl.Finish()

streams := ioutils.Streams{}
spawner := bazel_mock.NewMockSpawner(ctrl)
spawner.
bzl := bazel_mock.NewMockBazel(ctrl)
bzl.
EXPECT().
Spawn([]string{"build", "--bes_backend=grpc://127.0.0.1:12345", "//..."}).
Return(0, nil)
Expand All @@ -110,7 +110,7 @@ func TestBuild(t *testing.T) {
Errors().
Times(1)

b := build.New(streams, spawner)
b := build.New(streams, bzl)
err := b.Run([]string{"//..."}, besBackend)

g.Expect(err).To(BeNil())
Expand Down
8 changes: 4 additions & 4 deletions pkg/aspect/clean/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type PromptRunner interface {
// Clean represents the aspect clean command.
type Clean struct {
ioutils.Streams
bzl bazel.Spawner
bzl bazel.Bazel
isInteractiveMode bool

Behavior SelectRunner
Expand All @@ -70,7 +70,7 @@ type Clean struct {
// New creates a Clean command.
func New(
streams ioutils.Streams,
bzl bazel.Spawner,
bzl bazel.Bazel,
isInteractiveMode bool) *Clean {
return &Clean{
Streams: streams,
Expand All @@ -79,10 +79,10 @@ func New(
}
}

func NewDefault(isInteractive bool) *Clean {
func NewDefault(bzl bazel.Bazel, isInteractive bool) *Clean {
c := New(
ioutils.DefaultStreams,
bazel.New(),
bzl,
isInteractive)
c.Behavior = &promptui.Select{
Label: "Clean can have a few behaviors. Which do you want?",
Expand Down
Loading

0 comments on commit 6da6743

Please sign in to comment.