Class: ElasticGraph::AdminLambda::RakeAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/admin_lambda/rake_adapter.rb

Defined Under Namespace

Classes: Application

Constant Summary collapse

RAKEFILE =
File.expand_path("../Rakefile", __FILE__)

Class Method Summary collapse

Class Method Details

.capture_outputObject

Captures stdout/stderr into a string so we can return it from the lambda. Inspired by a similar utility in RSpec: github.com/rspec/rspec-expectations/blob/v3.9.2/lib/rspec/matchers/built_in/output.rb#L172-L197



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/elastic_graph/admin_lambda/rake_adapter.rb', line 33

def self.capture_output
  original_stdout = $stdout.clone
  original_stderr = $stderr.clone
  captured_stream = Tempfile.new

  begin
    captured_stream.sync = true
    $stdout.reopen(captured_stream)
    $stderr.reopen(captured_stream)

    yield

    captured_stream.rewind
    captured_stream.read
  ensure
    $stdout.reopen(original_stdout)
    $stderr.reopen(original_stderr)
    captured_stream.close
    captured_stream.unlink
  end
end

.run_rake(argv) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/elastic_graph/admin_lambda/rake_adapter.rb', line 18

def self.run_rake(argv)
  capture_output do
    # We need to instantiate a new application on each invocation, because Rake is normally
    # designed to run once and exit. It keeps track of tasks that have already run and will
    # no-op when you try to run a task a 2nd or 3rd time. Using a new application instance
    # each time avoids this issue.
    ::Rake.with_application(Application.new) do |application|
      application.run(argv)
    end
  end
end