class GitHooks::Action

Attributes

benchmark[R]
errors[R]
on[R]
section[R]
success[R]
success?[R]
title[R]
warnings[R]

Public Class Methods

new(title, section, &block) click to toggle source
# File lib/githooks/action.rb, line 31
def initialize(title, section, &block)
  fail ArgumentError, 'Missing required block' unless block_given?

  @title     = title
  @section   = section
  @on        = nil
  @limiters  = {}
  @success   = true
  @errors    = []
  @warnings  = []
  @benchmark = 0

  instance_eval(&block)

  waiting!
end

Public Instance Methods

colored_title() click to toggle source
# File lib/githooks/action.rb, line 56
def colored_title
  return title.color_skipped! if skipped?
  return title.color_unknown! unless finished?
  success? ? title.color_success! : title.color_failure!
end
config_file(*path_components) click to toggle source
# File lib/githooks/action.rb, line 151
def config_file(*path_components)
  config_path.join(*path_components)
end
config_path() click to toggle source
# File lib/githooks/action.rb, line 147
def config_path
  GitHooks.hooks_root.join('configs')
end
lib_file(*path_components) click to toggle source
# File lib/githooks/action.rb, line 159
def lib_file(*path_components)
  lib_path.join(*path_components)
end
lib_path() click to toggle source
# File lib/githooks/action.rb, line 155
def lib_path
  GitHooks.hooks_root.join('lib')
end
limit(type) click to toggle source
# File lib/githooks/action.rb, line 163
def limit(type)
  unless @limiters.include? type
    @limiters[type] ||= Repository::Limiter.new(type)
  end
  @limiters[type]
end
limiters() click to toggle source
# File lib/githooks/action.rb, line 48
def limiters
  section.limiters.merge(@limiters)
end
manifest() click to toggle source
# File lib/githooks/action.rb, line 52
def manifest
  @manifest ||= section.hook.manifest.filter(limiters)
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/githooks/action.rb, line 132
def method_missing(method, *args, &block)
  command = section.hook.find_command(method)
  return super unless command
  run_command(command, *args, &block)
end
on_all_files() { |manifest| ... } click to toggle source
# File lib/githooks/action.rb, line 174
def on_all_files
  @on = -> { yield manifest }
end
on_argv() { |hook.args| ... } click to toggle source
# File lib/githooks/action.rb, line 178
def on_argv
  @on = -> { yield section.hook.args }
end
on_each_file() { |file| ... } click to toggle source
# File lib/githooks/action.rb, line 170
def on_each_file
  @on = -> { manifest.collect { |file| yield file }.all? }
end
respond_to_missing?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/githooks/action.rb, line 128
def respond_to_missing?(method, include_private = false)
  section.hook.find_command(method) || super
end
run() click to toggle source
# File lib/githooks/action.rb, line 73
def run # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  running!
  with_benchmark do
    with_captured_output {
      begin
        was_skipped = catch(:skip) do
          @success &= @on.call
          # was_skipped gets set to the return value of the block
          # which we want to be false unless `throw :skip` is called
          false
        end
        return @success
      rescue StandardError => e
        $stderr.puts "Exception thrown during action call: #{e.class.name}: #{e.message}"
        if GitHooks.debug?
          $stderr.puts "#{e.class}: #{e.message}:\n\t#{e.backtrace.join("\n\t")}"
        else
          hooks_files = e.backtrace.select! { |line| line =~ %r{/hooks/} }
          hooks_files.collect! { |line| line.split(':')[0..1].join(':') }
          $stderr.puts "  -> in hook file:line, #{hooks_files.join("\n\t")}" unless hooks_files.empty?
        end
        @success = false
      ensure
        STDERR.puts "WAS_SKIPPED? -> #{was_skipped.inspect} (#{@status.inspect})" if GitHooks.debug?
        was_skipped ? skipped! : finished!
      end
    }
  end
end
skip!() click to toggle source

FIXME: these should be switched to behaviors that are included into this classs

# File lib/githooks/action.rb, line 143
def skip!
  throw :skip, true
end
status_symbol() click to toggle source
# File lib/githooks/action.rb, line 62
def status_symbol
  return GitHooks::SKIPPED_SYMBOL if skipped?
  return GitHooks::UNKNOWN_SYMBOL unless finished?
  success? ? GitHooks::SUCCESS_SYMBOL : GitHooks::FAILURE_SYMBOL
end
with_benchmark() { || ... } click to toggle source
# File lib/githooks/action.rb, line 118
def with_benchmark(&_block)
  fail ArgumentError, 'expected block, none given' unless block_given?
  begin
    start_time = Time.now
    yield
  ensure
    @benchmark = Time.now - start_time
  end
end
with_captured_output() { || ... } click to toggle source
# File lib/githooks/action.rb, line 103
def with_captured_output(&_block)
  fail ArgumentError, 'expected block, none given' unless block_given?

  begin
    $stdout = warnings = StringIO.new
    $stderr = errors   = StringIO.new
    yield
  ensure
    @errors   = errors.rewind && errors.read.split(/\n/)
    @warnings = warnings.rewind && warnings.read.split(/\n/)
    $stdout   = STDOUT
    $stderr   = STDERR
  end
end

Private Instance Methods

run_command(command, *args, &block) click to toggle source
# File lib/githooks/action.rb, line 188
def run_command(command, *args, &block)
  prefix = nil
  args.extract_options.tap { |options|
    prefix = options.delete(:prefix_output)
  }

  result = command.execute(*args, &block)
  result.output_lines(prefix).each { |line| $stdout.puts line }
  result.error_lines(prefix).each { |line| $stderr.puts line }
  result.status.success?
end