github.com/bazelbuild/bazel-watcher@v0.25.2/internal/ibazel/command/default_command.go (about)

     1  // Copyright 2017 The Bazel Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package command
    16  
    17  import (
    18  	"bytes"
    19  	"os"
    20  	"sync"
    21  
    22  	"github.com/bazelbuild/bazel-watcher/internal/ibazel/log"
    23  	"github.com/bazelbuild/bazel-watcher/internal/ibazel/process_group"
    24  )
    25  
    26  type defaultCommand struct {
    27  	target      string
    28  	startupArgs []string
    29  	bazelArgs   []string
    30  	args        []string
    31  	pg          process_group.ProcessGroup
    32  	termSync    sync.Once
    33  }
    34  
    35  // DefaultCommand is the normal mode of interacting with iBazel. If you start a
    36  // server in this mode and notify of changes the server will be killed and
    37  // restarted.
    38  func DefaultCommand(startupArgs []string, bazelArgs []string, target string, args []string) Command {
    39  	return &defaultCommand{
    40  		target:      target,
    41  		startupArgs: startupArgs,
    42  		bazelArgs:   bazelArgs,
    43  		args:        args,
    44  	}
    45  }
    46  
    47  func (c *defaultCommand) Terminate() {
    48  	if !c.IsSubprocessRunning() {
    49  		c.pg = nil
    50  		return
    51  	}
    52  	c.termSync.Do(func() {
    53  		terminate(c.pg)
    54  	})
    55  	c.pg = nil
    56  }
    57  
    58  func (c *defaultCommand) Kill() {
    59  	if c.pg != nil {
    60  		kill(c.pg)
    61  	}
    62  }
    63  
    64  func (c *defaultCommand) Start() (*bytes.Buffer, error) {
    65  	b := bazelNew()
    66  	b.SetStartupArgs(c.startupArgs)
    67  	b.SetArguments(c.bazelArgs)
    68  
    69  	b.WriteToStderr(true)
    70  	b.WriteToStdout(true)
    71  
    72  	var outputBuffer *bytes.Buffer
    73  	outputBuffer, c.pg = start(b, c.target, c.args)
    74  
    75  	c.pg.RootProcess().Env = os.Environ()
    76  
    77  	var err error
    78  	if err = c.pg.Start(); err != nil {
    79  		log.Errorf("Error starting process: %v", err)
    80  		return outputBuffer, err
    81  	}
    82  	log.Log("Starting...")
    83  	c.termSync = sync.Once{}
    84  	return outputBuffer, nil
    85  }
    86  
    87  func (c *defaultCommand) NotifyOfChanges() *bytes.Buffer {
    88  	c.Terminate()
    89  	c.Start()
    90  	return nil
    91  }
    92  
    93  func (c *defaultCommand) IsSubprocessRunning() bool {
    94  	return c.pg != nil && subprocessRunning(c.pg.RootProcess())
    95  }