From b67b38042bca54cfb39ae580ad87f8f8f062d582 Mon Sep 17 00:00:00 2001 From: Charles Lowell Date: Wed, 17 Mar 2010 08:28:02 -0500 Subject: [PATCH] Add some nice sugary sweetness to the command line. --- bin/therubyracer | 56 +---------------------- bin/v8 | 14 ++++++ lib/v8/cli.rb | 104 +++++++++++++++++++++++++++++++++++++++++++ therubyracer.gemspec | 5 +-- 4 files changed, 122 insertions(+), 57 deletions(-) create mode 100755 bin/v8 create mode 100644 lib/v8/cli.rb diff --git a/bin/therubyracer b/bin/therubyracer index 4e8d9f3..0b9a025 100755 --- a/bin/therubyracer +++ b/bin/therubyracer @@ -9,58 +9,6 @@ rescue LoadError require 'rubygems' require 'v8' end +require 'v8/cli' -trap("SIGINT") do - puts "^C" -end - -class V8::Shell - def exit(status = 0) - Kernel.exit(status) - end - - def help(*args) - <<-HELP -exit(status = 0) - exit the shell - also: quit() - -ruby(source) - evaluate some ruby source -HELP - - end - - def to_s - "[object TheRubyRacer]" - end - - def print(string) - puts string - end - - def ruby(src) - eval(src) - end - - alias_method :quit, :exit -end - -puts "help() for help" -puts "The Ruby Racer #{V8::VERSION}" -puts "Vroom Vroom!" - -V8::Context.open(:with => V8::Shell.new) do |cxt| - loop do - line = Readline.readline('therubyracer> ', true) - begin - result = cxt.eval(line) - puts(result) unless result.nil? - - rescue V8::JavascriptError => e - puts e.javascript_stacktrace - rescue StandardError => e - puts e - end - end -end +V8::CLI.run(File.basename(__FILE__), ARGV) diff --git a/bin/v8 b/bin/v8 new file mode 100755 index 0000000..0b9a025 --- /dev/null +++ b/bin/v8 @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby +lib = File.expand_path(File.dirname(__FILE__) + '/../lib') +$:.unshift(lib) if File.exists?(lib) unless $:.member?(lib) + +require 'readline' +begin + require 'v8' +rescue LoadError + require 'rubygems' + require 'v8' +end +require 'v8/cli' + +V8::CLI.run(File.basename(__FILE__), ARGV) diff --git a/lib/v8/cli.rb b/lib/v8/cli.rb new file mode 100644 index 0000000..47e372b --- /dev/null +++ b/lib/v8/cli.rb @@ -0,0 +1,104 @@ +require 'optparse' +require 'ostruct' + +module V8 + module CLI + def self.run(exename = 'v8', args = []) + options = OpenStruct.new + options.libs = [] + options.libdirs = [] + parser = OptionParser.new + parser.banner = "Usage: #{exename} [options]" + parser.on("-r", "--require FILE", "load and execute FILE as JavaScript source") {|r| options.libs << r} + parser.on("-i", "--interactive", "interactive mode") {options.interactive = true} + parser.on("-e", "--execute JAVASCRIPT", String, "evaluate JAVASCRIPT and exit") {|src| options.execute = src} + parser.on_tail("-v", "--version", "Show version and exit") {options.version_info = true} + parser.on_tail("-h", "--help", "Show this message") do + puts parser + exit + end + parser.parse!(args.dup) + if options.version_info + puts "The Ruby Racer #{V8::VERSION}" + puts "V8 Version 2.0.6" + exit + end + Context.open(:with => Shell.new) do |cxt| + for libfile in options.libs do + load(cxt,libfile) + end + if options.interactive + repl(cxt, exename) + elsif options.execute + cxt.eval(options.execute, '') + else + cxt.eval($stdin, '') + end + end + end + + def self.load(cxt, libfile) + begin + cxt.load(libfile) + rescue V8::JavascriptError => e + puts e.javascript_stacktrace + rescue StandardError => e + puts e + end + end + + def self.repl(cxt, exename) + puts "help() for help. quit() to quit." + puts "The Ruby Racer #{V8::VERSION}" + puts "Vroom Vroom!" + trap("SIGINT") do + puts "^C" + end + loop do + line = Readline.readline("#{exename}> ", true) + begin + result = cxt.eval(line, '') + puts(result) unless result.nil? + rescue V8::JavascriptError => e + puts e.javascript_stacktrace + rescue StandardError => e + puts e + end + end + end + + class Shell + def to_s + "[object Shell]" + end + + def print(string) + puts string + end + + def evalrb(src) + eval(src) + end + + def exit(status = 0) + Kernel.exit(status) + end + + alias_method :quit, :exit + + def help(*args) + <<-HELP + print(msg) + print msg to STDOUT + + exit(status = 0) + exit the shell + also: quit() + + evalrb(source) + evaluate some ruby source + HELP + end + end + end +end \ No newline at end of file diff --git a/therubyracer.gemspec b/therubyracer.gemspec index 1e8aa65..6346f95 100644 --- a/therubyracer.gemspec +++ b/therubyracer.gemspec @@ -9,11 +9,10 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Charles Lowell", "Bill Robertson"] - s.date = %q{2010-03-15} - s.default_executable = %q{therubyracer} + s.date = %q{2010-03-17} s.description = %q{Call javascript code and manipulate javascript objects from ruby. Call ruby code and manipulate ruby objects from javascript.} s.email = %q{cowboyd@thefrontside.net} - s.executables = ["therubyracer"] + s.executables = ["therubyracer", "v8"] s.extensions = ["ext/v8/extconf.rb"] s.extra_rdoc_files = [ "README.rdoc"