From ea106cf051e4fafcaf775f75bc4af17e89197a28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 23 Jun 2009 20:42:29 +0200 Subject: [PATCH] Added plugin generators (and a couple of TODOs). --- railties/lib/generators/base.rb | 11 +++-- railties/lib/generators/error.rb | 6 --- railties/lib/generators/rails/plugin/USAGE | 13 ++++++ .../rails/plugin/plugin_generator.rb | 41 +++++++++++++++++++ .../rails/plugin/templates/MIT-LICENSE | 20 +++++++++ .../generators/rails/plugin/templates/README | 13 ++++++ .../rails/plugin/templates/Rakefile | 23 +++++++++++ .../%file_name%/%file_name%_generator.rb.tt | 2 + .../templates/generators/%file_name%/USAGE.tt | 8 ++++ .../%file_name%/templates/.empty_directory | 0 .../generators/rails/plugin/templates/init.rb | 1 + .../rails/plugin/templates/install.rb | 1 + .../plugin/templates/lib/%file_name%.rb.tt | 1 + .../templates/tasks/%file_name%_tasks.rake.tt | 4 ++ .../rails/plugin/templates/uninstall.rb | 1 + railties/lib/generators/test_unit.rb | 4 +- .../lib/generators/test_unit/plugin/USAGE | 13 ++++++ .../test_unit/plugin/plugin_generator.rb | 14 +++++++ .../plugin/templates/test/%file_name%_test.rb | 8 ++++ .../plugin/templates/test/test_helper.rb | 3 ++ 20 files changed, 175 insertions(+), 12 deletions(-) delete mode 100644 railties/lib/generators/error.rb create mode 100644 railties/lib/generators/rails/plugin/USAGE create mode 100644 railties/lib/generators/rails/plugin/plugin_generator.rb create mode 100644 railties/lib/generators/rails/plugin/templates/MIT-LICENSE create mode 100644 railties/lib/generators/rails/plugin/templates/README create mode 100644 railties/lib/generators/rails/plugin/templates/Rakefile create mode 100644 railties/lib/generators/rails/plugin/templates/generators/%file_name%/%file_name%_generator.rb.tt create mode 100644 railties/lib/generators/rails/plugin/templates/generators/%file_name%/USAGE.tt create mode 100644 railties/lib/generators/rails/plugin/templates/generators/%file_name%/templates/.empty_directory create mode 100644 railties/lib/generators/rails/plugin/templates/init.rb create mode 100644 railties/lib/generators/rails/plugin/templates/install.rb create mode 100644 railties/lib/generators/rails/plugin/templates/lib/%file_name%.rb.tt create mode 100644 railties/lib/generators/rails/plugin/templates/tasks/%file_name%_tasks.rake.tt create mode 100644 railties/lib/generators/rails/plugin/templates/uninstall.rb create mode 100644 railties/lib/generators/test_unit/plugin/USAGE create mode 100644 railties/lib/generators/test_unit/plugin/plugin_generator.rb create mode 100644 railties/lib/generators/test_unit/plugin/templates/test/%file_name%_test.rb create mode 100644 railties/lib/generators/test_unit/plugin/templates/test/test_helper.rb diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb index c888316df7..b530713805 100644 --- a/railties/lib/generators/base.rb +++ b/railties/lib/generators/base.rb @@ -1,8 +1,10 @@ require 'generators/actions' -require 'generators/error' module Rails module Generators + class Error < Thor::Error + end + class Base < Thor::Group include Rails::Generators::Actions include Thor::Actions @@ -45,7 +47,7 @@ def self.base_name # def self.generator_name @generator_name ||= begin - klass_name = self.name.gsub(/^Rails::Generators::/, '') + klass_name = self.name.split('::').last klass_name.gsub!(/Generator$/, '') klass_name.underscore end @@ -71,7 +73,8 @@ def self.add_shebang_option! # Small macro to add test_framework option and invoke it. # def self.add_test_framework_option! - class_option :test_framework, :type => :string, :aliases => "-t", :default => "testunit", + # TODO Reduce the example name + class_option :test_framework, :type => :string, :aliases => "-t", :default => "test_unit", :desc => "Test framework to be invoked by this generator" define_method :invoke_test_framework do @@ -80,7 +83,7 @@ def self.add_test_framework_option! begin invoke name - rescue Thor::UndefinedTaskError + rescue Thor::UndefinedTaskError # TODO Ensure this message is called. say "Could not find and/or invoke #{name}." end end diff --git a/railties/lib/generators/error.rb b/railties/lib/generators/error.rb deleted file mode 100644 index 79b8be59cf..0000000000 --- a/railties/lib/generators/error.rb +++ /dev/null @@ -1,6 +0,0 @@ -module Rails - module Generators - class Error < Thor::Error - end - end -end diff --git a/railties/lib/generators/rails/plugin/USAGE b/railties/lib/generators/rails/plugin/USAGE new file mode 100644 index 0000000000..8a17fa4dec --- /dev/null +++ b/railties/lib/generators/rails/plugin/USAGE @@ -0,0 +1,13 @@ +Description: + Stubs out a new plugin at vendor/plugins. Pass the plugin name, either + CamelCased or under_scored, as an argument. + +Example: + `./script/generate plugin BrowserFilters` + + creates a standard browser_filters plugin: + vendor/plugins/browser_filters/README + vendor/plugins/browser_filters/init.rb + vendor/plugins/browser_filters/install.rb + vendor/plugins/browser_filters/lib/browser_filters.rb + vendor/plugins/browser_filters/test/browser_filters_test.rb diff --git a/railties/lib/generators/rails/plugin/plugin_generator.rb b/railties/lib/generators/rails/plugin/plugin_generator.rb new file mode 100644 index 0000000000..38afb3984e --- /dev/null +++ b/railties/lib/generators/rails/plugin/plugin_generator.rb @@ -0,0 +1,41 @@ +module Rails + module Generators + class PluginGenerator < NamedBase + def create_root + self.root = File.expand_path("vendor/plugins/#{file_name}", root) + empty_directory '.' + FileUtils.cd(root) + end + + # TODO Check class collision + + def create_root_files + %w(README MIT-LICENSE Rakefile init.rb install.rb uninstall.rb).each do |file| + template file + end + end + + def create_lib_files + directory 'lib' + end + + add_test_framework_option! + + class_option :with_tasks, :type => :boolean, :aliases => "-r", :default => false, + :desc => "When supplied creates tasks base files." + + class_option :with_generator, :type => :boolean, :aliases => "-g", :default => false, + :desc => "When supplied creates generator base files." + + def create_tasks_files + return unless options[:with_tasks] + directory 'tasks' + end + + def create_generator_files + return unless options[:with_generator] + directory 'generators' + end + end + end +end diff --git a/railties/lib/generators/rails/plugin/templates/MIT-LICENSE b/railties/lib/generators/rails/plugin/templates/MIT-LICENSE new file mode 100644 index 0000000000..8717df053d --- /dev/null +++ b/railties/lib/generators/rails/plugin/templates/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright (c) <%= Date.today.year %> [name of plugin creator] + +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/generators/rails/plugin/templates/README b/railties/lib/generators/rails/plugin/templates/README new file mode 100644 index 0000000000..702db07cb1 --- /dev/null +++ b/railties/lib/generators/rails/plugin/templates/README @@ -0,0 +1,13 @@ +<%= class_name %> +<%= "=" * class_name.size %> + +Introduction goes here. + + +Example +======= + +Example goes here. + + +Copyright (c) <%= Date.today.year %> [name of plugin creator], released under the MIT license diff --git a/railties/lib/generators/rails/plugin/templates/Rakefile b/railties/lib/generators/rails/plugin/templates/Rakefile new file mode 100644 index 0000000000..85e8ff1834 --- /dev/null +++ b/railties/lib/generators/rails/plugin/templates/Rakefile @@ -0,0 +1,23 @@ +require 'rake' +require 'rake/testtask' +require 'rake/rdoctask' + +desc 'Default: run unit tests.' +task :default => :test + +desc 'Test the <%= file_name %> plugin.' +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.libs << 'test' + t.pattern = 'test/**/*_test.rb' + t.verbose = true +end + +desc 'Generate documentation for the <%= file_name %> plugin.' +Rake::RDocTask.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = '<%= class_name %>' + rdoc.options << '--line-numbers' << '--inline-source' + rdoc.rdoc_files.include('README') + rdoc.rdoc_files.include('lib/**/*.rb') +end diff --git a/railties/lib/generators/rails/plugin/templates/generators/%file_name%/%file_name%_generator.rb.tt b/railties/lib/generators/rails/plugin/templates/generators/%file_name%/%file_name%_generator.rb.tt new file mode 100644 index 0000000000..90aa557eeb --- /dev/null +++ b/railties/lib/generators/rails/plugin/templates/generators/%file_name%/%file_name%_generator.rb.tt @@ -0,0 +1,2 @@ +class <%= class_name %>Generator < Rails::Generators::NamedBase +end diff --git a/railties/lib/generators/rails/plugin/templates/generators/%file_name%/USAGE.tt b/railties/lib/generators/rails/plugin/templates/generators/%file_name%/USAGE.tt new file mode 100644 index 0000000000..ea9f4f12cc --- /dev/null +++ b/railties/lib/generators/rails/plugin/templates/generators/%file_name%/USAGE.tt @@ -0,0 +1,8 @@ +Description: + Explain the generator + +Example: + ./script/generate <%= file_name %> Thing + + This will create: + what/will/it/create diff --git a/railties/lib/generators/rails/plugin/templates/generators/%file_name%/templates/.empty_directory b/railties/lib/generators/rails/plugin/templates/generators/%file_name%/templates/.empty_directory new file mode 100644 index 0000000000..e69de29bb2 diff --git a/railties/lib/generators/rails/plugin/templates/init.rb b/railties/lib/generators/rails/plugin/templates/init.rb new file mode 100644 index 0000000000..3c19a743c9 --- /dev/null +++ b/railties/lib/generators/rails/plugin/templates/init.rb @@ -0,0 +1 @@ +# Include hook code here diff --git a/railties/lib/generators/rails/plugin/templates/install.rb b/railties/lib/generators/rails/plugin/templates/install.rb new file mode 100644 index 0000000000..f7732d3796 --- /dev/null +++ b/railties/lib/generators/rails/plugin/templates/install.rb @@ -0,0 +1 @@ +# Install hook code here diff --git a/railties/lib/generators/rails/plugin/templates/lib/%file_name%.rb.tt b/railties/lib/generators/rails/plugin/templates/lib/%file_name%.rb.tt new file mode 100644 index 0000000000..d8d908a959 --- /dev/null +++ b/railties/lib/generators/rails/plugin/templates/lib/%file_name%.rb.tt @@ -0,0 +1 @@ +# <%= class_name %> diff --git a/railties/lib/generators/rails/plugin/templates/tasks/%file_name%_tasks.rake.tt b/railties/lib/generators/rails/plugin/templates/tasks/%file_name%_tasks.rake.tt new file mode 100644 index 0000000000..72920a9d3a --- /dev/null +++ b/railties/lib/generators/rails/plugin/templates/tasks/%file_name%_tasks.rake.tt @@ -0,0 +1,4 @@ +# desc "Explaining what the task does" +# task :<%= file_name %> do +# # Task goes here +# end diff --git a/railties/lib/generators/rails/plugin/templates/uninstall.rb b/railties/lib/generators/rails/plugin/templates/uninstall.rb new file mode 100644 index 0000000000..9738333463 --- /dev/null +++ b/railties/lib/generators/rails/plugin/templates/uninstall.rb @@ -0,0 +1 @@ +# Uninstall hook code here diff --git a/railties/lib/generators/test_unit.rb b/railties/lib/generators/test_unit.rb index 69bc2f573c..ecf4c546ed 100644 --- a/railties/lib/generators/test_unit.rb +++ b/railties/lib/generators/test_unit.rb @@ -1,8 +1,8 @@ require 'generators/named_base' -module Rails +module TestUnit module Generators - class TestUnit < NamedBase + class Base < Rails::Generators::NamedBase protected def self.base_name 'test_unit' diff --git a/railties/lib/generators/test_unit/plugin/USAGE b/railties/lib/generators/test_unit/plugin/USAGE new file mode 100644 index 0000000000..8a17fa4dec --- /dev/null +++ b/railties/lib/generators/test_unit/plugin/USAGE @@ -0,0 +1,13 @@ +Description: + Stubs out a new plugin at vendor/plugins. Pass the plugin name, either + CamelCased or under_scored, as an argument. + +Example: + `./script/generate plugin BrowserFilters` + + creates a standard browser_filters plugin: + vendor/plugins/browser_filters/README + vendor/plugins/browser_filters/init.rb + vendor/plugins/browser_filters/install.rb + vendor/plugins/browser_filters/lib/browser_filters.rb + vendor/plugins/browser_filters/test/browser_filters_test.rb diff --git a/railties/lib/generators/test_unit/plugin/plugin_generator.rb b/railties/lib/generators/test_unit/plugin/plugin_generator.rb new file mode 100644 index 0000000000..1c987ab1c9 --- /dev/null +++ b/railties/lib/generators/test_unit/plugin/plugin_generator.rb @@ -0,0 +1,14 @@ +module TestUnit + module Generators + class PluginGenerator < Base + desc <Test < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end diff --git a/railties/lib/generators/test_unit/plugin/templates/test/test_helper.rb b/railties/lib/generators/test_unit/plugin/templates/test/test_helper.rb new file mode 100644 index 0000000000..cf148b8b47 --- /dev/null +++ b/railties/lib/generators/test_unit/plugin/templates/test/test_helper.rb @@ -0,0 +1,3 @@ +require 'rubygems' +require 'active_support' +require 'active_support/test_case' \ No newline at end of file -- GitLab