module Rake::Sh

Public Class Methods

execute(line) click to toggle source
# File lib/rake/sh.rb, line 29
def execute(line)
  if command = Command.find(line)
    arg = line.split(/\s+/, 2)[1] rescue nil
    command.call(arg)
  else
    invoke(line.strip)
  end
end
invoke(line) click to toggle source
# File lib/rake/sh.rb, line 63
def invoke(line)
  start = Time.now

  name, *args = line.split(/\s+/)
  pid = fork do
    args.each do |arg|
      env, value = arg.split('=')
      next unless env && !env.empty? && value && !value.empty?
      ENV[env] = value
    end
    Rake.application[name].invoke
  end
  Process.waitpid(pid)

  puts "\e[34m#{Time.now - start}sec\e[0m"
end
invoke_eager_tasks(name) click to toggle source
# File lib/rake/sh.rb, line 57
def invoke_eager_tasks(name)
  Rake.application[name].invoke
  puts "\e[44mInvoke task eagerly: `#{name}`\e[0m"
rescue
end
rake_init() click to toggle source
# File lib/rake/sh.rb, line 48
def rake_init
  $stdout = StringIO.new
  Rake.application = Rake::Application.new
  Rake.application.init
  Rake.application.load_rakefile
ensure
  $stdout = STDOUT
end
setup_readline() click to toggle source
# File lib/rake/sh.rb, line 38
def setup_readline
  Readline.basic_word_break_characters = ""
  Readline.completion_proc = lambda do |word|
    (
      Command.command_names.map { |name| name.to_s } +
      Rake.application.tasks.map{ |t| t.name }
    ).grep(/#{Regexp.quote(word)}/)
  end
end
start(eager_tasks = []) click to toggle source
# File lib/rake/sh.rb, line 10
def start(eager_tasks = [])
  rake_init
  eager_tasks.each { |task| invoke_eager_tasks(task) }
  setup_readline

  while buf = Readline.readline("rake> ", true)
    line = buf.strip
    next if line.empty?
    begin
      execute(line)
    rescue SystemExit
      raise
    rescue Exception => e
      puts "\e[41m#{e.message}\n#{e.backtrace.join("\n")}\e[0m"
    end
    setup_readline
  end
end