Add Temporary and DeleteTemporaryFiles to RuleBuilder
Temporary marks an output path as a temporary file that is not necessary after the rule completes, removing it from the result of Outputs. DeleteTemporaryFiles adds a command to the command line that deletes all files that have been marked with Temporary. Test: rule_builder_test.go Change-Id: Ie509ed800992962747fb287858e148e975eee54a
This commit is contained in:
parent
758290d7ff
commit
5cb5b093d1
|
@ -29,12 +29,15 @@ import (
|
|||
type RuleBuilder struct {
|
||||
commands []*RuleBuilderCommand
|
||||
installs []RuleBuilderInstall
|
||||
temporariesSet map[string]bool
|
||||
restat bool
|
||||
}
|
||||
|
||||
// NewRuleBuilder returns a newly created RuleBuilder.
|
||||
func NewRuleBuilder() *RuleBuilder {
|
||||
return &RuleBuilder{}
|
||||
return &RuleBuilder{
|
||||
temporariesSet: make(map[string]bool),
|
||||
}
|
||||
}
|
||||
|
||||
// RuleBuilderInstall is a tuple of install from and to locations.
|
||||
|
@ -63,6 +66,25 @@ func (r *RuleBuilder) Command() *RuleBuilderCommand {
|
|||
return command
|
||||
}
|
||||
|
||||
// Temporary marks an output of a command as an intermediate file that will be used as an input to another command
|
||||
// in the same rule, and should not be listed in Outputs.
|
||||
func (r *RuleBuilder) Temporary(path string) {
|
||||
r.temporariesSet[path] = true
|
||||
}
|
||||
|
||||
// DeleteTemporaryFiles adds a command to the rule that deletes any outputs that have been marked using Temporary
|
||||
// when the rule runs. DeleteTemporaryFiles should be called after all calls to Temporary.
|
||||
func (r *RuleBuilder) DeleteTemporaryFiles() {
|
||||
var temporariesList []string
|
||||
|
||||
for intermediate := range r.temporariesSet {
|
||||
temporariesList = append(temporariesList, intermediate)
|
||||
}
|
||||
sort.Strings(temporariesList)
|
||||
|
||||
r.Command().Text("rm").Flag("-f").Outputs(temporariesList)
|
||||
}
|
||||
|
||||
// Inputs returns the list of paths that were passed to the RuleBuilderCommand methods that take input paths, such
|
||||
// as RuleBuilderCommand.Input, RuleBuilderComand.Implicit, or RuleBuilderCommand.FlagWithInput. Inputs to a command
|
||||
// that are also outputs of another command in the same RuleBuilder are filtered out.
|
||||
|
@ -104,8 +126,10 @@ func (r *RuleBuilder) Outputs() []string {
|
|||
|
||||
var outputList []string
|
||||
for output := range outputs {
|
||||
if !r.temporariesSet[output] {
|
||||
outputList = append(outputList, output)
|
||||
}
|
||||
}
|
||||
sort.Strings(outputList)
|
||||
return outputList
|
||||
}
|
||||
|
@ -115,13 +139,27 @@ func (r *RuleBuilder) Installs() []RuleBuilderInstall {
|
|||
return append([]RuleBuilderInstall(nil), r.installs...)
|
||||
}
|
||||
|
||||
func (r *RuleBuilder) toolsSet() map[string]bool {
|
||||
tools := make(map[string]bool)
|
||||
for _, c := range r.commands {
|
||||
for _, tool := range c.tools {
|
||||
tools[tool] = true
|
||||
}
|
||||
}
|
||||
|
||||
return tools
|
||||
}
|
||||
|
||||
// Tools returns the list of paths that were passed to the RuleBuilderCommand.Tool method.
|
||||
func (r *RuleBuilder) Tools() []string {
|
||||
var tools []string
|
||||
for _, c := range r.commands {
|
||||
tools = append(tools, c.tools...)
|
||||
toolsSet := r.toolsSet()
|
||||
|
||||
var toolsList []string
|
||||
for tool := range toolsSet {
|
||||
toolsList = append(toolsList, tool)
|
||||
}
|
||||
return tools
|
||||
sort.Strings(toolsList)
|
||||
return toolsList
|
||||
}
|
||||
|
||||
// Commands returns a slice containing a the built command line for each call to RuleBuilder.Command.
|
||||
|
|
|
@ -45,6 +45,45 @@ func ExampleRuleBuilder() {
|
|||
// outputs: ["linked"]
|
||||
}
|
||||
|
||||
func ExampleRuleBuilder_Temporary() {
|
||||
rule := NewRuleBuilder()
|
||||
|
||||
rule.Command().Tool("cp").Input("a").Output("b")
|
||||
rule.Command().Tool("cp").Input("b").Output("c")
|
||||
rule.Temporary("b")
|
||||
|
||||
fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
|
||||
fmt.Printf("tools: %q\n", rule.Tools())
|
||||
fmt.Printf("inputs: %q\n", rule.Inputs())
|
||||
fmt.Printf("outputs: %q\n", rule.Outputs())
|
||||
|
||||
// Output:
|
||||
// commands: "cp a b && cp b c"
|
||||
// tools: ["cp"]
|
||||
// inputs: ["a"]
|
||||
// outputs: ["c"]
|
||||
}
|
||||
|
||||
func ExampleRuleBuilder_DeleteTemporaryFiles() {
|
||||
rule := NewRuleBuilder()
|
||||
|
||||
rule.Command().Tool("cp").Input("a").Output("b")
|
||||
rule.Command().Tool("cp").Input("b").Output("c")
|
||||
rule.Temporary("b")
|
||||
rule.DeleteTemporaryFiles()
|
||||
|
||||
fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
|
||||
fmt.Printf("tools: %q\n", rule.Tools())
|
||||
fmt.Printf("inputs: %q\n", rule.Inputs())
|
||||
fmt.Printf("outputs: %q\n", rule.Outputs())
|
||||
|
||||
// Output:
|
||||
// commands: "cp a b && cp b c && rm -f b"
|
||||
// tools: ["cp"]
|
||||
// inputs: ["a"]
|
||||
// outputs: ["c"]
|
||||
}
|
||||
|
||||
func ExampleRuleBuilderCommand() {
|
||||
rule := NewRuleBuilder()
|
||||
|
||||
|
|
Loading…
Reference in New Issue