Permalink
Browse files

Add -e cmd line option

Allows evaluating scripts inline. Ideal for small, one-off rules
  • Loading branch information...
1 parent 2a82f36 commit 1d3f506c9d8ccf92f28e1e8fd16bd44aa954448a @mynyml committed Feb 10, 2010
Showing with 18 additions and 0 deletions.
  1. +18 −0 bin/watchr
View
@@ -2,6 +2,7 @@
require 'pathname'
require 'optparse'
+require 'tempfile'
require 'watchr'
@@ -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
@@ -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
@@ -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 }

0 comments on commit 1d3f506

Please sign in to comment.