From 901ed2001bc4d00aee8b9d45832ce1a41568b6e7 Mon Sep 17 00:00:00 2001 From: rdp Date: Wed, 16 Dec 2009 12:40:40 -0700 Subject: [PATCH] bump gem version --- bin/whichr | 4 +- lib/whichr.rb | 112 +++++++++--------- .../{test_whichr.spec => spec.test_whichr.rb} | 16 ++- whichr.gemspec | 5 +- 4 files changed, 70 insertions(+), 67 deletions(-) rename spec/{test_whichr.spec => spec.test_whichr.rb} (93%) diff --git a/bin/whichr b/bin/whichr index 8e10078..80f75e7 100755 --- a/bin/whichr +++ b/bin/whichr @@ -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 diff --git a/lib/whichr.rb b/lib/whichr.rb index 0ce7e47..3849dbd 100755 --- a/lib/whichr.rb +++ b/lib/whichr.rb @@ -1,5 +1,3 @@ -# stolen from the gnuplot gem -# and then modified require 'rubygems' require 'sane' @@ -7,12 +5,16 @@ 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 @@ -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 diff --git a/spec/test_whichr.spec b/spec/spec.test_whichr.rb similarity index 93% rename from spec/test_whichr.spec rename to spec/spec.test_whichr.rb index 8aeb7df..32daa44 100755 --- a/spec/test_whichr.spec +++ b/spec/spec.test_whichr.rb @@ -1,4 +1,5 @@ require File.dirname(__FILE__) + '/../lib/whichr' +require 'spec/autorun' require 'fileutils' @@ -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' @@ -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' @@ -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' @@ -49,7 +46,6 @@ 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') @@ -57,7 +53,6 @@ def setup end it "should give you cwd if on doze" do - setup Dir.chdir 'test' do outs = @a.which('abc') if RUBY_PLATFORM =~ /mingw|mswin/ @@ -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 diff --git a/whichr.gemspec b/whichr.gemspec index 42e9411..5b0d0ae 100644 --- a/whichr.gemspec +++ b/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"] @@ -13,6 +13,9 @@ Gem::Specification.new do |s| s.add_dependency(%q, [">= 2.3.0"]) # so that I don't need a lib directory :P s.add_dependency(%q) # 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