Class: IMW::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/imw/runner.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :requires  => [],
  :selectors => [],
  :dry_run   => false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Runner

Returns a new instance of Runner.



18
19
20
21
22
# File 'lib/imw/runner.rb', line 18

def initialize *args
  @args    = args
  @options = DEFAULT_OPTIONS.dup
  parser.parse!(args)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



16
17
18
# File 'lib/imw/runner.rb', line 16

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/imw/runner.rb', line 16

def options
  @options
end

Instance Method Details

#datasetsObject



88
89
90
# File 'lib/imw/runner.rb', line 88

def datasets
  handles.map { |handle| IMW.repository[handle] }
end

#handlesObject



78
79
80
81
82
83
84
85
86
# File 'lib/imw/runner.rb', line 78

def handles
  if options[:selectors].blank?
    IMW.repository.keys.sort
  else
    IMW.repository.handles.map do |handle|
      handle if options[:selectors].all? { |selector| handle.to_s =~ Regexp.new(selector) }
    end.compact.sort
  end
end

#list!Object



92
93
94
95
# File 'lib/imw/runner.rb', line 92

def list!
  puts handles
  exit
end

#parserObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/imw/runner.rb', line 24

def parser
  OptionParser.new do |opts|
    opts.banner = "usage: imw [OPTIONS] TASK"
    opts.separator <<EOF

  Run TASK for all datasets in the repository.  IMW will read any
  *.imw files in the current directory by default.

  Options include

EOF

    opts.on('-v', '--verbose', "Print verbose output") do
      IMW.verbose = true      # class level, see IMW::Runner.verbose?
    end

    opts.on('-d', '--skip-dependencies', "Execute given tasks without invoking dependencies first") do
      options[:execute] = true
    end

    opts.on('-l', '--list', "List datasets in repository") do
      options[:list] = true
    end

    opts.on('-s', '--selector SELECTOR', "Filter datasets by regexp SELECTOR.  Can be given more than once.") do |selector|
      options[:selectors] << selector
    end
    
    opts.on('-r', '--require PATH', "Require PATH.  Can be given more than once.") do |path|
      options[:requires] << path
    end

  end
end

#require_filesObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/imw/runner.rb', line 59

def require_files
  Dir['*.imw'].each { |path| load File.expand_path(path) }
  Dir['*.rb'].each { |path| require path.gsub(/\.rb$/,'') }
  options[:requires].each do |path|
    IMW.open(path) do |requireable|
      if requireable.directory?
        requireable["**/*.rb"].each  { |file| require file }
        requireable["**/*.imw"].each { |file| load    file }
      else
        require requireable.path
      end
    end
  end
end

#run!Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/imw/runner.rb', line 104

def run!
  require_files
  case
  when options[:list]
    list!
  when task.blank?
    puts parser
    exit 1
  else
    run_task!
  end
end

#run_task!Object



97
98
99
100
101
102
# File 'lib/imw/runner.rb', line 97

def run_task!
  datasets.each do |dataset|
    dataset[task].send(options[:execute] ? :execute : :invoke)
  end
  exit
end

#taskObject



74
75
76
# File 'lib/imw/runner.rb', line 74

def task
  args.first
end