Added a command builder to the list of default builders

This commit is contained in:
Michael D. Lowis 2014-10-13 12:27:05 -04:00
parent 469f575564
commit e279959d79
4 changed files with 61 additions and 0 deletions

View File

@ -210,6 +210,7 @@ end
Rscons ships with a number of builders:
* Command, which executes a user-defined command to produce the target
* CFile, which builds a C or C++ source file from a lex or yacc input file
* Disassemble, which disassembles an object file to a disassembly listing
* Library, which collects object files into a static library archive file
@ -220,6 +221,18 @@ Rscons ships with a number of builders:
If you want to create an Environment that does not contain any builders,
you can use the `exclude_builders` key to the Environment constructor.
#### Command
```ruby
env.Command(target, sources, 'CMD' => command)
# Example
env.Command("docs.html", "docs.md",
CMD => ['pandoc', '-fmarkdown', '-thtml', '-o${_TARGET}', '${_SOURCES}'])
```
The command builder executes a user-defined command in order to produce the
desired target file based on the provided source files.
#### CFile
```ruby

View File

@ -6,6 +6,7 @@ require_relative "rscons/varset"
require_relative "rscons/version"
# default builders
require_relative "rscons/builders/command"
require_relative "rscons/builders/cfile"
require_relative "rscons/builders/disassemble"
require_relative "rscons/builders/library"
@ -18,6 +19,7 @@ module Rscons
# Names of the default builders which will be added to all newly created
# {Environment} objects.
DEFAULT_BUILDERS = [
:Command,
:CFile,
:Disassemble,
:Library,

View File

@ -0,0 +1,30 @@
module Rscons
module Builders
# Execute a command that will produce the given target based on the given
# sources.
#
# Example::
# env.Command("docs.html", "docs.md",
# CMD => ['pandoc', '-fmarkdown', '-thtml', '-o${_TARGET}', '${_SOURCES}'])
class Command < Builder
# Run the builder to produce a build target.
#
# @param target [String] Target file name.
# @param sources [Array<String>] Source file name(s).
# @param cache [Cache] The Cache object.
# @param env [Environment] The Environment executing the builder.
# @param vars [Hash,VarSet] Extra construction variables.
#
# @return [String,false]
# Name of the target file on success or false on failure.
def run(target, sources, cache, env, vars)
vars = vars.merge({
"_TARGET" => target,
"_SOURCES" => sources,
})
command = env.build_command("${CMD}", vars)
standard_build("CMD #{target}", target, command, sources, env, cache)
end
end
end
end

View File

@ -0,0 +1,16 @@
module Rscons
module Builders
describe Command do
let(:command) { ['pandoc', '-fmarkdown', '-thtml', '-o${_TARGET}', '${_SOURCES}'] }
let(:env) {Environment.new}
subject {Command.new}
it "invokes the command to build the target" do
expected_cmd = ['pandoc', '-fmarkdown', '-thtml', '-ofoo.html', 'foo.md']
expect(subject).to receive(:standard_build).with("CMD foo.html", "foo.html", expected_cmd, ["foo.md"], env, :cache)
subject.run("foo.html", ["foo.md"], :cache, env, {'CMD' => command})
end
end
end
end