Class: Cch::Runner

Inherits:
Object
  • Object
show all
Includes:
Commands::FileSystem, Commands::Shell
Defined in:
lib/cch/runner.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Commands::FileSystem

#filter_files

Methods included from Commands::Shell

#backtiq_command, #system_command

Constructor Details

#initialize(name, options = {}) ⇒ Runner

Returns a new instance of Runner.



37
38
39
40
41
42
# File 'lib/cch/runner.rb', line 37

def initialize(name, options = {})
  @name = name
  @command = "bundle exec #{options[:gem] || name} %{files}"
  @on = false
  @patterns = {}
end

Class Attribute Details

.runnersObject

Returns the value of attribute runners.



7
8
9
# File 'lib/cch/runner.rb', line 7

def runners
  @runners
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



35
36
37
# File 'lib/cch/runner.rb', line 35

def command
  @command
end

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/cch/runner.rb', line 34

def name
  @name
end

#onObject

Returns the value of attribute on.



35
36
37
# File 'lib/cch/runner.rb', line 35

def on
  @on
end

#patternsObject (readonly)

Returns the value of attribute patterns.



34
35
36
# File 'lib/cch/runner.rb', line 34

def patterns
  @patterns
end

Class Method Details

.allObject



19
20
21
# File 'lib/cch/runner.rb', line 19

def all
  runners.values
end

.configure(runner, options = {}) {|@runners.fetch(runner)| ... } ⇒ Object

Yields:



9
10
11
12
13
# File 'lib/cch/runner.rb', line 9

def configure(runner, options = {})
  @runners ||= {}
  @runners[runner] = Runner.new(runner, options)
  yield @runners.fetch(runner) if block_given?
end

.run(runners) ⇒ Object



15
16
17
# File 'lib/cch/runner.rb', line 15

def run(runners)
  Array(runners).each { |runner| @runners.fetch(runner).on = true }
end

.where(query = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/cch/runner.rb', line 23

def where(query = {})
  runners = all
  runners = runners.select(&:on) if query[:on?]
  if (names = [query[:name]].flatten.compact).any?
    runners = runners.select { |r| names.include?(r.name.to_s) }
  end

  runners
end

Instance Method Details

#run(files) ⇒ Object



48
49
50
51
52
53
# File 'lib/cch/runner.rb', line 48

def run(files)
  files = filter_files(files, patterns)
  return unless run?(files)

  system_command(command % { files: files.join(' ') })
end

#run?(files) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/cch/runner.rb', line 55

def run?(files)
  message = "running #{name.to_s.color(:black, :green)}"
  message << " for #{files.size.to_s.color(:yellow)} files=#{files}"
  Cch.logger.info(message)
  files.any?
end

#watch(pattern, transform = nil) ⇒ Object



44
45
46
# File 'lib/cch/runner.rb', line 44

def watch(pattern, transform = nil)
  @patterns[pattern] = transform
end