From ec8d8652f36bc4bf2ea19b8f7dd264187efc99ae Mon Sep 17 00:00:00 2001 From: schneems Date: Mon, 1 Jul 2013 00:02:19 +0300 Subject: s/plugin_new/plugin There are historical reasons that the `plugin` command was `plugin_new`, now those are no longer applicable, we should remove the naming edge case from the project. This PR is based off of comments from #11176 ATP Railties --- railties/CHANGELOG.md | 4 + railties/lib/rails/cli.rb | 2 +- railties/lib/rails/commands.rb | 2 +- railties/lib/rails/commands/plugin.rb | 9 + railties/lib/rails/commands/plugin_new.rb | 9 - railties/lib/rails/generators.rb | 2 +- railties/lib/rails/generators/rails/plugin/USAGE | 10 + .../generators/rails/plugin/plugin_generator.rb | 368 +++++++++++++++++++++ .../rails/plugin/templates/%name%.gemspec | 27 ++ .../generators/rails/plugin/templates/Gemfile | 30 ++ .../generators/rails/plugin/templates/MIT-LICENSE | 20 ++ .../generators/rails/plugin/templates/README.rdoc | 3 + .../generators/rails/plugin/templates/Rakefile | 25 ++ .../%name%/application_controller.rb.tt | 4 + .../app/helpers/%name%/application_helper.rb.tt | 4 + .../plugin/templates/app/mailers/.empty_directory | 0 .../plugin/templates/app/models/.empty_directory | 0 .../views/layouts/%name%/application.html.erb.tt | 14 + .../generators/rails/plugin/templates/bin/rails.tt | 7 + .../rails/plugin/templates/config/routes.rb | 6 + .../generators/rails/plugin/templates/gitignore | 10 + .../rails/plugin/templates/lib/%name%.rb | 6 + .../rails/plugin/templates/lib/%name%/engine.rb | 7 + .../rails/plugin/templates/lib/%name%/version.rb | 3 + .../plugin/templates/lib/tasks/%name%_tasks.rake | 4 + .../rails/plugin/templates/rails/application.rb | 17 + .../rails/plugin/templates/rails/boot.rb | 5 + .../rails/plugin/templates/rails/javascripts.js | 13 + .../rails/plugin/templates/rails/routes.rb | 4 + .../rails/plugin/templates/rails/stylesheets.css | 13 + .../rails/plugin/templates/test/%name%_test.rb | 7 + .../templates/test/integration/navigation_test.rb | 12 + .../rails/plugin/templates/test/test_helper.rb | 15 + .../lib/rails/generators/rails/plugin_new/USAGE | 10 - .../rails/plugin_new/plugin_new_generator.rb | 368 --------------------- .../rails/plugin_new/templates/%name%.gemspec | 27 -- .../generators/rails/plugin_new/templates/Gemfile | 30 -- .../rails/plugin_new/templates/MIT-LICENSE | 20 -- .../rails/plugin_new/templates/README.rdoc | 3 - .../generators/rails/plugin_new/templates/Rakefile | 25 -- .../%name%/application_controller.rb.tt | 4 - .../app/helpers/%name%/application_helper.rb.tt | 4 - .../templates/app/mailers/.empty_directory | 0 .../templates/app/models/.empty_directory | 0 .../views/layouts/%name%/application.html.erb.tt | 14 - .../rails/plugin_new/templates/bin/rails.tt | 7 - .../rails/plugin_new/templates/config/routes.rb | 6 - .../rails/plugin_new/templates/gitignore | 10 - .../rails/plugin_new/templates/lib/%name%.rb | 6 - .../plugin_new/templates/lib/%name%/engine.rb | 7 - .../plugin_new/templates/lib/%name%/version.rb | 3 - .../templates/lib/tasks/%name%_tasks.rake | 4 - .../plugin_new/templates/rails/application.rb | 17 - .../rails/plugin_new/templates/rails/boot.rb | 5 - .../plugin_new/templates/rails/javascripts.js | 13 - .../rails/plugin_new/templates/rails/routes.rb | 4 - .../plugin_new/templates/rails/stylesheets.css | 13 - .../rails/plugin_new/templates/test/%name%_test.rb | 7 - .../templates/test/integration/navigation_test.rb | 12 - .../rails/plugin_new/templates/test/test_helper.rb | 15 - railties/test/generators/plugin_generator_test.rb | 350 ++++++++++++++++++++ .../test/generators/plugin_new_generator_test.rb | 350 -------------------- railties/test/generators_test.rb | 2 +- 63 files changed, 1001 insertions(+), 997 deletions(-) create mode 100644 railties/lib/rails/commands/plugin.rb delete mode 100644 railties/lib/rails/commands/plugin_new.rb create mode 100644 railties/lib/rails/generators/rails/plugin/USAGE create mode 100644 railties/lib/rails/generators/rails/plugin/plugin_generator.rb create mode 100644 railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec create mode 100644 railties/lib/rails/generators/rails/plugin/templates/Gemfile create mode 100644 railties/lib/rails/generators/rails/plugin/templates/MIT-LICENSE create mode 100644 railties/lib/rails/generators/rails/plugin/templates/README.rdoc create mode 100644 railties/lib/rails/generators/rails/plugin/templates/Rakefile create mode 100644 railties/lib/rails/generators/rails/plugin/templates/app/controllers/%name%/application_controller.rb.tt create mode 100644 railties/lib/rails/generators/rails/plugin/templates/app/helpers/%name%/application_helper.rb.tt create mode 100644 railties/lib/rails/generators/rails/plugin/templates/app/mailers/.empty_directory create mode 100644 railties/lib/rails/generators/rails/plugin/templates/app/models/.empty_directory create mode 100644 railties/lib/rails/generators/rails/plugin/templates/app/views/layouts/%name%/application.html.erb.tt create mode 100644 railties/lib/rails/generators/rails/plugin/templates/bin/rails.tt create mode 100644 railties/lib/rails/generators/rails/plugin/templates/config/routes.rb create mode 100644 railties/lib/rails/generators/rails/plugin/templates/gitignore create mode 100644 railties/lib/rails/generators/rails/plugin/templates/lib/%name%.rb create mode 100644 railties/lib/rails/generators/rails/plugin/templates/lib/%name%/engine.rb create mode 100644 railties/lib/rails/generators/rails/plugin/templates/lib/%name%/version.rb create mode 100644 railties/lib/rails/generators/rails/plugin/templates/lib/tasks/%name%_tasks.rake create mode 100644 railties/lib/rails/generators/rails/plugin/templates/rails/application.rb create mode 100644 railties/lib/rails/generators/rails/plugin/templates/rails/boot.rb create mode 100644 railties/lib/rails/generators/rails/plugin/templates/rails/javascripts.js create mode 100644 railties/lib/rails/generators/rails/plugin/templates/rails/routes.rb create mode 100644 railties/lib/rails/generators/rails/plugin/templates/rails/stylesheets.css create mode 100644 railties/lib/rails/generators/rails/plugin/templates/test/%name%_test.rb create mode 100644 railties/lib/rails/generators/rails/plugin/templates/test/integration/navigation_test.rb create mode 100644 railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb delete mode 100644 railties/lib/rails/generators/rails/plugin_new/USAGE delete mode 100644 railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/Gemfile delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/MIT-LICENSE delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/README.rdoc delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/Rakefile delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/app/controllers/%name%/application_controller.rb.tt delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/app/helpers/%name%/application_helper.rb.tt delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/app/mailers/.empty_directory delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/bin/rails.tt delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/config/routes.rb delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/gitignore delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%/engine.rb delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%/version.rb delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/lib/tasks/%name%_tasks.rake delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/rails/javascripts.js delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/rails/routes.rb delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/rails/stylesheets.css delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/test/%name%_test.rb delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/test/integration/navigation_test.rb delete mode 100644 railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb create mode 100644 railties/test/generators/plugin_generator_test.rb delete mode 100644 railties/test/generators/plugin_new_generator_test.rb (limited to 'railties') diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 639dd70152..a7132d0a9a 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,7 @@ +* Rename `commands/plugin_new.rb` to `commands/plugin.rb` and fix references + + *Richard Schneeman* + * Fix `rails plugin --help` command. *Richard Schneeman* diff --git a/railties/lib/rails/cli.rb b/railties/lib/rails/cli.rb index e5341ac436..20313a2608 100644 --- a/railties/lib/rails/cli.rb +++ b/railties/lib/rails/cli.rb @@ -10,7 +10,7 @@ Signal.trap("INT") { puts; exit(1) } if ARGV.first == 'plugin' ARGV.shift - require 'rails/commands/plugin_new' + require 'rails/commands/plugin' else require 'rails/commands/application' end diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index c2ce2b4d2a..3e8163490f 100644 --- a/railties/lib/rails/commands.rb +++ b/railties/lib/rails/commands.rb @@ -36,7 +36,7 @@ command = aliases[command] || command case command when 'plugin' - require "rails/commands/plugin_new" + require "rails/commands/plugin" when 'generate', 'destroy' require 'rails/generators' diff --git a/railties/lib/rails/commands/plugin.rb b/railties/lib/rails/commands/plugin.rb new file mode 100644 index 0000000000..837fe0ec10 --- /dev/null +++ b/railties/lib/rails/commands/plugin.rb @@ -0,0 +1,9 @@ +if ARGV.first != "new" + ARGV[0] = "--help" +else + ARGV.shift +end + +require 'rails/generators' +require 'rails/generators/rails/plugin/plugin_generator' +Rails::Generators::PluginGenerator.start diff --git a/railties/lib/rails/commands/plugin_new.rb b/railties/lib/rails/commands/plugin_new.rb deleted file mode 100644 index 4d7bf3c9f3..0000000000 --- a/railties/lib/rails/commands/plugin_new.rb +++ /dev/null @@ -1,9 +0,0 @@ -if ARGV.first != "new" - ARGV[0] = "--help" -else - ARGV.shift -end - -require 'rails/generators' -require 'rails/generators/rails/plugin_new/plugin_new_generator' -Rails::Generators::PluginNewGenerator.start \ No newline at end of file diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index 4b767ea0c6..6b34db3e3f 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -225,7 +225,7 @@ module Rails rails = groups.delete("rails") rails.map! { |n| n.sub(/^rails:/, '') } rails.delete("app") - rails.delete("plugin_new") + rails.delete("plugin") print_list("rails", rails) hidden_namespaces.each { |n| groups.delete(n.to_s) } diff --git a/railties/lib/rails/generators/rails/plugin/USAGE b/railties/lib/rails/generators/rails/plugin/USAGE new file mode 100644 index 0000000000..9a7bf9f396 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/USAGE @@ -0,0 +1,10 @@ +Description: + The 'rails plugin new' command creates a skeleton for developing any + kind of Rails extension with ability to run tests using dummy Rails + application. + +Example: + rails plugin new ~/Code/Ruby/blog + + This generates a skeletal Rails plugin in ~/Code/Ruby/blog. + See the README in the newly created plugin to get going. diff --git a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb new file mode 100644 index 0000000000..13f5472ede --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb @@ -0,0 +1,368 @@ +require 'active_support/core_ext/hash/slice' +require "rails/generators/rails/app/app_generator" +require 'date' + +module Rails + # The plugin builder allows you to override elements of the plugin + # generator without being forced to reverse the operations of the default + # generator. + # + # This allows you to override entire operations, like the creation of the + # Gemfile, README, or JavaScript files, without needing to know exactly + # what those operations do so you can create another template action. + class PluginBuilder + def rakefile + template "Rakefile" + end + + def app + if mountable? + directory 'app' + empty_directory_with_keep_file "app/assets/images/#{name}" + elsif full? + empty_directory_with_keep_file 'app/models' + empty_directory_with_keep_file 'app/controllers' + empty_directory_with_keep_file 'app/views' + empty_directory_with_keep_file 'app/helpers' + empty_directory_with_keep_file 'app/mailers' + empty_directory_with_keep_file "app/assets/images/#{name}" + end + end + + def readme + template "README.rdoc" + end + + def gemfile + template "Gemfile" + end + + def license + template "MIT-LICENSE" + end + + def gemspec + template "%name%.gemspec" + end + + def gitignore + template "gitignore", ".gitignore" + end + + def lib + template "lib/%name%.rb" + template "lib/tasks/%name%_tasks.rake" + template "lib/%name%/version.rb" + template "lib/%name%/engine.rb" if engine? + end + + def config + template "config/routes.rb" if engine? + end + + def test + template "test/test_helper.rb" + template "test/%name%_test.rb" + append_file "Rakefile", <<-EOF +#{rakefile_test_tasks} + +task default: :test + EOF + if engine? + template "test/integration/navigation_test.rb" + end + end + + PASSTHROUGH_OPTIONS = [ + :skip_active_record, :skip_javascript, :database, :javascript, :quiet, :pretend, :force, :skip + ] + + def generate_test_dummy(force = false) + opts = (options || {}).slice(*PASSTHROUGH_OPTIONS) + opts[:force] = force + opts[:skip_bundle] = true + + invoke Rails::Generators::AppGenerator, + [ File.expand_path(dummy_path, destination_root) ], opts + end + + def test_dummy_config + template "rails/boot.rb", "#{dummy_path}/config/boot.rb", force: true + template "rails/application.rb", "#{dummy_path}/config/application.rb", force: true + if mountable? + template "rails/routes.rb", "#{dummy_path}/config/routes.rb", force: true + end + end + + def test_dummy_assets + template "rails/javascripts.js", "#{dummy_path}/app/assets/javascripts/application.js", force: true + template "rails/stylesheets.css", "#{dummy_path}/app/assets/stylesheets/application.css", force: true + end + + def test_dummy_clean + inside dummy_path do + remove_file ".gitignore" + remove_file "db/seeds.rb" + remove_file "doc" + remove_file "Gemfile" + remove_file "lib/tasks" + remove_file "public/robots.txt" + remove_file "README" + remove_file "test" + remove_file "vendor" + end + end + + def stylesheets + if mountable? + copy_file "rails/stylesheets.css", + "app/assets/stylesheets/#{name}/application.css" + elsif full? + empty_directory_with_keep_file "app/assets/stylesheets/#{name}" + end + end + + def javascripts + return if options.skip_javascript? + + if mountable? + template "rails/javascripts.js", + "app/assets/javascripts/#{name}/application.js" + elsif full? + empty_directory_with_keep_file "app/assets/javascripts/#{name}" + end + end + + def bin(force = false) + return unless engine? + + directory "bin", force: force do |content| + "#{shebang}\n" + content + end + chmod "bin", 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 + class PluginGenerator < AppBase # :nodoc: + add_shared_options_for "plugin" + + alias_method :plugin_path, :app_path + + class_option :dummy_path, type: :string, default: "test/dummy", + desc: "Create dummy application at given path" + + class_option :full, type: :boolean, default: false, + desc: "Generate a rails engine with bundled Rails application for testing" + + class_option :mountable, type: :boolean, default: false, + desc: "Generate mountable isolated application" + + 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 new --help" if args[0].blank? + + @dummy_path = nil + super + end + + public_task :create_root + + def create_root_files + build(:readme) + build(:rakefile) + build(:gemspec) unless options[:skip_gemspec] + build(:license) + build(:gitignore) unless options[:skip_git] + build(:gemfile) unless options[:skip_gemfile] + end + + def create_app_files + build(:app) + end + + def create_config_files + build(:config) + end + + def create_lib_files + build(:lib) + end + + def create_public_stylesheets_files + build(:stylesheets) + end + + def create_javascript_files + build(:javascripts) + end + + def create_images_directory + build(:images) + end + + def create_bin_files + build(:bin) + end + + def create_test_files + build(:test) unless options[:skip_test_unit] + end + + def create_test_dummy_files + return unless with_dummy_app? + create_dummy_app + end + + def update_gemfile + build(:gemfile_entry) unless options[:skip_gemfile_entry] + end + + def finish_template + build(:leftovers) + end + + public_task :apply_rails_template, :run_bundle + + def name + @name ||= begin + # same as ActiveSupport::Inflector#underscore except not replacing '-' + underscored = original_name.dup + underscored.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') + underscored.gsub!(/([a-z\d])([A-Z])/,'\1_\2') + underscored.downcase! + + underscored + end + end + + protected + + def app_templates_dir + "../../app/templates" + end + + def create_dummy_app(path = nil) + dummy_path(path) if path + + say_status :vendor_app, dummy_path + mute do + build(:generate_test_dummy) + store_application_definition! + build(:test_dummy_config) + build(:test_dummy_assets) + build(:test_dummy_clean) + # ensure that bin/rails has proper dummy_path + build(:bin, true) + end + end + + def engine? + full? || mountable? + end + + def full? + options[:full] + end + + def mountable? + options[:mountable] + end + + def with_dummy_app? + options[:skip_test_unit].blank? || options[:dummy_path] != 'test/dummy' + end + + def self.banner + "rails plugin new #{self.arguments.map(&:usage).join(' ')} [options]" + end + + def original_name + @original_name ||= File.basename(destination_root) + end + + def camelized + @camelized ||= name.gsub(/\W/, '_').squeeze('_').camelize + end + + def valid_const? + if original_name =~ /[^0-9a-zA-Z_]+/ + raise Error, "Invalid plugin name #{original_name}. Please give a name which use only alphabetic or numeric or \"_\" characters." + elsif camelized =~ /^\d/ + raise Error, "Invalid plugin name #{original_name}. Please give a name which does not start with numbers." + elsif RESERVED_NAMES.include?(name) + raise Error, "Invalid plugin name #{original_name}. Please give a name which does not match one of the reserved rails words." + elsif Object.const_defined?(camelized) + raise Error, "Invalid plugin name #{original_name}, constant #{camelized} is already in use. Please choose another plugin name." + end + end + + def application_definition + @application_definition ||= begin + + dummy_application_path = File.expand_path("#{dummy_path}/config/application.rb", destination_root) + unless options[:pretend] || !File.exists?(dummy_application_path) + contents = File.read(dummy_application_path) + contents[(contents.index(/module ([\w]+)\n(.*)class Application/m))..-1] + end + end + end + alias :store_application_definition! :application_definition + + def get_builder_class + defined?(::PluginBuilder) ? ::PluginBuilder : Rails::PluginBuilder + end + + def rakefile_test_tasks + <<-RUBY +require 'rake/testtask' + +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.libs << 'test' + t.pattern = 'test/**/*_test.rb' + t.verbose = false +end + RUBY + end + + def dummy_path(path = nil) + @dummy_path = path if path + @dummy_path || options[:dummy_path] + end + + 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 diff --git a/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec b/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec new file mode 100644 index 0000000000..5fdf0e1554 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec @@ -0,0 +1,27 @@ +$:.push File.expand_path("../lib", __FILE__) + +# Maintain your gem's version: +require "<%= name %>/version" + +# Describe your gem and declare its dependencies: +Gem::Specification.new do |s| + s.name = "<%= name %>" + s.version = <%= camelized %>::VERSION + s.authors = ["TODO: Your name"] + s.email = ["TODO: Your email"] + s.homepage = "TODO" + s.summary = "TODO: Summary of <%= camelized %>." + s.description = "TODO: Description of <%= camelized %>." + s.license = "MIT" + + s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] +<% unless options.skip_test_unit? -%> + s.test_files = Dir["test/**/*"] +<% end -%> + + <%= '# ' if options.dev? || options.edge? -%>s.add_dependency "rails", "~> <%= Rails::VERSION::STRING %>" +<% unless options[:skip_active_record] -%> + + s.add_development_dependency "<%= gem_for_database %>" +<% end -%> +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/Gemfile b/railties/lib/rails/generators/rails/plugin/templates/Gemfile new file mode 100644 index 0000000000..3f2b78f2fd --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/Gemfile @@ -0,0 +1,30 @@ +source "https://rubygems.org" + +<% if options[:skip_gemspec] -%> +<%= '# ' if options.dev? || options.edge? -%>gem "rails", "~> <%= Rails::VERSION::STRING %>" +<% else -%> +# Declare your gem's dependencies in <%= name %>.gemspec. +# Bundler will treat runtime dependencies like base dependencies, and +# development dependencies will be added by default to the :development group. +gemspec +<% end -%> + +<% if options[:skip_gemspec] -%> +group :development do + gem "<%= gem_for_database %>" +end +<% else -%> +# Declare any dependencies that are still in development here instead of in +# your gemspec. These might include edge Rails or gems from your path or +# Git. Remember to move these dependencies to your gemspec before releasing +# your gem to rubygems.org. +<% end -%> + +<% if options.dev? || options.edge? -%> +# Your gem is dependent on dev or edge Rails. Once you can lock this +# dependency down to a specific version, move it to your gemspec. +<%= rails_gemfile_entry -%> + +<% end -%> +# To use debugger +# gem 'debugger' diff --git a/railties/lib/rails/generators/rails/plugin/templates/MIT-LICENSE b/railties/lib/rails/generators/rails/plugin/templates/MIT-LICENSE new file mode 100644 index 0000000000..d7a9109894 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright <%= Date.today.year %> YOURNAME + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/railties/lib/rails/generators/rails/plugin/templates/README.rdoc b/railties/lib/rails/generators/rails/plugin/templates/README.rdoc new file mode 100644 index 0000000000..301d647731 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/README.rdoc @@ -0,0 +1,3 @@ += <%= camelized %> + +This project rocks and uses MIT-LICENSE. \ No newline at end of file diff --git a/railties/lib/rails/generators/rails/plugin/templates/Rakefile b/railties/lib/rails/generators/rails/plugin/templates/Rakefile new file mode 100644 index 0000000000..0ba899176c --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/Rakefile @@ -0,0 +1,25 @@ +begin + require 'bundler/setup' +rescue LoadError + puts 'You must `gem install bundler` and `bundle install` to run rake tasks' +end + +require 'rdoc/task' + +RDoc::Task.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = '<%= camelized %>' + rdoc.options << '--line-numbers' + rdoc.rdoc_files.include('README.rdoc') + rdoc.rdoc_files.include('lib/**/*.rb') +end + +<% if engine? && !options[:skip_active_record] && with_dummy_app? -%> +APP_RAKEFILE = File.expand_path("../<%= dummy_path -%>/Rakefile", __FILE__) +load 'rails/tasks/engine.rake' +<% end %> + +<% unless options[:skip_gemspec] -%> + +Bundler::GemHelper.install_tasks +<% end %> diff --git a/railties/lib/rails/generators/rails/plugin/templates/app/controllers/%name%/application_controller.rb.tt b/railties/lib/rails/generators/rails/plugin/templates/app/controllers/%name%/application_controller.rb.tt new file mode 100644 index 0000000000..448ad7f989 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/app/controllers/%name%/application_controller.rb.tt @@ -0,0 +1,4 @@ +module <%= camelized %> + class ApplicationController < ActionController::Base + end +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/app/helpers/%name%/application_helper.rb.tt b/railties/lib/rails/generators/rails/plugin/templates/app/helpers/%name%/application_helper.rb.tt new file mode 100644 index 0000000000..40ae9f52c2 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/app/helpers/%name%/application_helper.rb.tt @@ -0,0 +1,4 @@ +module <%= camelized %> + module ApplicationHelper + end +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/app/mailers/.empty_directory b/railties/lib/rails/generators/rails/plugin/templates/app/mailers/.empty_directory new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/lib/rails/generators/rails/plugin/templates/app/models/.empty_directory b/railties/lib/rails/generators/rails/plugin/templates/app/models/.empty_directory new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/lib/rails/generators/rails/plugin/templates/app/views/layouts/%name%/application.html.erb.tt b/railties/lib/rails/generators/rails/plugin/templates/app/views/layouts/%name%/application.html.erb.tt new file mode 100644 index 0000000000..1d380420b4 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/app/views/layouts/%name%/application.html.erb.tt @@ -0,0 +1,14 @@ + + + + <%= camelized %> + <%%= stylesheet_link_tag "<%= name %>/application", media: "all" %> + <%%= javascript_include_tag "<%= name %>/application" %> + <%%= csrf_meta_tags %> + + + +<%%= yield %> + + + diff --git a/railties/lib/rails/generators/rails/plugin/templates/bin/rails.tt b/railties/lib/rails/generators/rails/plugin/templates/bin/rails.tt new file mode 100644 index 0000000000..c8de9f3e0f --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/bin/rails.tt @@ -0,0 +1,7 @@ +# This command will automatically be run when you run "rails" with Rails 4 gems installed from the root of your application. + +ENGINE_ROOT = File.expand_path('../..', __FILE__) +ENGINE_PATH = File.expand_path('../../lib/<%= name -%>/engine', __FILE__) + +require 'rails/all' +require 'rails/engine/commands' diff --git a/railties/lib/rails/generators/rails/plugin/templates/config/routes.rb b/railties/lib/rails/generators/rails/plugin/templates/config/routes.rb new file mode 100644 index 0000000000..8e158d5831 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/config/routes.rb @@ -0,0 +1,6 @@ +<% if mountable? -%> +<%= camelized %>::Engine.routes.draw do +<% else -%> +Rails.application.routes.draw do +<% end -%> +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/gitignore b/railties/lib/rails/generators/rails/plugin/templates/gitignore new file mode 100644 index 0000000000..086d87818a --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/gitignore @@ -0,0 +1,10 @@ +.bundle/ +log/*.log +pkg/ +<% unless options[:skip_test_unit] && options[:dummy_path] == 'test/dummy' -%> +<%= dummy_path %>/db/*.sqlite3 +<%= dummy_path %>/db/*.sqlite3-journal +<%= dummy_path %>/log/*.log +<%= dummy_path %>/tmp/ +<%= dummy_path %>/.sass-cache +<% end -%> \ No newline at end of file diff --git a/railties/lib/rails/generators/rails/plugin/templates/lib/%name%.rb b/railties/lib/rails/generators/rails/plugin/templates/lib/%name%.rb new file mode 100644 index 0000000000..40c074cced --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/lib/%name%.rb @@ -0,0 +1,6 @@ +<% if engine? -%> +require "<%= name %>/engine" + +<% end -%> +module <%= camelized %> +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/lib/%name%/engine.rb b/railties/lib/rails/generators/rails/plugin/templates/lib/%name%/engine.rb new file mode 100644 index 0000000000..967668fe66 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/lib/%name%/engine.rb @@ -0,0 +1,7 @@ +module <%= camelized %> + class Engine < ::Rails::Engine +<% if mountable? -%> + isolate_namespace <%= camelized %> +<% end -%> + end +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/lib/%name%/version.rb b/railties/lib/rails/generators/rails/plugin/templates/lib/%name%/version.rb new file mode 100644 index 0000000000..ef07ef2e19 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/lib/%name%/version.rb @@ -0,0 +1,3 @@ +module <%= camelized %> + VERSION = "0.0.1" +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/lib/tasks/%name%_tasks.rake b/railties/lib/rails/generators/rails/plugin/templates/lib/tasks/%name%_tasks.rake new file mode 100644 index 0000000000..7121f5ae23 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/lib/tasks/%name%_tasks.rake @@ -0,0 +1,4 @@ +# desc "Explaining what the task does" +# task :<%= name %> do +# # Task goes here +# end diff --git a/railties/lib/rails/generators/rails/plugin/templates/rails/application.rb b/railties/lib/rails/generators/rails/plugin/templates/rails/application.rb new file mode 100644 index 0000000000..310c975262 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/rails/application.rb @@ -0,0 +1,17 @@ +require File.expand_path('../boot', __FILE__) + +<% if include_all_railties? -%> +require 'rails/all' +<% else -%> +# Pick the frameworks you want: +<%= comment_if :skip_active_record %>require "active_record/railtie" +require "action_controller/railtie" +require "action_mailer/railtie" +<%= comment_if :skip_sprockets %>require "sprockets/railtie" +<%= comment_if :skip_test_unit %>require "rails/test_unit/railtie" +<% end -%> + +Bundler.require(*Rails.groups) +require "<%= name %>" + +<%= application_definition %> diff --git a/railties/lib/rails/generators/rails/plugin/templates/rails/boot.rb b/railties/lib/rails/generators/rails/plugin/templates/rails/boot.rb new file mode 100644 index 0000000000..ef360470a3 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/rails/boot.rb @@ -0,0 +1,5 @@ +# Set up gems listed in the Gemfile. +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__) + +require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) +$LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__) diff --git a/railties/lib/rails/generators/rails/plugin/templates/rails/javascripts.js b/railties/lib/rails/generators/rails/plugin/templates/rails/javascripts.js new file mode 100644 index 0000000000..5bc2e1c8b5 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/rails/javascripts.js @@ -0,0 +1,13 @@ +// This is a manifest file that'll be compiled into application.js, which will include all the files +// listed below. +// +// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, +// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. +// +// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the +// compiled file. +// +// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details +// about supported directives. +// +//= require_tree . diff --git a/railties/lib/rails/generators/rails/plugin/templates/rails/routes.rb b/railties/lib/rails/generators/rails/plugin/templates/rails/routes.rb new file mode 100644 index 0000000000..730ee31c3d --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/rails/routes.rb @@ -0,0 +1,4 @@ +Rails.application.routes.draw do + + mount <%= camelized %>::Engine => "/<%= name %>" +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/rails/stylesheets.css b/railties/lib/rails/generators/rails/plugin/templates/rails/stylesheets.css new file mode 100644 index 0000000000..3192ec897b --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/rails/stylesheets.css @@ -0,0 +1,13 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, + * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path. + * + * You're free to add application-wide styles to this file and they'll appear at the top of the + * compiled file, but it's generally better to create a new file per style scope. + * + *= require_self + *= require_tree . + */ diff --git a/railties/lib/rails/generators/rails/plugin/templates/test/%name%_test.rb b/railties/lib/rails/generators/rails/plugin/templates/test/%name%_test.rb new file mode 100644 index 0000000000..0a8bbd4aaf --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/test/%name%_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class <%= camelized %>Test < ActiveSupport::TestCase + test "truth" do + assert_kind_of Module, <%= camelized %> + end +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/test/integration/navigation_test.rb b/railties/lib/rails/generators/rails/plugin/templates/test/integration/navigation_test.rb new file mode 100644 index 0000000000..824caecb24 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/test/integration/navigation_test.rb @@ -0,0 +1,12 @@ +require 'test_helper' + +class NavigationTest < ActionDispatch::IntegrationTest +<% unless options[:skip_active_record] -%> + fixtures :all +<% end -%> + + # test "the truth" do + # assert true + # end +end + diff --git a/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb b/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb new file mode 100644 index 0000000000..1e26a313cd --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb @@ -0,0 +1,15 @@ +# Configure Rails Environment +ENV["RAILS_ENV"] = "test" + +require File.expand_path("../dummy/config/environment.rb", __FILE__) +require "rails/test_help" + +Rails.backtrace_cleaner.remove_silencers! + +# Load support files +Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } + +# Load fixtures from the engine +if ActiveSupport::TestCase.method_defined?(:fixture_path=) + ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__) +end diff --git a/railties/lib/rails/generators/rails/plugin_new/USAGE b/railties/lib/rails/generators/rails/plugin_new/USAGE deleted file mode 100644 index 9a7bf9f396..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/USAGE +++ /dev/null @@ -1,10 +0,0 @@ -Description: - The 'rails plugin new' command creates a skeleton for developing any - kind of Rails extension with ability to run tests using dummy Rails - application. - -Example: - rails plugin new ~/Code/Ruby/blog - - This generates a skeletal Rails plugin in ~/Code/Ruby/blog. - See the README in the newly created plugin to get going. diff --git a/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb b/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb deleted file mode 100644 index 850c9d5c0d..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +++ /dev/null @@ -1,368 +0,0 @@ -require 'active_support/core_ext/hash/slice' -require "rails/generators/rails/app/app_generator" -require 'date' - -module Rails - # The plugin builder allows you to override elements of the plugin - # generator without being forced to reverse the operations of the default - # generator. - # - # This allows you to override entire operations, like the creation of the - # Gemfile, README, or JavaScript files, without needing to know exactly - # what those operations do so you can create another template action. - class PluginBuilder - def rakefile - template "Rakefile" - end - - def app - if mountable? - directory 'app' - empty_directory_with_keep_file "app/assets/images/#{name}" - elsif full? - empty_directory_with_keep_file 'app/models' - empty_directory_with_keep_file 'app/controllers' - empty_directory_with_keep_file 'app/views' - empty_directory_with_keep_file 'app/helpers' - empty_directory_with_keep_file 'app/mailers' - empty_directory_with_keep_file "app/assets/images/#{name}" - end - end - - def readme - template "README.rdoc" - end - - def gemfile - template "Gemfile" - end - - def license - template "MIT-LICENSE" - end - - def gemspec - template "%name%.gemspec" - end - - def gitignore - template "gitignore", ".gitignore" - end - - def lib - template "lib/%name%.rb" - template "lib/tasks/%name%_tasks.rake" - template "lib/%name%/version.rb" - template "lib/%name%/engine.rb" if engine? - end - - def config - template "config/routes.rb" if engine? - end - - def test - template "test/test_helper.rb" - template "test/%name%_test.rb" - append_file "Rakefile", <<-EOF -#{rakefile_test_tasks} - -task default: :test - EOF - if engine? - template "test/integration/navigation_test.rb" - end - end - - PASSTHROUGH_OPTIONS = [ - :skip_active_record, :skip_javascript, :database, :javascript, :quiet, :pretend, :force, :skip - ] - - def generate_test_dummy(force = false) - opts = (options || {}).slice(*PASSTHROUGH_OPTIONS) - opts[:force] = force - opts[:skip_bundle] = true - - invoke Rails::Generators::AppGenerator, - [ File.expand_path(dummy_path, destination_root) ], opts - end - - def test_dummy_config - template "rails/boot.rb", "#{dummy_path}/config/boot.rb", force: true - template "rails/application.rb", "#{dummy_path}/config/application.rb", force: true - if mountable? - template "rails/routes.rb", "#{dummy_path}/config/routes.rb", force: true - end - end - - def test_dummy_assets - template "rails/javascripts.js", "#{dummy_path}/app/assets/javascripts/application.js", force: true - template "rails/stylesheets.css", "#{dummy_path}/app/assets/stylesheets/application.css", force: true - end - - def test_dummy_clean - inside dummy_path do - remove_file ".gitignore" - remove_file "db/seeds.rb" - remove_file "doc" - remove_file "Gemfile" - remove_file "lib/tasks" - remove_file "public/robots.txt" - remove_file "README" - remove_file "test" - remove_file "vendor" - end - end - - def stylesheets - if mountable? - copy_file "rails/stylesheets.css", - "app/assets/stylesheets/#{name}/application.css" - elsif full? - empty_directory_with_keep_file "app/assets/stylesheets/#{name}" - end - end - - def javascripts - return if options.skip_javascript? - - if mountable? - template "rails/javascripts.js", - "app/assets/javascripts/#{name}/application.js" - elsif full? - empty_directory_with_keep_file "app/assets/javascripts/#{name}" - end - end - - def bin(force = false) - return unless engine? - - directory "bin", force: force do |content| - "#{shebang}\n" + content - end - chmod "bin", 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 - class PluginNewGenerator < AppBase # :nodoc: - add_shared_options_for "plugin" - - alias_method :plugin_path, :app_path - - class_option :dummy_path, type: :string, default: "test/dummy", - desc: "Create dummy application at given path" - - class_option :full, type: :boolean, default: false, - desc: "Generate a rails engine with bundled Rails application for testing" - - class_option :mountable, type: :boolean, default: false, - desc: "Generate mountable isolated application" - - 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 new --help" if args[0].blank? - - @dummy_path = nil - super - end - - public_task :create_root - - def create_root_files - build(:readme) - build(:rakefile) - build(:gemspec) unless options[:skip_gemspec] - build(:license) - build(:gitignore) unless options[:skip_git] - build(:gemfile) unless options[:skip_gemfile] - end - - def create_app_files - build(:app) - end - - def create_config_files - build(:config) - end - - def create_lib_files - build(:lib) - end - - def create_public_stylesheets_files - build(:stylesheets) - end - - def create_javascript_files - build(:javascripts) - end - - def create_images_directory - build(:images) - end - - def create_bin_files - build(:bin) - end - - def create_test_files - build(:test) unless options[:skip_test_unit] - end - - def create_test_dummy_files - return unless with_dummy_app? - create_dummy_app - end - - def update_gemfile - build(:gemfile_entry) unless options[:skip_gemfile_entry] - end - - def finish_template - build(:leftovers) - end - - public_task :apply_rails_template, :run_bundle - - def name - @name ||= begin - # same as ActiveSupport::Inflector#underscore except not replacing '-' - underscored = original_name.dup - underscored.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') - underscored.gsub!(/([a-z\d])([A-Z])/,'\1_\2') - underscored.downcase! - - underscored - end - end - - protected - - def app_templates_dir - "../../app/templates" - end - - def create_dummy_app(path = nil) - dummy_path(path) if path - - say_status :vendor_app, dummy_path - mute do - build(:generate_test_dummy) - store_application_definition! - build(:test_dummy_config) - build(:test_dummy_assets) - build(:test_dummy_clean) - # ensure that bin/rails has proper dummy_path - build(:bin, true) - end - end - - def engine? - full? || mountable? - end - - def full? - options[:full] - end - - def mountable? - options[:mountable] - end - - def with_dummy_app? - options[:skip_test_unit].blank? || options[:dummy_path] != 'test/dummy' - end - - def self.banner - "rails plugin new #{self.arguments.map(&:usage).join(' ')} [options]" - end - - def original_name - @original_name ||= File.basename(destination_root) - end - - def camelized - @camelized ||= name.gsub(/\W/, '_').squeeze('_').camelize - end - - def valid_const? - if original_name =~ /[^0-9a-zA-Z_]+/ - raise Error, "Invalid plugin name #{original_name}. Please give a name which use only alphabetic or numeric or \"_\" characters." - elsif camelized =~ /^\d/ - raise Error, "Invalid plugin name #{original_name}. Please give a name which does not start with numbers." - elsif RESERVED_NAMES.include?(name) - raise Error, "Invalid plugin name #{original_name}. Please give a name which does not match one of the reserved rails words." - elsif Object.const_defined?(camelized) - raise Error, "Invalid plugin name #{original_name}, constant #{camelized} is already in use. Please choose another plugin name." - end - end - - def application_definition - @application_definition ||= begin - - dummy_application_path = File.expand_path("#{dummy_path}/config/application.rb", destination_root) - unless options[:pretend] || !File.exists?(dummy_application_path) - contents = File.read(dummy_application_path) - contents[(contents.index(/module ([\w]+)\n(.*)class Application/m))..-1] - end - end - end - alias :store_application_definition! :application_definition - - def get_builder_class - defined?(::PluginBuilder) ? ::PluginBuilder : Rails::PluginBuilder - end - - def rakefile_test_tasks - <<-RUBY -require 'rake/testtask' - -Rake::TestTask.new(:test) do |t| - t.libs << 'lib' - t.libs << 'test' - t.pattern = 'test/**/*_test.rb' - t.verbose = false -end - RUBY - end - - def dummy_path(path = nil) - @dummy_path = path if path - @dummy_path || options[:dummy_path] - end - - 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 diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec b/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec deleted file mode 100644 index 5fdf0e1554..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec +++ /dev/null @@ -1,27 +0,0 @@ -$:.push File.expand_path("../lib", __FILE__) - -# Maintain your gem's version: -require "<%= name %>/version" - -# Describe your gem and declare its dependencies: -Gem::Specification.new do |s| - s.name = "<%= name %>" - s.version = <%= camelized %>::VERSION - s.authors = ["TODO: Your name"] - s.email = ["TODO: Your email"] - s.homepage = "TODO" - s.summary = "TODO: Summary of <%= camelized %>." - s.description = "TODO: Description of <%= camelized %>." - s.license = "MIT" - - s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] -<% unless options.skip_test_unit? -%> - s.test_files = Dir["test/**/*"] -<% end -%> - - <%= '# ' if options.dev? || options.edge? -%>s.add_dependency "rails", "~> <%= Rails::VERSION::STRING %>" -<% unless options[:skip_active_record] -%> - - s.add_development_dependency "<%= gem_for_database %>" -<% end -%> -end diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile b/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile deleted file mode 100644 index 3f2b78f2fd..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile +++ /dev/null @@ -1,30 +0,0 @@ -source "https://rubygems.org" - -<% if options[:skip_gemspec] -%> -<%= '# ' if options.dev? || options.edge? -%>gem "rails", "~> <%= Rails::VERSION::STRING %>" -<% else -%> -# Declare your gem's dependencies in <%= name %>.gemspec. -# Bundler will treat runtime dependencies like base dependencies, and -# development dependencies will be added by default to the :development group. -gemspec -<% end -%> - -<% if options[:skip_gemspec] -%> -group :development do - gem "<%= gem_for_database %>" -end -<% else -%> -# Declare any dependencies that are still in development here instead of in -# your gemspec. These might include edge Rails or gems from your path or -# Git. Remember to move these dependencies to your gemspec before releasing -# your gem to rubygems.org. -<% end -%> - -<% if options.dev? || options.edge? -%> -# Your gem is dependent on dev or edge Rails. Once you can lock this -# dependency down to a specific version, move it to your gemspec. -<%= rails_gemfile_entry -%> - -<% end -%> -# To use debugger -# gem 'debugger' diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/MIT-LICENSE b/railties/lib/rails/generators/rails/plugin_new/templates/MIT-LICENSE deleted file mode 100644 index d7a9109894..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/MIT-LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -Copyright <%= Date.today.year %> YOURNAME - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/README.rdoc b/railties/lib/rails/generators/rails/plugin_new/templates/README.rdoc deleted file mode 100644 index 301d647731..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/README.rdoc +++ /dev/null @@ -1,3 +0,0 @@ -= <%= camelized %> - -This project rocks and uses MIT-LICENSE. \ No newline at end of file diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile b/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile deleted file mode 100644 index 0ba899176c..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile +++ /dev/null @@ -1,25 +0,0 @@ -begin - require 'bundler/setup' -rescue LoadError - puts 'You must `gem install bundler` and `bundle install` to run rake tasks' -end - -require 'rdoc/task' - -RDoc::Task.new(:rdoc) do |rdoc| - rdoc.rdoc_dir = 'rdoc' - rdoc.title = '<%= camelized %>' - rdoc.options << '--line-numbers' - rdoc.rdoc_files.include('README.rdoc') - rdoc.rdoc_files.include('lib/**/*.rb') -end - -<% if engine? && !options[:skip_active_record] && with_dummy_app? -%> -APP_RAKEFILE = File.expand_path("../<%= dummy_path -%>/Rakefile", __FILE__) -load 'rails/tasks/engine.rake' -<% end %> - -<% unless options[:skip_gemspec] -%> - -Bundler::GemHelper.install_tasks -<% end %> diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/app/controllers/%name%/application_controller.rb.tt b/railties/lib/rails/generators/rails/plugin_new/templates/app/controllers/%name%/application_controller.rb.tt deleted file mode 100644 index 448ad7f989..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/app/controllers/%name%/application_controller.rb.tt +++ /dev/null @@ -1,4 +0,0 @@ -module <%= camelized %> - class ApplicationController < ActionController::Base - end -end diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/app/helpers/%name%/application_helper.rb.tt b/railties/lib/rails/generators/rails/plugin_new/templates/app/helpers/%name%/application_helper.rb.tt deleted file mode 100644 index 40ae9f52c2..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/app/helpers/%name%/application_helper.rb.tt +++ /dev/null @@ -1,4 +0,0 @@ -module <%= camelized %> - module ApplicationHelper - end -end diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/app/mailers/.empty_directory b/railties/lib/rails/generators/rails/plugin_new/templates/app/mailers/.empty_directory deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory b/railties/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt b/railties/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt deleted file mode 100644 index 1d380420b4..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt +++ /dev/null @@ -1,14 +0,0 @@ - - - - <%= camelized %> - <%%= stylesheet_link_tag "<%= name %>/application", media: "all" %> - <%%= javascript_include_tag "<%= name %>/application" %> - <%%= csrf_meta_tags %> - - - -<%%= yield %> - - - diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/bin/rails.tt b/railties/lib/rails/generators/rails/plugin_new/templates/bin/rails.tt deleted file mode 100644 index c8de9f3e0f..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/bin/rails.tt +++ /dev/null @@ -1,7 +0,0 @@ -# This command will automatically be run when you run "rails" with Rails 4 gems installed from the root of your application. - -ENGINE_ROOT = File.expand_path('../..', __FILE__) -ENGINE_PATH = File.expand_path('../../lib/<%= name -%>/engine', __FILE__) - -require 'rails/all' -require 'rails/engine/commands' diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/config/routes.rb b/railties/lib/rails/generators/rails/plugin_new/templates/config/routes.rb deleted file mode 100644 index 8e158d5831..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/config/routes.rb +++ /dev/null @@ -1,6 +0,0 @@ -<% if mountable? -%> -<%= camelized %>::Engine.routes.draw do -<% else -%> -Rails.application.routes.draw do -<% end -%> -end diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/gitignore b/railties/lib/rails/generators/rails/plugin_new/templates/gitignore deleted file mode 100644 index 086d87818a..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/gitignore +++ /dev/null @@ -1,10 +0,0 @@ -.bundle/ -log/*.log -pkg/ -<% unless options[:skip_test_unit] && options[:dummy_path] == 'test/dummy' -%> -<%= dummy_path %>/db/*.sqlite3 -<%= dummy_path %>/db/*.sqlite3-journal -<%= dummy_path %>/log/*.log -<%= dummy_path %>/tmp/ -<%= dummy_path %>/.sass-cache -<% end -%> \ No newline at end of file diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb b/railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb deleted file mode 100644 index 40c074cced..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb +++ /dev/null @@ -1,6 +0,0 @@ -<% if engine? -%> -require "<%= name %>/engine" - -<% end -%> -module <%= camelized %> -end diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%/engine.rb b/railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%/engine.rb deleted file mode 100644 index 967668fe66..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%/engine.rb +++ /dev/null @@ -1,7 +0,0 @@ -module <%= camelized %> - class Engine < ::Rails::Engine -<% if mountable? -%> - isolate_namespace <%= camelized %> -<% end -%> - end -end diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%/version.rb b/railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%/version.rb deleted file mode 100644 index ef07ef2e19..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/lib/%name%/version.rb +++ /dev/null @@ -1,3 +0,0 @@ -module <%= camelized %> - VERSION = "0.0.1" -end diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/lib/tasks/%name%_tasks.rake b/railties/lib/rails/generators/rails/plugin_new/templates/lib/tasks/%name%_tasks.rake deleted file mode 100644 index 7121f5ae23..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/lib/tasks/%name%_tasks.rake +++ /dev/null @@ -1,4 +0,0 @@ -# desc "Explaining what the task does" -# task :<%= name %> do -# # Task goes here -# end diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb b/railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb deleted file mode 100644 index 310c975262..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +++ /dev/null @@ -1,17 +0,0 @@ -require File.expand_path('../boot', __FILE__) - -<% if include_all_railties? -%> -require 'rails/all' -<% else -%> -# Pick the frameworks you want: -<%= comment_if :skip_active_record %>require "active_record/railtie" -require "action_controller/railtie" -require "action_mailer/railtie" -<%= comment_if :skip_sprockets %>require "sprockets/railtie" -<%= comment_if :skip_test_unit %>require "rails/test_unit/railtie" -<% end -%> - -Bundler.require(*Rails.groups) -require "<%= name %>" - -<%= application_definition %> diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb b/railties/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb deleted file mode 100644 index ef360470a3..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb +++ /dev/null @@ -1,5 +0,0 @@ -# Set up gems listed in the Gemfile. -ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__) - -require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) -$LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__) diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/rails/javascripts.js b/railties/lib/rails/generators/rails/plugin_new/templates/rails/javascripts.js deleted file mode 100644 index 5bc2e1c8b5..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/rails/javascripts.js +++ /dev/null @@ -1,13 +0,0 @@ -// This is a manifest file that'll be compiled into application.js, which will include all the files -// listed below. -// -// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, -// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. -// -// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the -// compiled file. -// -// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details -// about supported directives. -// -//= require_tree . diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/rails/routes.rb b/railties/lib/rails/generators/rails/plugin_new/templates/rails/routes.rb deleted file mode 100644 index 730ee31c3d..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/rails/routes.rb +++ /dev/null @@ -1,4 +0,0 @@ -Rails.application.routes.draw do - - mount <%= camelized %>::Engine => "/<%= name %>" -end diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/rails/stylesheets.css b/railties/lib/rails/generators/rails/plugin_new/templates/rails/stylesheets.css deleted file mode 100644 index 3192ec897b..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/rails/stylesheets.css +++ /dev/null @@ -1,13 +0,0 @@ -/* - * This is a manifest file that'll be compiled into application.css, which will include all the files - * listed below. - * - * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, - * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path. - * - * You're free to add application-wide styles to this file and they'll appear at the top of the - * compiled file, but it's generally better to create a new file per style scope. - * - *= require_self - *= require_tree . - */ diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/test/%name%_test.rb b/railties/lib/rails/generators/rails/plugin_new/templates/test/%name%_test.rb deleted file mode 100644 index 0a8bbd4aaf..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/test/%name%_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class <%= camelized %>Test < ActiveSupport::TestCase - test "truth" do - assert_kind_of Module, <%= camelized %> - end -end diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/test/integration/navigation_test.rb b/railties/lib/rails/generators/rails/plugin_new/templates/test/integration/navigation_test.rb deleted file mode 100644 index 824caecb24..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/test/integration/navigation_test.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'test_helper' - -class NavigationTest < ActionDispatch::IntegrationTest -<% unless options[:skip_active_record] -%> - fixtures :all -<% end -%> - - # test "the truth" do - # assert true - # end -end - diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb b/railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb deleted file mode 100644 index 1e26a313cd..0000000000 --- a/railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb +++ /dev/null @@ -1,15 +0,0 @@ -# Configure Rails Environment -ENV["RAILS_ENV"] = "test" - -require File.expand_path("../dummy/config/environment.rb", __FILE__) -require "rails/test_help" - -Rails.backtrace_cleaner.remove_silencers! - -# Load support files -Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } - -# Load fixtures from the engine -if ActiveSupport::TestCase.method_defined?(:fixture_path=) - ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__) -end diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb new file mode 100644 index 0000000000..068eb66bc6 --- /dev/null +++ b/railties/test/generators/plugin_generator_test.rb @@ -0,0 +1,350 @@ +require 'generators/generators_test_helper' +require 'rails/generators/rails/plugin/plugin_generator' +require 'generators/shared_generator_tests' + +DEFAULT_PLUGIN_FILES = %w( + .gitignore + Gemfile + Rakefile + README.rdoc + bukkits.gemspec + MIT-LICENSE + lib + lib/bukkits.rb + lib/tasks/bukkits_tasks.rake + lib/bukkits/version.rb + test/bukkits_test.rb + test/test_helper.rb + test/dummy +) + +class PluginGeneratorTest < Rails::Generators::TestCase + include GeneratorsTestHelper + destination File.join(Rails.root, "tmp/bukkits") + arguments [destination_root] + + # brings setup, teardown, and some tests + include SharedGeneratorTests + + def test_invalid_plugin_name_raises_an_error + content = capture(:stderr){ run_generator [File.join(destination_root, "things-43")] } + assert_equal "Invalid plugin name things-43. Please give a name which use only alphabetic or numeric or \"_\" characters.\n", content + + content = capture(:stderr){ run_generator [File.join(destination_root, "things4.3")] } + assert_equal "Invalid plugin name things4.3. Please give a name which use only alphabetic or numeric or \"_\" characters.\n", content + + content = capture(:stderr){ run_generator [File.join(destination_root, "43things")] } + assert_equal "Invalid plugin name 43things. Please give a name which does not start with numbers.\n", content + end + + def test_camelcase_plugin_name_underscores_filenames + run_generator [File.join(destination_root, "CamelCasedName")] + assert_no_file "CamelCasedName/lib/CamelCasedName.rb" + assert_file "CamelCasedName/lib/camel_cased_name.rb", /module CamelCasedName/ + end + + def test_generating_without_options + run_generator + assert_file "README.rdoc", /Bukkits/ + assert_no_file "config/routes.rb" + assert_file "test/test_helper.rb" + assert_file "test/bukkits_test.rb", /assert_kind_of Module, Bukkits/ + end + + def test_generating_test_files_in_full_mode + run_generator [destination_root, "--full"] + assert_directory "test/integration/" + + assert_file "test/integration/navigation_test.rb", /ActionDispatch::IntegrationTest/ + end + + def test_generating_test_files_in_full_mode_without_unit_test_files + run_generator [destination_root, "-T", "--full"] + + assert_no_directory "test/integration/" + assert_no_file "test" + assert_file "Rakefile" do |contents| + assert_no_match(/APP_RAKEFILE/, contents) + end + end + + def test_generating_adds_dummy_app_rake_tasks_without_unit_test_files + run_generator [destination_root, "-T", "--mountable", '--dummy-path', 'my_dummy_app'] + assert_file "Rakefile", /APP_RAKEFILE/ + end + + def test_generating_adds_dummy_app_without_javascript_and_assets_deps + run_generator [destination_root] + + assert_file "test/dummy/app/assets/stylesheets/application.css" + + assert_file "test/dummy/app/assets/javascripts/application.js" do |contents| + assert_no_match(/jquery/, contents) + end + end + + def test_ensure_that_plugin_options_are_not_passed_to_app_generator + FileUtils.cd(Rails.root) + assert_no_match(/It works from file!.*It works_from_file/, run_generator([destination_root, "-m", "lib/template.rb"])) + end + + def test_ensure_that_test_dummy_can_be_generated_from_a_template + FileUtils.cd(Rails.root) + run_generator([destination_root, "-m", "lib/create_test_dummy_template.rb", "--skip-test-unit"]) + assert_file "spec/dummy" + assert_no_file "test" + end + + def test_database_entry_is_generated_for_sqlite3_by_default_in_full_mode + run_generator([destination_root, "--full"]) + assert_file "test/dummy/config/database.yml", /sqlite/ + assert_file "bukkits.gemspec", /sqlite3/ + end + + def test_config_another_database + run_generator([destination_root, "-d", "mysql", "--full"]) + assert_file "test/dummy/config/database.yml", /mysql/ + assert_file "bukkits.gemspec", /mysql/ + end + + def test_dont_generate_development_dependency + run_generator [destination_root, "--skip-active-record"] + + assert_file "bukkits.gemspec" do |contents| + assert_no_match(/s.add_development_dependency "sqlite3"/, contents) + end + end + + def test_active_record_is_removed_from_frameworks_if_skip_active_record_is_given + run_generator [destination_root, "--skip-active-record"] + assert_file "test/dummy/config/application.rb", /#\s+require\s+["']active_record\/railtie["']/ + end + + def test_ensure_that_skip_active_record_option_is_passed_to_app_generator + run_generator [destination_root, "--skip_active_record"] + assert_no_file "test/dummy/config/database.yml" + assert_file "test/test_helper.rb" do |contents| + assert_no_match(/ActiveRecord/, contents) + end + end + + def test_ensure_that_database_option_is_passed_to_app_generator + run_generator [destination_root, "--database", "postgresql"] + assert_file "test/dummy/config/database.yml", /postgres/ + end + + def test_generation_runs_bundle_install_with_full_and_mountable + result = run_generator [destination_root, "--mountable", "--full", "--dev"] + assert_file "#{destination_root}/Gemfile.lock" do |contents| + assert_match(/bukkits/, contents) + end + assert_match(/run bundle install/, result) + assert_match(/Using bukkits \(0\.0\.1\)/, result) + assert_match(/Your bundle is complete/, result) + assert_equal 1, result.scan("Your bundle is complete").size + end + + def test_skipping_javascripts_without_mountable_option + run_generator + assert_no_file "app/assets/javascripts/bukkits/application.js" + end + + def test_javascripts_generation + run_generator [destination_root, "--mountable"] + assert_file "app/assets/javascripts/bukkits/application.js" + end + + def test_skip_javascripts + run_generator [destination_root, "--skip-javascript", "--mountable"] + assert_no_file "app/assets/javascripts/bukkits/application.js" + end + + def test_template_from_dir_pwd + FileUtils.cd(Rails.root) + assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"])) + end + + def test_ensure_that_tests_work + run_generator + FileUtils.cd destination_root + quietly { system 'bundle install' } + assert_match(/1 runs, 1 assertions, 0 failures, 0 errors/, `bundle exec rake test`) + end + + def test_ensure_that_tests_works_in_full_mode + run_generator [destination_root, "--full", "--skip_active_record"] + FileUtils.cd destination_root + quietly { system 'bundle install' } + assert_match(/1 runs, 1 assertions, 0 failures, 0 errors/, `bundle exec rake test`) + end + + def test_ensure_that_migration_tasks_work_with_mountable_option + run_generator [destination_root, "--mountable"] + FileUtils.cd destination_root + quietly { system 'bundle install' } + `bundle exec rake db:migrate` + assert_equal 0, $?.exitstatus + end + + def test_creating_engine_in_full_mode + run_generator [destination_root, "--full"] + assert_file "app/assets/javascripts/bukkits" + assert_file "app/assets/stylesheets/bukkits" + assert_file "app/assets/images/bukkits" + assert_file "app/models" + assert_file "app/controllers" + assert_file "app/views" + assert_file "app/helpers" + assert_file "app/mailers" + assert_file "bin/rails" + assert_file "config/routes.rb", /Rails.application.routes.draw do/ + assert_file "lib/bukkits/engine.rb", /module Bukkits\n class Engine < ::Rails::Engine\n end\nend/ + assert_file "lib/bukkits.rb", /require "bukkits\/engine"/ + end + + def test_being_quiet_while_creating_dummy_application + assert_no_match(/create\s+config\/application.rb/, run_generator) + end + + def test_create_mountable_application_with_mountable_option + run_generator [destination_root, "--mountable"] + assert_file "app/assets/javascripts/bukkits" + assert_file "app/assets/stylesheets/bukkits" + assert_file "app/assets/images/bukkits" + assert_file "config/routes.rb", /Bukkits::Engine.routes.draw do/ + assert_file "lib/bukkits/engine.rb", /isolate_namespace Bukkits/ + assert_file "test/dummy/config/routes.rb", /mount Bukkits::Engine => "\/bukkits"/ + assert_file "app/controllers/bukkits/application_controller.rb", /module Bukkits\n class ApplicationController < ActionController::Base/ + assert_file "app/helpers/bukkits/application_helper.rb", /module Bukkits\n module ApplicationHelper/ + assert_file "app/views/layouts/bukkits/application.html.erb" do |contents| + assert_match "Bukkits", contents + assert_match(/stylesheet_link_tag\s+['"]bukkits\/application['"]/, contents) + assert_match(/javascript_include_tag\s+['"]bukkits\/application['"]/, contents) + end + end + + def test_creating_gemspec + run_generator + assert_file "bukkits.gemspec", /s.name\s+= "bukkits"/ + assert_file "bukkits.gemspec", /s.files = Dir\["\{app,config,db,lib\}\/\*\*\/\*", "MIT-LICENSE", "Rakefile", "README\.rdoc"\]/ + assert_file "bukkits.gemspec", /s.test_files = Dir\["test\/\*\*\/\*"\]/ + assert_file "bukkits.gemspec", /s.version\s+ = Bukkits::VERSION/ + end + + def test_usage_of_engine_commands + run_generator [destination_root, "--full"] + assert_file "bin/rails", /ENGINE_PATH = File.expand_path\('..\/..\/lib\/bukkits\/engine', __FILE__\)/ + assert_file "bin/rails", /ENGINE_ROOT = File.expand_path\('..\/..', __FILE__\)/ + assert_file "bin/rails", /require 'rails\/all'/ + assert_file "bin/rails", /require 'rails\/engine\/commands'/ + end + + def test_shebang + run_generator [destination_root, "--full"] + assert_file "bin/rails", /#!\/usr\/bin\/env ruby/ + end + + def test_passing_dummy_path_as_a_parameter + run_generator [destination_root, "--dummy_path", "spec/dummy"] + assert_file "spec/dummy" + assert_file "spec/dummy/config/application.rb" + assert_no_file "test/dummy" + end + + def test_creating_dummy_application_with_different_name + run_generator [destination_root, "--dummy_path", "spec/fake"] + assert_file "spec/fake" + assert_file "spec/fake/config/application.rb" + assert_no_file "test/dummy" + end + + def test_creating_dummy_without_tests_but_with_dummy_path + run_generator [destination_root, "--dummy_path", "spec/dummy", "--skip-test-unit"] + assert_file "spec/dummy" + assert_file "spec/dummy/config/application.rb" + assert_no_file "test" + assert_file '.gitignore' do |contents| + assert_match(/spec\/dummy/, contents) + end + 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"]) + assert_file ".gitignore" do |contents| + assert_match(/spec\/dummy/, contents) + end + end + + def test_skipping_test_unit + run_generator [destination_root, "--skip-test-unit"] + assert_no_file "test" + assert_file "bukkits.gemspec" do |contents| + assert_no_match(/s.test_files = Dir\["test\/\*\*\/\*"\]/, contents) + end + assert_file '.gitignore' do |contents| + assert_no_match(/test\dummy/, contents) + end + end + + def test_skipping_gemspec + run_generator [destination_root, "--skip-gemspec"] + assert_no_file "bukkits.gemspec" + assert_file "Gemfile" do |contents| + assert_no_match('gemspec', contents) + assert_match(/gem "rails", "~> #{Rails.version}"/, contents) + assert_match(/group :development do\n gem "sqlite3"\nend/, contents) + assert_no_match(/# gem "jquery-rails"/, contents) + end + end + + def test_skipping_gemspec_in_full_mode + run_generator [destination_root, "--skip-gemspec", "--full"] + assert_no_file "bukkits.gemspec" + assert_file "Gemfile" do |contents| + assert_no_match('gemspec', contents) + assert_match(/gem "rails", "~> #{Rails.version}"/, contents) + assert_match(/group :development do\n gem "sqlite3"\nend/, contents) + end + 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) + silence(:stdout){ generator.send(*args, &block) } + end + + def default_files + ::DEFAULT_PLUGIN_FILES + end +end diff --git a/railties/test/generators/plugin_new_generator_test.rb b/railties/test/generators/plugin_new_generator_test.rb deleted file mode 100644 index 32c7612a8f..0000000000 --- a/railties/test/generators/plugin_new_generator_test.rb +++ /dev/null @@ -1,350 +0,0 @@ -require 'generators/generators_test_helper' -require 'rails/generators/rails/plugin_new/plugin_new_generator' -require 'generators/shared_generator_tests' - -DEFAULT_PLUGIN_FILES = %w( - .gitignore - Gemfile - Rakefile - README.rdoc - bukkits.gemspec - MIT-LICENSE - lib - lib/bukkits.rb - lib/tasks/bukkits_tasks.rake - lib/bukkits/version.rb - test/bukkits_test.rb - test/test_helper.rb - test/dummy -) - -class PluginNewGeneratorTest < Rails::Generators::TestCase - include GeneratorsTestHelper - destination File.join(Rails.root, "tmp/bukkits") - arguments [destination_root] - - # brings setup, teardown, and some tests - include SharedGeneratorTests - - def test_invalid_plugin_name_raises_an_error - content = capture(:stderr){ run_generator [File.join(destination_root, "things-43")] } - assert_equal "Invalid plugin name things-43. Please give a name which use only alphabetic or numeric or \"_\" characters.\n", content - - content = capture(:stderr){ run_generator [File.join(destination_root, "things4.3")] } - assert_equal "Invalid plugin name things4.3. Please give a name which use only alphabetic or numeric or \"_\" characters.\n", content - - content = capture(:stderr){ run_generator [File.join(destination_root, "43things")] } - assert_equal "Invalid plugin name 43things. Please give a name which does not start with numbers.\n", content - end - - def test_camelcase_plugin_name_underscores_filenames - run_generator [File.join(destination_root, "CamelCasedName")] - assert_no_file "CamelCasedName/lib/CamelCasedName.rb" - assert_file "CamelCasedName/lib/camel_cased_name.rb", /module CamelCasedName/ - end - - def test_generating_without_options - run_generator - assert_file "README.rdoc", /Bukkits/ - assert_no_file "config/routes.rb" - assert_file "test/test_helper.rb" - assert_file "test/bukkits_test.rb", /assert_kind_of Module, Bukkits/ - end - - def test_generating_test_files_in_full_mode - run_generator [destination_root, "--full"] - assert_directory "test/integration/" - - assert_file "test/integration/navigation_test.rb", /ActionDispatch::IntegrationTest/ - end - - def test_generating_test_files_in_full_mode_without_unit_test_files - run_generator [destination_root, "-T", "--full"] - - assert_no_directory "test/integration/" - assert_no_file "test" - assert_file "Rakefile" do |contents| - assert_no_match(/APP_RAKEFILE/, contents) - end - end - - def test_generating_adds_dummy_app_rake_tasks_without_unit_test_files - run_generator [destination_root, "-T", "--mountable", '--dummy-path', 'my_dummy_app'] - assert_file "Rakefile", /APP_RAKEFILE/ - end - - def test_generating_adds_dummy_app_without_javascript_and_assets_deps - run_generator [destination_root] - - assert_file "test/dummy/app/assets/stylesheets/application.css" - - assert_file "test/dummy/app/assets/javascripts/application.js" do |contents| - assert_no_match(/jquery/, contents) - end - end - - def test_ensure_that_plugin_options_are_not_passed_to_app_generator - FileUtils.cd(Rails.root) - assert_no_match(/It works from file!.*It works_from_file/, run_generator([destination_root, "-m", "lib/template.rb"])) - end - - def test_ensure_that_test_dummy_can_be_generated_from_a_template - FileUtils.cd(Rails.root) - run_generator([destination_root, "-m", "lib/create_test_dummy_template.rb", "--skip-test-unit"]) - assert_file "spec/dummy" - assert_no_file "test" - end - - def test_database_entry_is_generated_for_sqlite3_by_default_in_full_mode - run_generator([destination_root, "--full"]) - assert_file "test/dummy/config/database.yml", /sqlite/ - assert_file "bukkits.gemspec", /sqlite3/ - end - - def test_config_another_database - run_generator([destination_root, "-d", "mysql", "--full"]) - assert_file "test/dummy/config/database.yml", /mysql/ - assert_file "bukkits.gemspec", /mysql/ - end - - def test_dont_generate_development_dependency - run_generator [destination_root, "--skip-active-record"] - - assert_file "bukkits.gemspec" do |contents| - assert_no_match(/s.add_development_dependency "sqlite3"/, contents) - end - end - - def test_active_record_is_removed_from_frameworks_if_skip_active_record_is_given - run_generator [destination_root, "--skip-active-record"] - assert_file "test/dummy/config/application.rb", /#\s+require\s+["']active_record\/railtie["']/ - end - - def test_ensure_that_skip_active_record_option_is_passed_to_app_generator - run_generator [destination_root, "--skip_active_record"] - assert_no_file "test/dummy/config/database.yml" - assert_file "test/test_helper.rb" do |contents| - assert_no_match(/ActiveRecord/, contents) - end - end - - def test_ensure_that_database_option_is_passed_to_app_generator - run_generator [destination_root, "--database", "postgresql"] - assert_file "test/dummy/config/database.yml", /postgres/ - end - - def test_generation_runs_bundle_install_with_full_and_mountable - result = run_generator [destination_root, "--mountable", "--full", "--dev"] - assert_file "#{destination_root}/Gemfile.lock" do |contents| - assert_match(/bukkits/, contents) - end - assert_match(/run bundle install/, result) - assert_match(/Using bukkits \(0\.0\.1\)/, result) - assert_match(/Your bundle is complete/, result) - assert_equal 1, result.scan("Your bundle is complete").size - end - - def test_skipping_javascripts_without_mountable_option - run_generator - assert_no_file "app/assets/javascripts/bukkits/application.js" - end - - def test_javascripts_generation - run_generator [destination_root, "--mountable"] - assert_file "app/assets/javascripts/bukkits/application.js" - end - - def test_skip_javascripts - run_generator [destination_root, "--skip-javascript", "--mountable"] - assert_no_file "app/assets/javascripts/bukkits/application.js" - end - - def test_template_from_dir_pwd - FileUtils.cd(Rails.root) - assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"])) - end - - def test_ensure_that_tests_work - run_generator - FileUtils.cd destination_root - quietly { system 'bundle install' } - assert_match(/1 runs, 1 assertions, 0 failures, 0 errors/, `bundle exec rake test`) - end - - def test_ensure_that_tests_works_in_full_mode - run_generator [destination_root, "--full", "--skip_active_record"] - FileUtils.cd destination_root - quietly { system 'bundle install' } - assert_match(/1 runs, 1 assertions, 0 failures, 0 errors/, `bundle exec rake test`) - end - - def test_ensure_that_migration_tasks_work_with_mountable_option - run_generator [destination_root, "--mountable"] - FileUtils.cd destination_root - quietly { system 'bundle install' } - `bundle exec rake db:migrate` - assert_equal 0, $?.exitstatus - end - - def test_creating_engine_in_full_mode - run_generator [destination_root, "--full"] - assert_file "app/assets/javascripts/bukkits" - assert_file "app/assets/stylesheets/bukkits" - assert_file "app/assets/images/bukkits" - assert_file "app/models" - assert_file "app/controllers" - assert_file "app/views" - assert_file "app/helpers" - assert_file "app/mailers" - assert_file "bin/rails" - assert_file "config/routes.rb", /Rails.application.routes.draw do/ - assert_file "lib/bukkits/engine.rb", /module Bukkits\n class Engine < ::Rails::Engine\n end\nend/ - assert_file "lib/bukkits.rb", /require "bukkits\/engine"/ - end - - def test_being_quiet_while_creating_dummy_application - assert_no_match(/create\s+config\/application.rb/, run_generator) - end - - def test_create_mountable_application_with_mountable_option - run_generator [destination_root, "--mountable"] - assert_file "app/assets/javascripts/bukkits" - assert_file "app/assets/stylesheets/bukkits" - assert_file "app/assets/images/bukkits" - assert_file "config/routes.rb", /Bukkits::Engine.routes.draw do/ - assert_file "lib/bukkits/engine.rb", /isolate_namespace Bukkits/ - assert_file "test/dummy/config/routes.rb", /mount Bukkits::Engine => "\/bukkits"/ - assert_file "app/controllers/bukkits/application_controller.rb", /module Bukkits\n class ApplicationController < ActionController::Base/ - assert_file "app/helpers/bukkits/application_helper.rb", /module Bukkits\n module ApplicationHelper/ - assert_file "app/views/layouts/bukkits/application.html.erb" do |contents| - assert_match "Bukkits", contents - assert_match(/stylesheet_link_tag\s+['"]bukkits\/application['"]/, contents) - assert_match(/javascript_include_tag\s+['"]bukkits\/application['"]/, contents) - end - end - - def test_creating_gemspec - run_generator - assert_file "bukkits.gemspec", /s.name\s+= "bukkits"/ - assert_file "bukkits.gemspec", /s.files = Dir\["\{app,config,db,lib\}\/\*\*\/\*", "MIT-LICENSE", "Rakefile", "README\.rdoc"\]/ - assert_file "bukkits.gemspec", /s.test_files = Dir\["test\/\*\*\/\*"\]/ - assert_file "bukkits.gemspec", /s.version\s+ = Bukkits::VERSION/ - end - - def test_usage_of_engine_commands - run_generator [destination_root, "--full"] - assert_file "bin/rails", /ENGINE_PATH = File.expand_path\('..\/..\/lib\/bukkits\/engine', __FILE__\)/ - assert_file "bin/rails", /ENGINE_ROOT = File.expand_path\('..\/..', __FILE__\)/ - assert_file "bin/rails", /require 'rails\/all'/ - assert_file "bin/rails", /require 'rails\/engine\/commands'/ - end - - def test_shebang - run_generator [destination_root, "--full"] - assert_file "bin/rails", /#!\/usr\/bin\/env ruby/ - end - - def test_passing_dummy_path_as_a_parameter - run_generator [destination_root, "--dummy_path", "spec/dummy"] - assert_file "spec/dummy" - assert_file "spec/dummy/config/application.rb" - assert_no_file "test/dummy" - end - - def test_creating_dummy_application_with_different_name - run_generator [destination_root, "--dummy_path", "spec/fake"] - assert_file "spec/fake" - assert_file "spec/fake/config/application.rb" - assert_no_file "test/dummy" - end - - def test_creating_dummy_without_tests_but_with_dummy_path - run_generator [destination_root, "--dummy_path", "spec/dummy", "--skip-test-unit"] - assert_file "spec/dummy" - assert_file "spec/dummy/config/application.rb" - assert_no_file "test" - assert_file '.gitignore' do |contents| - assert_match(/spec\/dummy/, contents) - end - 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"]) - assert_file ".gitignore" do |contents| - assert_match(/spec\/dummy/, contents) - end - end - - def test_skipping_test_unit - run_generator [destination_root, "--skip-test-unit"] - assert_no_file "test" - assert_file "bukkits.gemspec" do |contents| - assert_no_match(/s.test_files = Dir\["test\/\*\*\/\*"\]/, contents) - end - assert_file '.gitignore' do |contents| - assert_no_match(/test\dummy/, contents) - end - end - - def test_skipping_gemspec - run_generator [destination_root, "--skip-gemspec"] - assert_no_file "bukkits.gemspec" - assert_file "Gemfile" do |contents| - assert_no_match('gemspec', contents) - assert_match(/gem "rails", "~> #{Rails.version}"/, contents) - assert_match(/group :development do\n gem "sqlite3"\nend/, contents) - assert_no_match(/# gem "jquery-rails"/, contents) - end - end - - def test_skipping_gemspec_in_full_mode - run_generator [destination_root, "--skip-gemspec", "--full"] - assert_no_file "bukkits.gemspec" - assert_file "Gemfile" do |contents| - assert_no_match('gemspec', contents) - assert_match(/gem "rails", "~> #{Rails.version}"/, contents) - assert_match(/group :development do\n gem "sqlite3"\nend/, contents) - end - 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) - silence(:stdout){ generator.send(*args, &block) } - end - - def default_files - ::DEFAULT_PLUGIN_FILES - end -end diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb index 361784f509..5130b285a9 100644 --- a/railties/test/generators_test.rb +++ b/railties/test/generators_test.rb @@ -106,7 +106,7 @@ class GeneratorsTest < Rails::Generators::TestCase def test_rails_generators_help_does_not_include_app_nor_plugin_new output = capture(:stdout){ Rails::Generators.help } assert_no_match(/app/, output) - assert_no_match(/plugin_new/, output) + assert_no_match(/[^:]plugin/, output) end def test_rails_generators_with_others_information -- cgit v1.2.3