authorails 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORAILS_README +20 -0
- data/CHANGELOG +1366 -0
- data/MIT-LICENSE +20 -0
- data/README +182 -0
- data/Rakefile +334 -0
- data/bin/about +3 -0
- data/bin/authorails +19 -0
- data/bin/breakpointer +3 -0
- data/bin/console +3 -0
- data/bin/destroy +3 -0
- data/bin/generate +3 -0
- data/bin/performance/benchmarker +3 -0
- data/bin/performance/profiler +3 -0
- data/bin/plugin +3 -0
- data/bin/process/inspector +3 -0
- data/bin/process/reaper +3 -0
- data/bin/process/spawner +3 -0
- data/bin/runner +3 -0
- data/bin/server +3 -0
- data/builtin/rails_info/rails/info.rb +123 -0
- data/builtin/rails_info/rails/info_controller.rb +9 -0
- data/builtin/rails_info/rails/info_helper.rb +2 -0
- data/builtin/rails_info/rails_info_controller.rb +2 -0
- data/configs/apache.conf +40 -0
- data/configs/databases/frontbase.yml +28 -0
- data/configs/databases/mysql.yml +48 -0
- data/configs/databases/oracle.yml +39 -0
- data/configs/databases/postgresql.yml +44 -0
- data/configs/databases/sqlite2.yml +16 -0
- data/configs/databases/sqlite3.yml +19 -0
- data/configs/empty.log +0 -0
- data/configs/lighttpd.conf +54 -0
- data/configs/routes.rb +23 -0
- data/controllers/accounts_controller.rb +11 -0
- data/controllers/admin_controller.rb +76 -0
- data/controllers/begin_controller.rb +5 -0
- data/controllers/login_controller.rb +101 -0
- data/controllers/permission_controller.rb +91 -0
- data/controllers/roles_controller.rb +58 -0
- data/controllers/tables_controller.rb +714 -0
- data/dispatches/dispatch.fcgi +24 -0
- data/dispatches/dispatch.rb +10 -0
- data/dispatches/gateway.cgi +97 -0
- data/doc/README_FOR_APP +2 -0
- data/environments/boot.rb +45 -0
- data/environments/development.rb +21 -0
- data/environments/environment.rb +63 -0
- data/environments/production.rb +18 -0
- data/environments/test.rb +19 -0
- data/fresh_rakefile +10 -0
- data/helpers/application.rb +607 -0
- data/helpers/application_helper.rb +315 -0
- data/helpers/permission_helper.rb +27 -0
- data/helpers/test_helper.rb +28 -0
- data/html/404.html +30 -0
- data/html/500.html +30 -0
- data/html/favicon.ico +0 -0
- data/html/images/ArrowDn.png +0 -0
- data/html/images/ArrowUp.png +0 -0
- data/html/images/arrow.gif +0 -0
- data/html/images/authorails_logo.gif +0 -0
- data/html/images/delete.png +0 -0
- data/html/images/edit.png +0 -0
- data/html/images/minus.gif +0 -0
- data/html/images/plus.gif +0 -0
- data/html/images/rails.png +0 -0
- data/html/images/show.png +0 -0
- data/html/images/spinner.gif +0 -0
- data/html/index.html +273 -0
- data/html/javascripts/application.js +2 -0
- data/html/javascripts/controls.js +833 -0
- data/html/javascripts/dragdrop.js +942 -0
- data/html/javascripts/effects.js +1088 -0
- data/html/javascripts/live_validation.js +4 -0
- data/html/javascripts/prototype.js +2515 -0
- data/html/robots.txt +1 -0
- data/lib/authorails/version.rb +9 -0
- data/lib/binding_of_caller.rb +85 -0
- data/lib/breakpoint.rb +553 -0
- data/lib/breakpoint_client.rb +196 -0
- data/lib/code_statistics.rb +107 -0
- data/lib/commands.rb +17 -0
- data/lib/commands/about.rb +2 -0
- data/lib/commands/breakpointer.rb +1 -0
- data/lib/commands/console.rb +25 -0
- data/lib/commands/destroy.rb +6 -0
- data/lib/commands/generate.rb +6 -0
- data/lib/commands/ncgi/listener +86 -0
- data/lib/commands/ncgi/tracker +69 -0
- data/lib/commands/performance/benchmarker.rb +24 -0
- data/lib/commands/performance/profiler.rb +50 -0
- data/lib/commands/plugin.rb +918 -0
- data/lib/commands/process/inspector.rb +68 -0
- data/lib/commands/process/reaper.rb +149 -0
- data/lib/commands/process/spawner.rb +209 -0
- data/lib/commands/process/spinner.rb +57 -0
- data/lib/commands/runner.rb +48 -0
- data/lib/commands/server.rb +39 -0
- data/lib/commands/servers/base.rb +19 -0
- data/lib/commands/servers/lighttpd.rb +94 -0
- data/lib/commands/servers/mongrel.rb +65 -0
- data/lib/commands/servers/webrick.rb +59 -0
- data/lib/commands/update.rb +4 -0
- data/lib/console_app.rb +27 -0
- data/lib/console_sandbox.rb +6 -0
- data/lib/console_with_helpers.rb +23 -0
- data/lib/dispatcher.rb +173 -0
- data/lib/fcgi_handler.rb +225 -0
- data/lib/initializer.rb +702 -0
- data/lib/rails_generator.rb +43 -0
- data/lib/rails_generator/base.rb +261 -0
- data/lib/rails_generator/commands.rb +581 -0
- data/lib/rails_generator/generated_attribute.rb +42 -0
- data/lib/rails_generator/generators/applications/app/USAGE +16 -0
- data/lib/rails_generator/generators/applications/app/app_generator.rb +341 -0
- data/lib/rails_generator/generators/components/controller/USAGE +30 -0
- data/lib/rails_generator/generators/components/controller/controller_generator.rb +37 -0
- data/lib/rails_generator/generators/components/controller/templates/controller.rb +10 -0
- data/lib/rails_generator/generators/components/controller/templates/functional_test.rb +18 -0
- data/lib/rails_generator/generators/components/controller/templates/helper.rb +2 -0
- data/lib/rails_generator/generators/components/controller/templates/view.rhtml +2 -0
- data/lib/rails_generator/generators/components/integration_test/USAGE +14 -0
- data/lib/rails_generator/generators/components/integration_test/integration_test_generator.rb +16 -0
- data/lib/rails_generator/generators/components/integration_test/templates/integration_test.rb +10 -0
- data/lib/rails_generator/generators/components/mailer/USAGE +18 -0
- data/lib/rails_generator/generators/components/mailer/mailer_generator.rb +34 -0
- data/lib/rails_generator/generators/components/mailer/templates/fixture.rhtml +3 -0
- data/lib/rails_generator/generators/components/mailer/templates/mailer.rb +13 -0
- data/lib/rails_generator/generators/components/mailer/templates/unit_test.rb +37 -0
- data/lib/rails_generator/generators/components/mailer/templates/view.rhtml +3 -0
- data/lib/rails_generator/generators/components/migration/USAGE +14 -0
- data/lib/rails_generator/generators/components/migration/migration_generator.rb +7 -0
- data/lib/rails_generator/generators/components/migration/templates/migration.rb +7 -0
- data/lib/rails_generator/generators/components/model/USAGE +26 -0
- data/lib/rails_generator/generators/components/model/model_generator.rb +38 -0
- data/lib/rails_generator/generators/components/model/templates/fixtures.yml +11 -0
- data/lib/rails_generator/generators/components/model/templates/migration.rb +13 -0
- data/lib/rails_generator/generators/components/model/templates/model.rb +2 -0
- data/lib/rails_generator/generators/components/model/templates/unit_test.rb +10 -0
- data/lib/rails_generator/generators/components/observer/USAGE +15 -0
- data/lib/rails_generator/generators/components/observer/observer_generator.rb +16 -0
- data/lib/rails_generator/generators/components/observer/templates/observer.rb +2 -0
- data/lib/rails_generator/generators/components/observer/templates/unit_test.rb +10 -0
- data/lib/rails_generator/generators/components/plugin/USAGE +35 -0
- data/lib/rails_generator/generators/components/plugin/plugin_generator.rb +38 -0
- data/lib/rails_generator/generators/components/plugin/templates/README +4 -0
- data/lib/rails_generator/generators/components/plugin/templates/Rakefile +22 -0
- data/lib/rails_generator/generators/components/plugin/templates/USAGE +8 -0
- data/lib/rails_generator/generators/components/plugin/templates/generator.rb +8 -0
- data/lib/rails_generator/generators/components/plugin/templates/init.rb +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/install.rb +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/plugin.rb +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/tasks.rake +4 -0
- data/lib/rails_generator/generators/components/plugin/templates/uninstall.rb +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/unit_test.rb +8 -0
- data/lib/rails_generator/generators/components/resource/resource_generator.rb +76 -0
- data/lib/rails_generator/generators/components/resource/templates/USAGE +18 -0
- data/lib/rails_generator/generators/components/resource/templates/controller.rb +2 -0
- data/lib/rails_generator/generators/components/resource/templates/fixtures.yml +11 -0
- data/lib/rails_generator/generators/components/resource/templates/functional_test.rb +20 -0
- data/lib/rails_generator/generators/components/resource/templates/helper.rb +2 -0
- data/lib/rails_generator/generators/components/resource/templates/migration.rb +13 -0
- data/lib/rails_generator/generators/components/resource/templates/model.rb +2 -0
- data/lib/rails_generator/generators/components/resource/templates/unit_test.rb +10 -0
- data/lib/rails_generator/generators/components/scaffold/USAGE +32 -0
- data/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb +208 -0
- data/lib/rails_generator/generators/components/scaffold/templates/controller.rb +134 -0
- data/lib/rails_generator/generators/components/scaffold/templates/form.rhtml +3 -0
- data/lib/rails_generator/generators/components/scaffold/templates/form_scaffolding.rhtml +35 -0
- data/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb +102 -0
- data/lib/rails_generator/generators/components/scaffold/templates/helper.rb +2 -0
- data/lib/rails_generator/generators/components/scaffold/templates/layout.rhtml +17 -0
- data/lib/rails_generator/generators/components/scaffold/templates/partial_list_elements.rhtml +127 -0
- data/lib/rails_generator/generators/components/scaffold/templates/style.css +74 -0
- data/lib/rails_generator/generators/components/scaffold/templates/view_edit.rhtml +75 -0
- data/lib/rails_generator/generators/components/scaffold/templates/view_list.rhtml +30 -0
- data/lib/rails_generator/generators/components/scaffold/templates/view_new.rhtml +8 -0
- data/lib/rails_generator/generators/components/scaffold/templates/view_show.rhtml +37 -0
- data/lib/rails_generator/generators/components/scaffold_resource/USAGE +29 -0
- data/lib/rails_generator/generators/components/scaffold_resource/scaffold_resource_generator.rb +92 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/controller.rb +79 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/fixtures.yml +11 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/functional_test.rb +57 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/helper.rb +2 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/layout.rhtml +17 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/migration.rb +13 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/model.rb +2 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/style.css +74 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/unit_test.rb +10 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/view_edit.rhtml +19 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/view_index.rhtml +24 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/view_new.rhtml +18 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/view_show.rhtml +10 -0
- data/lib/rails_generator/generators/components/session_migration/USAGE +15 -0
- data/lib/rails_generator/generators/components/session_migration/session_migration_generator.rb +18 -0
- data/lib/rails_generator/generators/components/session_migration/templates/migration.rb +16 -0
- data/lib/rails_generator/generators/components/web_service/USAGE +28 -0
- data/lib/rails_generator/generators/components/web_service/templates/api_definition.rb +5 -0
- data/lib/rails_generator/generators/components/web_service/templates/controller.rb +8 -0
- data/lib/rails_generator/generators/components/web_service/templates/functional_test.rb +19 -0
- data/lib/rails_generator/generators/components/web_service/web_service_generator.rb +29 -0
- data/lib/rails_generator/lookup.rb +209 -0
- data/lib/rails_generator/manifest.rb +53 -0
- data/lib/rails_generator/options.rb +143 -0
- data/lib/rails_generator/scripts.rb +83 -0
- data/lib/rails_generator/scripts/destroy.rb +7 -0
- data/lib/rails_generator/scripts/generate.rb +7 -0
- data/lib/rails_generator/scripts/update.rb +12 -0
- data/lib/rails_generator/simple_logger.rb +46 -0
- data/lib/rails_generator/spec.rb +44 -0
- data/lib/railties_path.rb +1 -0
- data/lib/ruby_version_check.rb +17 -0
- data/lib/rubyprof_ext.rb +35 -0
- data/lib/tasks/databases.rake +187 -0
- data/lib/tasks/documentation.rake +82 -0
- data/lib/tasks/framework.rake +112 -0
- data/lib/tasks/log.rake +9 -0
- data/lib/tasks/misc.rake +4 -0
- data/lib/tasks/pre_namespace_aliases.rake +53 -0
- data/lib/tasks/rails.rb +8 -0
- data/lib/tasks/statistics.rake +18 -0
- data/lib/tasks/testing.rake +120 -0
- data/lib/tasks/tmp.rake +37 -0
- data/lib/test_help.rb +19 -0
- data/lib/webrick_server.rb +166 -0
- data/libs/migration_helper.rb +11 -0
- data/migrations/001_create_logins.rb +22 -0
- data/migrations/002_create_admins.rb +16 -0
- data/migrations/003_create_roles.rb +13 -0
- data/migrations/004_create_tables.rb +11 -0
- data/migrations/005_create_attr_types.rb +13 -0
- data/migrations/006_create_table_fields.rb +22 -0
- data/migrations/007_create_tab_operations.rb +13 -0
- data/migrations/008_create_attr_operations.rb +13 -0
- data/migrations/009_create_tab_permissions.rb +21 -0
- data/migrations/010_create_attr_permissions.rb +21 -0
- data/migrations/011_create_relations.rb +23 -0
- data/migrations/012_create_scaffolds.rb +11 -0
- data/migrations/013_create_relation_permissions.rb +20 -0
- data/models/admin.rb +38 -0
- data/models/attr_operation.rb +3 -0
- data/models/attr_permission.rb +5 -0
- data/models/attr_type.rb +3 -0
- data/models/login.rb +51 -0
- data/models/relation.rb +5 -0
- data/models/relation_permission.rb +4 -0
- data/models/role.rb +13 -0
- data/models/scaffold.rb +2 -0
- data/models/tab_operation.rb +3 -0
- data/models/tab_permission.rb +5 -0
- data/models/table.rb +18 -0
- data/models/table_field.rb +6 -0
- data/plugins/redhillonrails_core/CHANGELOG +186 -0
- data/plugins/redhillonrails_core/MIT-LICENSE +20 -0
- data/plugins/redhillonrails_core/README +152 -0
- data/plugins/redhillonrails_core/init.rb +1 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/base.rb +54 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/abstract_adapter.rb +46 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/column.rb +21 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb +26 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/index_definition.rb +11 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/mysql_adapter.rb +94 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/mysql_column.rb +8 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/postgresql_adapter.rb +131 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/schema_statements.rb +23 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/sqlite3_adapter.rb +9 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/table_definition.rb +27 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/schema.rb +25 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/schema_dumper.rb +58 -0
- data/plugins/redhillonrails_core/lib/redhillonrails_core.rb +19 -0
- data/plugins/redhillonrails_core/tasks/db/comments.rake +9 -0
- data/plugins/schema_validations/CHANGELOG +149 -0
- data/plugins/schema_validations/MIT-LICENSE +20 -0
- data/plugins/schema_validations/README +54 -0
- data/plugins/schema_validations/about.yml +5 -0
- data/plugins/schema_validations/init.rb +1 -0
- data/plugins/schema_validations/install.rb +1 -0
- data/plugins/schema_validations/lib/red_hill_consulting/schema_validations/active_record/base.rb +108 -0
- data/plugins/schema_validations/lib/schema_validations.rb +1 -0
- data/plugins/svn/README +69 -0
- data/plugins/svn/Rakefile +22 -0
- data/plugins/svn/assets/javascripts/live_validation.js +4 -0
- data/plugins/svn/assets/stylesheets/live_validation.css +28 -0
- data/plugins/svn/init.rb +2 -0
- data/plugins/svn/install.rb +1 -0
- data/plugins/svn/lib/form_helpers.rb +49 -0
- data/plugins/svn/lib/live_validations.rb +73 -0
- data/plugins/svn/tasks/live_validation_tasks.rake +17 -0
- data/plugins/svn/test/form_helpers_test.rb +214 -0
- data/plugins/svn/test/live_validations_test.rb +124 -0
- data/plugins/svn/test/resource.rb +17 -0
- data/plugins/svn/uninstall.rb +1 -0
- data/stylesheets/live_validation.css +28 -0
- data/stylesheets/print.css +72 -0
- data/stylesheets/scaffold.css +175 -0
- data/views/accounts/index.rhtml +12 -0
- data/views/accounts/show.rhtml +5 -0
- data/views/admin/_form.rhtml +15 -0
- data/views/admin/add_user.rhtml +21 -0
- data/views/admin/change_pwd.rhtml +23 -0
- data/views/admin/edit_user.rhtml +12 -0
- data/views/admin/index.rhtml +19 -0
- data/views/begin/index.rhtml +0 -0
- data/views/layouts/general.rhtml +59 -0
- data/views/layouts/login.rhtml +18 -0
- data/views/login/change_pwd.rhtml +21 -0
- data/views/login/expired_pwd.rhtml +19 -0
- data/views/login/index.rhtml +15 -0
- data/views/permission/index.rhtml +17 -0
- data/views/permission/relations.rhtml +28 -0
- data/views/permission/roles.rhtml +45 -0
- data/views/permission/tables.rhtml +53 -0
- data/views/roles/_form.rhtml +8 -0
- data/views/roles/edit.rhtml +11 -0
- data/views/roles/list.rhtml +28 -0
- data/views/roles/new.rhtml +8 -0
- data/views/roles/show.rhtml +14 -0
- data/views/shared_views/_advanced_search.rhtml +35 -0
- data/views/shared_views/_child_elements.rhtml +30 -0
- data/views/shared_views/_childs.rhtml +20 -0
- data/views/shared_views/_father_elements.rhtml +27 -0
- data/views/shared_views/_fathers.rhtml +21 -0
- data/views/shared_views/_fathers_header.rhtml +58 -0
- data/views/shared_views/_many_to_many_elements.rhtml +50 -0
- data/views/shared_views/_many_to_manys.rhtml +18 -0
- data/views/shared_views/_one_to_one_elements.rhtml +24 -0
- data/views/shared_views/_one_to_one_father_elements.rhtml +26 -0
- data/views/shared_views/_one_to_ones.rhtml +42 -0
- data/views/shared_views/_one_to_ones_header.rhtml +117 -0
- data/views/shared_views/edit_child.rhtml +89 -0
- data/views/shared_views/edit_many.rhtml +75 -0
- data/views/tables/_cascade.rhtml +10 -0
- data/views/tables/_field.rhtml +10 -0
- data/views/tables/_fk.rhtml +9 -0
- data/views/tables/_form.rhtml +9 -0
- data/views/tables/_join_table_name.rhtml +16 -0
- data/views/tables/_relation_types.rhtml +5 -0
- data/views/tables/_tables.rhtml +15 -0
- data/views/tables/attributes.rhtml +15 -0
- data/views/tables/edit.rhtml +11 -0
- data/views/tables/edit_field.rhtml +18 -0
- data/views/tables/list.rhtml +39 -0
- data/views/tables/new.rhtml +8 -0
- data/views/tables/new_field.rhtml +25 -0
- data/views/tables/relations.rhtml +24 -0
- data/views/tables/show.rhtml +24 -0
- metadata +518 -0
@@ -0,0 +1,43 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2004 Jeremy Kemper
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
|
24
|
+
$:.unshift(File.dirname(__FILE__))
|
25
|
+
$:.unshift(File.dirname(__FILE__) + "/../../activesupport/lib")
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'active_support'
|
29
|
+
rescue LoadError
|
30
|
+
require 'rubygems'
|
31
|
+
gem 'activesupport'
|
32
|
+
end
|
33
|
+
|
34
|
+
require 'rails_generator/base'
|
35
|
+
require 'rails_generator/lookup'
|
36
|
+
require 'rails_generator/commands'
|
37
|
+
|
38
|
+
Rails::Generator::Base.send(:include, Rails::Generator::Lookup)
|
39
|
+
Rails::Generator::Base.send(:include, Rails::Generator::Commands)
|
40
|
+
|
41
|
+
# Set up a default logger for convenience.
|
42
|
+
require 'rails_generator/simple_logger'
|
43
|
+
Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new(STDOUT)
|
@@ -0,0 +1,261 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/options'
|
2
|
+
require File.dirname(__FILE__) + '/manifest'
|
3
|
+
require File.dirname(__FILE__) + '/spec'
|
4
|
+
require File.dirname(__FILE__) + '/generated_attribute'
|
5
|
+
|
6
|
+
module Rails
|
7
|
+
# Rails::Generator is a code generation platform tailored for the Rails
|
8
|
+
# web application framework. Generators are easily invoked within Rails
|
9
|
+
# applications to add and remove components such as models and controllers.
|
10
|
+
# New generators are easy to create and may be distributed as RubyGems,
|
11
|
+
# tarballs, or Rails plugins for inclusion system-wide, per-user,
|
12
|
+
# or per-application.
|
13
|
+
#
|
14
|
+
# For actual examples see the rails_generator/generators directory in the
|
15
|
+
# Rails source (or the +railties+ directory if you have frozen the Rails
|
16
|
+
# source in your application).
|
17
|
+
#
|
18
|
+
# Generators may subclass other generators to provide variations that
|
19
|
+
# require little or no new logic but replace the template files.
|
20
|
+
#
|
21
|
+
# For a RubyGem, put your generator class and templates in the +lib+
|
22
|
+
# directory. For a Rails plugin, make a +generators+ directory at the
|
23
|
+
# root of your plugin.
|
24
|
+
#
|
25
|
+
# The layout of generator files can be seen in the built-in
|
26
|
+
# +controller+ generator:
|
27
|
+
#
|
28
|
+
# generators/
|
29
|
+
# controller/
|
30
|
+
# controller_generator.rb
|
31
|
+
# templates/
|
32
|
+
# controller.rb
|
33
|
+
# functional_test.rb
|
34
|
+
# helper.rb
|
35
|
+
# view.rhtml
|
36
|
+
#
|
37
|
+
# The directory name (+controller+) matches the name of the generator file
|
38
|
+
# (controller_generator.rb) and class (+ControllerGenerator+). The files
|
39
|
+
# that will be copied or used as templates are stored in the +templates+
|
40
|
+
# directory.
|
41
|
+
#
|
42
|
+
# The filenames of the templates don't matter, but choose something that
|
43
|
+
# will be self-explatatory since you will be referencing these in the
|
44
|
+
# +manifest+ method inside your generator subclass.
|
45
|
+
#
|
46
|
+
#
|
47
|
+
module Generator
|
48
|
+
class GeneratorError < StandardError; end
|
49
|
+
class UsageError < GeneratorError; end
|
50
|
+
|
51
|
+
|
52
|
+
# The base code generator is bare-bones. It sets up the source and
|
53
|
+
# destination paths and tells the logger whether to keep its trap shut.
|
54
|
+
#
|
55
|
+
# It's useful for copying files such as stylesheets, images, or
|
56
|
+
# javascripts.
|
57
|
+
#
|
58
|
+
# For more comprehensive template-based passive code generation with
|
59
|
+
# arguments, you'll want Rails::Generator::NamedBase.
|
60
|
+
#
|
61
|
+
# Generators create a manifest of the actions they perform then hand
|
62
|
+
# the manifest to a command which replays the actions to do the heavy
|
63
|
+
# lifting (such as checking for existing files or creating directories
|
64
|
+
# if needed). Create, destroy, and list commands are included. Since a
|
65
|
+
# single manifest may be used by any command, creating new generators is
|
66
|
+
# as simple as writing some code templates and declaring what you'd like
|
67
|
+
# to do with them.
|
68
|
+
#
|
69
|
+
# The manifest method must be implemented by subclasses, returning a
|
70
|
+
# Rails::Generator::Manifest. The +record+ method is provided as a
|
71
|
+
# convenience for manifest creation. Example:
|
72
|
+
#
|
73
|
+
# class StylesheetGenerator < Rails::Generator::Base
|
74
|
+
# def manifest
|
75
|
+
# record do |m|
|
76
|
+
# m.directory('public/stylesheets')
|
77
|
+
# m.file('application.css', 'public/stylesheets/application.css')
|
78
|
+
# end
|
79
|
+
# end
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# See Rails::Generator::Commands::Create for a list of methods available
|
83
|
+
# to the manifest.
|
84
|
+
class Base
|
85
|
+
include Options
|
86
|
+
|
87
|
+
# Declare default options for the generator. These options
|
88
|
+
# are inherited to subclasses.
|
89
|
+
default_options :collision => :ask, :quiet => false
|
90
|
+
|
91
|
+
# A logger instance available everywhere in the generator.
|
92
|
+
cattr_accessor :logger
|
93
|
+
|
94
|
+
# Every generator that is dynamically looked up is tagged with a
|
95
|
+
# Spec describing where it was found.
|
96
|
+
class_inheritable_accessor :spec
|
97
|
+
|
98
|
+
attr_reader :source_root, :destination_root, :args
|
99
|
+
|
100
|
+
def initialize(runtime_args, runtime_options = {})
|
101
|
+
@args = runtime_args
|
102
|
+
parse!(@args, runtime_options)
|
103
|
+
|
104
|
+
# Derive source and destination paths.
|
105
|
+
@source_root = options[:source] || File.join(spec.path, 'templates')
|
106
|
+
if options[:destination]
|
107
|
+
@destination_root = options[:destination]
|
108
|
+
elsif defined? ::RAILS_ROOT
|
109
|
+
@destination_root = ::RAILS_ROOT
|
110
|
+
end
|
111
|
+
|
112
|
+
# Silence the logger if requested.
|
113
|
+
logger.quiet = options[:quiet]
|
114
|
+
|
115
|
+
# Raise usage error if help is requested.
|
116
|
+
usage if options[:help]
|
117
|
+
end
|
118
|
+
|
119
|
+
# Generators must provide a manifest. Use the +record+ method to create
|
120
|
+
# a new manifest and record your generator's actions.
|
121
|
+
def manifest
|
122
|
+
raise NotImplementedError, "No manifest for '#{spec.name}' generator."
|
123
|
+
end
|
124
|
+
|
125
|
+
# Return the full path from the source root for the given path.
|
126
|
+
# Example for source_root = '/source':
|
127
|
+
# source_path('some/path.rb') == '/source/some/path.rb'
|
128
|
+
#
|
129
|
+
# The given path may include a colon ':' character to indicate that
|
130
|
+
# the file belongs to another generator. This notation allows any
|
131
|
+
# generator to borrow files from another. Example:
|
132
|
+
# source_path('model:fixture.yml') = '/model/source/path/fixture.yml'
|
133
|
+
def source_path(relative_source)
|
134
|
+
# Check whether we're referring to another generator's file.
|
135
|
+
name, path = relative_source.split(':', 2)
|
136
|
+
|
137
|
+
# If not, return the full path to our source file.
|
138
|
+
if path.nil?
|
139
|
+
File.join(source_root, name)
|
140
|
+
|
141
|
+
# Otherwise, ask our referral for the file.
|
142
|
+
else
|
143
|
+
# FIXME: this is broken, though almost always true. Others'
|
144
|
+
# source_root are not necessarily the templates dir.
|
145
|
+
File.join(self.class.lookup(name).path, 'templates', path)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
# Return the full path from the destination root for the given path.
|
150
|
+
# Example for destination_root = '/dest':
|
151
|
+
# destination_path('some/path.rb') == '/dest/some/path.rb'
|
152
|
+
def destination_path(relative_destination)
|
153
|
+
File.join(destination_root, relative_destination)
|
154
|
+
end
|
155
|
+
|
156
|
+
protected
|
157
|
+
# Convenience method for generator subclasses to record a manifest.
|
158
|
+
def record
|
159
|
+
Rails::Generator::Manifest.new(self) { |m| yield m }
|
160
|
+
end
|
161
|
+
|
162
|
+
# Override with your own usage banner.
|
163
|
+
def banner
|
164
|
+
"Usage: #{$0} #{spec.name} [options]"
|
165
|
+
end
|
166
|
+
|
167
|
+
# Read USAGE from file in generator base path.
|
168
|
+
def usage_message
|
169
|
+
File.read(File.join(spec.path, 'USAGE')) rescue ''
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
|
174
|
+
# The base generator for named components: models, controllers, mailers,
|
175
|
+
# etc. The target name is taken as the first argument and inflected to
|
176
|
+
# singular, plural, class, file, and table forms for your convenience.
|
177
|
+
# The remaining arguments are aliased to +actions+ as an array for
|
178
|
+
# controller and mailer convenience.
|
179
|
+
#
|
180
|
+
# Several useful local variables and methods are populated in the
|
181
|
+
# +initialize+ method. See below for a list of Attributes and
|
182
|
+
# External Aliases available to both the manifest and to all templates.
|
183
|
+
#
|
184
|
+
# If no name is provided, the generator raises a usage error with content
|
185
|
+
# optionally read from the USAGE file in the generator's base path.
|
186
|
+
#
|
187
|
+
# For example, the +controller+ generator takes the first argument as
|
188
|
+
# the name of the class and subsequent arguments as the names of
|
189
|
+
# actions to be generated:
|
190
|
+
#
|
191
|
+
# ./script/generate controller Article index new create
|
192
|
+
#
|
193
|
+
# See Rails::Generator::Base for a discussion of manifests,
|
194
|
+
# Rails::Generator::Commands::Create for methods available to the manifest,
|
195
|
+
# and Rails::Generator for a general discussion of generators.
|
196
|
+
class NamedBase < Base
|
197
|
+
attr_reader :name, :class_name, :singular_name, :plural_name, :table_name
|
198
|
+
attr_reader :class_path, :file_path, :class_nesting, :class_nesting_depth
|
199
|
+
alias_method :file_name, :singular_name
|
200
|
+
alias_method :actions, :args
|
201
|
+
|
202
|
+
def initialize(runtime_args, runtime_options = {})
|
203
|
+
super
|
204
|
+
|
205
|
+
# Name argument is required.
|
206
|
+
usage if runtime_args.empty?
|
207
|
+
|
208
|
+
@args = runtime_args.dup
|
209
|
+
base_name = @args.shift
|
210
|
+
assign_names!(base_name)
|
211
|
+
end
|
212
|
+
|
213
|
+
protected
|
214
|
+
# Override with your own usage banner.
|
215
|
+
def banner
|
216
|
+
"Usage: #{$0} #{spec.name} #{spec.name.camelize}Name [options]"
|
217
|
+
end
|
218
|
+
|
219
|
+
def attributes
|
220
|
+
@attributes ||= @args.collect do |attribute|
|
221
|
+
Rails::Generator::GeneratedAttribute.new(*attribute.split(":"))
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
|
226
|
+
private
|
227
|
+
def assign_names!(name)
|
228
|
+
@name = name
|
229
|
+
base_name, @class_path, @file_path, @class_nesting, @class_nesting_depth = extract_modules(@name)
|
230
|
+
@class_name_without_nesting, @singular_name, @plural_name = inflect_names(base_name)
|
231
|
+
@table_name = ActiveRecord::Base.pluralize_table_names ? plural_name : singular_name
|
232
|
+
if @class_nesting.empty?
|
233
|
+
@class_name = @class_name_without_nesting
|
234
|
+
else
|
235
|
+
@table_name = @class_nesting.underscore << "_" << @table_name
|
236
|
+
@class_name = "#{@class_nesting}::#{@class_name_without_nesting}"
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
# Extract modules from filesystem-style or ruby-style path:
|
241
|
+
# good/fun/stuff
|
242
|
+
# Good::Fun::Stuff
|
243
|
+
# produce the same results.
|
244
|
+
def extract_modules(name)
|
245
|
+
modules = name.include?('/') ? name.split('/') : name.split('::')
|
246
|
+
name = modules.pop
|
247
|
+
path = modules.map { |m| m.underscore }
|
248
|
+
file_path = (path + [name.underscore]).join('/')
|
249
|
+
nesting = modules.map { |m| m.camelize }.join('::')
|
250
|
+
[name, path, file_path, nesting, modules.size]
|
251
|
+
end
|
252
|
+
|
253
|
+
def inflect_names(name)
|
254
|
+
camel = name.camelize
|
255
|
+
under = camel.underscore
|
256
|
+
plural = under.pluralize
|
257
|
+
[camel, under, plural]
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
@@ -0,0 +1,581 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
require 'optparse'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'erb'
|
6
|
+
|
7
|
+
module Rails
|
8
|
+
module Generator
|
9
|
+
module Commands
|
10
|
+
# Here's a convenient way to get a handle on generator commands.
|
11
|
+
# Command.instance('destroy', my_generator) instantiates a Destroy
|
12
|
+
# delegate of my_generator ready to do your dirty work.
|
13
|
+
def self.instance(command, generator)
|
14
|
+
const_get(command.to_s.camelize).new(generator)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Even more convenient access to commands. Include Commands in
|
18
|
+
# the generator Base class to get a nice #command instance method
|
19
|
+
# which returns a delegate for the requested command.
|
20
|
+
def self.included(base)
|
21
|
+
base.send(:define_method, :command) do |command|
|
22
|
+
Commands.instance(command, self)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
# Generator commands delegate Rails::Generator::Base and implement
|
28
|
+
# a standard set of actions. Their behavior is defined by the way
|
29
|
+
# they respond to these actions: Create brings life; Destroy brings
|
30
|
+
# death; List passively observes.
|
31
|
+
#
|
32
|
+
# Commands are invoked by replaying (or rewinding) the generator's
|
33
|
+
# manifest of actions. See Rails::Generator::Manifest and
|
34
|
+
# Rails::Generator::Base#manifest method that generator subclasses
|
35
|
+
# are required to override.
|
36
|
+
#
|
37
|
+
# Commands allows generators to "plug in" invocation behavior, which
|
38
|
+
# corresponds to the GoF Strategy pattern.
|
39
|
+
class Base < DelegateClass(Rails::Generator::Base)
|
40
|
+
# Replay action manifest. RewindBase subclass rewinds manifest.
|
41
|
+
def invoke!
|
42
|
+
manifest.replay(self)
|
43
|
+
end
|
44
|
+
|
45
|
+
def dependency(generator_name, args, runtime_options = {})
|
46
|
+
logger.dependency(generator_name) do
|
47
|
+
self.class.new(instance(generator_name, args, full_options(runtime_options))).invoke!
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Does nothing for all commands except Create.
|
52
|
+
def class_collisions(*class_names)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Does nothing for all commands except Create.
|
56
|
+
def readme(*args)
|
57
|
+
end
|
58
|
+
|
59
|
+
protected
|
60
|
+
def migration_directory(relative_path)
|
61
|
+
directory(@migration_directory = relative_path)
|
62
|
+
end
|
63
|
+
|
64
|
+
def existing_migrations(file_name)
|
65
|
+
Dir.glob("#{@migration_directory}/[0-9]*_*.rb").grep(/[0-9]+_#{file_name}.rb$/)
|
66
|
+
end
|
67
|
+
|
68
|
+
def migration_exists?(file_name)
|
69
|
+
not existing_migrations(file_name).empty?
|
70
|
+
end
|
71
|
+
|
72
|
+
def current_migration_number
|
73
|
+
Dir.glob("#{@migration_directory}/[0-9]*.rb").inject(0) do |max, file_path|
|
74
|
+
n = File.basename(file_path).split('_', 2).first.to_i
|
75
|
+
if n > max then n else max end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def next_migration_number
|
80
|
+
current_migration_number + 1
|
81
|
+
end
|
82
|
+
|
83
|
+
def next_migration_string(padding = 3)
|
84
|
+
"%.#{padding}d" % next_migration_number
|
85
|
+
end
|
86
|
+
|
87
|
+
def gsub_file(relative_destination, regexp, *args, &block)
|
88
|
+
path = destination_path(relative_destination)
|
89
|
+
content = File.read(path).gsub(regexp, *args, &block)
|
90
|
+
File.open(path, 'wb') { |file| file.write(content) }
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
# Ask the user interactively whether to force collision.
|
95
|
+
def force_file_collision?(destination, src, dst, file_options = {}, &block)
|
96
|
+
$stdout.print "overwrite #{destination}? [Ynaqd] "
|
97
|
+
case $stdin.gets
|
98
|
+
when /d/i
|
99
|
+
Tempfile.open(File.basename(destination), File.dirname(dst)) do |temp|
|
100
|
+
temp.write render_file(src, file_options, &block)
|
101
|
+
temp.rewind
|
102
|
+
$stdout.puts `#{diff_cmd} #{dst} #{temp.path}`
|
103
|
+
end
|
104
|
+
puts "retrying"
|
105
|
+
raise 'retry diff'
|
106
|
+
when /a/i
|
107
|
+
$stdout.puts "forcing #{spec.name}"
|
108
|
+
options[:collision] = :force
|
109
|
+
when /q/i
|
110
|
+
$stdout.puts "aborting #{spec.name}"
|
111
|
+
raise SystemExit
|
112
|
+
when /n/i then :skip
|
113
|
+
else :force
|
114
|
+
end
|
115
|
+
rescue
|
116
|
+
retry
|
117
|
+
end
|
118
|
+
|
119
|
+
def diff_cmd
|
120
|
+
ENV['RAILS_DIFF'] || 'diff -u'
|
121
|
+
end
|
122
|
+
|
123
|
+
def render_template_part(template_options)
|
124
|
+
# Getting Sandbox to evaluate part template in it
|
125
|
+
part_binding = template_options[:sandbox].call.sandbox_binding
|
126
|
+
part_rel_path = template_options[:insert]
|
127
|
+
part_path = source_path(part_rel_path)
|
128
|
+
|
129
|
+
# Render inner template within Sandbox binding
|
130
|
+
rendered_part = ERB.new(File.readlines(part_path).join, nil, '-').result(part_binding)
|
131
|
+
begin_mark = template_part_mark(template_options[:begin_mark], template_options[:mark_id])
|
132
|
+
end_mark = template_part_mark(template_options[:end_mark], template_options[:mark_id])
|
133
|
+
begin_mark + rendered_part + end_mark
|
134
|
+
end
|
135
|
+
|
136
|
+
def template_part_mark(name, id)
|
137
|
+
"<!--[#{name}:#{id}]-->\n"
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# Base class for commands which handle generator actions in reverse, such as Destroy.
|
142
|
+
class RewindBase < Base
|
143
|
+
# Rewind action manifest.
|
144
|
+
def invoke!
|
145
|
+
manifest.rewind(self)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
|
150
|
+
# Create is the premier generator command. It copies files, creates
|
151
|
+
# directories, renders templates, and more.
|
152
|
+
class Create < Base
|
153
|
+
|
154
|
+
# Check whether the given class names are already taken by
|
155
|
+
# Ruby or Rails. In the future, expand to check other namespaces
|
156
|
+
# such as the rest of the user's app.
|
157
|
+
def class_collisions(*class_names)
|
158
|
+
class_names.flatten.each do |class_name|
|
159
|
+
# Convert to string to allow symbol arguments.
|
160
|
+
class_name = class_name.to_s
|
161
|
+
|
162
|
+
# Skip empty strings.
|
163
|
+
next if class_name.strip.empty?
|
164
|
+
|
165
|
+
# Split the class from its module nesting.
|
166
|
+
nesting = class_name.split('::')
|
167
|
+
name = nesting.pop
|
168
|
+
|
169
|
+
# Extract the last Module in the nesting.
|
170
|
+
last = nesting.inject(Object) { |last, nest|
|
171
|
+
break unless last.const_defined?(nest)
|
172
|
+
last.const_get(nest)
|
173
|
+
}
|
174
|
+
|
175
|
+
# If the last Module exists, check whether the given
|
176
|
+
# class exists and raise a collision if so.
|
177
|
+
if last and last.const_defined?(name.camelize)
|
178
|
+
raise_class_collision(class_name)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
# Copy a file from source to destination with collision checking.
|
184
|
+
#
|
185
|
+
# The file_options hash accepts :chmod and :shebang and :collision options.
|
186
|
+
# :chmod sets the permissions of the destination file:
|
187
|
+
# file 'config/empty.log', 'log/test.log', :chmod => 0664
|
188
|
+
# :shebang sets the #!/usr/bin/ruby line for scripts
|
189
|
+
# file 'bin/generate.rb', 'script/generate', :chmod => 0755, :shebang => '/usr/bin/env ruby'
|
190
|
+
# :collision sets the collision option only for the destination file:
|
191
|
+
# file 'settings/server.yml', 'config/server.yml', :collision => :skip
|
192
|
+
#
|
193
|
+
# Collisions are handled by checking whether the destination file
|
194
|
+
# exists and either skipping the file, forcing overwrite, or asking
|
195
|
+
# the user what to do.
|
196
|
+
def file(relative_source, relative_destination, file_options = {}, &block)
|
197
|
+
# Determine full paths for source and destination files.
|
198
|
+
source = source_path(relative_source)
|
199
|
+
destination = destination_path(relative_destination)
|
200
|
+
destination_exists = File.exists?(destination)
|
201
|
+
|
202
|
+
# If source and destination are identical then we're done.
|
203
|
+
if destination_exists and identical?(source, destination, &block)
|
204
|
+
return logger.identical(relative_destination)
|
205
|
+
end
|
206
|
+
|
207
|
+
# Check for and resolve file collisions.
|
208
|
+
if destination_exists
|
209
|
+
|
210
|
+
# Make a choice whether to overwrite the file. :force and
|
211
|
+
# :skip already have their mind made up, but give :ask a shot.
|
212
|
+
choice = case (file_options[:collision] || options[:collision]).to_sym #|| :ask
|
213
|
+
when :ask then force_file_collision?(relative_destination, source, destination, file_options, &block)
|
214
|
+
when :force then :force
|
215
|
+
when :skip then :skip
|
216
|
+
else raise "Invalid collision option: #{options[:collision].inspect}"
|
217
|
+
end
|
218
|
+
|
219
|
+
# Take action based on our choice. Bail out if we chose to
|
220
|
+
# skip the file; otherwise, log our transgression and continue.
|
221
|
+
case choice
|
222
|
+
when :force then logger.force(relative_destination)
|
223
|
+
when :skip then return(logger.skip(relative_destination))
|
224
|
+
else raise "Invalid collision choice: #{choice}.inspect"
|
225
|
+
end
|
226
|
+
|
227
|
+
# File doesn't exist so log its unbesmirched creation.
|
228
|
+
else
|
229
|
+
logger.create relative_destination
|
230
|
+
end
|
231
|
+
|
232
|
+
# If we're pretending, back off now.
|
233
|
+
return if options[:pretend]
|
234
|
+
|
235
|
+
# Write destination file with optional shebang. Yield for content
|
236
|
+
# if block given so templaters may render the source file. If a
|
237
|
+
# shebang is requested, replace the existing shebang or insert a
|
238
|
+
# new one.
|
239
|
+
File.open(destination, 'wb') do |dest|
|
240
|
+
dest.write render_file(source, file_options, &block)
|
241
|
+
end
|
242
|
+
|
243
|
+
# Optionally change permissions.
|
244
|
+
if file_options[:chmod]
|
245
|
+
FileUtils.chmod(file_options[:chmod], destination)
|
246
|
+
end
|
247
|
+
|
248
|
+
# Optionally add file to subversion
|
249
|
+
system("svn add #{destination}") if options[:svn]
|
250
|
+
end
|
251
|
+
|
252
|
+
# Checks if the source and the destination file are identical. If
|
253
|
+
# passed a block then the source file is a template that needs to first
|
254
|
+
# be evaluated before being compared to the destination.
|
255
|
+
def identical?(source, destination, &block)
|
256
|
+
return false if File.directory? destination
|
257
|
+
source = block_given? ? File.open(source) {|sf| yield(sf)} : IO.read(source)
|
258
|
+
destination = IO.read(destination)
|
259
|
+
source == destination
|
260
|
+
end
|
261
|
+
|
262
|
+
# Generate a file for a Rails application using an ERuby template.
|
263
|
+
# Looks up and evalutes a template by name and writes the result.
|
264
|
+
#
|
265
|
+
# The ERB template uses explicit trim mode to best control the
|
266
|
+
# proliferation of whitespace in generated code. <%- trims leading
|
267
|
+
# whitespace; -%> trims trailing whitespace including one newline.
|
268
|
+
#
|
269
|
+
# A hash of template options may be passed as the last argument.
|
270
|
+
# The options accepted by the file are accepted as well as :assigns,
|
271
|
+
# a hash of variable bindings. Example:
|
272
|
+
# template 'foo', 'bar', :assigns => { :action => 'view' }
|
273
|
+
#
|
274
|
+
# Template is implemented in terms of file. It calls file with a
|
275
|
+
# block which takes a file handle and returns its rendered contents.
|
276
|
+
def template(relative_source, relative_destination, template_options = {})
|
277
|
+
file(relative_source, relative_destination, template_options) do |file|
|
278
|
+
# Evaluate any assignments in a temporary, throwaway binding.
|
279
|
+
vars = template_options[:assigns] || {}
|
280
|
+
b = binding
|
281
|
+
vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b }
|
282
|
+
|
283
|
+
# Render the source file with the temporary binding.
|
284
|
+
ERB.new(file.read, nil, '-').result(b)
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
def complex_template(relative_source, relative_destination, template_options = {})
|
289
|
+
options = template_options.dup
|
290
|
+
options[:assigns] ||= {}
|
291
|
+
options[:assigns]['template_for_inclusion'] = render_template_part(template_options)
|
292
|
+
template(relative_source, relative_destination, options)
|
293
|
+
end
|
294
|
+
|
295
|
+
# Create a directory including any missing parent directories.
|
296
|
+
# Always directories which exist.
|
297
|
+
def directory(relative_path)
|
298
|
+
path = destination_path(relative_path)
|
299
|
+
if File.exists?(path)
|
300
|
+
logger.exists relative_path
|
301
|
+
else
|
302
|
+
logger.create relative_path
|
303
|
+
unless options[:pretend]
|
304
|
+
FileUtils.mkdir_p(path)
|
305
|
+
|
306
|
+
# Subversion doesn't do path adds, so we need to add
|
307
|
+
# each directory individually.
|
308
|
+
# So stack up the directory tree and add the paths to
|
309
|
+
# subversion in order without recursion.
|
310
|
+
if options[:svn]
|
311
|
+
stack=[relative_path]
|
312
|
+
until File.dirname(stack.last) == stack.last # dirname('.') == '.'
|
313
|
+
stack.push File.dirname(stack.last)
|
314
|
+
end
|
315
|
+
stack.reverse_each do |rel_path|
|
316
|
+
svn_path = destination_path(rel_path)
|
317
|
+
system("svn add -N #{svn_path}") unless File.directory?(File.join(svn_path, '.svn'))
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
# Display a README.
|
325
|
+
def readme(*relative_sources)
|
326
|
+
relative_sources.flatten.each do |relative_source|
|
327
|
+
logger.readme relative_source
|
328
|
+
puts File.read(source_path(relative_source)) unless options[:pretend]
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
332
|
+
# When creating a migration, it knows to find the first available file in db/migrate and use the migration.rb template.
|
333
|
+
def migration_template(relative_source, relative_destination, template_options = {})
|
334
|
+
migration_directory relative_destination
|
335
|
+
migration_file_name = template_options[:migration_file_name] || file_name
|
336
|
+
raise "Another migration is already named #{migration_file_name}: #{existing_migrations(migration_file_name).first}" if migration_exists?(migration_file_name)
|
337
|
+
template(relative_source, "#{relative_destination}/#{next_migration_string}_#{migration_file_name}.rb", template_options)
|
338
|
+
end
|
339
|
+
|
340
|
+
def route_resources(*resources)
|
341
|
+
resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
|
342
|
+
sentinel = 'ActionController::Routing::Routes.draw do |map|'
|
343
|
+
|
344
|
+
logger.route "map.resources #{resource_list}"
|
345
|
+
unless options[:pretend]
|
346
|
+
gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
|
347
|
+
"#{match}\n map.resources #{resource_list}\n"
|
348
|
+
end
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
private
|
353
|
+
def render_file(path, options = {})
|
354
|
+
File.open(path, 'rb') do |file|
|
355
|
+
if block_given?
|
356
|
+
yield file
|
357
|
+
else
|
358
|
+
content = ''
|
359
|
+
if shebang = options[:shebang]
|
360
|
+
content << "#!#{shebang}\n"
|
361
|
+
if line = file.gets
|
362
|
+
content << "line\n" if line !~ /^#!/
|
363
|
+
end
|
364
|
+
end
|
365
|
+
content << file.read
|
366
|
+
end
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
# Raise a usage error with an informative WordNet suggestion.
|
371
|
+
# Thanks to Florian Gross (flgr).
|
372
|
+
def raise_class_collision(class_name)
|
373
|
+
message = <<end_message
|
374
|
+
The name '#{class_name}' is reserved by Ruby on Rails.
|
375
|
+
Please choose an alternative and run this generator again.
|
376
|
+
end_message
|
377
|
+
if suggest = find_synonyms(class_name)
|
378
|
+
message << "\n Suggestions: \n\n"
|
379
|
+
message << suggest.join("\n")
|
380
|
+
end
|
381
|
+
raise UsageError, message
|
382
|
+
end
|
383
|
+
|
384
|
+
SYNONYM_LOOKUP_URI = "http://wordnet.princeton.edu/cgi-bin/webwn2.0?stage=2&word=%s&posnumber=1&searchtypenumber=2&senses=&showglosses=1"
|
385
|
+
|
386
|
+
# Look up synonyms on WordNet. Thanks to Florian Gross (flgr).
|
387
|
+
def find_synonyms(word)
|
388
|
+
require 'open-uri'
|
389
|
+
require 'timeout'
|
390
|
+
timeout(5) do
|
391
|
+
open(SYNONYM_LOOKUP_URI % word) do |stream|
|
392
|
+
data = stream.read.gsub(" ", " ").gsub("<BR>", "")
|
393
|
+
data.scan(/^Sense \d+\n.+?\n\n/m)
|
394
|
+
end
|
395
|
+
end
|
396
|
+
rescue Exception
|
397
|
+
return nil
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
|
402
|
+
# Undo the actions performed by a generator. Rewind the action
|
403
|
+
# manifest and attempt to completely erase the results of each action.
|
404
|
+
class Destroy < RewindBase
|
405
|
+
# Remove a file if it exists and is a file.
|
406
|
+
def file(relative_source, relative_destination, file_options = {})
|
407
|
+
destination = destination_path(relative_destination)
|
408
|
+
if File.exists?(destination)
|
409
|
+
logger.rm relative_destination
|
410
|
+
unless options[:pretend]
|
411
|
+
if options[:svn]
|
412
|
+
# If the file has been marked to be added
|
413
|
+
# but has not yet been checked in, revert and delete
|
414
|
+
if options[:svn][relative_destination]
|
415
|
+
system("svn revert #{destination}")
|
416
|
+
FileUtils.rm(destination)
|
417
|
+
else
|
418
|
+
# If the directory is not in the status list, it
|
419
|
+
# has no modifications so we can simply remove it
|
420
|
+
system("svn rm #{destination}")
|
421
|
+
end
|
422
|
+
else
|
423
|
+
FileUtils.rm(destination)
|
424
|
+
end
|
425
|
+
end
|
426
|
+
else
|
427
|
+
logger.missing relative_destination
|
428
|
+
return
|
429
|
+
end
|
430
|
+
end
|
431
|
+
|
432
|
+
# Templates are deleted just like files and the actions take the
|
433
|
+
# same parameters, so simply alias the file method.
|
434
|
+
alias_method :template, :file
|
435
|
+
|
436
|
+
# Remove each directory in the given path from right to left.
|
437
|
+
# Remove each subdirectory if it exists and is a directory.
|
438
|
+
def directory(relative_path)
|
439
|
+
parts = relative_path.split('/')
|
440
|
+
until parts.empty?
|
441
|
+
partial = File.join(parts)
|
442
|
+
path = destination_path(partial)
|
443
|
+
if File.exists?(path)
|
444
|
+
if Dir[File.join(path, '*')].empty?
|
445
|
+
logger.rmdir partial
|
446
|
+
unless options[:pretend]
|
447
|
+
if options[:svn]
|
448
|
+
# If the directory has been marked to be added
|
449
|
+
# but has not yet been checked in, revert and delete
|
450
|
+
if options[:svn][relative_path]
|
451
|
+
system("svn revert #{path}")
|
452
|
+
FileUtils.rmdir(path)
|
453
|
+
else
|
454
|
+
# If the directory is not in the status list, it
|
455
|
+
# has no modifications so we can simply remove it
|
456
|
+
system("svn rm #{path}")
|
457
|
+
end
|
458
|
+
else
|
459
|
+
FileUtils.rmdir(path)
|
460
|
+
end
|
461
|
+
end
|
462
|
+
else
|
463
|
+
logger.notempty partial
|
464
|
+
end
|
465
|
+
else
|
466
|
+
logger.missing partial
|
467
|
+
end
|
468
|
+
parts.pop
|
469
|
+
end
|
470
|
+
end
|
471
|
+
|
472
|
+
def complex_template(*args)
|
473
|
+
# nothing should be done here
|
474
|
+
end
|
475
|
+
|
476
|
+
# When deleting a migration, it knows to delete every file named "[0-9]*_#{file_name}".
|
477
|
+
def migration_template(relative_source, relative_destination, template_options = {})
|
478
|
+
migration_directory relative_destination
|
479
|
+
|
480
|
+
migration_file_name = template_options[:migration_file_name] || file_name
|
481
|
+
unless migration_exists?(migration_file_name)
|
482
|
+
puts "There is no migration named #{migration_file_name}"
|
483
|
+
return
|
484
|
+
end
|
485
|
+
|
486
|
+
|
487
|
+
existing_migrations(migration_file_name).each do |file_path|
|
488
|
+
file(relative_source, file_path, template_options)
|
489
|
+
end
|
490
|
+
end
|
491
|
+
|
492
|
+
def route_resources(*resources)
|
493
|
+
resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
|
494
|
+
look_for = "\n map.resources #{resource_list}\n"
|
495
|
+
logger.route "map.resources #{resource_list}"
|
496
|
+
gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
|
497
|
+
end
|
498
|
+
end
|
499
|
+
|
500
|
+
|
501
|
+
# List a generator's action manifest.
|
502
|
+
class List < Base
|
503
|
+
def dependency(generator_name, args, options = {})
|
504
|
+
logger.dependency "#{generator_name}(#{args.join(', ')}, #{options.inspect})"
|
505
|
+
end
|
506
|
+
|
507
|
+
def class_collisions(*class_names)
|
508
|
+
logger.class_collisions class_names.join(', ')
|
509
|
+
end
|
510
|
+
|
511
|
+
def file(relative_source, relative_destination, options = {})
|
512
|
+
logger.file relative_destination
|
513
|
+
end
|
514
|
+
|
515
|
+
def template(relative_source, relative_destination, options = {})
|
516
|
+
logger.template relative_destination
|
517
|
+
end
|
518
|
+
|
519
|
+
def complex_template(relative_source, relative_destination, options = {})
|
520
|
+
logger.template "#{options[:insert]} inside #{relative_destination}"
|
521
|
+
end
|
522
|
+
|
523
|
+
def directory(relative_path)
|
524
|
+
logger.directory "#{destination_path(relative_path)}/"
|
525
|
+
end
|
526
|
+
|
527
|
+
def readme(*args)
|
528
|
+
logger.readme args.join(', ')
|
529
|
+
end
|
530
|
+
|
531
|
+
def migration_template(relative_source, relative_destination, options = {})
|
532
|
+
migration_directory relative_destination
|
533
|
+
logger.migration_template file_name
|
534
|
+
end
|
535
|
+
|
536
|
+
def route_resources(*resources)
|
537
|
+
resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
|
538
|
+
logger.route "map.resources #{resource_list}"
|
539
|
+
end
|
540
|
+
end
|
541
|
+
|
542
|
+
# Update generator's action manifest.
|
543
|
+
class Update < Create
|
544
|
+
def file(relative_source, relative_destination, options = {})
|
545
|
+
# logger.file relative_destination
|
546
|
+
end
|
547
|
+
|
548
|
+
def template(relative_source, relative_destination, options = {})
|
549
|
+
# logger.template relative_destination
|
550
|
+
end
|
551
|
+
|
552
|
+
def complex_template(relative_source, relative_destination, template_options = {})
|
553
|
+
|
554
|
+
begin
|
555
|
+
dest_file = destination_path(relative_destination)
|
556
|
+
source_to_update = File.readlines(dest_file).join
|
557
|
+
rescue Errno::ENOENT
|
558
|
+
logger.missing relative_destination
|
559
|
+
return
|
560
|
+
end
|
561
|
+
|
562
|
+
logger.refreshing "#{template_options[:insert].gsub(/\.rhtml/,'')} inside #{relative_destination}"
|
563
|
+
|
564
|
+
begin_mark = Regexp.quote(template_part_mark(template_options[:begin_mark], template_options[:mark_id]))
|
565
|
+
end_mark = Regexp.quote(template_part_mark(template_options[:end_mark], template_options[:mark_id]))
|
566
|
+
|
567
|
+
# Refreshing inner part of the template with freshly rendered part.
|
568
|
+
rendered_part = render_template_part(template_options)
|
569
|
+
source_to_update.gsub!(/#{begin_mark}.*?#{end_mark}/m, rendered_part)
|
570
|
+
|
571
|
+
File.open(dest_file, 'w') { |file| file.write(source_to_update) }
|
572
|
+
end
|
573
|
+
|
574
|
+
def directory(relative_path)
|
575
|
+
# logger.directory "#{destination_path(relative_path)}/"
|
576
|
+
end
|
577
|
+
end
|
578
|
+
|
579
|
+
end
|
580
|
+
end
|
581
|
+
end
|