Skip to content

Commit

Permalink
Add mrun option to run multiple targets at once
Browse files Browse the repository at this point in the history
iBazel currently only supports "run" for single target. Running multiple
targets requires separate Bazel command executions and busy waiting for
build processes.

Implement "mrun" command which takes in arbitrary number of targets, and
run all of them at once. We can pass in corresponding arguments with
prefix "--arg=".
  • Loading branch information
borkaehw committed Jul 31, 2018
1 parent 0166b88 commit 634c628
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 34 deletions.
2 changes: 1 addition & 1 deletion bazel/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (b *bazel) Test(args ...string) (*bytes.Buffer, error) {
func (b *bazel) Run(args ...string) (*exec.Cmd, *bytes.Buffer, error) {
b.WriteToStderr(true)
b.WriteToStdout(true)
stdoutBuffer, stderrBuffer := b.newCommand("run", args...)
stdoutBuffer, stderrBuffer := b.newCommand("run", append(b.args, args...)...)
b.cmd.Stdin = os.Stdin

_, _ = stdoutBuffer.Write(stderrBuffer.Bytes())
Expand Down
15 changes: 10 additions & 5 deletions ibazel/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ var bazelNew = bazel.New
// Command is an object that wraps the logic of running a task in Bazel and
// manipulating it.
type Command interface {
Start() (*bytes.Buffer, error)
Start(logFile *os.File) (*bytes.Buffer, error)
Terminate()
NotifyOfChanges() *bytes.Buffer
NotifyOfChanges(logFile *os.File) *bytes.Buffer
IsSubprocessRunning() bool
}

// start will be called by most implementations since this logic is extremely
// common.
func start(b bazel.Bazel, target string, args []string) (*bytes.Buffer, *exec.Cmd) {
func start(b bazel.Bazel, target string, args []string, logFile *os.File) (*bytes.Buffer, *exec.Cmd) {
tmpfile, err := ioutil.TempFile("", "bazel_script_path")
if err != nil {
fmt.Print(err)
Expand All @@ -57,8 +57,13 @@ func start(b bazel.Bazel, target string, args []string) (*bytes.Buffer, *exec.Cm
// Now that we have built the target, construct a executable form of it for
// execution in a go routine.
cmd := execCommand(runScriptPath, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if logFile != nil {
cmd.Stdout = logFile
cmd.Stderr = logFile
} else {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}

// Set a process group id (PGID) on the subprocess. This is
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
Expand Down
10 changes: 5 additions & 5 deletions ibazel/command/default_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ func (c *defaultCommand) Terminate() {
c.cmd = nil
}

func (c *defaultCommand) Start() (*bytes.Buffer, error) {
func (c *defaultCommand) Start(logFile *os.File) (*bytes.Buffer, error) {
b := bazelNew()
b.SetArguments(c.bazelArgs)

b.WriteToStderr(true)
b.WriteToStdout(true)

outputBuffer, foo := start(b, c.target, c.args)
outputBuffer, foo := start(b, c.target, c.args, logFile)
c.cmd = foo

c.cmd.Env = os.Environ()
Expand All @@ -76,10 +76,10 @@ func (c *defaultCommand) Start() (*bytes.Buffer, error) {
return outputBuffer, nil
}

func (c *defaultCommand) NotifyOfChanges() *bytes.Buffer {
func (c *defaultCommand) NotifyOfChanges(logFile *os.File) *bytes.Buffer {
c.Terminate()
c.Start()
return nil
outputBuffer, _ := c.Start(logFile)
return outputBuffer
}

func (c *defaultCommand) IsSubprocessRunning() bool {
Expand Down
6 changes: 3 additions & 3 deletions ibazel/command/notify_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ func (c *notifyCommand) Terminate() {
c.cmd = nil
}

func (c *notifyCommand) Start() (*bytes.Buffer, error) {
func (c *notifyCommand) Start(logFile *os.File) (*bytes.Buffer, error) {
b := bazelNew()
b.SetArguments(c.bazelArgs)

b.WriteToStderr(true)
b.WriteToStdout(true)

outputBuffer, foo := start(b, c.target, c.args)
outputBuffer, foo := start(b, c.target, c.args, logFile)
c.cmd = foo
// Keep the writer around.
var err error
Expand All @@ -84,7 +84,7 @@ func (c *notifyCommand) Start() (*bytes.Buffer, error) {
return outputBuffer, nil
}

func (c *notifyCommand) NotifyOfChanges() *bytes.Buffer {
func (c *notifyCommand) NotifyOfChanges(logFile *os.File) *bytes.Buffer {
b := bazelNew()
b.SetArguments(c.bazelArgs)

Expand Down
Loading

0 comments on commit 634c628

Please sign in to comment.