Created
December 28, 2010 07:19
-
-
Save noqisofon/757006 to your computer and use it in GitHub Desktop.
Ruby で書かれた which コマンド(Windows 用)。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!c:/bin/ruby/bin/ruby | |
# encoding: shift_jis | |
# いわゆる「パスの通った」ディレクトリリスト。 | |
PATH = ENV["PATH"].split( /;/ ).collect { |path| path.gsub(/\\/, "/") } | |
# Windows で実行可能な拡張子。 | |
EXECUTABLE_EXTENSION = ENV["PATHEXT"].split( /;/ ).collect { |ext| ext.gsub(/\./, "").downcase } | |
def search_command(pattern) | |
PATH.each do |path| | |
# 存在しないディレクトリはスキップします。 | |
next unless Dir.exists? path | |
# パスが通っているディレクトリのファイルを 1 つずつ調べます。 | |
Dir.foreach( path ) do |file| | |
if file =~ pattern then | |
# 見つけたら、これ以上の探索を取りやめます。 | |
return "/#{path.gsub(/\\/, '/')}/#{file}" | |
end | |
end | |
end | |
return nil | |
end | |
if ARGV.length > 0 then | |
ARGV.each do |target_name| | |
pattern = Regexp.compile( "^#{target_name}(\\.(#{EXECUTABLE_EXTENSION.join('|')}))?$" ) | |
ret = search_command pattern | |
puts ret unless ret.nil? | |
end | |
else | |
puts "#{__FILE__}: syntax error: missing agrument" | |
puts "usage: #{__FILE__} <command-name>" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment