Class: C42::Runner

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

Constant Summary collapse

C42_HOME_FILE =
File.expand_path("~/.c42/tasks.rb").freeze

Class Method Summary collapse

Class Method Details

.run(c42file) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
58
59
60
# File 'lib/c42/runner.rb', line 9

def self.run(c42file)
  c42s = []

  if c42file.is_a?(String)
    c42file = File.expand_path(c42file).freeze
    if File.exists?(c42file)
      c42s << c42file
      fwd = Pathname.new(File.dirname(c42file))
    else
      raise "File not found: #{c42file}"
    end
  else
    fwd = Pathname.pwd
  end

  fwd.ascend do |path|
    c42s << Dir[Thor::Util.escape_globs(path)+"/tasks.rb"]
    c42s << Dir[Thor::Util.escape_globs(path)+"/tasks/*.c42.rb"].sort
    c42s << Dir[Thor::Util.escape_globs(path)+"/.c42/tasks.rb"]
    c42s << Dir[Thor::Util.escape_globs(path)+"/.c42/tasks/*.c42.rb"].sort
  end
  c42s.flatten!

  c42s << C42_HOME_FILE if File.exists?(C42_HOME_FILE)
  
  c42s.uniq!
  c42s.reverse!

  c42file = C42::C42file
  c42s.each do |file|
    content = File.binread(file)

    c42file = Class.new(c42file)

    filedir = File.dirname(file)
    sources_path = filedir
    file_pwd = filedir.gsub(/\/\.c42(\/tasks)?$/, "")

    path_configs = %{
      source_paths << "#{sources_path}"
      destination_root = "#{file_pwd}"
      class << self
        @@file_pwd = "#{file_pwd}"
      end
    };

    c42file.class_eval(path_configs);
    c42file.class_eval(content)
    break if c42file.stopped_here?
  end
  c42file.start
end