Class: KaiserRuby::CLI

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

Overview

command line interface for kaiser-ruby command

Instance Method Summary collapse

Instance Method Details

#execute(filename) ⇒ Object



51
52
53
54
55
56
# File 'lib/kaiser_ruby/cli.rb', line 51

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

#parse(filename) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/kaiser_ruby/cli.rb', line 12

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

  puts output
  say
end

#rockObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kaiser_ruby/cli.rb', line 60

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

  loop do
    begin
      input = ask('\m/>')
      break 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"
    rescue Exception => e
      say "THE STAGE IS ON FIRE! #{e}: #{e.message}"
    end
  end
end

#transpile(filename) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kaiser_ruby/cli.rb', line 23

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 <<~REQ
      require 'kaiser_ruby/refinements'
      using KaiserRuby::Refinements

    REQ
    out.write output
    out.close
    say "Saved output in `#{options[:save]}`", :green
  else
    say output
  end
  say
end