Class | String |
In: |
lib/rake.rb
|
Parent: | Object |
User defined methods to be added to String.
Replace the file extension with newext. If there is no extenson 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.
# File lib/rake.rb, line 83 83: def ext(newext='') 84: return self.dup if ['.', '..'].include? self 85: if newext != '' 86: newext = (newext =~ /^\./) ? newext : ("." + newext) 87: end 88: dup.sub!(%r(([^/\\])\.[^./\\]*$)) { $1 + newext } || self + newext 89: end
Map the path according to the given specification. The specification controls the details of the mapping. The following special patterns are recognized:
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 substititions on a particular part of the path. The pattern and replacement are speparated 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"). Muliple 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"
# File lib/rake.rb, line 207 207: def pathmap(spec=nil, &block) 208: return self if spec.nil? 209: result = '' 210: spec.scan(/%\{[^}]*\}-?\d*[sdpfnxX%]|%-?\d+d|%.|[^%]+/) do |frag| 211: case frag 212: when '%f' 213: result << File.basename(self) 214: when '%n' 215: result << File.basename(self).ext 216: when '%d' 217: result << File.dirname(self) 218: when '%x' 219: result << $1 if self =~ /[^\/](\.[^.]+)$/ 220: when '%X' 221: if self =~ /^(.+[^\/])(\.[^.]+)$/ 222: result << $1 223: else 224: result << self 225: end 226: when '%p' 227: result << self 228: when '%s' 229: result << (File::ALT_SEPARATOR || File::SEPARATOR) 230: when '%-' 231: # do nothing 232: when '%%' 233: result << "%" 234: when /%(-?\d+)d/ 235: result << pathmap_partial($1.to_i) 236: when /^%\{([^}]*)\}(\d*[dpfnxX])/ 237: patterns, operator = $1, $2 238: result << pathmap('%' + operator).pathmap_replace(patterns, &block) 239: when /^%/ 240: fail ArgumentError, "Unknown pathmap specifier #{frag} in '#{spec}'" 241: else 242: result << frag 243: end 244: end 245: result 246: end
Explode a path into individual components. Used by pathmap.
# File lib/rake.rb, line 94 94: def pathmap_explode 95: head, tail = File.split(self) 96: return [self] if head == self 97: return [tail] if head == '.' || tail == '/' 98: return [head, tail] if head == '/' 99: return head.pathmap_explode + [tail] 100: end
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.
# File lib/rake.rb, line 107 107: def pathmap_partial(n) 108: target = File.dirname(self) 109: dirs = target.pathmap_explode 110: if n > 0 111: File.join(dirs[0...n]) 112: elsif n < 0 113: partial = dirs[n..-1] 114: if partial.nil? || partial.empty? 115: target 116: else 117: File.join(partial) 118: end 119: else 120: "." 121: end 122: end
Preform the pathmap replacement operations on the given path. The patterns take the form ‘pat1,rep1;pat2,rep2…’.
# File lib/rake.rb, line 127 127: def pathmap_replace(patterns, &block) 128: result = self 129: patterns.split(';').each do |pair| 130: pattern, replacement = pair.split(',') 131: pattern = Regexp.new(pattern) 132: if replacement == '*' && block_given? 133: result = result.sub(pattern, &block) 134: elsif replacement 135: result = result.sub(pattern, replacement) 136: else 137: result = result.sub(pattern, '') 138: end 139: end 140: result 141: end