Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add therubyracer/therubyrhino to Gemfile when there isn't a JS Runtime available in the system #3619

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions railties/lib/rails/generators/app_base.rb
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions railties/test/generators/app_generator_test.rb
Expand Up @@ -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"
Expand Down