Class: Subtrigger::Path
- Inherits:
-
Object
- Object
- Subtrigger::Path
- Defined in:
- lib/subtrigger/path.rb
Overview
Our own little implementation of the path, allowing us to look up the location of executable files. This is because Subversion hooks run in a clean environment, without any environment variables such as $PATH. We therefore need to run (for example) /usr/bin/svn update rather than svn update.
There is a list of default locations that will be searched, but you may add your own if you want to. This is useful if you’ve got a custom installation on your machine you want to use.
Note: testing whether an executable exists in a given path is done using the unix program test, which will most likely not work on windows machines (untested).
Constant Summary collapse
- DEFAULT_PATHS =
The default list of paths to look in, covering most of the use cases.
%w{/opt/subversion/bin /usr/sbin /usr/bin}
- NotFound =
Custom exception raised when a program is not found in any of the locations known.
Class.new(Exception)
Instance Attribute Summary collapse
-
#locations ⇒ Object
readonly
A list of absolute paths on te filesystems to where the svn executables might be located.
Instance Method Summary collapse
-
#<<(new_path) ⇒ Array<String>
Add a new path to the stack before the existing ones.
-
#initialize ⇒ Path
constructor
Start a new list of paths, starting with the
DEFAULT_PATHS. -
#to(program) ⇒ String
Scan all the known paths to find the given program.
Constructor Details
#initialize ⇒ Path
Start a new list of paths, starting with the DEFAULT_PATHS
41 42 43 44 45 46 |
# File 'lib/subtrigger/path.rb', line 41 def initialize @locations = DEFAULT_PATHS.dup # use a copy to prevent global state @exists = Hash.new do |hash, p| hash[p] = system('test -x ' + p) end end |
Instance Attribute Details
#locations ⇒ Object (readonly)
A list of absolute paths on te filesystems to where the svn executables might be located. These are scanned in order to find the executables to use.
38 39 40 |
# File 'lib/subtrigger/path.rb', line 38 def locations @locations end |
Instance Method Details
#<<(new_path) ⇒ Array<String>
Add a new path to the stack before the existing ones.
52 53 54 |
# File 'lib/subtrigger/path.rb', line 52 def <<(new_path) @locations.unshift(new_path) end |
#to(program) ⇒ String
implement memoization per argument
Scan all the known paths to find the given program.
Note: this probably only works on unix-like systems.
66 67 68 69 70 |
# File 'lib/subtrigger/path.rb', line 66 def to(program) location = locations.find { |path| exists? File.join(path, program) } raise NotFound.new(program) unless location location end |