Class: Subtrigger::Path

Inherits:
Object
  • Object
show all
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).

Examples:

Getting the path to an executable

Path.new.to('svn') #=> '/usr/bin'

Adding a preferred location

path = Path.new
path << '/opt/local'
path.to('svn') => '/opt/local'

Author:

  • Arjan van der Gaag

Since:

  • 0.3.0

Constant Summary collapse

DEFAULT_PATHS =

The default list of paths to look in, covering most of the use cases.

Since:

  • 0.3.0

%w{/opt/subversion/bin /usr/sbin /usr/bin}
NotFound =

Custom exception raised when a program is not found in any of the locations known.

Since:

  • 0.3.0

Class.new(Exception)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePath

Start a new list of paths, starting with the DEFAULT_PATHS

Since:

  • 0.3.0



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

#locationsObject (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.

Since:

  • 0.3.0



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.

Parameters:

  • new_path (String)

    is a new possible location of executables

Returns:

  • (Array<String>)

    the total list of paths

Since:

  • 0.3.0



52
53
54
# File 'lib/subtrigger/path.rb', line 52

def <<(new_path)
  @locations.unshift(new_path)
end

#to(program) ⇒ String

TODO:

implement memoization per argument

Scan all the known paths to find the given program.

Note: this probably only works on unix-like systems.

Parameters:

  • program (String)

    is the name of the executable to find, like svn

Returns:

  • (String)

    the correct path to this program or nil

Raises:

  • NotFoundException when the program is not found in any of the known locations

Since:

  • 0.3.0



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