Class: KaiserRuby::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/kaiser_ruby/cli.rb

Instance Method Summary collapse

Instance Method Details

#execute(filename) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/kaiser_ruby/cli.rb', line 29

def execute(filename)
  file = File.read filename
  output = KaiserRuby.transpile(file)

  eval output
  say
end

#rockObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kaiser_ruby/cli.rb', line 39

def rock
  say "Type 'exit' to exit the console. Otherwise, rock on!"

  # grab the outside block's binding, so that we can use it to eval code
  # this makes it not lose local variables throughout the loop
  b = binding

  begin
    input = ask('\m/>')
    if input != 'exit'
      code = KaiserRuby.transpile(input)
      say "\\m/> #{code}", :blue if options[:debug]
      output = b.eval(code)
      output = 'nil' if output.nil?
      say "  => #{output}\n"
    end
  end until input == 'exit'
end

#transpile(filename) ⇒ Object



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

def transpile(filename)
  file = File.read filename
  output = KaiserRuby.transpile(file)

  if options['show-source'.to_sym]
    say file
    say "-" * 40, :green
  end

  if options[:save]
    out = File.new(options[:save], 'w')
    out.write output
    out.close
    say "Saved output in `#{options[:save]}`", :green
  else
    say output
  end
  say
end