Module: Rails::Sh::Forkable

Includes:
Helpers
Included in:
Bundler, Rails, Rake
Defined in:
lib/rails/sh/forkable.rb

Instance Method Summary collapse

Methods included from Helpers

#lesspipe

Instance Method Details

#_invokeObject

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/rails/sh/forkable.rb', line 30

def _invoke
  raise NotImplementedError
end

#after_fork(&block) ⇒ Object



38
39
40
# File 'lib/rails/sh/forkable.rb', line 38

def after_fork(&block)
  @after_fork = block
end

#before_fork(&block) ⇒ Object



34
35
36
# File 'lib/rails/sh/forkable.rb', line 34

def before_fork(&block)
  @before_fork = block
end

#invoke(line, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rails/sh/forkable.rb', line 8

def invoke(line, options = {})
  run_before_fork
  pid = fork do
    run_after_fork

    if options[:pager]
      begin
        $stdout = StringIO.new
        _invoke(line)
      ensure
        output = $stdout.string
        lesspipe output
        $stdout = STDOUT
      end
    else
      _invoke(line)
    end

  end
  Process.waitpid(pid)
end

#run_after_fork(&block) ⇒ Object



46
47
48
# File 'lib/rails/sh/forkable.rb', line 46

def run_after_fork(&block)
  @after_fork.call if @after_fork
end

#run_before_fork(&block) ⇒ Object



42
43
44
# File 'lib/rails/sh/forkable.rb', line 42

def run_before_fork(&block)
  @before_fork.call if @before_fork
end