Skip to content

Commit

Permalink
Add -e cmd line option
Browse files Browse the repository at this point in the history
Allows evaluating scripts inline. Ideal for small, one-off rules
  • Loading branch information
mynyml committed Feb 10, 2010
1 parent 2a82f36 commit 1d3f506
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions bin/watchr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'pathname'
require 'optparse'
require 'tempfile'

require 'watchr'

Expand All @@ -12,6 +13,8 @@ module Watchr

DEFAULT_SCRIPT_PATH = Pathname.new('specs.watchr')

attr_accessor :path

def usage
"Usage: watchr [opts] path/to/script"
end
Expand All @@ -30,6 +33,7 @@ module Watchr
#--
# raise instead?
def path!
return @path unless @path.nil?
rel = relative_path or abort( usage )
find_in_load_path(rel) or abort("no script found: file #{rel.to_s.inspect} is not in path.")
end
Expand Down Expand Up @@ -76,6 +80,20 @@ opts = OptionParser.new do |opts|
exit
}

def assert_syntax(code)
catch(:ok) { Object.new.instance_eval("BEGIN { throw :ok }; #{code}", %|-e "#{code}"|, 0) }
rescue SyntaxError => e
puts e.message.split("\n")[1]
exit
end

opts.on('-e', '--eval INLINE_SCRIPT', %|Evaluate script inline ($ watchr -e "watch('foo') { puts 'bar' }")|) {|code|
assert_syntax(code)

Tempfile.open('foo') {|f| f << code; @__path = f.path }
Watchr::Bin.path = Pathname(@__path)
}

opts.on_tail('-h', '--help', "Print inline help") { puts opts; exit }
opts.on_tail('-v', '--version', "Print version" ) { puts Watchr::Bin.version; exit }

Expand Down

0 comments on commit 1d3f506

Please sign in to comment.