Skip to content

Instantly share code, notes, and snippets.

@netzpirat
Created September 9, 2010 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netzpirat/571916 to your computer and use it in GitHub Desktop.
Save netzpirat/571916 to your computer and use it in GitHub Desktop.
Generate jspec run configurations (dom.html, rhino.js and server.html) out of a central configuration file with file globbing!
#
# Rake task for generating the different jspec run configuration from
# a single, central configuration file that supports file globbing
#
# Author: Michael Kessler aka. netzpirat
# License: MIT
#
namespace :jspec do
desc 'Generate the run configurations for all environments'
task :config do
require 'yaml'
# Read configuration file
config = YAML::load_file(File.expand_path(File.dirname(__FILE__) + '/../../config/jspec.yml'))
# Get actual gem lib path
spec = Bundler.load.specs.find{|s| s.name == 'jspec' }
jspec_lib_path = spec.full_gem_path + '/lib'
#
# DOM configuration file
#
# Get jspec library files
processed = []
jspecs = config['dom']['jspec'].inject('') do |result, element|
Dir.glob("#{ jspec_lib_path }/#{ element }.js").each do |f|
if !processed.include?(f)
result += " <script src=\"#{ f }\"></script>\n"
processed << f
end
end
result
end
# Get users javascripts
processed = []
scripts = config['dom']['scripts'].inject('') do |result, element|
base_dir = File.expand_path(File.dirname(__FILE__) + '/../../public/javascripts')
Dir.glob("#{ base_dir }/#{ element }.js").each do |f|
relative_file = Pathname.new(File.new(f).path).relative_path_from(Pathname.new(base_dir))
if !processed.include?(relative_file)
result += " <script src=\"../public/javascripts/#{ relative_file }\"></script>\n"
processed << relative_file
end
end
result
end
# Get jspec test files
processed = []
specs = config['dom']['specs'].inject('') do |result, element|
base_dir = File.expand_path(File.dirname(__FILE__) + '/../../jspec')
Dir.glob("#{ base_dir }/#{ element }.js").each do |f|
relative_file = Pathname.new(File.new(f).path).relative_path_from(Pathname.new(base_dir))
if !processed.include?(relative_file)
result += " .exec('#{ relative_file }')\n"
processed << relative_file
end
end
result
end.gsub(/\n$/, '')
# Get the run parameters
params = config['dom']['params'].inject('') do |result, element|
result += "#{ element[0] }: #{ element[1] }, "
end.gsub(/, $/, '')
dom = <<-DOM
<!-- DO NOT EDIT THIS GENERATED FILE - EDIT config/jspec.yml INSTEAD! -->
<html>
<head>
<title>JSpec Testrunner</title>
<link type="text/css" rel="stylesheet" href="#{ jspec_lib_path }/jspec.css" />
#{ scripts }
#{ jspecs }
<script>
function runSuites() {
JSpec
#{ specs }
.run({ #{ params } })
.report()
}
</script>
</head>
<body class="jspec" onLoad="runSuites();">
<div id="jspec-top"><h2 id="jspec-title">JSpec <em><script>document.write(JSpec.version)</script></em></h2></div>
<div id="jspec"></div>
<div id="jspec-bottom"></div>
</body>
</html>
DOM
puts "Writing dom.html"
File.open(File.expand_path(File.dirname(__FILE__) + '/../../jspec/dom.html'), 'w') { |f| f.write(dom.gsub('.run({ })','.run()')) }
#
# RHINO configuration file
#
# Get jspec library files
processed = []
jspecs = config['rhino']['jspec'].inject('') do |result, element|
Dir.glob("#{ jspec_lib_path }/#{ element }.js").each do |f|
if !processed.include?(f)
result += "load('#{ f }')\n"
processed << f
end
end
result
end
# Get users javascripts
processed = []
scripts = config['rhino']['scripts'].inject('') do |result, element|
base_dir = File.expand_path(File.dirname(__FILE__) + '/../../public/javascripts')
Dir.glob("#{ base_dir }/#{ element }.js").each do |f|
relative_file = Pathname.new(File.new(f).path).relative_path_from(Pathname.new(base_dir))
if !processed.include?(relative_file)
result += "load('public/javascripts/#{ relative_file }')\n"
processed << relative_file
end
end
result
end
# Get jspec test files
processed = []
specs = config['rhino']['specs'].inject('') do |result, element|
base_dir = File.expand_path(File.dirname(__FILE__) + '/../../jspec')
Dir.glob("#{ base_dir }/#{ element }.js").each do |f|
relative_file = Pathname.new(File.new(f).path).relative_path_from(Pathname.new(base_dir))
if !processed.include?(relative_file)
result += ".exec('jspec/#{ relative_file }')\n"
processed << relative_file
end
end
result
end.gsub(/\n$/, '')
# Get the run parameters
params = config['rhino']['params'].inject('') do |result, element|
result += "#{ element[0] }: #{ element[1] }, "
end.gsub(/, $/, '')
rhino = <<-RHINO
// DO NOT EDIT THIS GENERATED FILE - EDIT config/jspec.yml INSTEAD!
load('jspec/support/env.rhino.js')
#{ scripts }
#{ jspecs }
JSpec
#{ specs }
.run({ #{ params } })
.report()
RHINO
puts "Writing rhino.js"
File.open(File.expand_path(File.dirname(__FILE__) + '/../../jspec/rhino.js'), 'w') { |f| f.write(rhino.gsub('.run({ })','.run()')) }
#
# SERVER configuration file
#
# Get jspec library files
processed = []
jspecs = config['server']['jspec'].inject('') do |result, element|
Dir.glob("#{ jspec_lib_path }/#{ element }.js").each do |f|
if !processed.include?(f)
result += " <script src=\"#{ f }\"></script>\n"
processed << f
end
end
result
end
# Get users javascripts
processed = []
scripts = config['server']['scripts'].inject('') do |result, element|
base_dir = File.expand_path(File.dirname(__FILE__) + '/../../public/javascripts')
Dir.glob("#{ base_dir }/#{ element }.js").each do |f|
relative_file = Pathname.new(File.new(f).path).relative_path_from(Pathname.new(base_dir))
if !processed.include?(relative_file)
result += " <script src=\"../public/javascripts/#{ relative_file }\"></script>\n"
processed << relative_file
end
end
result
end
# Get jspec test files
processed = []
specs = config['server']['specs'].inject('') do |result, element|
base_dir = File.expand_path(File.dirname(__FILE__) + '/../../jspec')
Dir.glob("#{ base_dir }/#{ element }.js").each do |f|
relative_file = Pathname.new(File.new(f).path).relative_path_from(Pathname.new(base_dir))
if !processed.include?(relative_file)
result += " .exec('#{ relative_file }')\n"
processed << relative_file
end
end
result
end.gsub(/\n$/, '')
# Get the run parameters
params = config['server']['params'].inject('') do |result, element|
result += "#{ element[0] }: #{ element[1] }, "
end.gsub(/, $/, '')
server = <<-SERVER
<!-- DO NOT EDIT THIS GENERATED FILE - EDIT config/jspec.yml INSTEAD! -->
<html>
<head>
<title>JSpec Testrunner</title>
#{ scripts }
#{ jspecs }
<script>
function runSuites() {
JSpec
#{ specs }
.run({ #{ params } })
.report()
}
</script>
</head>
<body class="jspec" onLoad="runSuites();">
</body>
</html>
SERVER
puts "Writing server.html"
File.open(File.expand_path(File.dirname(__FILE__) + '/../../jspec/server.html'), 'w') { |f| f.write(server.gsub('.run({ })','.run()')) }
end
end
defaults: &defaults
jspec:
- jspec
- jspec.jquery
- jspec.xhr
- jspec.timers
scripts:
- jquery/jquery-1.4.2
- underscore
- compiled/**/*
specs:
- unit/**/*
params: {}
dom:
<<: *defaults
rhino:
<<: *defaults
jspec:
- jspec
- jspec*
params:
reporter: JSpec.reporters.Terminal
fixturePath: "'/spec/fixtures'"
server:
<<: *defaults
params:
reporter: JSpec.reporters.Server
verbose: true
failuresOnly: true
fixturePath: "'/spec/fixtures'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment