module GitHooks::SystemUtils

Public Class Methods

command(name) click to toggle source
# File lib/githooks/system_utils.rb, line 36
def command(name)
  (@commands ||= {})[name] ||= begin
    Command.new(name).tap { |cmd|
      define_method("command_#{cmd.name}") { |*args| cmd.execute(*args) }
      alias_method cmd.method, "command_#{cmd.name}"
    }
  end
end
commands(*names) click to toggle source
# File lib/githooks/system_utils.rb, line 46
def commands(*names)
  names.each { |name| command(name) }
end
find_bin(name) click to toggle source
# File lib/githooks/system_utils.rb, line 15
def find_bin(name)
  ENV['PATH'].split(/:/).collect { |path|
    Pathname.new(path) + name.to_s
  }.select { |path|
    path.exist? && path.executable?
  }.collect(&:to_s)
end
which(name) click to toggle source
# File lib/githooks/system_utils.rb, line 10
def which(name)
  find_bin(name).first
end
with_path(path) { |path| ... } click to toggle source
# File lib/githooks/system_utils.rb, line 24
def with_path(path, &_block)
  fail ArgumentError, 'Missing required block' unless block_given?
  begin
    cwd = Dir.getwd
    Dir.chdir path
    yield path
  ensure
    Dir.chdir cwd
  end
end

Private Instance Methods

command(name) click to toggle source
# File lib/githooks/system_utils.rb, line 36
def command(name)
  (@commands ||= {})[name] ||= begin
    Command.new(name).tap { |cmd|
      define_method("command_#{cmd.name}") { |*args| cmd.execute(*args) }
      alias_method cmd.method, "command_#{cmd.name}"
    }
  end
end
commands(*names) click to toggle source
# File lib/githooks/system_utils.rb, line 46
def commands(*names)
  names.each { |name| command(name) }
end
find_bin(name) click to toggle source
# File lib/githooks/system_utils.rb, line 15
def find_bin(name)
  ENV['PATH'].split(/:/).collect { |path|
    Pathname.new(path) + name.to_s
  }.select { |path|
    path.exist? && path.executable?
  }.collect(&:to_s)
end
which(name) click to toggle source
# File lib/githooks/system_utils.rb, line 10
def which(name)
  find_bin(name).first
end
with_path(path) { |path| ... } click to toggle source
# File lib/githooks/system_utils.rb, line 24
def with_path(path, &_block)
  fail ArgumentError, 'Missing required block' unless block_given?
  begin
    cwd = Dir.getwd
    Dir.chdir path
    yield path
  ensure
    Dir.chdir cwd
  end
end