bin/code in code-ruby-0.4.0 vs bin/code in code-ruby-0.5.0

- old
+ new

@@ -1,51 +1,76 @@ #!/usr/bin/env ruby require "optparse" require_relative "../lib/template-ruby" -options = { timeout: 0 } +options = { timeout: 0, profile: false } -OptionParser.new do |opts| - opts.banner = "Usage: bin/code [options]" +OptionParser + .new do |opts| + opts.banner = "Usage: bin/code [options]" - opts.on( - "-i INPUT", - "--input=INPUT", - "Input in the code language (String or File)" - ) do |input| - if File.exists?(input) - input = File.read(input) + opts.on( + "-i INPUT", + "--input=INPUT", + "Input in the code language (String or File)" + ) do |input| + input = File.read(input) if File.exists?(input) + + options[:input] = input end - options[:input] = input - end + opts.on( + "-c CONTEXT", + "--context=CONTEXT", + "Context in the code language (String or File)" + ) do |context| + context = File.read(context) if File.exists?(context) - opts.on( - "-c CONTEXT", - "--context=CONTEXT", - "Context in the code language (String or File)" - ) do |context| - if File.exists?(context) - context = File.read(context) + options[:context] = context end - options[:context] = context - end + opts.on("-p", "--parse", "Get parser results for input") do |parse| + options[:parse] = parse + end - opts.on("-p", "--parse", "Get parser results for input") do |parse| - options[:parse] = parse - end + opts.on( + "-t TIMEOUT", + "--timeout=TIMEOUT", + "Set timeout in seconds" + ) { |timeout| options[:timeout] = timeout.to_f } - opts.on("-t TIMEOUT", "--timeout=TIMEOUT", "Set timeout in seconds") do |timeout| - options[:timeout] = timeout.to_f + opts.on( + "--profile", + "Profile Ruby code" + ) do |timeout| + require "ruby-prof" + options[:profile] = true + end end -end.parse! + .parse! input = options.fetch(:input, "") context = options.fetch(:context, "") +if options[:profile] + RubyProf.start +end + if options[:parse] - pp ::Code::Parser::Code.new.parse(input) + pp ::Code::Parser.parse(input).to_raw else - print Code.evaluate(input, context, io: $stdout, timeout: options[:timeout]).to_s + print( + Code.evaluate( + input, + context, + io: $stdout, + timeout: options[:timeout] + ).to_s + ) +end + +if options[:profile] + result = RubyProf.stop + printer = RubyProf::FlatPrinter.new(result) + printer.print($stdout) end