summaryrefslogtreecommitdiff
path: root/lib/rake/ext
diff options
context:
space:
mode:
authordrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-06-23 22:11:55 +0000
committerdrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-06-23 22:11:55 +0000
commitd001539a0538cba0e48be2ebdafe29e67b006a4e (patch)
treeabf1591ca3b56f04f46cc2cce9918700620a3ab1 /lib/rake/ext
parent3fbc9440feb66cf762834b6d66e6f3a893bab5b7 (diff)
* lib/rake: Import Rake 0.9.2
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32217 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rake/ext')
-rw-r--r--lib/rake/ext/core.rb27
-rw-r--r--lib/rake/ext/module.rb39
-rw-r--r--lib/rake/ext/string.rb167
-rw-r--r--lib/rake/ext/time.rb14
4 files changed, 247 insertions, 0 deletions
diff --git a/lib/rake/ext/core.rb b/lib/rake/ext/core.rb
new file mode 100644
index 0000000..1f3a738
--- /dev/null
+++ b/lib/rake/ext/core.rb
@@ -0,0 +1,27 @@
+######################################################################
+# Core extension library
+#
+class Module
+ # Check for an existing method in the current class before extending. IF
+ # the method already exists, then a warning is printed and the extension is
+ # not added. Otherwise the block is yielded and any definitions in the
+ # block will take effect.
+ #
+ # Usage:
+ #
+ # class String
+ # rake_extension("xyz") do
+ # def xyz
+ # ...
+ # end
+ # end
+ # end
+ #
+ def rake_extension(method)
+ if method_defined?(method)
+ $stderr.puts "WARNING: Possible conflict with Rake extension: #{self}##{method} already exists"
+ else
+ yield
+ end
+ end
+end
diff --git a/lib/rake/ext/module.rb b/lib/rake/ext/module.rb
new file mode 100644
index 0000000..3f64aef
--- /dev/null
+++ b/lib/rake/ext/module.rb
@@ -0,0 +1,39 @@
+require 'rake/ext/core'
+require 'rake/task'
+require 'rake/file_task'
+require 'rake/file_creation_task'
+require 'rake/application'
+require 'rake/task_manager'
+
+######################################################################
+# Rake extensions to Module.
+#
+class Module
+
+ # Rename the original handler to make it available.
+ alias :rake_original_const_missing :const_missing
+
+ # Check for deprecated uses of top level (i.e. in Object) uses of
+ # Rake class names. If someone tries to reference the constant
+ # name, display a warning and return the proper object. Using the
+ # --classic-namespace command line option will define these
+ # constants in Object and avoid this handler.
+ def const_missing(const_name)
+ case const_name
+ when :Task
+ Rake.application.const_warning(const_name)
+ Rake::Task
+ when :FileTask
+ Rake.application.const_warning(const_name)
+ Rake::FileTask
+ when :FileCreationTask
+ Rake.application.const_warning(const_name)
+ Rake::FileCreationTask
+ when :RakeApp
+ Rake.application.const_warning(const_name)
+ Rake::Application
+ else
+ rake_original_const_missing(const_name)
+ end
+ end
+end
diff --git a/lib/rake/ext/string.rb b/lib/rake/ext/string.rb
new file mode 100644
index 0000000..fb22a9d
--- /dev/null
+++ b/lib/rake/ext/string.rb
@@ -0,0 +1,167 @@
+require 'rake/ext/core'
+
+######################################################################
+# Rake extension methods for String.
+#
+class String
+ rake_extension("ext") do
+ # Replace the file extension with +newext+. If there is no extension on
+ # the string, append the new extension to the end. If the new extension
+ # is not given, or is the empty string, remove any existing extension.
+ #
+ # +ext+ is a user added method for the String class.
+ def ext(newext='')
+ return self.dup if ['.', '..'].include? self
+ if newext != ''
+ newext = (newext =~ /^\./) ? newext : ("." + newext)
+ end
+ self.chomp(File.extname(self)) << newext
+ end
+ end
+
+ rake_extension("pathmap") do
+ # Explode a path into individual components. Used by +pathmap+.
+ def pathmap_explode
+ head, tail = File.split(self)
+ return [self] if head == self
+ return [tail] if head == '.' || tail == '/'
+ return [head, tail] if head == '/'
+ return head.pathmap_explode + [tail]
+ end
+ protected :pathmap_explode
+
+ # Extract a partial path from the path. Include +n+ directories from the
+ # front end (left hand side) if +n+ is positive. Include |+n+|
+ # directories from the back end (right hand side) if +n+ is negative.
+ def pathmap_partial(n)
+ dirs = File.dirname(self).pathmap_explode
+ partial_dirs =
+ if n > 0
+ dirs[0...n]
+ elsif n < 0
+ dirs.reverse[0...-n].reverse
+ else
+ "."
+ end
+ File.join(partial_dirs)
+ end
+ protected :pathmap_partial
+
+ # Preform the pathmap replacement operations on the given path. The
+ # patterns take the form 'pat1,rep1;pat2,rep2...'.
+ def pathmap_replace(patterns, &block)
+ result = self
+ patterns.split(';').each do |pair|
+ pattern, replacement = pair.split(',')
+ pattern = Regexp.new(pattern)
+ if replacement == '*' && block_given?
+ result = result.sub(pattern, &block)
+ elsif replacement
+ result = result.sub(pattern, replacement)
+ else
+ result = result.sub(pattern, '')
+ end
+ end
+ result
+ end
+ protected :pathmap_replace
+
+ # Map the path according to the given specification. The specification
+ # controls the details of the mapping. The following special patterns are
+ # recognized:
+ #
+ # * <b>%p</b> -- The complete path.
+ # * <b>%f</b> -- The base file name of the path, with its file extension,
+ # but without any directories.
+ # * <b>%n</b> -- The file name of the path without its file extension.
+ # * <b>%d</b> -- The directory list of the path.
+ # * <b>%x</b> -- The file extension of the path. An empty string if there
+ # is no extension.
+ # * <b>%X</b> -- Everything *but* the file extension.
+ # * <b>%s</b> -- The alternate file separator if defined, otherwise use
+ # the standard file separator.
+ # * <b>%%</b> -- A percent sign.
+ #
+ # The %d specifier can also have a numeric prefix (e.g. '%2d'). If the
+ # number is positive, only return (up to) +n+ directories in the path,
+ # starting from the left hand side. If +n+ is negative, return (up to)
+ # |+n+| directories from the right hand side of the path.
+ #
+ # Examples:
+ #
+ # 'a/b/c/d/file.txt'.pathmap("%2d") => 'a/b'
+ # 'a/b/c/d/file.txt'.pathmap("%-2d") => 'c/d'
+ #
+ # Also the %d, %p, %f, %n, %x, and %X operators can take a
+ # pattern/replacement argument to perform simple string substitutions on a
+ # particular part of the path. The pattern and replacement are separated
+ # by a comma and are enclosed by curly braces. The replacement spec comes
+ # after the % character but before the operator letter. (e.g.
+ # "%{old,new}d"). Multiple replacement specs should be separated by
+ # semi-colons (e.g. "%{old,new;src,bin}d").
+ #
+ # Regular expressions may be used for the pattern, and back refs may be
+ # used in the replacement text. Curly braces, commas and semi-colons are
+ # excluded from both the pattern and replacement text (let's keep parsing
+ # reasonable).
+ #
+ # For example:
+ #
+ # "src/org/onestepback/proj/A.java".pathmap("%{^src,bin}X.class")
+ #
+ # returns:
+ #
+ # "bin/org/onestepback/proj/A.class"
+ #
+ # If the replacement text is '*', then a block may be provided to perform
+ # some arbitrary calculation for the replacement.
+ #
+ # For example:
+ #
+ # "/path/to/file.TXT".pathmap("%X%{.*,*}x") { |ext|
+ # ext.downcase
+ # }
+ #
+ # Returns:
+ #
+ # "/path/to/file.txt"
+ #
+ def pathmap(spec=nil, &block)
+ return self if spec.nil?
+ result = ''
+ spec.scan(/%\{[^}]*\}-?\d*[sdpfnxX%]|%-?\d+d|%.|[^%]+/) do |frag|
+ case frag
+ when '%f'
+ result << File.basename(self)
+ when '%n'
+ result << File.basename(self).ext
+ when '%d'
+ result << File.dirname(self)
+ when '%x'
+ result << File.extname(self)
+ when '%X'
+ result << self.ext
+ when '%p'
+ result << self
+ when '%s'
+ result << (File::ALT_SEPARATOR || File::SEPARATOR)
+ when '%-'
+ # do nothing
+ when '%%'
+ result << "%"
+ when /%(-?\d+)d/
+ result << pathmap_partial($1.to_i)
+ when /^%\{([^}]*)\}(\d*[dpfnxX])/
+ patterns, operator = $1, $2
+ result << pathmap('%' + operator).pathmap_replace(patterns, &block)
+ when /^%/
+ fail ArgumentError, "Unknown pathmap specifier #{frag} in '#{spec}'"
+ else
+ result << frag
+ end
+ end
+ result
+ end
+ end
+end # class String
+
diff --git a/lib/rake/ext/time.rb b/lib/rake/ext/time.rb
new file mode 100644
index 0000000..ca3ea2a
--- /dev/null
+++ b/lib/rake/ext/time.rb
@@ -0,0 +1,14 @@
+# ###########################################################################
+# Extensions to time to allow comparisons with an early time class.
+#
+class Time
+ alias rake_original_time_compare :<=>
+ def <=>(other)
+ if Rake::EarlyTime === other
+ - other.<=>(self)
+ else
+ rake_original_time_compare(other)
+ end
+ end
+end # class Time
+