Module: ScoutRake

Defined in:
lib/scout/resource/produce/rake.rb

Defined Under Namespace

Classes: TaskNotFound

Class Method Summary collapse

Class Method Details

.run(rakefile, dir, task, &block) ⇒ Object



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
61
62
63
64
65
66
67
68
# File 'lib/scout/resource/produce/rake.rb', line 27

def self.run(rakefile, dir, task, &block)
  old_pwd = FileUtils.pwd

  Rake::Task.clear
  Rake::FileTask.clear_files

  t = nil
  pid = Process.fork{
    if block_given?
      TOPLEVEL_BINDING.receiver.instance_exec &block
    else
      if Path.is_filename? rakefile
        rakefile = rakefile.produce.find
        load rakefile
      else
        TmpFile.with_file(rakefile) do |tmpfile|
          load tmpfile
        end
      end
    end

    raise TaskNotFound if Rake::Task[task].nil?

    #Misc.pre_fork
    begin
      Misc.in_dir(dir) do
        Rake::Task[task].invoke

        Rake::Task.clear
        Rake::FileTask.clear_files
      end
    rescue Exception
      Log.error "Error in rake: #{$!.message}"
      Log.exception $!
      Kernel.exit! -1
    end
    Kernel.exit! 0
  }
  Process.waitpid(pid)
  raise "Rake failed" unless $?.success?

end