Class: Texas::Runner

Inherits:
Object
  • Object
show all
Includes:
OutputHelper
Defined in:
lib/texas/runner.rb

Overview

An instance of Texas::Runner is used to run a build process.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OutputHelper

#trace, #verbose, #warning

Constructor Details

#initialize(args = nil) ⇒ Runner

Initializes the Runner with an array or hash of options.

Example:

Texas::Runner.new(%w(-w --no-color))
Texas::Runner.new(:warnings => true, :colors => false)


18
19
20
21
22
23
24
25
26
27
# File 'lib/texas/runner.rb', line 18

def initialize(args = nil)
  @options = options_from_args args
  extend_string_class
  Texas.verbose = @options.verbose
  Texas.warnings = @options.warnings
  load_local_libs if @options.load_local_libs
  verbose { TraceInfo.new(:starting, task_class, :magenta) }
  @task_instance = task_class.new(@options)
  run
end

Instance Attribute Details

#task_instanceObject (readonly)

Returns the instance of the performed task. Normally a Texas::Build::Base derived object.



10
11
12
# File 'lib/texas/runner.rb', line 10

def task_instance
  @task_instance
end

Instance Method Details

#extend_string_classObject

Extends String with Term::ANSIColor.



31
32
33
34
# File 'lib/texas/runner.rb', line 31

def extend_string_class
  String.send(:include, Term::ANSIColor)
  Term::ANSIColor::coloring = @options.colors
end

#fallback_task_classObject



67
68
69
70
71
72
73
74
75
# File 'lib/texas/runner.rb', line 67

def fallback_task_class 
  class_name = @options.task.to_s.split('_').map(&:capitalize).join
  begin
    eval("::Texas::Task::#{class_name}")
  rescue
    trace "Failed to fallback for Texas::Task::#{class_name}"
    exit
  end
end

#load_local_libsObject

Load lib/init.rb if present in current project.



38
39
40
41
# File 'lib/texas/runner.rb', line 38

def load_local_libs
  init_file = File.join(@options.work_dir, "lib", "init.rb")
  load init_file if File.exist?(init_file)
end

#options_from_args(args) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/texas/runner.rb', line 43

def options_from_args(args)
  if args.is_a?(Hash)
    opts = Texas::OptionParser.new([]).parse
    args.each { |k, v| opts.send("#{k}=", v) }
    opts
  else
    Texas::OptionParser.new(args).parse
  end
end

#runObject



53
54
55
# File 'lib/texas/runner.rb', line 53

def run
  Build.run_with_nice_errors(@task_instance) { exit 1 }
end

#task_classObject

Returns the class for the given task.



59
60
61
62
63
64
65
# File 'lib/texas/runner.rb', line 59

def task_class
  map = {
    :build      => Build::Final,
    :dry        => Build::Dry,
  }
  map[@options.task] || fallback_task_class
end