whichr 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/whichr +2 -2
- data/lib/whichr.rb +55 -55
- metadata +2 -2
    
        data/bin/whichr
    CHANGED
    
    
    
        data/lib/whichr.rb
    CHANGED
    
    | @@ -1,5 +1,3 @@ | |
| 1 | 
            -
            # stolen from the gnuplot gem
         | 
| 2 | 
            -
            # and then modified
         | 
| 3 1 | 
             
            require 'rubygems'
         | 
| 4 2 | 
             
            require 'sane'
         | 
| 5 3 |  | 
| @@ -7,12 +5,16 @@ class RubyWhich | |
| 7 5 | 
             
              # search the path for the given names
         | 
| 8 6 | 
             
              # like ['abc'] (in windows, also searches for abc.bat)
         | 
| 9 7 | 
             
              # or ['ab*'] (a glob, in windows, also reveals ab*.bat)
         | 
| 10 | 
            -
              def which( names, return_non_executables_too = false )
         | 
| 11 | 
            -
                 | 
| 8 | 
            +
              def which( names, return_non_executables_too = false, realtime_output = false )
         | 
| 9 | 
            +
                
         | 
| 10 | 
            +
                puts "higher in the list is executed first" if realtime_output
         | 
| 11 | 
            +
                
         | 
| 12 | 
            +
                names = Array(names)
         | 
| 12 13 |  | 
| 13 14 | 
             
                if OS.windows?
         | 
| 14 15 | 
             
                  for name in names.dup # avoid recursion
         | 
| 15 16 | 
             
                    # windows compat.
         | 
| 17 | 
            +
                    # add .bat, .exe, etc.
         | 
| 16 18 | 
             
                    for extension in  ENV['PATHEXT'].split(';') do
         | 
| 17 19 | 
             
                      names << name + extension
         | 
| 18 20 | 
             
                    end
         | 
| @@ -21,68 +23,66 @@ class RubyWhich | |
| 21 23 |  | 
| 22 24 | 
             
                all_found = []
         | 
| 23 25 | 
             
                path = ENV['PATH']
         | 
| 24 | 
            -
                # on windows add  | 
| 26 | 
            +
                # on windows add cwd
         | 
| 25 27 | 
             
                path += (File::PATH_SEPARATOR + '.') if OS.windows?
         | 
| 28 | 
            +
                
         | 
| 26 29 | 
             
                path.split(File::PATH_SEPARATOR).each do |dir|
         | 
| 30 | 
            +
                  
         | 
| 27 31 | 
             
                  for name in names
         | 
| 28 32 | 
             
                    if OS.windows?
         | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 33 | 
            +
                	    names2 = Dir.glob(dir.gsub("\\", "/") + '/' + name.strip)
         | 
| 34 | 
            +
                	    unless return_non_executables_too
         | 
| 35 | 
            +
                	      names2 = names2.select{|name| File.executable?(name)} # only real execs
         | 
| 36 | 
            +
                	    end
         | 
| 37 | 
            +
                	    names2.collect!{|name| File.expand_path(name)} # get the right capitalization
         | 
| 34 38 | 
             
                    else
         | 
| 35 | 
            -
             | 
| 39 | 
            +
                	    names2 = Dir.glob(dir + '/' + name.strip)
         | 
| 36 40 | 
             
              	    end
         | 
| 41 | 
            +
              	    
         | 
| 42 | 
            +
              	    # expand paths
         | 
| 43 | 
            +
              	    names2.collect!{|name| File.expand_path(name).gsub(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR)}
         | 
| 44 | 
            +
              	    
         | 
| 45 | 
            +
              	    # make sure we aren't repeating a previous
         | 
| 46 | 
            +
                	  uniques = names2.select{|new|
         | 
| 47 | 
            +
                	    new = new.downcase if OS.windows?
         | 
| 48 | 
            +
                	    am_unique = true
         | 
| 49 | 
            +
                	    all_found.each{|old|
         | 
| 50 | 
            +
                	      old = old.downcase if OS.windows?
         | 
| 51 | 
            +
                	      if old == new
         | 
| 52 | 
            +
                	        am_unique = false
         | 
| 53 | 
            +
                	        break
         | 
| 54 | 
            +
                	      end
         | 
| 55 | 
            +
              	      }
         | 
| 56 | 
            +
              	      am_unique
         | 
| 57 | 
            +
              	    }
         | 
| 58 | 
            +
              	    
         | 
| 59 | 
            +
              	    if realtime_output
         | 
| 60 | 
            +
              	     uniques.each{ |file|
         | 
| 61 | 
            +
              	       print file
         | 
| 62 | 
            +
              	       
         | 
| 63 | 
            +
              	       if !File.executable? file
         | 
| 64 | 
            +
                         print += ' (is not executable)'
         | 
| 65 | 
            +
                       end
         | 
| 66 | 
            +
                       
         | 
| 67 | 
            +
                       if File.directory?(file)
         | 
| 68 | 
            +
                         print ' (is a directory)'
         | 
| 69 | 
            +
                       end
         | 
| 70 | 
            +
                       puts
         | 
| 71 | 
            +
                     }
         | 
| 72 | 
            +
            	      end
         | 
| 73 | 
            +
            	      
         | 
| 74 | 
            +
                    all_found += uniques        
         | 
| 37 75 |  | 
| 38 | 
            -
                    all_found += names2.collect{|name| File.expand_path(name)}
         | 
| 39 76 | 
             
                  end
         | 
| 40 77 | 
             
                end
         | 
| 41 | 
            -
             | 
| 42 | 
            -
                 | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
                  previous = {}
         | 
| 46 | 
            -
                  all_found.each {|entry|
         | 
| 47 | 
            -
                    if previous[entry.downcase] 
         | 
| 48 | 
            -
                      # do nothing
         | 
| 49 | 
            -
                    else
         | 
| 50 | 
            -
                       previous[entry.downcase] = 'ja'
         | 
| 51 | 
            -
                       unique << entry
         | 
| 52 | 
            -
                    end
         | 
| 53 | 
            -
                  }
         | 
| 54 | 
            -
                  all_found = unique
         | 
| 55 | 
            -
                else
         | 
| 56 | 
            -
                  all_found.uniq!
         | 
| 57 | 
            -
                end
         | 
| 58 | 
            -
                all_found
         | 
| 59 | 
            -
              end
         | 
| 60 | 
            -
             | 
| 61 | 
            -
              def process(names, all = false)
         | 
| 62 | 
            -
                candidates = which(names, all)
         | 
| 63 | 
            -
                if candidates == []
         | 
| 64 | 
            -
                	puts 'none found (' + names.inspect + ')'
         | 
| 65 | 
            -
                else
         | 
| 66 | 
            -
                	print output(candidates)
         | 
| 67 | 
            -
                end
         | 
| 68 | 
            -
              end
         | 
| 69 | 
            -
             | 
| 70 | 
            -
              def output all
         | 
| 71 | 
            -
                output = "higher in the list is executed first\n"
         | 
| 72 | 
            -
                for candidate in all
         | 
| 73 | 
            -
                  # might just match the name and not be executable
         | 
| 74 | 
            -
                  candidate = Dir.glob(candidate + '*')[0] if OS.windows?
         | 
| 75 | 
            -
                  output << candidate
         | 
| 76 | 
            -
                  if !File.executable? candidate
         | 
| 77 | 
            -
                    output += ' (is not executable)'
         | 
| 78 | 
            -
                    if(File.directory?(candidate))
         | 
| 79 | 
            -
                      output << ' (is a directory)'
         | 
| 80 | 
            -
                    end
         | 
| 78 | 
            +
                
         | 
| 79 | 
            +
                if realtime_output
         | 
| 80 | 
            +
                  if all_found == []
         | 
| 81 | 
            +
                	  puts 'none found (' + names.inspect + ')'
         | 
| 81 82 | 
             
                  end
         | 
| 82 | 
            -
                  output << "\n"
         | 
| 83 83 | 
             
                end
         | 
| 84 | 
            -
                output
         | 
| 85 | 
            -
              end
         | 
| 86 84 |  | 
| 85 | 
            +
                 all_found
         | 
| 86 | 
            +
              end
         | 
| 87 87 |  | 
| 88 88 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: whichr
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.3.1
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors: 
         | 
| 7 7 | 
             
            - Roger Pack
         | 
| @@ -9,7 +9,7 @@ autorequire: | |
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 11 |  | 
| 12 | 
            -
            date: 2009- | 
| 12 | 
            +
            date: 2009-12-18 00:00:00 -07:00
         | 
| 13 13 | 
             
            default_executable: 
         | 
| 14 14 | 
             
            dependencies: 
         | 
| 15 15 | 
             
            - !ruby/object:Gem::Dependency 
         |