bin/jspec in visionmedia-jspec-2.5.1 vs bin/jspec in visionmedia-jspec-2.6.0
- old
+ new
@@ -9,34 +9,29 @@
require 'fileutils'
RHINO = 'java org.mozilla.javascript.tools.shell.Main'
program :name, 'JSpec'
-program :version, '2.5.1'
+program :version, '2.6.0'
program :description, 'JavaScript BDD Testing Framework'
default_command :bind
command :init do |c|
c.syntax = 'jspec init [dest]'
c.summary = 'Initialize a JSpec project template'
c.description = 'Initialize a JSpec project template. Defaults to the current directory
when [dest] is not specified. The template includes several files for
running via Rhino, DOM, and the JSpec Rack server.'
c.example 'Create a directory foo, initialized with a jspec template', 'jspec init foo'
+ c.option '-R', '--rails', 'Initialize rails template'
c.when_called do |args, options|
dest = args.shift || '.'
- unless Dir[dest + '/*'].empty?
- abort unless agree "'#{dest}' is not empty; continue? "
+ if options.rails
+ initialize_rails_to dest
+ else
+ initialize_to dest
end
- template = File.join JSPEC_ROOT, 'templates', 'default', '.'
- FileUtils.mkdir_p dest
- FileUtils.cp_r template, dest
- %w( spec/spec.dom.html spec/spec.rhino.js ).each do |path|
- path = File.join dest, path
- contents = File.read(path).gsub 'JSPEC_ROOT', JSPEC_ROOT
- File.open(path, 'w') { |file| file.write contents }
- end
say "Template initialized at '#{dest}'"
end
end
command :update do |c|
@@ -44,17 +39,20 @@
c.summary = 'Update JSpec releases'
c.description = 'Update JSpec release in [paths], this will allow you to utilize
the latest JSpec features. Execute from JSpec project root without [paths] to
update the default template spec files.'
c.when_called do |args, options|
- paths = args.empty? ? %w( spec/spec.dom.html spec/spec.rhino.js ) : args
- paths.each do |path|
- next unless File.exists? path
- contents = File.read(path).gsub /visionmedia-jspec-(\d+\.\d+\.\d+)/, "visionmedia-jspec-#{program(:version)}"
- File.open(path, 'r+'){ |file| file.write contents }
- say "Updated #{path} to JSpec #{program(:version)}"
+ if args.empty?
+ if rails?
+ paths = 'jspec/spec.dom.html', 'jspec/spec.rhino.js'
+ else
+ paths = 'spec/spec.dom.html', 'spec/spec.rhino.js'
+ end
+ else
+ paths = args
end
+ update_version_in *paths
end
end
command :run do |c|
c.syntax = 'jspec run [path] [options]'
@@ -83,21 +81,27 @@
c.option '-B', '--bind', 'Auto-run specs when source files or specs are altered'
c.option '-R', '--rhino', 'Run specs using Rhino'
c.option '-S', '--server', 'Run specs using the JSpec server'
c.option '-s', '--server-only', 'Start JSpec server without running browsers'
c.when_called do |args, options|
- options.default :browsers => %w( Safari ), :paths => ['lib/**/*.js', 'spec/**/*.js']
+ # Rails
+ if rails?
+ options.default :browsers => %w( Safari ), :paths => ['public/javascripts/**/*.js', 'jspec/**/*.js']
+ else
+ options.default :browsers => %w( Safari ), :paths => ['lib/**/*.js', 'spec/**/*.js']
+ end
+
# Actions
if options.rhino
- spec = args.shift || 'spec/spec.rhino.js'
+ spec = args.shift || path_to('spec.rhino.js')
action = lambda { rhino spec }
elsif options.server
- spec = args.shift || 'spec/spec.server.html'
+ spec = args.shift || path_to('spec.server.html')
action = lambda { start_server options, spec }
else
- spec = args.shift || 'spec/spec.dom.html'
+ spec = args.shift || path_to('spec.dom.html')
action = Bind::Actions::RefreshBrowsers.new spec, *options.browsers
end
# Binding
if options.bind
@@ -108,14 +112,69 @@
end
end
end
alias_command :bind, :run, '--bind'
+def initialize_to dest
+ unless Dir[dest + '/*'].empty?
+ abort unless agree "'#{dest}' is not empty; continue? "
+ end
+ copy_template_to 'default', dest
+ replace_root_in dest, 'spec/spec.dom.html', 'spec/spec.rhino.js'
+end
+
+def initialize_rails_to dest
+ unless looks_like_rails_root?(dest)
+ abort unless agree "'#{dest}' does not look like root of a rails project; continue? "
+ end
+ copy_template_to 'rails', "#{dest}/jspec"
+ replace_root_in "#{dest}/jspec", 'spec.dom.html', 'spec.rhino.js'
+end
+
+def copy_template_to name, dest
+ FileUtils.mkdir_p dest
+ FileUtils.cp_r path_to_template(name), dest
+end
+
+def path_to_template name
+ File.join JSPEC_ROOT, 'templates', name, '.'
+end
+
+def path_to file
+ rails? ? "jspec/#{file}" : "spec/#{file}"
+end
+
def rhino file
abort "#{file} not found" unless File.exists? file
system "#{RHINO} #{file}"
end
def start_server options, spec
require 'server/server'
JSpec::Server.start options, spec
-end
\ No newline at end of file
+end
+
+def rails?
+ File.directory? 'jspec'
+end
+
+def replace_root_in dest, *paths
+ paths.each do |path|
+ path = File.join dest, path
+ contents = File.read(path).gsub 'JSPEC_ROOT', JSPEC_ROOT
+ File.open(path, 'w') { |file| file.write contents }
+ end
+end
+
+def update_version_in *paths
+ paths.each do |path|
+ next unless File.exists? path
+ contents = File.read(path).gsub /visionmedia-jspec-(\d+\.\d+\.\d+)/, "visionmedia-jspec-#{program(:version)}"
+ File.open(path, 'r+'){ |file| file.write contents }
+ say "Updated #{path}; #{$1} -> #{program(:version)}"
+ end
+ say "Finished updating JSpec"
+end
+
+def looks_like_rails_root? path = '.'
+ File.directory? "#{path}/vendor"
+end