| 1 |
require 'rake/ext/core' |
| 2 |
|
| 3 |
###################################################################### |
| 4 |
# Rake extension methods for String. |
| 5 |
# |
| 6 |
class String |
| 7 |
rake_extension("ext") do |
| 8 |
# Replace the file extension with +newext+. If there is no extension on |
| 9 |
# the string, append the new extension to the end. If the new extension |
| 10 |
# is not given, or is the empty string, remove any existing extension. |
| 11 |
# |
| 12 |
# +ext+ is a user added method for the String class. |
| 13 |
def ext(newext='') |
| 14 |
return self.dup if ['.', '..'].include? self |
| 15 |
if newext != '' |
| 16 |
newext = (newext =~ /^\./) ? newext : ("." + newext) |
| 17 |
end |
| 18 |
self.chomp(File.extname(self)) << newext |
| 19 |
end |
| 20 |
end |
| 21 |
|
| 22 |
rake_extension("pathmap") do |
| 23 |
# Explode a path into individual components. Used by +pathmap+. |
| 24 |
def pathmap_explode |
| 25 |
head, tail = File.split(self) |
| 26 |
return [self] if head == self |
| 27 |
return [tail] if head == '.' || tail == '/' |
| 28 |
return [head, tail] if head == '/' |
| 29 |
return head.pathmap_explode + [tail] |
| 30 |
end |
| 31 |
protected :pathmap_explode |
| 32 |
|
| 33 |
# Extract a partial path from the path. Include +n+ directories from the |
| 34 |
# front end (left hand side) if +n+ is positive. Include |+n+| |
| 35 |
# directories from the back end (right hand side) if +n+ is negative. |
| 36 |
def pathmap_partial(n) |
| 37 |
dirs = File.dirname(self).pathmap_explode |
| 38 |
partial_dirs = |
| 39 |
if n > 0 |
| 40 |
dirs[0...n] |
| 41 |
elsif n < 0 |
| 42 |
dirs.reverse[0...-n].reverse |
| 43 |
else |
| 44 |
"." |
| 45 |
end |
| 46 |
File.join(partial_dirs) |
| 47 |
end |
| 48 |
protected :pathmap_partial |
| 49 |
|
| 50 |
# Preform the pathmap replacement operations on the given path. The |
| 51 |
# patterns take the form 'pat1,rep1;pat2,rep2...'. |
| 52 |
def pathmap_replace(patterns, &block) |
| 53 |
result = self |
| 54 |
patterns.split(';').each do |pair| |
| 55 |
pattern, replacement = pair.split(',') |
| 56 |
pattern = Regexp.new(pattern) |
| 57 |
if replacement == '*' && block_given? |
| 58 |
result = result.sub(pattern, &block) |
| 59 |
elsif replacement |
| 60 |
result = result.sub(pattern, replacement) |
| 61 |
else |
| 62 |
result = result.sub(pattern, '') |
| 63 |
end |
| 64 |
end |
| 65 |
result |
| 66 |
end |
| 67 |
protected :pathmap_replace |
| 68 |
|
| 69 |
# Map the path according to the given specification. The specification |
| 70 |
# controls the details of the mapping. The following special patterns are |
| 71 |
# recognized: |
| 72 |
# |
| 73 |
# * <b>%p</b> -- The complete path. |
| 74 |
# * <b>%f</b> -- The base file name of the path, with its file extension, |
| 75 |
# but without any directories. |
| 76 |
# * <b>%n</b> -- The file name of the path without its file extension. |
| 77 |
# * <b>%d</b> -- The directory list of the path. |
| 78 |
# * <b>%x</b> -- The file extension of the path. An empty string if there |
| 79 |
# is no extension. |
| 80 |
# * <b>%X</b> -- Everything *but* the file extension. |
| 81 |
# * <b>%s</b> -- The alternate file separator if defined, otherwise use |
| 82 |
# the standard file separator. |
| 83 |
# * <b>%%</b> -- A percent sign. |
| 84 |
# |
| 85 |
# The %d specifier can also have a numeric prefix (e.g. '%2d'). If the |
| 86 |
# number is positive, only return (up to) +n+ directories in the path, |
| 87 |
# starting from the left hand side. If +n+ is negative, return (up to) |
| 88 |
# |+n+| directories from the right hand side of the path. |
| 89 |
# |
| 90 |
# Examples: |
| 91 |
# |
| 92 |
# 'a/b/c/d/file.txt'.pathmap("%2d") => 'a/b' |
| 93 |
# 'a/b/c/d/file.txt'.pathmap("%-2d") => 'c/d' |
| 94 |
# |
| 95 |
# Also the %d, %p, %f, %n, %x, and %X operators can take a |
| 96 |
# pattern/replacement argument to perform simple string substitutions on a |
| 97 |
# particular part of the path. The pattern and replacement are separated |
| 98 |
# by a comma and are enclosed by curly braces. The replacement spec comes |
| 99 |
# after the % character but before the operator letter. (e.g. |
| 100 |
# "%{old,new}d"). Multiple replacement specs should be separated by |
| 101 |
# semi-colons (e.g. "%{old,new;src,bin}d"). |
| 102 |
# |
| 103 |
# Regular expressions may be used for the pattern, and back refs may be |
| 104 |
# used in the replacement text. Curly braces, commas and semi-colons are |
| 105 |
# excluded from both the pattern and replacement text (let's keep parsing |
| 106 |
# reasonable). |
| 107 |
# |
| 108 |
# For example: |
| 109 |
# |
| 110 |
# "src/org/onestepback/proj/A.java".pathmap("%{^src,bin}X.class") |
| 111 |
# |
| 112 |
# returns: |
| 113 |
# |
| 114 |
# "bin/org/onestepback/proj/A.class" |
| 115 |
# |
| 116 |
# If the replacement text is '*', then a block may be provided to perform |
| 117 |
# some arbitrary calculation for the replacement. |
| 118 |
# |
| 119 |
# For example: |
| 120 |
# |
| 121 |
# "/path/to/file.TXT".pathmap("%X%{.*,*}x") { |ext| |
| 122 |
# ext.downcase |
| 123 |
# } |
| 124 |
# |
| 125 |
# Returns: |
| 126 |
# |
| 127 |
# "/path/to/file.txt" |
| 128 |
# |
| 129 |
def pathmap(spec=nil, &block) |
| 130 |
return self if spec.nil? |
| 131 |
result = '' |
| 132 |
spec.scan(/%\{[^}]*\}-?\d*[sdpfnxX%]|%-?\d+d|%.|[^%]+/) do |frag| |
| 133 |
case frag |
| 134 |
when '%f' |
| 135 |
result << File.basename(self) |
| 136 |
when '%n' |
| 137 |
result << File.basename(self).ext |
| 138 |
when '%d' |
| 139 |
result << File.dirname(self) |
| 140 |
when '%x' |
| 141 |
result << File.extname(self) |
| 142 |
when '%X' |
| 143 |
result << self.ext |
| 144 |
when '%p' |
| 145 |
result << self |
| 146 |
when '%s' |
| 147 |
result << (File::ALT_SEPARATOR || File::SEPARATOR) |
| 148 |
when '%-' |
| 149 |
# do nothing |
| 150 |
when '%%' |
| 151 |
result << "%" |
| 152 |
when /%(-?\d+)d/ |
| 153 |
result << pathmap_partial($1.to_i) |
| 154 |
when /^%\{([^}]*)\}(\d*[dpfnxX])/ |
| 155 |
patterns, operator = $1, $2 |
| 156 |
result << pathmap('%' + operator).pathmap_replace(patterns, &block) |
| 157 |
when /^%/ |
| 158 |
fail ArgumentError, "Unknown pathmap specifier #{frag} in '#{spec}'" |
| 159 |
else |
| 160 |
result << frag |
| 161 |
end |
| 162 |
end |
| 163 |
result |
| 164 |
end |
| 165 |
end |
| 166 |
end # class String |
| 167 |
|