Skip to content

Commit

Permalink
bump gem version
Browse files Browse the repository at this point in the history
  • Loading branch information
rdp committed Dec 16, 2009
1 parent 88ec75a commit 901ed20
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 67 deletions.
4 changes: 2 additions & 2 deletions bin/whichr
Expand Up @@ -20,7 +20,7 @@ if ARGV[0].in? ['--help', '-h']
end

if ARGV.include? '-a'
RubyWhich.new.process(ARGV[0..-2], true)
RubyWhich.new.which(ARGV[0..-2], true, true)
else
RubyWhich.new.process(ARGV, false)
RubyWhich.new.which(ARGV, false, true)
end
112 changes: 57 additions & 55 deletions lib/whichr.rb
@@ -1,18 +1,20 @@
# stolen from the gnuplot gem
# and then modified
require 'rubygems'
require 'sane'

class RubyWhich
# search the path for the given names
# like ['abc'] (in windows, also searches for abc.bat)
# or ['ab*'] (a glob, in windows, also reveals ab*.bat)
def which( names, return_non_executables_too = false )
names = [names] unless names.is_a? Array
def which( names, return_non_executables_too = false, realtime_output = false )

puts "higher in the list is executed first" if realtime_output

names = Array(names)

if OS.windows?
for name in names.dup # avoid recursion
# windows compat.
# add .bat, .exe, etc.
for extension in ENV['PATHEXT'].split(';') do
names << name + extension
end
Expand All @@ -21,68 +23,68 @@ def which( names, return_non_executables_too = false )

all_found = []
path = ENV['PATH']
# on windows add . [the cwd]
# on windows add cwd
path += (File::PATH_SEPARATOR + '.') if OS.windows?

path.split(File::PATH_SEPARATOR).each do |dir|

for name in names
if OS.windows?
names2 = Dir.glob(dir.gsub("\\", "/") + '/' + name.strip)
unless return_non_executables_too
names2 = names2.select{|name| File.executable?(name)} # only real execs
end
names2.collect!{|name| File.expand_path(name)} # get the right capitalization
names2 = Dir.glob(dir.gsub("\\", "/") + '/' + name.strip)
unless return_non_executables_too
names2 = names2.select{|name| File.executable?(name)} # only real execs
end
names2.collect!{|name| File.expand_path(name)} # get the right capitalization
else
names2 = Dir.glob(dir + '/' + name.strip)
names2 = Dir.glob(dir + '/' + name.strip)
end

# expand paths
names2.collect!{|name| File.expand_path(name).gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR)}

# make sure we aren't repeating a previous
uniques = names2.select{|new|
new = new.downcase if OS.windows?
am_unique = true
all_found.each{|old|
old = old.downcase if OS.windows?
if old == new
am_unique = false
break
end
}
am_unique
}

if realtime_output
uniques.each{ |file|
print file

if !File.executable? file
output += ' (is not executable)'
end

if File.directory?(file)
output << ' (is a directory)'
end
print
}
end

all_found += uniques

all_found += names2.collect{|name| File.expand_path(name)}
end
end

# parse out same spelled fellas in doze
if OS.windows?
unique = []
previous = {}
all_found.each {|entry|
if previous[entry.downcase]
# do nothing
else
previous[entry.downcase] = 'ja'
unique << entry
end
}
all_found = unique
else
all_found.uniq!
end
all_found
end

def process(names, all = false)
candidates = which(names, all)
if candidates == []
puts 'none found (' + names.inspect + ')'
else
print output(candidates)
end
end

def output all
output = "higher in the list is executed first\n"
for candidate in all
# might just match the name and not be executable
candidate = Dir.glob(candidate + '*')[0] if OS.windows?
output << candidate
if !File.executable? candidate
output += ' (is not executable)'
if(File.directory?(candidate))
output << ' (is a directory)'
end

if realtime_output
if all_found == []
puts 'none found (' + names.inspect + ')'
else
puts
end
output << "\n"
end
output
end

all_found
end

end
16 changes: 7 additions & 9 deletions spec/test_whichr.spec → spec/spec.test_whichr.rb
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/../lib/whichr'
require 'spec/autorun'

require 'fileutils'

Expand All @@ -8,17 +9,15 @@
__DIR__.should_not == nil
end

def setup
before do
ENV['PATH'] = 'test'
FileUtils.rm_rf 'test'
Dir.mkdir 'test'
@a = RubyWhich.new
FileUtils.touch 'test/abc.bat'

end

it "should not show duplicates with different cased drive letters" do
setup
path = File.expand_path 'test'
ENV['PATH'] = path.upcase + ';' + path.downcase
outs = @a.which 'abc'
Expand All @@ -27,7 +26,6 @@ def setup
end

it "should not show non execs" do
setup
FileUtils.rm 'test/abc.bat' # clean it up
FileUtils.touch 'test/abc'
File.chmod 0777, 'test/abc'
Expand All @@ -39,7 +37,6 @@ def setup
end

it "should show non execs if you want it to" do
setup
FileUtils.rm 'test/abc.bat' # clean it up
FileUtils.touch 'test/abc'
File.chmod 0777, 'test/abc'
Expand All @@ -49,15 +46,13 @@ def setup
end

it "should only give uniq k?" do
setup
ENV['PATH'] = 'test' + File::PATH_SEPARATOR + 'test'
FileUtils.touch 'test/abc.bat'
outs = @a.which('abc')
assert outs.length == 1
end

it "should give you cwd if on doze" do
setup
Dir.chdir 'test' do
outs = @a.which('abc')
if RUBY_PLATFORM =~ /mingw|mswin/
Expand All @@ -68,12 +63,15 @@ def setup
end

end

end

it "should not be ugly" do
setup
outs = @a.which('abc')
assert !outs[0].include?('abc.BAT')
# should not have /'s on doze
assert !outs[0].include?('/')
end



end
5 changes: 4 additions & 1 deletion whichr.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = %q{whichr}
s.version = "0.2.0"
s.version = "0.3.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Roger Pack"]
Expand All @@ -13,6 +13,9 @@ Gem::Specification.new do |s|
s.add_dependency(%q<rdoc>, [">= 2.3.0"]) # so that I don't need a lib directory :P
s.add_dependency(%q<sane>) # Object.in?


# 0.3.1 cleanup inline
# 0.3.0 inline
# 0.1.0 initial release happy birthday
# 0.1.1 bump README
# 0.1.2 respect ENV['PATHEXT'] and '.' on windows
Expand Down

0 comments on commit 901ed20

Please sign in to comment.