diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-29 08:12:29 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-11-29 08:12:29 +0000 |
commit | d22ac50ad493256c707b7b39eadf3ac46d989224 (patch) | |
tree | ce0cb41e12b6389e4d36e1c182b8f5bbb2b63226 /tool/vpath.rb | |
parent | 2f80fddadc25d13c2c9862b03d16c0445265c027 (diff) |
tool/vpath.rb
* tool/generic_erb.rb, tool/id2token.rb: add --path-separator option
for mingw where make and built ruby live in different world.
* tool/vpath.rb: extract from tool/instruction.rb.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37985 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'tool/vpath.rb')
-rw-r--r-- | tool/vpath.rb | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/tool/vpath.rb b/tool/vpath.rb new file mode 100644 index 0000000000..5241d2d544 --- /dev/null +++ b/tool/vpath.rb @@ -0,0 +1,79 @@ +# -*- coding: us-ascii -*- + +class VPath + attr_accessor :separator + + def initialize(*list) + @list = list + @additional = [] + @separator = File::PATH_SEPARATOR + 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] + } + opt.on("--path-separator=SEP", /\A\W\z/, "separator for vpath") {|sep| + @separator = sep + } + end + + def list + @additional.reject! do |dirs| + case dirs + when String + @list << dirs + when Array + @list.concat(dirs[0].split(@separator)) + end + true + end + @list + end +end |