Skip to content

Commit

Permalink
Add Gemfile entry when creating a plugin in application's directory
Browse files Browse the repository at this point in the history
After vendor/plugins were removed from rails, the new method to create
plugins is to create gem plugins. Most of the time if you create a
new plugin in rails application's directory, you want to extract
something from that application and use it immediately, ie. add
such line to Gemfile:

  gem 'foo', :path => './vendor/foo'

This commit makes plugin new generator to add such line automatically.
  • Loading branch information
drogus committed Jan 7, 2012
1 parent 0c1846e commit 0204153
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ def script(force = false)
end
chmod "script", 0755, :verbose => false
end

def gemfile_entry
return unless inside_application?

gemfile_in_app_path = File.join(rails_app_path, "Gemfile")
if File.exist? gemfile_in_app_path
entry = "gem '#{name}', :path => '#{relative_path}'"
append_file gemfile_in_app_path, entry
end
end
end

module Generators
Expand All @@ -153,6 +163,10 @@ class PluginNewGenerator < AppBase
class_option :skip_gemspec, :type => :boolean, :default => false,
:desc => "Skip gemspec file"

class_option :skip_gemfile_entry, :type => :boolean, :default => false,
:desc => "If creating plugin in application's directory " +
"skip adding entry to Gemfile"

def initialize(*args)
raise Error, "Options should be given after the plugin name. For details run: rails plugin --help" if args[0].blank?

Expand Down Expand Up @@ -208,6 +222,10 @@ def create_test_dummy_files
create_dummy_app
end

def update_gemfile
build(:gemfile_entry) unless options[:skip_gemfile_entry]
end

def finish_template
build(:leftovers)
end
Expand Down Expand Up @@ -313,6 +331,19 @@ def dummy_path(path = nil)
def mute(&block)
shell.mute(&block)
end

def rails_app_path
APP_PATH.sub("/config/application", "") if defined?(APP_PATH)
end

def inside_application?
rails_app_path && app_path =~ /^#{rails_app_path}/
end

def relative_path
return unless inside_application?
app_path.sub(/^#{rails_app_path}\//, '')
end
end
end
end
33 changes: 32 additions & 1 deletion railties/test/generators/plugin_new_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def test_creating_dummy_without_tests_but_with_dummy_path
assert_file "spec/dummy/config/application.rb"
assert_no_file "test"
end

def test_ensure_that_gitignore_can_be_generated_from_a_template_for_dummy_path
FileUtils.cd(Rails.root)
run_generator([destination_root, "--dummy_path", "spec/dummy" "--skip-test-unit"])
Expand All @@ -263,6 +263,37 @@ def test_skipping_gemspec
assert_no_file "bukkits.gemspec"
end

def test_creating_plugin_in_app_directory_adds_gemfile_entry
# simulate application existance
gemfile_path = "#{Rails.root}/Gemfile"
Object.const_set('APP_PATH', Rails.root)
FileUtils.touch gemfile_path

run_generator [destination_root]

assert_file gemfile_path, /gem 'bukkits', :path => '.\/tmp\/bukkits'/
ensure
Object.send(:remove_const, 'APP_PATH')
FileUtils.rm gemfile_path
end

def test_skipping_gemfile_entry
# simulate application existance
gemfile_path = "#{Rails.root}/Gemfile"
Object.const_set('APP_PATH', Rails.root)
FileUtils.touch gemfile_path

run_generator [destination_root, "--skip-gemfile-entry"]

assert_file gemfile_path do |contents|
assert_no_match(/gem 'bukkits', :path => '.\/tmp\/bukkits'/, contents)
end
ensure
Object.send(:remove_const, 'APP_PATH')
FileUtils.rm gemfile_path
end


protected

def action(*args, &block)
Expand Down

0 comments on commit 0204153

Please sign in to comment.