fixes, generators, rails engines, and others

This commit is contained in:
John Bintz 2012-03-17 15:03:33 -04:00
parent f6cef98b39
commit 4b463428ea
11 changed files with 126 additions and 9 deletions

View File

@ -22,6 +22,17 @@ class Flowerbox::CLI < Thor
end end
end end
desc "transplant DIR", "Convert an existing Jasmine gem-style project to Flowerbox"
def transplant(dir)
Flowerbox.transplant(dir)
end
desc "clean", "Clean the Sprockets cache"
def clean
FileUtils.rm_rf(Flowerbox::CACHE_DIR)
puts "Sprockets cache cleaned."
end
default_task :test default_task :test
no_tasks do no_tasks do

View File

@ -17,6 +17,12 @@ module Flowerbox
require 'flowerbox/reporter' require 'flowerbox/reporter'
if defined?(Rails::Engine)
require 'flowerbox/rails/engine'
end
CACHE_DIR = '.tmp-sprockets'
class << self class << self
attr_writer :reporters attr_writer :reporters
attr_accessor :port attr_accessor :port
@ -103,6 +109,12 @@ module Flowerbox
@browsers = {} @browsers = {}
end end
def transplant(dir)
Flowerbox::TestEnvironment.transplantable_environments.each do |env|
break if env.transplant(dir)
end
end
end end
end end

View File

@ -66,6 +66,8 @@ module Flowerbox
get %r{^/__F__/(.*)$} do |file| get %r{^/__F__/(.*)$} do |file|
asset = sprockets.asset_for(file, :bundle => false) asset = sprockets.asset_for(file, :bundle => false)
halt(404) if !asset
content_type asset.content_type content_type asset.content_type
asset.body asset.body
end end

View File

@ -0,0 +1,15 @@
require 'rails/engine'
module Flowerbox
module Rails
class Engine < ::Rails::Engine
rake_tasks do
require 'flowerbox/task'
Flowerbox::Task.create("flowerbox:spec", :dir => "spec/javascripts")
Flowerbox::Task.create("flowerbox:integration", :dir => "js-features")
end
end
end
end

View File

@ -22,7 +22,7 @@ module Flowerbox
def configured? def configured?
File.directory?(File.join(Dir.pwd, 'node_modules/jsdom')) && File.directory?(File.join(Dir.pwd, 'node_modules/jsdom')) &&
File.directory?(File.join(Dir.pwd, 'node_modules/XMLHttpRequest')) File.directory?(File.join(Dir.pwd, 'node_modules/xmlhttprequest'))
end end
def cleanup ; end def cleanup ; end

View File

@ -71,7 +71,7 @@ HTML
end end
def template_files def template_files
sprockets.files.collect { |file| %{<script type="text/javascript" src="/__F__/#{file.logical_path}"></script>} } sprockets.files.collect { |file| %{<script type="text/javascript" src="/__F__/#{sprockets.logical_path_for(file)}"></script>} }
end end
end end
end end

View File

@ -20,7 +20,7 @@ module Flowerbox
require 'flowerbox/unique_asset_list' require 'flowerbox/unique_asset_list'
@files = Flowerbox::UniqueAssetList.new @files = Flowerbox::UniqueAssetList.new(self)
end end
def add(asset) def add(asset)
@ -39,7 +39,7 @@ module Flowerbox
return @environment if @environment return @environment if @environment
@environment = Sprockets::Environment.new @environment = Sprockets::Environment.new
@environment.cache = Sprockets::Cache::FileStore.new(".tmp") @environment.cache = Sprockets::Cache::FileStore.new(Flowerbox::CACHE_DIR)
self.class.gem_asset_paths.each { |path| append_path(path) } self.class.gem_asset_paths.each { |path| append_path(path) }
options[:asset_paths].each { |path| append_path(path) } options[:asset_paths].each { |path| append_path(path) }
@ -54,6 +54,18 @@ module Flowerbox
def add_paths_for_compiled_asset(path) def add_paths_for_compiled_asset(path)
asset_for(path, :bundle => false).to_a.each { |file_path| @files.add(file_path) } asset_for(path, :bundle => false).to_a.each { |file_path| @files.add(file_path) }
end end
def logical_path_for(asset)
asset_path = asset.pathname.to_s
environment.paths.each do |path|
if asset_path[%r{^#{path}}]
return asset_path.gsub(%r{^#{path}/}, '')
end
end
raise StandardError.new("Could not find logical path for #{asset_path}")
end
end end
end end

View File

@ -2,12 +2,18 @@ module Flowerbox
module TestEnvironment module TestEnvironment
extend Flowerbox::CoreExt::Module extend Flowerbox::CoreExt::Module
class << self def self.for(env)
def for(env) require "flowerbox/test_environment/#{env}"
require "flowerbox/test_environment/#{env}"
find_constant(env).new find_constant(env).new
end
def self.transplantable_environments
Dir[File.expand_path('../test_environment/*.rb', __FILE__)].each do |file|
require file
end end
constants.collect { |k| const_get(k) }.find_all(&:transplantable?)
end end
end end
end end

View File

@ -13,6 +13,10 @@ module Flowerbox
@reporters ||= [] @reporters ||= []
end end
def self.transplantable?
respond_to?(:transplant)
end
def set_additional_options(opts = nil) def set_additional_options(opts = nil)
@options = {} @options = {}

View File

@ -1,9 +1,56 @@
require 'flowerbox/test_environment/base' require 'flowerbox/test_environment/base'
require 'jasmine-core' require 'jasmine-core'
require 'yaml'
module Flowerbox module Flowerbox
module TestEnvironment module TestEnvironment
class Jasmine < Base class Jasmine < Base
def self.transplant(dir)
if File.file?(jasmine_yml = File.join(dir, 'support/jasmine.yml'))
puts "Transplanting #{jasmine_yml} into Flowerbox..."
config = [
%{f.test_with :jasmine},
%{f.run_with :firefox},
%{f.report_with :verbose}
]
YAML.load_file(jasmine_yml).each do |key, value|
case key
when 'src_dir'
[ value ].flatten.each do |dir|
config << %{f.asset_paths << "#{dir}"}
end
when 'src_files'
[ value ].flatten.each do |file|
config << %{f.additional_files << "#{file}"}
end
when 'helpers'
[ value ].flatten.each do |pattern|
Dir[File.join(dir, pattern)].each do |file|
config << %{f.additional_files << "#{file.gsub("#{dir}/", '')}"}
end
end
when 'spec_files'
[ value ].flatten.each do |pattern|
config << %{f.spec_patterns << "#{pattern}"}
end
end
end
File.open(target = File.join(dir, 'spec_helper.rb'), 'wb') do |fh|
fh.puts "Flowerbox.configure do |f|"
config.each do |line|
fh.puts " #{line}"
end
fh.puts "end"
end
puts "#{target} created. Run your tests with:"
puts " flowerbox test #{dir}"
end
end
def inject_into(sprockets) def inject_into(sprockets)
sprockets.append_path(::Jasmine::Core.path) sprockets.append_path(::Jasmine::Core.path)

View File

@ -1,11 +1,19 @@
module Flowerbox module Flowerbox
class UniqueAssetList < ::Array class UniqueAssetList < ::Array
attr_reader :sprockets
def initialize(sprockets)
super([])
@sprockets = sprockets
end
def add(files) def add(files)
[ files ].flatten.each { |file| self << file if !include?(file) } [ files ].flatten.each { |file| self << file if !include?(file) }
end end
def to_json def to_json
collect(&:logical_path) collect { |file| sprockets.logical_path_for(file) }
end end
private private