Class: GitHooks::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/githooks/action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, section, &block) ⇒ Action

Returns a new instance of Action.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



132
133
134
135
136
# 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

Instance Attribute Details

#benchmarkObject (readonly)

Returns the value of attribute benchmark.



27
28
29
# File 'lib/githooks/action.rb', line 27

def benchmark
  @benchmark
end

#errorsObject (readonly)

Returns the value of attribute errors.



27
28
29
# File 'lib/githooks/action.rb', line 27

def errors
  @errors
end

#on(*args) ⇒ Object (readonly)

Returns the value of attribute on.



26
27
28
# File 'lib/githooks/action.rb', line 26

def on
  @on
end

#successObject (readonly) Also known as: success?

Returns the value of attribute success.



27
28
29
# File 'lib/githooks/action.rb', line 27

def success
  @success
end

#titleObject (readonly)

Returns the value of attribute title.



26
27
28
# File 'lib/githooks/action.rb', line 26

def title
  @title
end

#warningsObject (readonly)

Returns the value of attribute warnings.



27
28
29
# File 'lib/githooks/action.rb', line 27

def warnings
  @warnings
end

Instance Method Details

#colored_titleObject



56
57
58
59
60
# 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) ⇒ Object



151
152
153
# File 'lib/githooks/action.rb', line 151

def config_file(*path_components)
  config_path.join(*path_components)
end

#config_pathObject



147
148
149
# File 'lib/githooks/action.rb', line 147

def config_path
  GitHooks.hooks_root.join('configs')
end

#lib_file(*path_components) ⇒ Object



159
160
161
# File 'lib/githooks/action.rb', line 159

def lib_file(*path_components)
  lib_path.join(*path_components)
end

#lib_pathObject



155
156
157
# File 'lib/githooks/action.rb', line 155

def lib_path
  GitHooks.hooks_root.join('lib')
end

#limit(type) ⇒ Object



163
164
165
166
167
168
# File 'lib/githooks/action.rb', line 163

def limit(type)
  unless @limiters.include? type
    @limiters[type] ||= Repository::Limiter.new(type)
  end
  @limiters[type]
end

#limitersObject



48
49
50
# File 'lib/githooks/action.rb', line 48

def limiters
  section.limiters.merge(@limiters)
end

#manifestObject



52
53
54
# File 'lib/githooks/action.rb', line 52

def manifest
  @manifest ||= section.hook.manifest.filter(limiters)
end

#on_all_filesObject



174
175
176
# File 'lib/githooks/action.rb', line 174

def on_all_files
  @on = -> { yield manifest }
end

#on_argvObject



178
179
180
# File 'lib/githooks/action.rb', line 178

def on_argv
  @on = -> { yield section.hook.args }
end

#on_each_fileObject



170
171
172
# 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) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/githooks/action.rb', line 128

def respond_to_missing?(method, include_private = false)
  section.hook.find_command(method) || super
end

#runObject

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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!Object

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



143
144
145
# File 'lib/githooks/action.rb', line 143

def skip!
  throw :skip, true
end

#status_symbolObject



62
63
64
65
66
# 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(&_block) ⇒ Object



118
119
120
121
122
123
124
125
126
# 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(&_block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# 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