Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added 'rails plugin new' generator which generates gem plugin skeleton.

This command is based on enginex gem by José Valim. It generates gem structure
and ads dummy application into test/dummy. This can be used to start developing
any kind of extension for rails 3.
  • Loading branch information...
commit cfcea1d53ae5ce38a7cbeb41e05958dc009988b0 1 parent d446392
Piotr Sarnacki authored October 15, 2010

Showing 21 changed files with 607 additions and 1 deletion. Show diff stats Hide diff stats

  1. 7  railties/lib/rails/cli.rb
  2. 16  railties/lib/rails/commands/plugin_new.rb
  3. 225  railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb
  4. 9  railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec
  5. 11  railties/lib/rails/generators/rails/plugin_new/templates/Gemfile
  6. 20  railties/lib/rails/generators/rails/plugin_new/templates/MIT-LICENSE
  7. 3  railties/lib/rails/generators/rails/plugin_new/templates/README.rdoc
  8. 46  railties/lib/rails/generators/rails/plugin_new/templates/Rakefile
  9. 6  railties/lib/rails/generators/rails/plugin_new/templates/gitignore
  10. 2  railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb.tt
  11. 12  railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb
  12. 10  railties/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb
  13. 5  railties/lib/rails/generators/rails/plugin_new/templates/script/rails.tt
  14. 7  railties/lib/rails/generators/rails/plugin_new/templates/test/%name%_test.rb.tt
  15. 7  railties/lib/rails/generators/rails/plugin_new/templates/test/integration/navigation_test.rb.tt
  16. 5  railties/lib/rails/generators/rails/plugin_new/templates/test/support/integration_case.rb
  17. 22  railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb
  18. 2  railties/test/fixtures/lib/plugin_builders/empty_builder.rb
  19. 7  railties/test/fixtures/lib/plugin_builders/simple_builder.rb
  20. 7  railties/test/fixtures/lib/plugin_builders/tweak_builder.rb
  21. 179  railties/test/generators/plugin_new_generator_test.rb
7  railties/lib/rails/cli.rb
@@ -11,4 +11,9 @@
11 11
 require 'rails/ruby_version_check'
12 12
 Signal.trap("INT") { puts; exit }
13 13
 
14  
-require 'rails/commands/application'
  14
+if ARGV.first == 'plugin'
  15
+  ARGV.shift
  16
+  require 'rails/commands/plugin_new'
  17
+else
  18
+  require 'rails/commands/application'
  19
+end
16  railties/lib/rails/commands/plugin_new.rb
... ...
@@ -0,0 +1,16 @@
  1
+require 'rails/version'
  2
+if %w(--version -v).include? ARGV.first
  3
+  puts "Rails #{Rails::VERSION::STRING}"
  4
+  exit(0)
  5
+end
  6
+
  7
+if ARGV.first != "new"
  8
+  ARGV[0] = "--help"
  9
+else
  10
+  ARGV.shift
  11
+end
  12
+
  13
+require 'rails/generators'
  14
+require 'rails/generators/rails/plugin_new/plugin_new_generator'
  15
+
  16
+Rails::Generators::PluginNewGenerator.start
225  railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb
... ...
@@ -0,0 +1,225 @@
  1
+require "rails/generators/rails/app/app_generator"
  2
+
  3
+module Rails
  4
+  class PluginBuilder
  5
+    def rakefile
  6
+      template "Rakefile"
  7
+    end
  8
+
  9
+    def readme
  10
+      copy_file "README.rdoc"
  11
+    end
  12
+
  13
+    def gemfile
  14
+      template "Gemfile"
  15
+    end
  16
+
  17
+    def license
  18
+      template "MIT-LICENSE"
  19
+    end
  20
+
  21
+    def gemspec
  22
+      template "%name%.gemspec"
  23
+    end
  24
+
  25
+    def gitignore
  26
+      copy_file "gitignore", ".gitignore"
  27
+    end
  28
+
  29
+    def lib
  30
+      directory "lib"
  31
+    end
  32
+
  33
+    def test
  34
+      directory "test"
  35
+    end
  36
+
  37
+    def test_dummy
  38
+      invoke Rails::Generators::AppGenerator,
  39
+        [ File.expand_path(dummy_path, destination_root) ]
  40
+    end
  41
+
  42
+    def test_dummy_config
  43
+      store_application_definition!
  44
+      template "rails/boot.rb", "#{dummy_path}/config/boot.rb", :force => true
  45
+      template "rails/application.rb", "#{dummy_path}/config/application.rb", :force => true
  46
+    end
  47
+
  48
+    def test_dummy_clean
  49
+      inside dummy_path do
  50
+        remove_file ".gitignore"
  51
+        remove_file "db/seeds.rb"
  52
+        remove_file "doc"
  53
+        remove_file "Gemfile"
  54
+        remove_file "lib/tasks"
  55
+        remove_file "public/images/rails.png"
  56
+        remove_file "public/index.html"
  57
+        remove_file "public/robots.txt"
  58
+        remove_file "README"
  59
+        remove_file "test"
  60
+        remove_file "vendor"
  61
+      end
  62
+    end
  63
+
  64
+    def script
  65
+      directory "script" do |content|
  66
+        "#{shebang}\n" + content
  67
+      end
  68
+      chmod "script", 0755, :verbose => false
  69
+    end
  70
+  end
  71
+
  72
+  module Generators
  73
+    class PluginNewGenerator < Base
  74
+      attr_accessor :rails_template
  75
+
  76
+      add_shebang_option!
  77
+
  78
+      argument :plugin_path,            :type => :string
  79
+
  80
+      class_option :builder,            :type => :string, :aliases => "-b",
  81
+                                        :desc => "Path to a plugin builder (can be a filesystem path or URL)"
  82
+
  83
+      class_option :skip_gemfile,       :type => :boolean, :default => false,
  84
+                                        :desc => "Don't create a Gemfile"
  85
+
  86
+      class_option :skip_git,           :type => :boolean, :aliases => "-G", :default => false,
  87
+                                        :desc => "Skip Git ignores and keeps"
  88
+
  89
+      class_option :help,               :type => :boolean, :aliases => "-h", :group => :rails,
  90
+                                        :desc => "Show this help message and quit"
  91
+
  92
+      def self.say_step(message)
  93
+        @step = (@step || 0) + 1
  94
+        class_eval <<-METHOD, __FILE__, __LINE__ + 1
  95
+          def step_#{@step}
  96
+            #{"puts" if @step > 1}
  97
+            say_status "STEP #{@step}", #{message.inspect}
  98
+          end
  99
+        METHOD
  100
+      end
  101
+
  102
+      def initialize(*args)
  103
+        raise Error, "Options should be given after plugin name. For details run: rails plugin --help" if args[0].blank?
  104
+
  105
+        @original_wd = Dir.pwd
  106
+
  107
+        super
  108
+      end
  109
+
  110
+      say_step "Creating gem skeleton"
  111
+
  112
+      def create_root
  113
+        self.destination_root = File.expand_path(plugin_path, destination_root)
  114
+        valid_plugin_const?
  115
+
  116
+        empty_directory '.'
  117
+        FileUtils.cd(destination_root) unless options[:pretend]
  118
+      end
  119
+
  120
+      def create_root_files
  121
+        build(:readme)
  122
+        build(:rakefile)
  123
+        build(:gemspec)
  124
+        build(:license)
  125
+        build(:gitignore) unless options[:skip_git]
  126
+        build(:gemfile)   unless options[:skip_gemfile]
  127
+      end
  128
+
  129
+      def create_config_files
  130
+        build(:config)
  131
+      end
  132
+
  133
+      def create_lib_files
  134
+        build(:lib)
  135
+      end
  136
+
  137
+      def create_script_files
  138
+        build(:script)
  139
+      end
  140
+
  141
+      def create_test_files
  142
+        build(:test) unless options[:skip_test_unit]
  143
+      end
  144
+
  145
+      say_step "Vendoring Rails application at test/dummy"
  146
+
  147
+      def create_test_dummy_files
  148
+        build(:test_dummy)
  149
+      end
  150
+
  151
+      say_step "Configuring Rails application"
  152
+
  153
+      def change_config_files
  154
+        build(:test_dummy_config)
  155
+      end
  156
+
  157
+      say_step "Removing unneeded files"
  158
+
  159
+      def remove_uneeded_rails_files
  160
+        build(:test_dummy_clean)
  161
+      end
  162
+
  163
+    protected
  164
+
  165
+      def self.banner
  166
+        "rails plugin new #{self.arguments.map(&:usage).join(' ')} [options]"
  167
+      end
  168
+
  169
+      def builder
  170
+        @builder ||= begin
  171
+          if path = options[:builder]
  172
+            if URI(path).is_a?(URI::HTTP)
  173
+              contents = open(path, "Accept" => "application/x-thor-template") {|io| io.read }
  174
+            else
  175
+              contents = open(File.expand_path(path, @original_wd)) {|io| io.read }
  176
+            end
  177
+
  178
+            prok = eval("proc { #{contents} }", TOPLEVEL_BINDING, path, 1)
  179
+            instance_eval(&prok)
  180
+          end
  181
+
  182
+          builder_class = defined?(::PluginBuilder) ? ::PluginBuilder : Rails::PluginBuilder
  183
+          builder_class.send(:include, ActionMethods)
  184
+          builder_class.new(self)
  185
+        end
  186
+      end
  187
+
  188
+      def build(meth, *args)
  189
+        builder.send(meth, *args) if builder.respond_to?(meth)
  190
+      end
  191
+
  192
+      def name
  193
+        @name ||= File.basename(destination_root)
  194
+      end
  195
+
  196
+      def camelized
  197
+        @camelized ||= name.gsub(/\W/, '_').squeeze('_').camelize
  198
+      end
  199
+
  200
+      def valid_plugin_const?
  201
+        if camelized =~ /^\d/
  202
+          raise Error, "Invalid plugin name #{name}. Please give a name which does not start with numbers."
  203
+        elsif RESERVED_NAMES.include?(name)
  204
+          raise Error, "Invalid plugin name #{name}. Please give a name which does not match one of the reserved rails words."
  205
+        elsif Object.const_defined?(camelized)
  206
+          raise Error, "Invalid plugin name #{name}, constant #{camelized} is already in use. Please choose another application name."
  207
+        end
  208
+      end
  209
+
  210
+      def dummy_path
  211
+        "test/dummy"
  212
+      end
  213
+
  214
+      def application_definition
  215
+        @application_definition ||= begin
  216
+          unless options[:pretend]
  217
+            contents = File.read(File.expand_path("#{dummy_path}/config/application.rb", destination_root))
  218
+            contents[(contents.index("module Dummy"))..-1]
  219
+          end
  220
+        end
  221
+      end
  222
+      alias :store_application_definition! :application_definition
  223
+    end
  224
+  end
  225
+end
9  railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec
... ...
@@ -0,0 +1,9 @@
  1
+# Provide a simple gemspec so you can easily use your
  2
+# project in your rails apps through git.
  3
+Gem::Specification.new do |s|
  4
+  s.name = "<%= name %>"
  5
+  s.summary = "Insert <%= camelized %> summary."
  6
+  s.description = "Insert <%= camelized %> description."
  7
+  s.files = Dir["lib/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
  8
+  s.version = "0.0.1"
  9
+end
11  railties/lib/rails/generators/rails/plugin_new/templates/Gemfile
... ...
@@ -0,0 +1,11 @@
  1
+source "http://rubygems.org"
  2
+
  3
+gem "rails", :git => "http://github.com/rails/rails.git"
  4
+gem "arel" , :git => "http://github.com/rails/arel.git"
  5
+gem "rack" , :git => "http://github.com/rack/rack.git"
  6
+gem "capybara", ">= 0.3.9"
  7
+gem "sqlite3-ruby", :require => "sqlite3"
  8
+
  9
+if RUBY_VERSION < '1.9'
  10
+  gem "ruby-debug", ">= 0.10.3"
  11
+end
20  railties/lib/rails/generators/rails/plugin_new/templates/MIT-LICENSE
... ...
@@ -0,0 +1,20 @@
  1
+Copyright <%= Date.today.year %> YOURNAME
  2
+
  3
+Permission is hereby granted, free of charge, to any person obtaining
  4
+a copy of this software and associated documentation files (the
  5
+"Software"), to deal in the Software without restriction, including
  6
+without limitation the rights to use, copy, modify, merge, publish,
  7
+distribute, sublicense, and/or sell copies of the Software, and to
  8
+permit persons to whom the Software is furnished to do so, subject to
  9
+the following conditions:
  10
+
  11
+The above copyright notice and this permission notice shall be
  12
+included in all copies or substantial portions of the Software.
  13
+
  14
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3  railties/lib/rails/generators/rails/plugin_new/templates/README.rdoc
Source Rendered
... ...
@@ -0,0 +1,3 @@
  1
+= <%= camelized %>
  2
+
  3
+This project rocks and uses MIT-LICENSE.
46  railties/lib/rails/generators/rails/plugin_new/templates/Rakefile
... ...
@@ -0,0 +1,46 @@
  1
+# encoding: UTF-8
  2
+require 'rubygems'
  3
+begin
  4
+  require 'bundler/setup'
  5
+rescue LoadError
  6
+  puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
  7
+end
  8
+
  9
+require 'rake'
  10
+require 'rake/rdoctask'
  11
+require 'rake/gempackagetask'
  12
+
  13
+require 'rake/testtask'
  14
+
  15
+Rake::TestTask.new(:test) do |t|
  16
+  t.libs << 'lib'
  17
+  t.libs << 'test'
  18
+  t.pattern = 'test/**/*_test.rb'
  19
+  t.verbose = false
  20
+end
  21
+
  22
+task :default => :test
  23
+
  24
+Rake::RDocTask.new(:rdoc) do |rdoc|
  25
+  rdoc.rdoc_dir = 'rdoc'
  26
+  rdoc.title    = '<%= camelized %>'
  27
+  rdoc.options << '--line-numbers' << '--inline-source'
  28
+  rdoc.rdoc_files.include('README.rdoc')
  29
+  rdoc.rdoc_files.include('lib/**/*.rb')
  30
+end
  31
+
  32
+spec = Gem::Specification.new do |s|
  33
+  s.name = "<%= name %>"
  34
+  s.summary = "Insert <%= camelized %> summary."
  35
+  s.description = "Insert <%= camelized %> description."
  36
+  s.files =  FileList["[A-Z]*", "lib/**/*"]
  37
+  s.version = "0.0.1"
  38
+end
  39
+
  40
+Rake::GemPackageTask.new(spec) do |pkg|
  41
+end
  42
+
  43
+desc "Install the gem #{spec.name}-#{spec.version}.gem"
  44
+task :install do
  45
+  system("gem install pkg/#{spec.name}-#{spec.version}.gem --no-ri --no-rdoc")
  46
+end
6  railties/lib/rails/generators/rails/plugin_new/templates/gitignore
... ...
@@ -0,0 +1,6 @@
  1
+.bundle/
  2
+log/*.log
  3
+pkg/
  4
+test/dummy/db/*.sqlite3
  5
+test/dummy/log/*.log
  6
+test/dummy/tmp/
2  railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb.tt
... ...
@@ -0,0 +1,2 @@
  1
+module <%= camelized %>
  2
+end
12  railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb
... ...
@@ -0,0 +1,12 @@
  1
+require File.expand_path('../boot', __FILE__)
  2
+
  3
+require "active_model/railtie"
  4
+require "active_record/railtie"
  5
+require "action_controller/railtie"
  6
+require "action_view/railtie"
  7
+require "action_mailer/railtie"
  8
+
  9
+Bundler.require
  10
+require "<%= name %>"
  11
+
  12
+<%= application_definition %>
10  railties/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb
... ...
@@ -0,0 +1,10 @@
  1
+require 'rubygems'
  2
+gemfile = File.expand_path('../../../../Gemfile', __FILE__)
  3
+
  4
+if File.exist?(gemfile)
  5
+  ENV['BUNDLE_GEMFILE'] = gemfile
  6
+  require 'bundler'
  7
+  Bundler.setup
  8
+end
  9
+
  10
+$:.unshift File.expand_path('../../../../lib', __FILE__)
5  railties/lib/rails/generators/rails/plugin_new/templates/script/rails.tt
... ...
@@ -0,0 +1,5 @@
  1
+#!/usr/bin/env ruby
  2
+# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
  3
+
  4
+ENGINE_PATH = File.expand_path('../..',  __FILE__)
  5
+load File.expand_path('../../test/dummy/script/rails',  __FILE__)
7  railties/lib/rails/generators/rails/plugin_new/templates/test/%name%_test.rb.tt
... ...
@@ -0,0 +1,7 @@
  1
+require 'test_helper'
  2
+
  3
+class <%= camelized %>Test < ActiveSupport::TestCase
  4
+  test "truth" do
  5
+    assert_kind_of Module, <%= camelized %>
  6
+  end
  7
+end
7  railties/lib/rails/generators/rails/plugin_new/templates/test/integration/navigation_test.rb.tt
... ...
@@ -0,0 +1,7 @@
  1
+require 'test_helper'
  2
+
  3
+class NagivationTest < ActiveSupport::IntegrationCase
  4
+  test "truth" do
  5
+    assert_kind_of Dummy::Application, Rails.application
  6
+  end
  7
+end
5  railties/lib/rails/generators/rails/plugin_new/templates/test/support/integration_case.rb
... ...
@@ -0,0 +1,5 @@
  1
+# Define a bare test case to use with Capybara
  2
+class ActiveSupport::IntegrationCase < ActiveSupport::TestCase
  3
+  include Capybara
  4
+  include Rails.application.routes.url_helpers
  5
+end
22  railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb
... ...
@@ -0,0 +1,22 @@
  1
+# Configure Rails Envinronment
  2
+ENV["RAILS_ENV"] = "test"
  3
+
  4
+require File.expand_path("../dummy/config/environment.rb",  __FILE__)
  5
+require "rails/test_help"
  6
+
  7
+ActionMailer::Base.delivery_method = :test
  8
+ActionMailer::Base.perform_deliveries = true
  9
+ActionMailer::Base.default_url_options[:host] = "test.com"
  10
+
  11
+Rails.backtrace_cleaner.remove_silencers!
  12
+
  13
+# Configure capybara for integration testing
  14
+require "capybara/rails"
  15
+Capybara.default_driver   = :rack_test
  16
+Capybara.default_selector = :css
  17
+
  18
+# Run any available migration
  19
+ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
  20
+
  21
+# Load support files
  22
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
2  railties/test/fixtures/lib/plugin_builders/empty_builder.rb
... ...
@@ -0,0 +1,2 @@
  1
+class PluginBuilder
  2
+end
7  railties/test/fixtures/lib/plugin_builders/simple_builder.rb
... ...
@@ -0,0 +1,7 @@
  1
+class PluginBuilder
  2
+  def gitignore
  3
+    create_file ".gitignore", <<-R.strip
  4
+foobar
  5
+    R
  6
+  end
  7
+end
7  railties/test/fixtures/lib/plugin_builders/tweak_builder.rb
... ...
@@ -0,0 +1,7 @@
  1
+class PluginBuilder < Rails::PluginBuilder
  2
+  def gitignore
  3
+    create_file ".gitignore", <<-R.strip
  4
+foobar
  5
+    R
  6
+  end
  7
+end
179  railties/test/generators/plugin_new_generator_test.rb
... ...
@@ -0,0 +1,179 @@
  1
+require 'abstract_unit'
  2
+require 'generators/generators_test_helper'
  3
+require 'rails/generators/rails/plugin_new/plugin_new_generator'
  4
+
  5
+DEFAULT_PLUGIN_FILES = %w(
  6
+  .gitignore
  7
+  Gemfile
  8
+  Rakefile
  9
+  bukkits.gemspec
  10
+  MIT-LICENSE
  11
+  lib
  12
+  lib/bukkits.rb
  13
+  script/rails
  14
+  test/bukkits_test.rb
  15
+  test/integration/navigation_test.rb
  16
+  test/support/integration_case.rb
  17
+  test/test_helper.rb
  18
+  test/dummy
  19
+)
  20
+
  21
+
  22
+class PluginNewGeneratorTest < Rails::Generators::TestCase
  23
+  include GeneratorsTestHelper
  24
+  destination File.join(Rails.root, "tmp/bukkits")
  25
+  arguments [destination_root]
  26
+
  27
+  def setup
  28
+    Rails.application = TestApp::Application
  29
+    super
  30
+    @bundle_command = File.basename(Thor::Util.ruby_command).sub(/ruby/, 'bundle')
  31
+
  32
+    Kernel::silence_warnings do
  33
+      Thor::Base.shell.send(:attr_accessor, :always_force)
  34
+      @shell = Thor::Base.shell.new
  35
+      @shell.send(:always_force=, true)
  36
+    end
  37
+  end
  38
+
  39
+  def teardown
  40
+    super
  41
+    Rails.application = TestApp::Application.instance
  42
+  end
  43
+
  44
+  def test_plugin_skeleton_is_created
  45
+    run_generator
  46
+
  47
+    DEFAULT_PLUGIN_FILES.each{ |path| assert_file path }
  48
+  end
  49
+
  50
+  def test_plugin_new_generate_pretend
  51
+    run_generator ["testapp", "--pretend"]
  52
+
  53
+    DEFAULT_PLUGIN_FILES.each{ |path| assert_no_file path }
  54
+  end
  55
+
  56
+  def test_options_before_plugin_name_raises_an_error
  57
+    content = capture(:stderr){ run_generator(["--pretend", destination_root]) }
  58
+    assert_equal "Options should be given after plugin name. For details run: rails plugin --help\n", content
  59
+  end
  60
+
  61
+  def test_name_collision_raises_an_error
  62
+    reserved_words = %w[application destroy plugin runner test]
  63
+    reserved_words.each do |reserved|
  64
+      content = capture(:stderr){ run_generator [File.join(destination_root, reserved)] }
  65
+      assert_equal "Invalid plugin name #{reserved}. Please give a name which does not match one of the reserved rails words.\n", content
  66
+    end
  67
+  end
  68
+
  69
+  def test_invalid_plugin_name_raises_an_error
  70
+    content = capture(:stderr){ run_generator [File.join(destination_root, "43-things")] }
  71
+    assert_equal "Invalid plugin name 43-things. Please give a name which does not start with numbers.\n", content
  72
+  end
  73
+
  74
+  def test_plugin_name_raises_an_error_if_name_already_used_constant
  75
+    %w{ String Hash Class Module Set Symbol }.each do |ruby_class|
  76
+      content = capture(:stderr){ run_generator [File.join(destination_root, ruby_class)] }
  77
+      assert_equal "Invalid plugin name #{ruby_class}, constant #{ruby_class} is already in use. Please choose another application name.\n", content
  78
+    end
  79
+  end
  80
+
  81
+  def test_invalid_plugin_name_is_fixed
  82
+    run_generator [File.join(destination_root, "things-43")]
  83
+    assert_file "things-43/lib/things-43.rb", /module Things43/
  84
+  end
  85
+
  86
+  def test_shebang_is_added_to_rails_file
  87
+    run_generator [destination_root, "--ruby", "foo/bar/baz"]
  88
+    assert_file "script/rails", /#!foo\/bar\/baz/
  89
+  end
  90
+
  91
+  def test_shebang_when_is_the_same_as_default_use_env
  92
+    run_generator [destination_root, "--ruby", Thor::Util.ruby_command]
  93
+    assert_file "script/rails", /#!\/usr\/bin\/env/
  94
+  end
  95
+
  96
+  def test_generating_test_files
  97
+    run_generator
  98
+    assert_file "test/test_helper.rb"
  99
+    assert_directory "test/support/"
  100
+    assert_directory "test/integration/"
  101
+
  102
+    assert_file "test/bukkits_test.rb", /assert_kind_of Module, Bukkits/
  103
+    assert_file "test/integration/navigation_test.rb", /assert_kind_of Dummy::Application, Rails.application/
  104
+    assert_file "test/support/integration_case.rb", /class ActiveSupport::IntegrationCase/
  105
+  end
  106
+
  107
+protected
  108
+
  109
+  def action(*args, &block)
  110
+    silence(:stdout){ generator.send(*args, &block) }
  111
+  end
  112
+
  113
+end
  114
+
  115
+class CustomPluginGeneratorTest < Rails::Generators::TestCase
  116
+  include GeneratorsTestHelper
  117
+  tests Rails::Generators::PluginNewGenerator
  118
+
  119
+  destination File.join(Rails.root, "tmp/bukkits")
  120
+  arguments [destination_root]
  121
+
  122
+  def setup
  123
+    Rails.application = TestApp::Application
  124
+    super
  125
+    @bundle_command = File.basename(Thor::Util.ruby_command).sub(/ruby/, 'bundle')
  126
+  end
  127
+
  128
+  def teardown
  129
+    super
  130
+    Object.class_eval { remove_const :PluginBuilder if const_defined?(:PluginBuilder) }
  131
+    Rails.application = TestApp::Application.instance
  132
+  end
  133
+
  134
+  def test_builder_option_with_empty_app_builder
  135
+    FileUtils.cd(destination_root)
  136
+    run_generator([destination_root, "-b", "#{Rails.root}/lib/plugin_builders/empty_builder.rb"])
  137
+    DEFAULT_PLUGIN_FILES.each{ |path| assert_no_file path }
  138
+  end
  139
+
  140
+  def test_builder_option_with_simple_plugin_builder
  141
+    FileUtils.cd(destination_root)
  142
+    run_generator([destination_root, "-b", "#{Rails.root}/lib/plugin_builders/simple_builder.rb"])
  143
+    (DEFAULT_PLUGIN_FILES - ['.gitignore']).each{ |path| assert_no_file path }
  144
+    assert_file ".gitignore", "foobar"
  145
+  end
  146
+
  147
+  def test_builder_option_with_relative_path
  148
+    here = File.expand_path(File.dirname(__FILE__))
  149
+    FileUtils.cd(here)
  150
+    run_generator([destination_root, "-b", "../fixtures/lib/plugin_builders/simple_builder.rb"])
  151
+    FileUtils.cd(destination_root)
  152
+    (DEFAULT_PLUGIN_FILES - ['.gitignore']).each{ |path| assert_no_file path }
  153
+    assert_file ".gitignore", "foobar"
  154
+  end
  155
+
  156
+  def test_builder_option_with_tweak_plugin_builder
  157
+    FileUtils.cd(destination_root)
  158
+    run_generator([destination_root, "-b", "#{Rails.root}/lib/plugin_builders/tweak_builder.rb"])
  159
+    DEFAULT_PLUGIN_FILES.each{ |path| assert_file path }
  160
+    assert_file ".gitignore", "foobar"
  161
+  end
  162
+
  163
+  def test_builder_option_with_http
  164
+    path = "http://gist.github.com/103208.txt"
  165
+    template = "class PluginBuilder; end"
  166
+    template.instance_eval "def read; self; end" # Make the string respond to read
  167
+
  168
+    generator([destination_root], :builder => path).expects(:open).with(path, 'Accept' => 'application/x-thor-template').returns(template)
  169
+    capture(:stdout) { generator.invoke_all }
  170
+
  171
+    DEFAULT_PLUGIN_FILES.each{ |path| assert_no_file path }
  172
+  end
  173
+
  174
+protected
  175
+
  176
+  def action(*args, &block)
  177
+    silence(:stdout){ generator.send(*args, &block) }
  178
+  end
  179
+end

0 notes on commit cfcea1d

Please sign in to comment.
Something went wrong with that request. Please try again.