Class: GitHooks::Action

Inherits:
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



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

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

#limitersObject (readonly)

Returns the value of attribute limiters.



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

def limiters
  @limiters
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



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

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



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

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

#config_pathObject



140
141
142
# File 'lib/githooks/action.rb', line 140

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

#lib_file(*path_components) ⇒ Object



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

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

#lib_pathObject



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

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

#limit(type) ⇒ Object



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

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

#manifestObject



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

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

#on_all_filesObject



167
168
169
# File 'lib/githooks/action.rb', line 167

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

#on_argvObject



171
172
173
# File 'lib/githooks/action.rb', line 171

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

#on_each_fileObject



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

def on_each_file
  @on = -> { manifest.collect { |file| yield file }.all? }
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/githooks/action.rb', line 124

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

#runObject

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



69
70
71
72
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
# File 'lib/githooks/action.rb', line 69

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

DSL Methods



136
137
138
# File 'lib/githooks/action.rb', line 136

def skip!
  throw :skip, true
end

#status_symbolObject



58
59
60
61
62
# File 'lib/githooks/action.rb', line 58

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



114
115
116
117
118
119
120
121
122
# File 'lib/githooks/action.rb', line 114

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/githooks/action.rb', line 99

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