Skip to content

Commit

Permalink
Bring back prefixing, and add a --noprefix flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejholmes committed Jan 15, 2017
1 parent 7d1c601 commit 9b19b53
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 34 deletions.
4 changes: 4 additions & 0 deletions docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 6 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {
var (
version = flag.Bool("version", false, "Print the version of walk and exit.")
verbose = flag.Bool("v", false, fmt.Sprintf("Show stdout from rules when executing the %s phase.", PhaseExec))
noprefix = flag.Bool("noprefix", false, "Disables the prefixing of stdout/stderr from rules with the name of the target.")
deps = flag.Bool("d", false, "Print the dependencies of the target.")
concurrency = flag.Uint("j", 0, "The number of targets that are executed in parallel.")
print = flag.String("p", "", "Print the graph that will be executed and exit.")
Expand All @@ -45,10 +46,6 @@ func main() {
os.Exit(0)
}

// All targets are relative to the current working directory.
wd, err := os.Getwd()
must(err)

targets := flag.Args()
if len(targets) == 0 {
targets = []string{DefaultTarget}
Expand All @@ -58,7 +55,10 @@ func main() {
}

plan := newPlan()
plan.NewTarget = newVerboseTarget(wd, stdout(*verbose), os.Stderr)
plan.NewTarget = NewTarget(TargetOptions{
Verbose: *verbose,
NoPrefix: *noprefix,
})

ctx, cancel := context.WithCancel(context.Background())

Expand Down Expand Up @@ -90,31 +90,13 @@ func main() {
}
}

func newVerboseTarget(wd string, stdout, stderr io.Writer) func(string) (Target, error) {
return func(name string) (Target, error) {
t := newTarget(wd, name)
t.stdout = stdout
t.stderr = stderr
vt := &verboseTarget{
target: t,
}
return vt, nil
}
}

func stdout(verbose bool) io.Writer {
if verbose {
return os.Stdout
}
return nil
}

func must(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", ansi("31", "error: %v", err))
os.Exit(1)
}
}

func isTerminal(w io.Writer) bool {
if w == nil {
return false
Expand Down
4 changes: 4 additions & 0 deletions man/walk.1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ Controls the number of targets that are executed in parallel\. By default, targe
\fB\-p\fR
Prints the underlying DAG to stdout, using the provided format\. Available formats are \fBdot\fR\.
.
.TP
\fB\-\-noprefix\fR
By default, the stdout/stderr output from rules is prefixed with the name of the target, followed by a tab character\. This flag disables the prefixing\. This can help with performance, or issues where you encounter "too many open files", since prefixing necessitates more file descriptors\.
.
.SH "TARGETS"
Targets can be used to represent a task, or a file that needs to be built\. They are synonymous with targets in make(1)\. In general, targets are relative paths to files that need to be built, like \fBsrc/hello\.o\fR\. When a target does not relate to an actual file on disk, it\'s synonymous with \fB\.PHONY\fR targets in make(1)\.
.
Expand Down
4 changes: 4 additions & 0 deletions man/walk.1.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions man/walk.1.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ dependencies can be built.
Prints the underlying DAG to stdout, using the provided format. Available
formats are `dot`.

* `--noprefix`:
By default, the stdout/stderr output from rules is prefixed with the name
of the target, followed by a tab character. This flag disables the
prefixing. This can help with performance, or issues where you encounter
"too many open files", since prefixing necessitates more file descriptors.

## TARGETS

Targets can be used to represent a task, or a file that needs to be built. They
Expand Down
67 changes: 57 additions & 10 deletions plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,60 @@ type Target interface {
Name() string
}

// TargetOptions are options passed to the NewTarget factory method.
type TargetOptions struct {
// The working directory that the target is relative to. The zero value
// is to use os.Getwd().
WorkingDir string

// Stdout/Stderr streams.
Stdout, Stderr io.Writer

// If true, the stdout from the targets will be attached to the Stdout
// provided above.
Verbose bool

// If true, disables prefixing of stdout/stderr
NoPrefix bool
}

// NewTarget returns a new Target instance.
func NewTarget(name string) (Target, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
func NewTarget(options TargetOptions) func(string) (Target, error) {
if options.Stdout == nil {
options.Stdout = os.Stdout
}

if options.Stderr == nil {
options.Stderr = os.Stderr
}

var err error
if options.WorkingDir == "" {
options.WorkingDir, err = os.Getwd()
}

return func(name string) (Target, error) {
if err != nil {
return nil, err
}

t := newTarget(options.WorkingDir, name)
if options.Verbose {
if options.NoPrefix {
t.stdout = options.Stdout
} else {
t.stdout = prefix(options.Stdout, t)
}
}
if options.NoPrefix {
t.stderr = options.Stderr
} else {
t.stderr = prefix(options.Stderr, t)
}
return &verboseTarget{
target: t,
}, nil
}
return newVerboseTarget(wd, nil, os.Stderr)(name)
}

// Plan is used to build a graph of all the targets and their dependencies. It
Expand Down Expand Up @@ -72,7 +119,7 @@ func Exec(ctx context.Context, semaphore Semaphore, targets ...string) error {
// newPlan returns a new initialized Plan instance.
func newPlan() *Plan {
return &Plan{
NewTarget: NewTarget,
NewTarget: NewTarget(TargetOptions{}),
graph: newGraph(),
}
}
Expand Down Expand Up @@ -294,9 +341,8 @@ func (t *target) Dependencies(ctx context.Context) ([]string, error) {
func (t *target) ruleCommand(ctx context.Context, phase string) *exec.Cmd {
name := filepath.Base(t.path)
cmd := exec.CommandContext(ctx, t.rulefile, phase, name)
pre := ansi("36", fmt.Sprintf("%s\t", t.name))
cmd.Stdout = prefix(t.stdout, pre)
cmd.Stderr = prefix(t.stderr, pre)
cmd.Stdout = t.stdout
cmd.Stderr = t.stderr
cmd.Dir = t.dir
return cmd
}
Expand Down Expand Up @@ -365,10 +411,11 @@ type prefixWriter struct {
b []byte
}

func prefix(w io.Writer, prefix string) io.Writer {
func prefix(w io.Writer, t Target) io.Writer {
if w == nil {
return w
}
prefix := ansi("36", fmt.Sprintf("%s\t", t.Name()))
return &prefixWriter{
prefix: []byte(prefix),
w: w,
Expand Down

0 comments on commit 9b19b53

Please sign in to comment.