Module: Rake::Sh

Defined in:
lib/rake/sh.rb,
lib/rake/sh/command.rb

Defined Under Namespace

Modules: Command

Class Method Summary collapse

Class Method Details

.execute(line) ⇒ Object



29
30
31
32
33
34
35
36
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



57
58
59
60
61
# 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_initObject



48
49
50
51
52
53
54
55
# 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_readlineObject



38
39
40
41
42
43
44
45
46
# 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 = []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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