Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add therubyracer/therubyrhino to Gemfile when there isn't a JS Runtim…

…e available in the system. Closes #2963
  • Loading branch information...
commit c267f11e83ada29e0ba5019ebfa6f9db45220ab2 1 parent df300a7
@guilleiguaran authored
View
30 railties/lib/rails/generators/app_base.rb
@@ -193,17 +193,47 @@ def ruby_debugger_gemfile_entry
end
def assets_gemfile_entry
+ gem_for_js_runtime = true unless js_runtime_available?
+
<<-GEMFILE.strip_heredoc
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', :git => 'git://github.com/rails/sass-rails.git'
gem 'coffee-rails', :git => 'git://github.com/rails/coffee-rails.git'
+
gem 'uglifier', '>= 1.0.3'
+
+ #{"# JavaScript runtime for CoffeeScript assets and Uglifier" if gem_for_js_runtime}
+ #{"# See https://github.com/sstephenson/execjs for alternative runtimes." if gem_for_js_runtime}
+ #{javascript_runtime_gemfile_entry if gem_for_js_runtime}
end
GEMFILE
end
+ def javascript_runtime_gemfile_entry
+ if defined?(JRUBY_VERSION)
+ "gem 'therubyrhino'"
+ else
+ "gem 'therubyracer'"
+ end
+ end
+
+ def js_runtime_available?
+ # Windows and OS X includes JavaScript Runtime by default
+ return true if RUBY_PLATFORM =~ /mswin32/ || RUBY_PLATFORM =~ /darwin/
+
+ # Prefer therubyrhino when running under JRuby
+ return false if defined?(JRUBY_VERSION)
+
+ # Node.js can be used if is installed in PATH
+ `which node`
+ return true if $?.success?
+
+ # Probably there isn't a javascript installed in the system
+ return false
+ end
+
def javascript_gemfile_entry
"gem '#{options[:javascript]}-rails'" unless options[:skip_javascript]
end
View
11 railties/test/generators/app_generator_test.rb
@@ -189,6 +189,17 @@ def test_config_jdbc_database_when_no_option_given
end
end
+ def test_javascript_runtime_is_added_to_gemfile
+ win_or_osx = (RUBY_PLATFORM =~ /mswin32/ || RUBY_PLATFORM =~ /darwin/)
+ `which node`
+ node = $?.success?
+
+ if defined?(JRUBY_VERSION) || (!win_or_osx && !node)
+ run_generator([destination_root])
+ assert_file "Gemfile", /gem\s+["']theruby(racer|rhino)["']/
+ end
+ end
+
def test_generator_if_skip_active_record_is_given
run_generator [destination_root, "--skip-active-record"]
assert_no_file "config/database.yml"
Please sign in to comment.
Something went wrong with that request. Please try again.