Skip to content

Commit

Permalink
Compute outfile from dir in golang.Binary (#31)
Browse files Browse the repository at this point in the history
* Add "autocleaning." For golang.Binary, default outfile to the basename of the input dir.

* Unwind the autoclean stuff, it needs a rethink.
  • Loading branch information
bobg committed May 12, 2023
1 parent 5a955ab commit 71cb1db
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
10 changes: 8 additions & 2 deletions clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// Clean is a Target that deletes the files named in Files when it runs.
// Files that don't exist are silently ignored.
// Files that already don't exist are silently ignored.
//
// A Clean target may be specified in YAML using the tag !Clean,
// which introduces a sequence.
Expand All @@ -30,10 +30,16 @@ type clean struct {
}

// Run implements Target.Run.
func (c *clean) Run(ctx context.Context, _ *Controller) error {
func (c *clean) Run(ctx context.Context, con *Controller) error {
if GetDryRun(ctx) {
if GetVerbose(ctx) {
con.Indentf(" would remove %v", c.Files)
}
return nil
}
if GetVerbose(ctx) {
con.Indentf(" removing %v", c.Files)
}
for _, f := range c.Files {
err := os.Remove(f)
if errors.Is(err, fs.ErrNotExist) {
Expand Down
21 changes: 11 additions & 10 deletions fab.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Build builds the fab binary.
Build: !go.Binary
Dir: cmd/fab
Out: fab

# Test runs "go test" with the race detector enabled, plus coverage reporting.
Test: !Command
Expand All @@ -24,18 +23,20 @@ Check: !Seq

# Cover produces a test-coverage profile and opens it in a browser.
Cover: !Deps
Pre:
- !Files
In: !go.Deps
Dir: cmd/fab
Recursive: true
Tests: true
Out: [cover.out]
Target: !Command
Shell: go test -coverprofile cover.out ./...
Pre: [CoverOut]
Post: !Command
Shell: go tool cover -html cover.out

# CoverOut produces cover.out by running "go test -coverprofile".
CoverOut: !Files
In: !go.Deps
Dir: cmd/fab
Recursive: true
Tests: true
Out: [cover.out]
Target: !Command
Shell: go test -coverprofile cover.out ./...

# Clean removes build-target output.
Clean: !Clean
- fab
Expand Down
15 changes: 14 additions & 1 deletion golang/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

// Binary is a target describing how to compile a Go binary whose main package is in `dir`.
// The resulting binary gets written to `outfile`.
// If outfile is empty,
// it defaults to the last path element of dir.
// Additional command-line arguments for `go build` can be specified with `flags`.
//
// A Binary target may be specified in YAML using the tag !go.Binary,
Expand All @@ -24,7 +26,13 @@ import (
// - Flags: a sequence of additional command-line flags for `go build`
//
// Both Dir and Out are either absolute or relative to the directory containing the YAML file.
// If Out is unspecified,
// it defaults to the last path element of Dir.
func Binary(dir, outfile string, flags ...string) (fab.Target, error) {
if outfile == "" {
outfile = filepath.Base(dir)
}

relOutfile, err := filepath.Rel(dir, outfile)
if err != nil {
return nil, errors.Wrapf(err, "getting relative path from %s to %s", dir, outfile)
Expand Down Expand Up @@ -63,12 +71,17 @@ func binaryDecoder(con *fab.Controller, node *yaml.Node, dir string) (fab.Target
return nil, errors.Wrap(err, "YAML error decoding go.Binary")
}

out := b.Out
if out == "" {
out = filepath.Base(b.Dir)
}

flags, err := fab.YAMLStringList(&b.Flags)
if err != nil {
return nil, errors.Wrap(err, "YAML error decoding go.Binary.Flags")
}

return Binary(con.JoinPath(dir, b.Dir), con.JoinPath(dir, b.Out), flags...)
return Binary(con.JoinPath(dir, b.Dir), con.JoinPath(dir, out), flags...)
}

// Deps produces the list of files involved in building the Go package in the given directory.
Expand Down
2 changes: 1 addition & 1 deletion proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// includes is a list of directories in which to find .proto files;
// otherOpts are options (other than -I / --proto_path options) for the protoc command line.
// Typically otherOpts includes at least "--foo_out=DIR" for some target language foo.
// This function uses [Deps] to find the dependencies of the input files.
// This function uses [fab.Deps] to find the dependencies of the input files.
//
// A Proto target may be specified in YAML using the !proto.Proto tag,
// which introduces a mapping whose fields are:
Expand Down
2 changes: 1 addition & 1 deletion yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func YAMLStringListFromNodes(nodes []*yaml.Node) ([]string, error) {
yamlStringListRegistryMu.Unlock()

if !ok {
return nil, fmt.Errorf("unknown YAML string-list type %s", tag)
return nil, UnknownStringListTagError{Tag: tag}
}

strs, err := fn(node)
Expand Down

0 comments on commit 71cb1db

Please sign in to comment.