Skip to content

Commit

Permalink
Install Spring preloader when generating new applications
Browse files Browse the repository at this point in the history
  • Loading branch information
jonleighton committed Dec 3, 2013
1 parent d5332de commit df50e30
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
7 changes: 7 additions & 0 deletions railties/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,10 @@
* The [Spring application
preloader](https://github.com/jonleighton/spring) is now installed
by default for new applications. It uses the development group of
the Gemfile, so will not be installed in production.

*Jon Leighton*

* Uses .railsrc while creating new plugin if it is available. * Uses .railsrc while creating new plugin if it is available.
Fixes #10700. Fixes #10700.


Expand Down
26 changes: 25 additions & 1 deletion railties/lib/rails/generators/app_base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def self.add_shared_options_for(name)
class_option :skip_sprockets, type: :boolean, aliases: '-S', default: false, class_option :skip_sprockets, type: :boolean, aliases: '-S', default: false,
desc: 'Skip Sprockets files' desc: 'Skip Sprockets files'


class_option :skip_spring, type: :boolean, default: false,
desc: "Don't install Spring application preloader"

class_option :database, type: :string, aliases: '-d', default: 'sqlite3', class_option :database, type: :string, aliases: '-d', default: 'sqlite3',
desc: "Preconfigure for selected database (options: #{DATABASES.join('/')})" desc: "Preconfigure for selected database (options: #{DATABASES.join('/')})"


Expand Down Expand Up @@ -109,6 +112,7 @@ def gemfile_entries
jbuilder_gemfile_entry, jbuilder_gemfile_entry,
sdoc_gemfile_entry, sdoc_gemfile_entry,
platform_dependent_gemfile_entry, platform_dependent_gemfile_entry,
spring_gemfile_entry,
@extra_entries].flatten.find_all(&@gem_filter) @extra_entries].flatten.find_all(&@gem_filter)
end end


Expand Down Expand Up @@ -365,6 +369,12 @@ def javascript_runtime_gemfile_entry
end end
end end


def spring_gemfile_entry
return [] unless spring_install?
comment = 'Spring speeds up development by keeping your application running in the background. Read more: https://github.com/jonleighton/spring'
GemfileEntry.new('spring', nil, comment, group: :development)
end

def bundle_command(command) def bundle_command(command)
say_status :run, "bundle #{command}" say_status :run, "bundle #{command}"


Expand All @@ -388,8 +398,22 @@ def bundle_command(command)
end end
end end


def bundle_install?
!(options[:skip_gemfile] || options[:skip_bundle] || options[:pretend])
end

def spring_install?
!options[:skip_spring] && Process.respond_to?(:fork)
end

def run_bundle def run_bundle
bundle_command('install') unless options[:skip_gemfile] || options[:skip_bundle] || options[:pretend] bundle_command('install') if bundle_install?
end

def generate_spring_binstubs
if bundle_install? && spring_install?
bundle_command("exec spring binstub --all")
end
end end


def empty_directory_with_keep_file(destination, config = {}) def empty_directory_with_keep_file(destination, config = {})
Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails/generators/rails/app/app_generator.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ def delete_js_folder_skipping_javascript


public_task :run_bundle public_task :run_bundle
public_task :replay_template public_task :replay_template
public_task :generate_spring_binstubs


protected protected


Expand Down
28 changes: 28 additions & 0 deletions railties/test/generators/app_generator_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -437,6 +437,34 @@ def test_application_name_with_spaces
assert_file "foo bar/config/initializers/session_store.rb", /key: '_foo_bar/ assert_file "foo bar/config/initializers/session_store.rb", /key: '_foo_bar/
end end


def test_spring
run_generator
assert_file "Gemfile", /gem 'spring'/
end

def test_spring_binstubs
generator.stubs(:bundle_command).with('install')
generator.expects(:bundle_command).with('exec spring binstub --all').once
quietly { generator.invoke_all }
end

def test_spring_no_fork
Process.stubs(:respond_to?).with(:fork).returns(false)
run_generator

assert_file "Gemfile" do |content|
assert_no_match(/spring/, content)
end
end

def test_skip_spring
run_generator [destination_root, "--skip-spring"]

assert_file "Gemfile" do |content|
assert_no_match(/spring/, content)
end
end

protected protected


def action(*args, &block) def action(*args, &block)
Expand Down
16 changes: 10 additions & 6 deletions railties/test/generators/shared_generator_tests.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ def test_skeleton_is_created
default_files.each { |path| assert_file path } default_files.each { |path| assert_file path }
end end


def test_generation_runs_bundle_install def assert_generates_with_bundler(options = {})
generator([destination_root]).expects(:bundle_command).with('install').once generator([destination_root], options)
generator.expects(:bundle_command).with('install').once
generator.stubs(:bundle_command).with('exec spring binstub --all')
quietly { generator.invoke_all } quietly { generator.invoke_all }
end end


def test_generation_runs_bundle_install
assert_generates_with_bundler
end

def test_plugin_new_generate_pretend def test_plugin_new_generate_pretend
run_generator ["testapp", "--pretend"] run_generator ["testapp", "--pretend"]
default_files.each{ |path| assert_no_file File.join("testapp",path) } default_files.each{ |path| assert_no_file File.join("testapp",path) }
Expand Down Expand Up @@ -96,15 +102,13 @@ def test_template_is_executed_when_supplied_an_https_path
end end


def test_dev_option def test_dev_option
generator([destination_root], dev: true).expects(:bundle_command).with('install').once assert_generates_with_bundler dev: true
quietly { generator.invoke_all }
rails_path = File.expand_path('../../..', Rails.root) rails_path = File.expand_path('../../..', Rails.root)
assert_file 'Gemfile', /^gem\s+["']rails["'],\s+path:\s+["']#{Regexp.escape(rails_path)}["']$/ assert_file 'Gemfile', /^gem\s+["']rails["'],\s+path:\s+["']#{Regexp.escape(rails_path)}["']$/
end end


def test_edge_option def test_edge_option
generator([destination_root], edge: true).expects(:bundle_command).with('install').once assert_generates_with_bundler edge: true
quietly { generator.invoke_all }
assert_file 'Gemfile', %r{^gem\s+["']rails["'],\s+github:\s+["']#{Regexp.escape("rails/rails")}["']$} assert_file 'Gemfile', %r{^gem\s+["']rails["'],\s+github:\s+["']#{Regexp.escape("rails/rails")}["']$}
end end


Expand Down

0 comments on commit df50e30

Please sign in to comment.