lib/core/common.rb in buildr-1.2.1 vs lib/core/common.rb in buildr-1.2.2
- old
+ new
@@ -248,19 +248,19 @@
temp = Tempfile.open(File.basename(args.to_s))
file(temp.path).tap do |task|
# Since temporary file exists, force a download.
class << task ; def needed?() ; true ; end ; end
task.sources << args
- task.enhance { args.download temp, :proxy=>Buildr.options.proxy }
+ task.enhance { args.download temp }
end
else
# Download to a file created by the task.
fail unless args.keys.size == 1
uri = URI.parse(args.values.first.to_s)
file_create(args.keys.first).tap do |task|
task.sources << uri
- task.enhance { uri.download task.name, :proxy=>Buildr.options.proxy }
+ task.enhance { uri.download task.name }
end
end
end
@@ -360,25 +360,50 @@
end
# The mapping. See #using.
attr_accessor :mapping
+ # The mapper to use. See #using.
+ attr_accessor :mapper
+
# :call-seq:
# using(mapping) => self
# using() { |file_name, contents| ... } => self
#
# Specifies the mapping to use and returns self.
#
- # The mapping can be a proc or a method called with the file name and content, returning
- # the modified content. Or the mapping can be a Hash for mapping each ${key} into a value.
- # Without any mapping, all files are copied as is.
+ # The most typical mapping uses a Hash, and the default mapping uses the Maven style, so
+ # <code>${key}</code> are mapped to the values. You can change that by passing a different
+ # format as the first argument. Currently supports:
+ # * :ant -- Map <code>@key@</code>.
+ # * :maven -- Map <code>${key}</code> (default).
+ # * :ruby -- Map <code>#{key}</code>.
+ # * Regexp -- Maps the matched data (e.g. <code>/=(.*?)=/</code>
#
# For example:
# filter.using "version"=>"1.2"
- # will replace all occurrences of "${version}" with "1.2".
- def using(mapping = nil, &block)
- self.mapping = mapping || block
+ # Is the same as:
+ # filter.using :maven, "version"=>"1.2"
+ #
+ # You can also pass a proc or method. It will be called with the file name and content,
+ # to return the mapped content.
+ #
+ # Without any mapping, all files are copied as is.
+ def using(*args, &block)
+ case args.first
+ when Hash # Maven hash mapping
+ using :maven, *args
+ when Symbol # Mapping from a method
+ raise ArgumentError, "Expected mapper type followed by mapping hash" unless args.size == 2 && Hash === args[1]
+ @mapper, @mapping = *args
+ when Regexp # Mapping using a regular expression
+ raise ArgumentError, "Expected regular expression followed by mapping hash" unless args.size == 2 && Hash === args[1]
+ @mapper, @mapping = *args
+ else
+ raise ArgumentError, "Expected proc, method or a block" if args.size > 1 || (args.first && block)
+ @mapping = args.first || block
+ end
self
end
# :call-seq:
# run() => boolean
@@ -412,12 +437,17 @@
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
- mapped = File.open(source, "rb") { |file| file.read }.
- gsub(/\$\{[^}]*\}/) { |str| mapping[str[2..-2]] || str }
+ 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
fail "Filter can be a hash (key=>value), or a proc/method; I don't understand #{mapping}"
@@ -429,11 +459,29 @@
end
# Returns the target directory.
def to_s()
@target.to_s
- end
-
+ end
+
+ private
+
+ def maven_mapper(content)
+ content.gsub(/\$\{.*?\}/) { |str| yield(str[2..-2]) || str }
+ end
+
+ def ant_mapper(content)
+ content.gsub(/@.*?@/) { |str| yield(str[1..-2]) || str }
+ end
+
+ def ruby_mapper(content)
+ content.gsub(/#\{.*?\}/) { |str| yield(str[2..-2]) || str }
+ end
+
+ def regexp_mapper(content)
+ content.gsub(@mapper) { |str| yield(str.scan(@mapper).join) || str }
+ end
+
end
# :call-seq:
# filter(*source) => Filter
#