Skip to content

Commit

Permalink
Fix bug with regexp when installing into coffeescript application file
Browse files Browse the repository at this point in the history
cleanup install generator
  • Loading branch information
John Duff committed Oct 27, 2011
1 parent 669ae21 commit 2be225f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
69 changes: 39 additions & 30 deletions lib/generators/batman/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,57 +27,66 @@ def create_directories
def inject_batman
with_app_name do
application_file = File.join(js_path, "application.js")
file_type = :javascript
pattern = /\/\/=(?!.*\/\/=).*?$/m

if File.exist?(File.join(destination_root, application_file))
file_type = :javascript
require_tree_pattern = /\/\/=(?!.*\/\/=).*?$/m
else
file_type = :coffeescript
require_tree_pattern = /#=(?!.*\/\/=).*?$/m
unless exists?(application_file)
application_file = "#{application_file}.coffee"
unless File.exist?(File.join(destination_root, application_file))
raise Thor::Error, "Couldn't find either application.js or application.js.coffee files for use with batman!"
end
file_type = :coffeescript
pattern = /#=(?!.*#=).*?$/m
end

inject_into_file application_file, :before=>require_tree_pattern do
code = <<-CODE
\n// Batman.js and its adapters
//= require batman/batman
//= require batman/batman.jquery
//= require batman/batman.rails
raise Thor::Error, "Couldn't find either application.js or application.js.coffee files for use with batman!" unless exists?(application_file)

//= require #{app_name}
inject_into_file application_file, :before=>pattern do
batman_requires(file_type)
end

//= require_tree ./models
//= require_tree ./controllers
//= require_tree ./helpers
\n
CODE
if file_type == :coffeescript
code.gsub!('//', '#')
end
code
inject_into_file application_file, :after=>pattern do
ready_function(file_type)
end
end
end

private

inject_into_file application_file, :after=>require_tree_pattern do
if file_type == :coffeescript
def ready_function(file_type=:javascript)
if file_type == :coffeescript
<<-CODE
\n# Run the Batman app
$(document).ready ->
#{js_app_name}.run()
CODE
else
else
<<-CODE
\n// Run the Batman app
$(document).ready(function(){
#{js_app_name}.run();
});
CODE
end
end
end
end

def batman_requires(file_type=:javascript)
code = <<-CODE
\n// Batman.js and its adapters
//= require batman/batman
//= require batman/batman.jquery
//= require batman/batman.rails
//= require #{app_name}
//= require_tree ./models
//= require_tree ./controllers
//= require_tree ./helpers
\n
CODE
file_type == :coffeescript ? code.gsub('//', '#') : code
end

def exists?(file)
File.exist?(File.join(destination_root, file))
end
end
end
end
6 changes: 3 additions & 3 deletions test/install_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ def test_applicationjs_require_batman_jquery_rails_and_dummy

assert_file "#{javascripts_path}/#{application_javascript_path}" do |app|
%W{batman batman.jquery batman.rails}.each do |require|
assert_match /require batman\/#{require}/, app
assert_equal 1, app.scan(%r{require batman\/#{require}$}).length
end

assert_match /require dummy/, app

%W{models controllers helpers}.each do |require|
assert_match /require_tree \.\/#{require}/, app
assert_equal 1, app.scan(/require_tree \.\/#{require}/).length
end

assert_match /Dummy\.run\(\)/, app
assert_equal 1, app.scan(/Dummy\.run\(\)/).length
end
end

Expand Down

0 comments on commit 2be225f

Please sign in to comment.