add a Directory builder - close #27

This commit is contained in:
Josh Holtrop 2015-01-29 09:31:00 -05:00
parent 73bc71a656
commit 859f87ddee
3 changed files with 67 additions and 0 deletions

View File

@ -8,6 +8,7 @@ require_relative "rscons/version"
# default builders
require_relative "rscons/builders/command"
require_relative "rscons/builders/cfile"
require_relative "rscons/builders/directory"
require_relative "rscons/builders/disassemble"
require_relative "rscons/builders/library"
require_relative "rscons/builders/object"
@ -22,6 +23,7 @@ module Rscons
DEFAULT_BUILDERS = [
:Command,
:CFile,
:Directory,
:Disassemble,
:Library,
:Object,

View File

@ -0,0 +1,31 @@
module Rscons
module Builders
# The Directory builder creates a directory.
class Directory < 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)
if File.directory?(target)
target
elsif File.exists?(target)
$stderr.puts "Error: `#{target}' already exists and is not a directory"
false
else
puts "Directory #{target}"
cache.mkdir_p(target)
target
end
end
end
end
end

View File

@ -798,4 +798,38 @@ EOF
])
end
context "Directory builder" do
it "creates the requested directory" do
test_dir("simple")
Rscons::Environment.new do |env|
env.Directory("teh_dir")
end
expect(File.directory?("teh_dir")).to be_truthy
expect(lines).to eq(["Directory teh_dir"])
end
it "succeeds when the requested directory already exists" do
test_dir("simple")
FileUtils.mkdir("teh_dir")
Rscons::Environment.new do |env|
env.Directory("teh_dir")
end
expect(File.directory?("teh_dir")).to be_truthy
expect(lines).to eq([])
end
it "fails when the target path is a file" do
test_dir("simple")
FileUtils.touch("teh_dir")
expect do
Rscons::Environment.new do |env|
env.Directory("teh_dir")
end
end.to raise_error /Failed to build teh_dir/
expect(lines).to eq([
"Error: `teh_dir' already exists and is not a directory",
])
end
end
end