Skip to content
This repository has been archived by the owner on Aug 18, 2018. It is now read-only.

Commit

Permalink
Add a bunch of improvements to bin/johnson. All of these can be speci…
Browse files Browse the repository at this point in the history
…fied

multiple times.

-I [DIRECTORY]
Adds DIRECTORY to the Ruby load path.

-i [FILE]
Pre-evaluate FILE before going into interactive mode.

-e [EXPRESSION]
Evaluate EXPRESSION and exit.
  • Loading branch information
jbarnette committed Apr 19, 2008
1 parent c27aeb6 commit 2c8606c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 7 deletions.
49 changes: 43 additions & 6 deletions bin/johnson
@@ -1,11 +1,16 @@
#!/usr/bin/env ruby

require "rubygems"
require "johnson"
begin
require "johnson"
rescue LoadError
require "rubygems"
require "johnson"
end

require "readline"

CONTEXT = cx = Johnson::Context.new
CONTEXT[:print] = lambda { |m| puts m }
CONTEXT[:alert] = lambda { |m| puts m }

EXIT_VERBS = %w(exit quit)

Expand Down Expand Up @@ -33,10 +38,37 @@ rescue Object => e
puts e.backtrace
end

def eval_in_js(expression)
rescued { puts "=> " + CONTEXT.evaluate(expression).inspect }
end

def eval_in_ruby(expression, bind_to)
rescued { puts "=> " + eval(expression, bind_to).inspect }
end

options = Johnson::CLI::Options.parse!(ARGV)
options.load_paths.each { |d| $LOAD_PATH << d }
options.files_to_preload.each { |f| CONTEXT.load(f) }

unless options.files_to_evaluate.empty?
options.files_to_evaluate.each { |f| CONTEXT.load(f) }
exit
end

unless options.expressions.empty?
options.expressions.each { |e| CONTEXT.evaluate(e) }
exit
end

loop do
input = Readline.readline("js> ", true)
break if EXIT_VERBS.include?(input)

if input =~ /^rb\s+(.+)$/
eval_in_ruby($1, local_binding)
next
end

if input == "rb"
js_readline = copy_history
paste_history(ruby_readline)
Expand All @@ -45,14 +77,19 @@ loop do
input = Readline.readline("rb> ", true)
break if input == "js"
exit if EXIT_VERBS.include?(input)

rescued { puts "=> " + eval(input, local_binding).inspect }

if input =~ /^js\s+(.+)$/
eval_in_js($1)
next
end

eval_in_ruby(input, local_binding)
end

ruby_readline = copy_history
paste_history(js_readline)
next
end

rescued { puts "=> " + CONTEXT.evaluate(input).inspect }
eval_in_js(input)
end
5 changes: 4 additions & 1 deletion lib/johnson.rb
@@ -1,6 +1,9 @@
require 'generator'
require "generator"
require "johnson/version"

# the command-line option parser
require "johnson/cli/options"

# the native SpiderMonkey extension
require "johnson/spidermonkey"

Expand Down
55 changes: 55 additions & 0 deletions lib/johnson/cli/options.rb
@@ -0,0 +1,55 @@
require "optparse"

module Johnson
module CLI
class Options
class << self
alias_method :parse!, :new
end

attr_reader :expressions, :load_paths, :files_to_preload, :files_to_evaluate

def initialize(*args)
argv = args.flatten
@expressions = []
@load_paths = []
@files_to_preload = []
@files_to_evaluate = []

parser = OptionParser.new do |parser|
parser.banner = "Usage: johnson [options] [files...]"
parser.version = Johnson::VERSION

parser.on("-e [EXPRESSION]", "Evaluate [EXPRESSION] and exit") do |expression|
@expressions << expression
end

parser.on("-I [DIRECTORY]", "Specify $LOAD_PATH directories") do |dir|
@load_paths << dir
end

parser.on("-i [FILE]", "Evaluate [FILE] before interaction") do |file|
@files_to_preload << file
end

parser.on("-i [FILE]", "Evaluate [FILE] before interaction") do |file|
@files_to_preload << file
end

parser.on("-h", "-?", "--help", "Show this message") do
puts parser
exit
end

parser.on("-v", "--version", "Show Johnson's version (#{Johnson::VERSION})") do
puts Johnson::VERSION
exit
end
end

parser.parse!(argv)
argv.each { |f| @files_to_evaluate << f }
end
end
end
end
4 changes: 4 additions & 0 deletions lib/johnson/context.rb
Expand Up @@ -23,5 +23,9 @@ def evaluate(expression)
def global
delegate.global
end

def load(file)
delegate.evaluate(IO.read(file))
end
end
end

0 comments on commit 2c8606c

Please sign in to comment.