Module: Pathmap

Included in:
String
Defined in:
lib/chirp/pathmap.rb

Overview

Copyright © 2003, 2004, 2005, 2006, 2007 Jim Weirich

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

Rake – Ruby Make

This is the main file for the Rake application. Normally it is referenced as a library via a require statement, but it can be distributed independently as an application.

Instance Method Summary collapse

Instance Method Details

#ext(newext = '') ⇒ Object

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.



38
39
40
41
42
43
44
# File 'lib/chirp/pathmap.rb', line 38

def ext(newext='')
  return self.dup if ['.', '..'].include? self
  if newext != ''
    newext = (newext =~ /^\./) ? newext : ("." + newext)
  end
  dup.sub!(%r(([^/\\])\.[^./\\]*$)) { $1 + newext } || self + newext
end

#pathmap(spec = nil, &block) ⇒ Object

Map the path according to the given specification. The specification controls the details of the mapping. The following special patterns are recognized:

  • %p – The complete path.

  • %f – The base file name of the path, with its file extension, but without any directories.

  • %n – The file name of the path without its file extension.

  • %d – The directory list of the path.

  • %x – The file extension of the path. An empty string if there is no extension.

  • %X – Everything but the file extension.

  • %s – The alternate file separater if defined, otherwise use the standard file separator.

  • %% – 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 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,newd”). Muliple replacement specs should be separated by semi-colons (e.g. “%old,new;src,bind”).

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"


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/chirp/pathmap.rb', line 156

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 << $1 if self =~ /[^\/](\.[^.]+)$/
    when '%X'
      if self =~ /^(.+[^\/])(\.[^.]+)$/
        result << $1
      else
        result << self
      end
    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