which 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/which.rb +75 -0
  2. data/test/which.rb +19 -0
  3. metadata +46 -0
data/lib/which.rb ADDED
@@ -0,0 +1,75 @@
1
+ require 'pathname'
2
+
3
+ #
4
+ # Skeleton module for the 'which' routine.
5
+ #
6
+ # Ideally, one would do this in their code to import the "which" call
7
+ # directly into their current namespace:
8
+ #
9
+ # require 'which'
10
+ # include Which
11
+ # # do something with which()
12
+ #
13
+ #
14
+ # It is recommended that you look at the documentation for the which()
15
+ # call directly for specific usage.
16
+ #
17
+ #--
18
+ #
19
+ # The compilation of software known as which.rb is distributed under the
20
+ # following terms:
21
+ # Copyright (C) 2005-2006 Erik Hollensbe. All rights reserved.
22
+ #
23
+ # Redistribution and use in source form, with or without
24
+ # modification, are permitted provided that the following conditions
25
+ # are met:
26
+ # 1. Redistributions of source code must retain the above copyright
27
+ # notice, this list of conditions and the following disclaimer.
28
+ #
29
+ # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
30
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32
+ # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
33
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35
+ # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36
+ # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37
+ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38
+ # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39
+ # SUCH DAMAGE.
40
+ #
41
+ #++
42
+
43
+ module Which
44
+
45
+ #
46
+ # Searches the path, or a provided path with given separators
47
+ # (path_sep, commonly ":") and a separator for directories (dir_sep,
48
+ # commonly "/" or "\\" on windows), will return all of the places
49
+ # that filename occurs. `filename' is included as a part of the
50
+ # output.
51
+ #
52
+ # Those familiar with the which(1) utility from UNIX will notice the
53
+ # similarities.
54
+ #
55
+
56
+ def which(filename, path=ENV["PATH"], path_sep=File::PATH_SEPARATOR, dir_sep=File::SEPARATOR)
57
+ dirs = path.split(/#{path_sep}/)
58
+
59
+ locations = []
60
+
61
+ dirs.each do |dir|
62
+ newfile = "#{dir}#{dir_sep}#{filename}"
63
+ # strip any extra dir separators
64
+ newfile.gsub!(/#{dir_sep}{2,}/, "#{dir_sep}")
65
+ p = Pathname.new(newfile)
66
+ if p.exist? and p.executable?
67
+ locations.push(newfile)
68
+ end
69
+ end
70
+
71
+ return locations
72
+ end
73
+
74
+ module_function :which
75
+ end
data/test/which.rb ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ load "lib/which.rb"
4
+ require "test/unit"
5
+
6
+ include Which
7
+
8
+ class TestWhich < Test::Unit::TestCase
9
+
10
+ def test_which
11
+ assert(which("cp").include?("/bin/cp"))
12
+ assert(which("sh").include?("/bin/sh"))
13
+ end
14
+
15
+ def test_which_custom
16
+ assert(which("cp", "/bin/").include?("/bin/cp"))
17
+ assert(which("sh", "/bin/").include?("/bin/sh"))
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: which
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2006-04-09 00:00:00 -07:00
8
+ summary: Module to emulate the 'which' utility from a unix system
9
+ require_paths:
10
+ - lib
11
+ email: erik@hollensbe.org
12
+ homepage:
13
+ rubyforge_project: shell-tools
14
+ description:
15
+ autorequire: which
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Erik Hollensbe
30
+ files:
31
+ - lib/which.rb
32
+ - test/which.rb
33
+ test_files:
34
+ - test/which.rb
35
+ rdoc_options: []
36
+
37
+ extra_rdoc_files: []
38
+
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ requirements: []
44
+
45
+ dependencies: []
46
+