2012-11-29 12:12:29 +04:00
|
|
|
# -*- coding: us-ascii -*-
|
|
|
|
|
|
|
|
class VPath
|
|
|
|
attr_accessor :separator
|
|
|
|
|
|
|
|
def initialize(*list)
|
|
|
|
@list = list
|
|
|
|
@additional = []
|
2013-01-15 18:12:36 +04:00
|
|
|
@separator = nil
|
2012-11-29 12:12:29 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
list.inspect
|
|
|
|
end
|
|
|
|
|
|
|
|
def search(meth, base, *rest)
|
|
|
|
begin
|
|
|
|
meth.call(base, *rest)
|
|
|
|
rescue Errno::ENOENT => error
|
|
|
|
list.each do |dir|
|
|
|
|
return meth.call(File.join(dir, base), *rest) rescue nil
|
|
|
|
end
|
|
|
|
raise error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process(*args, &block)
|
|
|
|
search(File.method(__callee__), *args, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
alias stat process
|
|
|
|
alias lstat process
|
|
|
|
|
|
|
|
def open(*args)
|
|
|
|
f = search(File.method(:open), *args)
|
|
|
|
if block_given?
|
|
|
|
begin
|
|
|
|
yield f
|
|
|
|
ensure
|
|
|
|
f.close unless f.closed?
|
|
|
|
end
|
|
|
|
else
|
|
|
|
f
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read(*args)
|
|
|
|
open(*args) {|f| f.read}
|
|
|
|
end
|
|
|
|
|
|
|
|
def foreach(file, *args, &block)
|
|
|
|
open(file) {|f| f.each(*args, &block)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_options(opt)
|
|
|
|
opt.on("-I", "--srcdir=DIR", "add a directory to search path") {|dir|
|
|
|
|
@additional << dir
|
|
|
|
}
|
|
|
|
opt.on("-L", "--vpath=PATH LIST", "add directories to search path") {|dirs|
|
|
|
|
@additional << [dirs]
|
|
|
|
}
|
2013-01-20 17:24:31 +04:00
|
|
|
opt.on("--path-separator=SEP", /\A(?:\W\z|\.(\W).+)/, "separator for vpath") {|sep, vsep|
|
|
|
|
# hack for msys make.
|
|
|
|
@separator = vsep || sep
|
2012-11-29 12:12:29 +04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def list
|
|
|
|
@additional.reject! do |dirs|
|
|
|
|
case dirs
|
|
|
|
when String
|
|
|
|
@list << dirs
|
|
|
|
when Array
|
2013-02-07 16:20:56 +04:00
|
|
|
raise "--path-separator option is needed for vpath list" unless @separator
|
|
|
|
# @separator ||= (require 'rbconfig'; RbConfig::CONFIG["PATH_SEPARATOR"])
|
2012-11-29 12:12:29 +04:00
|
|
|
@list.concat(dirs[0].split(@separator))
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
@list
|
|
|
|
end
|
2014-11-19 04:08:00 +03:00
|
|
|
|
|
|
|
def strip(path)
|
|
|
|
prefix = list.map {|dir| Regexp.quote(dir)}
|
|
|
|
path.sub(/\A#{prefix.join('|')}(?:\/|\z)/, '')
|
|
|
|
end
|
2012-11-29 12:12:29 +04:00
|
|
|
end
|