lib/core/common.rb in buildr-1.2.5 vs lib/core/common.rb in buildr-1.2.6

- old
+ new

@@ -167,12 +167,31 @@ # Returns the Buildr options. See Options. def options() Buildr.options end + # :call-seq: + # environment() => string or nil + # + # Returns the environment name. Use this when your build depends on the environment, + # for example, development, production, etc. The value comes from the BUILDR_ENV environment variable. + # + # For example: + # buildr -e production + def environment() + ENV['BUILDR_ENV'] + end # :call-seq: + # environment(env) + # + # Sets the environment name. + def environment=(env) + ENV['BUILDR_ENV'] = env + end + + # :call-seq: # struct(hash) => Struct # # Convenience method for creating an anonymous Struct. # # For example: @@ -332,11 +351,11 @@ # Sets the target directory into which files are copied and returns self. # # For example: # filter.from("src").into("target").using("build"=>Time.now) def into(dir) - @target = file(File.expand_path(dir.to_s)) + @target = file(File.expand_path(dir.to_s)) { |task| run if target == task && !sources.empty? } self end # :call-seq: # include(*files) => self @@ -415,11 +434,11 @@ sources.each { |source| raise "Source directory #{source} doesn't exist" unless File.exist?(source.to_s) } raise "No target directory specified, where am I going to copy the files to?" if target.nil? copy_map = sources.flatten.map(&:to_s).inject({}) do |map, source| base = Pathname.new(source) - files = FileList[File.join(source, "**/*")].reject { |file| File.directory?(file) }. + files = FileList.recursive(source). map { |file| Pathname.new(file).relative_path_from(base).to_s }. select { |file| @include.empty? || @include.any? { |pattern| File.fnmatch(pattern, file) } }. reject { |file| @exclude.any? { |pattern| File.fnmatch(pattern, file) } } files.each do |file| src, dest = File.expand_path(file, source), File.expand_path(file, target.to_s) @@ -432,27 +451,31 @@ verbose(Rake.application.options.trace || false) do mkpath target.to_s copy_map.each do |path, source| dest = File.expand_path(path, target.to_s) - mkpath File.dirname(dest) rescue nil - case mapping - when Proc, Method # Call on input, accept output. - mapped = mapping.call(path, File.open(source, "rb") { |file| file.read }) - File.open(dest, "wb") { |file| file.write mapped } - when Hash # Map ${key} to value - content = File.open(source, "rb") { |file| file.read } - if Symbol === @mapper - mapped = send("#{@mapper}_mapper", content) { |key| mapping[key] } + if File.directory?(source) + mkpath dest + else + mkpath File.dirname(dest) + case mapping + when Proc, Method # Call on input, accept output. + mapped = mapping.call(path, File.open(source, "rb") { |file| file.read }) + File.open(dest, "wb") { |file| file.write mapped } + when Hash # Map ${key} to value + content = File.open(source, "rb") { |file| file.read } + if Symbol === @mapper + mapped = send("#{@mapper}_mapper", content) { |key| mapping[key] } + else + mapped = regexp_mapper(content) { |key| mapping[key] } + end + #gsub(/\$\{[^}]*\}/) { |str| mapping[str[2..-2]] || str } + File.open(dest, "wb") { |file| file.write mapped } + when nil # No mapping. + cp source, dest else - mapped = regexp_mapper(content) { |key| mapping[key] } + fail "Filter can be a hash (key=>value), or a proc/method; I don't understand #{mapping}" end - #gsub(/\$\{[^}]*\}/) { |str| mapping[str[2..-2]] || str } - File.open(dest, "wb") { |file| file.write mapped } - when nil # No mapping. - cp source, dest - else - fail "Filter can be a hash (key=>value), or a proc/method; I don't understand #{mapping}" end end touch target.to_s end true