aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--railties/CHANGELOG4
-rw-r--r--railties/Rakefile1
-rw-r--r--railties/environments/boot.rb8
-rw-r--r--railties/environments/environment.rb96
-rwxr-xr-xrailties/fresh_rakefile227
-rw-r--r--railties/lib/initializer.rb158
-rw-r--r--railties/lib/rails_generator/generators/applications/app/app_generator.rb1
-rw-r--r--railties/lib/tasks/databases.rake93
-rw-r--r--railties/lib/tasks/documentation.rake44
-rw-r--r--railties/lib/tasks/misc.rake15
-rw-r--r--railties/lib/tasks/rails.rb7
-rw-r--r--railties/lib/tasks/statistics.rake13
-rw-r--r--railties/lib/tasks/testing.rake40
13 files changed, 396 insertions, 311 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index 0c73d68d5f..f06b03b784 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -1,5 +1,9 @@
*SVN*
+* Moved all the shared tasks from Rakefile into Rails, so that the Rakefile is empty and doesn't require updating.
+
+* Added Rails::Initializer and Rails::Configuration to abstract all of the common setup out of config/environment.rb (uses config/boot.rb to bootstrap the initializer and paths)
+
* Fixed the scaffold generator to fail right away if the database isn't accessible instead of in mid-air #1169 [Chad Fowler]
* Corrected project-local generator location in scripts.rb #2010 [Michael Schuerig]
diff --git a/railties/Rakefile b/railties/Rakefile
index e086c76ed6..3befcff3e9 100644
--- a/railties/Rakefile
+++ b/railties/Rakefile
@@ -156,6 +156,7 @@ task :copy_configs do
cp "configs/apache.conf", "#{PKG_DESTINATION}/public/.htaccess"
+ cp "environments/boot.rb", "#{PKG_DESTINATION}/config/boot.rb"
cp "environments/environment.rb", "#{PKG_DESTINATION}/config/environment.rb"
cp "environments/production.rb", "#{PKG_DESTINATION}/config/environments/production.rb"
cp "environments/development.rb", "#{PKG_DESTINATION}/config/environments/development.rb"
diff --git a/railties/environments/boot.rb b/railties/environments/boot.rb
new file mode 100644
index 0000000000..7f30e829a9
--- /dev/null
+++ b/railties/environments/boot.rb
@@ -0,0 +1,8 @@
+RAILS_ROOT = File.join(File.dirname(__FILE__), '..')
+
+unless File.directory?("#{RAILS_ROOT}/vendor/rails")
+ require 'rubygems'
+ require 'initializer'
+else
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
+end \ No newline at end of file
diff --git a/railties/environments/environment.rb b/railties/environments/environment.rb
index 896a8faf49..6e2c616032 100644
--- a/railties/environments/environment.rb
+++ b/railties/environments/environment.rb
@@ -1,93 +1,7 @@
-# Load the Rails framework and configure your application.
-# You can include your own configuration at the end of this file.
-#
# Be sure to restart your webserver when you modify this file.
+# Bootstrap the Rails environment, frameworks, and default configuration
+require File.join(File.dirname(__FILE__), 'boot')
-# Uncomment to set UTF-8 as the encoding for strings. The Ruby default
-# is US-ASCII (may differ on different platforms though). This setting is
-# required if you need to return non-ASCII UTF-8 strings from ActionWebService.
-#
-# $KCODE = 'UTF8'
-
-# The path to the root directory of your application.
-RAILS_ROOT = File.join(File.dirname(__FILE__), '..')
-
-# The environment your application is currently running. Don't set
-# this here; put it in your webserver's configuration as the RAILS_ENV
-# environment variable instead.
-#
-# See config/environments/*.rb for environment-specific configuration.
-RAILS_ENV = ENV['RAILS_ENV'] || 'development'
-
-
-# Load the Rails framework. Mock classes for testing come first.
-ADDITIONAL_LOAD_PATHS = ["#{RAILS_ROOT}/test/mocks/#{RAILS_ENV}"]
-
-# Then model subdirectories.
-ADDITIONAL_LOAD_PATHS.concat(Dir["#{RAILS_ROOT}/app/models/[_a-z]*"])
-ADDITIONAL_LOAD_PATHS.concat(Dir["#{RAILS_ROOT}/components/[_a-z]*"])
-
-# Followed by the standard includes.
-ADDITIONAL_LOAD_PATHS.concat %w(
- app
- app/models
- app/controllers
- app/helpers
- app/apis
- components
- config
- lib
- vendor
- vendor/rails/railties
- vendor/rails/railties/lib
- vendor/rails/actionpack/lib
- vendor/rails/activesupport/lib
- vendor/rails/activerecord/lib
- vendor/rails/actionmailer/lib
- vendor/rails/actionwebservice/lib
-).map { |dir| "#{RAILS_ROOT}/#{dir}" }.select { |dir| File.directory?(dir) }
-
-# Prepend to $LOAD_PATH
-ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) }
-
-# Require Rails libraries.
-require 'rubygems' unless File.directory?("#{RAILS_ROOT}/vendor/rails")
-
-require 'active_support'
-require 'active_record'
-require 'action_controller'
-require 'action_mailer'
-require 'action_web_service'
-
-# Environment-specific configuration.
-require_dependency "environments/#{RAILS_ENV}"
-ActiveRecord::Base.configurations = YAML::load(ERB.new((IO.read("#{RAILS_ROOT}/config/database.yml"))).result)
-ActiveRecord::Base.establish_connection
-
-
-# Configure defaults if the included environment did not.
-begin
- RAILS_DEFAULT_LOGGER = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}.log")
- RAILS_DEFAULT_LOGGER.level = (RAILS_ENV == 'production' ? Logger::INFO : Logger::DEBUG)
-rescue StandardError
- RAILS_DEFAULT_LOGGER = Logger.new(STDERR)
- RAILS_DEFAULT_LOGGER.level = Logger::WARN
- RAILS_DEFAULT_LOGGER.warn(
- "Rails Error: Unable to access log file. Please ensure that log/#{RAILS_ENV}.log exists and is chmod 0666. " +
- "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
- )
-end
-
-[ActiveRecord, ActionController, ActionMailer].each { |mod| mod::Base.logger ||= RAILS_DEFAULT_LOGGER }
-[ActionController, ActionMailer].each { |mod| mod::Base.template_root ||= "#{RAILS_ROOT}/app/views/" }
-
-# Set up routes.
-ActionController::Routing::Routes.reload
-
-Controllers = Dependencies::LoadingModule.root(
- File.join(RAILS_ROOT, 'app', 'controllers'),
- File.join(RAILS_ROOT, 'components')
-)
-
-# Include your app's configuration here:
-
+Rails::Initializer.run do |config|
+ # See Rails::Configuration for options
+end \ No newline at end of file
diff --git a/railties/fresh_rakefile b/railties/fresh_rakefile
index 6056508f5a..abe240099d 100755
--- a/railties/fresh_rakefile
+++ b/railties/fresh_rakefile
@@ -1,224 +1,11 @@
+# Add your own tasks in files placed in config/tasks
+# and they will automatically be available to Rake.
+
+RAILS_BOOT_PATH_ONLY = true
+require(File.join(File.dirname(__FILE__), 'config', 'boot'))
+
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
-$VERBOSE = nil
-TEST_CHANGES_SINCE = Time.now - 600
-
-desc "Run all the tests on a fresh test database"
-task :default => [ :test_units, :test_functional ]
-
-
-desc 'Require application environment.'
-task :environment do
- unless defined? RAILS_ROOT
- require File.dirname(__FILE__) + '/config/environment'
- end
-end
-
-desc "Generate API documentation, show coding stats"
-task :doc => [ :appdoc, :stats ]
-
-
-# Look up tests for recently modified sources.
-def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago)
- FileList[source_pattern].map do |path|
- if File.mtime(path) > touched_since
- test = "#{test_path}/#{File.basename(path, '.rb')}_test.rb"
- test if File.exists?(test)
- end
- end.compact
-end
-
-desc 'Test recent changes.'
-Rake::TestTask.new(:recent => [ :clone_structure_to_test ]) do |t|
- since = TEST_CHANGES_SINCE
- touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
- recent_tests('app/models/*.rb', 'test/unit', since) +
- recent_tests('app/controllers/*.rb', 'test/functional', since)
-
- t.libs << 'test'
- t.verbose = true
- t.test_files = touched.uniq
-end
-task :test_recent => [ :clone_structure_to_test ]
-
-desc "Run the unit tests in test/unit"
-Rake::TestTask.new("test_units") { |t|
- t.libs << "test"
- t.pattern = 'test/unit/**/*_test.rb'
- t.verbose = true
-}
-task :test_units => [ :clone_structure_to_test ]
-
-desc "Run the functional tests in test/functional"
-Rake::TestTask.new("test_functional") { |t|
- t.libs << "test"
- t.pattern = 'test/functional/**/*_test.rb'
- t.verbose = true
-}
-task :test_functional => [ :clone_structure_to_test ]
-
-desc "Generate documentation for the application"
-Rake::RDocTask.new("appdoc") { |rdoc|
- rdoc.rdoc_dir = 'doc/app'
- rdoc.title = "Rails Application Documentation"
- rdoc.options << '--line-numbers --inline-source'
- rdoc.rdoc_files.include('doc/README_FOR_APP')
- rdoc.rdoc_files.include('app/**/*.rb')
-}
-
-desc "Generate documentation for the Rails framework"
-Rake::RDocTask.new("apidoc") { |rdoc|
- rdoc.rdoc_dir = 'doc/api'
- rdoc.template = "#{ENV['template']}.rb" if ENV['template']
- rdoc.title = "Rails Framework Documentation"
- rdoc.options << '--line-numbers --inline-source'
- rdoc.rdoc_files.include('README')
- rdoc.rdoc_files.include('CHANGELOG')
- rdoc.rdoc_files.include('vendor/rails/railties/CHANGELOG')
- rdoc.rdoc_files.include('vendor/rails/railties/MIT-LICENSE')
- rdoc.rdoc_files.include('vendor/rails/activerecord/README')
- rdoc.rdoc_files.include('vendor/rails/activerecord/CHANGELOG')
- rdoc.rdoc_files.include('vendor/rails/activerecord/lib/active_record/**/*.rb')
- rdoc.rdoc_files.exclude('vendor/rails/activerecord/lib/active_record/vendor/*')
- rdoc.rdoc_files.include('vendor/rails/actionpack/README')
- rdoc.rdoc_files.include('vendor/rails/actionpack/CHANGELOG')
- rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_controller/**/*.rb')
- rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_view/**/*.rb')
- rdoc.rdoc_files.include('vendor/rails/actionmailer/README')
- rdoc.rdoc_files.include('vendor/rails/actionmailer/CHANGELOG')
- rdoc.rdoc_files.include('vendor/rails/actionmailer/lib/action_mailer/base.rb')
- rdoc.rdoc_files.include('vendor/rails/actionwebservice/README')
- rdoc.rdoc_files.include('vendor/rails/actionwebservice/CHANGELOG')
- rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service.rb')
- rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/*.rb')
- rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/api/*.rb')
- rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/client/*.rb')
- rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/container/*.rb')
- rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/dispatcher/*.rb')
- rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/protocol/*.rb')
- rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/support/*.rb')
- rdoc.rdoc_files.include('vendor/rails/activesupport/README')
- rdoc.rdoc_files.include('vendor/rails/activesupport/CHANGELOG')
- rdoc.rdoc_files.include('vendor/rails/activesupport/lib/active_support/**/*.rb')
-}
-
-desc "Report code statistics (KLOCs, etc) from the application"
-task :stats => [ :environment ] do
- require 'code_statistics'
- CodeStatistics.new(
- ["Helpers", "app/helpers"],
- ["Controllers", "app/controllers"],
- ["APIs", "app/apis"],
- ["Components", "components"],
- ["Functionals", "test/functional"],
- ["Models", "app/models"],
- ["Units", "test/unit"]
- ).to_s
-end
-
-desc "Recreate the test databases from the development structure"
-task :clone_structure_to_test => [ :db_structure_dump, :purge_test_database ] do
- abcs = ActiveRecord::Base.configurations
- case abcs["test"]["adapter"]
- when "mysql"
- ActiveRecord::Base.establish_connection(:test)
- ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
- IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
- ActiveRecord::Base.connection.execute(table)
- end
- when "postgresql"
- ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
- ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
- ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
- `psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}`
- when "sqlite", "sqlite3"
- `#{abcs[RAILS_ENV]["adapter"]} #{abcs["test"]["dbfile"]} < db/#{RAILS_ENV}_structure.sql`
- when "sqlserver"
- `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql`
- when "oci",
- ActiveRecord::Base.establish_connection(:test)
- IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl|
- ActiveRecord::Base.connection.execute(ddl)
- end
- else
- raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
- end
-end
-
-desc "Dump the database structure to a SQL file"
-task :db_structure_dump => :environment do
- abcs = ActiveRecord::Base.configurations
- case abcs[RAILS_ENV]["adapter"]
- when "mysql", "oci"
- ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
- File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
- when "postgresql"
- ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"]
- ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"]
- ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"]
- `pg_dump -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{abcs[RAILS_ENV]["database"]}`
- when "sqlite", "sqlite3"
- `#{abcs[RAILS_ENV]["adapter"]} #{abcs[RAILS_ENV]["dbfile"]} .schema > db/#{RAILS_ENV}_structure.sql`
- when "sqlserver"
- `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r`
- `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r`
- else
- raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
- end
-end
-
-desc "Empty the test database"
-task :purge_test_database => :environment do
- abcs = ActiveRecord::Base.configurations
- case abcs["test"]["adapter"]
- when "mysql"
- ActiveRecord::Base.establish_connection(:test)
- ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"])
- when "postgresql"
- ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
- ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
- ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
- `dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
- `createdb -T template0 -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
- when "sqlite","sqlite3"
- File.delete(abcs["test"]["dbfile"]) if File.exist?(abcs["test"]["dbfile"])
- when "sqlserver"
- dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-')
- `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}`
- `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql`
- when "oci"
- ActiveRecord::Base.establish_connection(:test)
- ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
- ActiveRecord::Base.connection.execute(ddl)
- end
- else
- raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
- end
-end
-
-desc "Clears all *.log files in log/"
-task :clear_logs do
- FileList["log/*.log"].each do |log_file|
- f = File.open(log_file, "w")
- f.close
- end
-end
-
-desc "Migrate the database according to the migrate scripts in db/migrate (only supported on PG/MySQL). A specific version can be targetted with VERSION=x"
-task :migrate => :environment do
- ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/db/migrate/', ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
-end
-
-desc "Load fixtures into the current environment's database"
-task :load_fixtures => :environment do
- require 'active_record/fixtures'
- ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
- Dir.glob(File.join(RAILS_ROOT, 'test', 'fixtures', '*.yml')).each do |fixture_file|
- Fixtures.create_fixtures('test/fixtures', File.basename(fixture_file, '.*'))
- end
-end
-
-# Load any custom rakefile extensions
-Dir["./config/tasks/**/*.rake"].each { |ext| load ext }
+require 'tasks/rails' \ No newline at end of file
diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb
new file mode 100644
index 0000000000..2a172cc53d
--- /dev/null
+++ b/railties/lib/initializer.rb
@@ -0,0 +1,158 @@
+require 'logger'
+
+RAILS_ENV = ENV['RAILS_ENV'] || 'development'
+
+module Rails
+ class Initializer
+ attr_reader :configuration
+
+ def self.run(command = :process, configuration = Configuration.new)
+ yield configuration if block_given?
+ new(configuration).send(command)
+ end
+
+ def initialize(configuration)
+ @configuration = configuration
+ end
+
+ def process
+ set_load_path
+
+ require_frameworks
+ require_environment
+
+ initialize_database
+ initialize_logger
+ initialize_framework_logging
+ initialize_routing
+ end
+
+ def set_load_path
+ configuration.load_paths.reverse.each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) }
+ $LOAD_PATH.uniq!
+ end
+
+ def require_frameworks
+ configuration.frameworks.each { |framework| require(framework.to_s) }
+ end
+
+ def require_environment
+ require_dependency(configuration.environment_file)
+ end
+
+ def initialize_database
+ return unless configuration.frameworks.include?(:active_record)
+ ActiveRecord::Base.configurations = configuration.database_configuration
+ ActiveRecord::Base.establish_connection
+ end
+
+ def initialize_logger
+ begin
+ logger = Logger.new(configuration.log_path)
+ logger.level = configuration.log_level
+ rescue StandardError
+ logger = Logger.new(STDERR)
+ logger.level = Logger::WARN
+ logger.warn(
+ "Rails Error: Unable to access log file. Please ensure that #{configuration.log_path} exists and is chmod 0666. " +
+ "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
+ )
+ end
+
+ Object.const_set "RAILS_DEFAULT_LOGGER", logger
+ end
+
+ def initialize_framework_logging
+ [ActiveRecord, ActionController, ActionMailer].each { |mod| mod::Base.logger ||= RAILS_DEFAULT_LOGGER }
+ end
+
+ def initialize_framework_views
+ [ActionController, ActionMailer].each { |mod| mod::Base.template_root ||= configuration.view_path }
+ end
+
+ def initialize_routing
+ return unless configuration.frameworks.include?(:action_controller)
+ ActionController::Routing::Routes.reload
+ Object.const_set "Controllers", Dependencies::LoadingModule.root(*configuration.controller_paths)
+ end
+ end
+
+ class Configuration
+ attr_accessor :frameworks, :load_paths, :log_level, :log_path, :database_configuration_file, :view_path, :controller_paths
+
+ def initialize
+ self.frameworks = default_frameworks
+ self.load_paths = default_load_paths
+ self.log_path = default_log_path
+ self.log_level = default_log_level
+ self.view_path = default_view_path
+ self.controller_paths = default_controller_paths
+ self.database_configuration_file = default_database_configuration_file
+ end
+
+ def database_configuration
+ YAML::load(ERB.new((IO.read(database_configuration_file))).result)
+ end
+
+ def environment_file
+ "environments/#{environment}"
+ end
+
+ def environment
+ ::RAILS_ENV
+ end
+
+ private
+ def default_frameworks
+ [ :active_support, :active_record, :action_controller, :action_mailer, :action_web_service ]
+ end
+
+ def default_load_paths
+ paths = ["#{environment}/test/mocks/#{environment}"]
+
+ # Then model subdirectories.
+ paths.concat(Dir["#{RAILS_ROOT}/app/models/[_a-z]*"])
+ paths.concat(Dir["#{RAILS_ROOT}/components/[_a-z]*"])
+
+ # Followed by the standard includes.
+ paths.concat %w(
+ app
+ app/models
+ app/controllers
+ app/helpers
+ app/apis
+ components
+ config
+ lib
+ vendor
+ vendor/rails/railties
+ vendor/rails/railties/lib
+ vendor/rails/actionpack/lib
+ vendor/rails/activesupport/lib
+ vendor/rails/activerecord/lib
+ vendor/rails/actionmailer/lib
+ vendor/rails/actionwebservice/lib
+ ).map { |dir| "#{RAILS_ROOT}/#{dir}" }.select { |dir| File.directory?(dir) }
+ end
+
+ def default_log_path
+ File.join(RAILS_ROOT, 'log', "#{environment}.log")
+ end
+
+ def default_log_level
+ environment == 'production' ? Logger::INFO : Logger::DEBUG
+ end
+
+ def default_database_configuration_file
+ File.join(RAILS_ROOT, 'config', 'database.yml')
+ end
+
+ def default_view_path
+ File.join(RAILS_ROOT, 'app', 'views')
+ end
+
+ def default_controller_paths
+ [ File.join(RAILS_ROOT, 'app', 'controllers'), File.join(RAILS_ROOT, 'components') ]
+ end
+ end
+end \ No newline at end of file
diff --git a/railties/lib/rails_generator/generators/applications/app/app_generator.rb b/railties/lib/rails_generator/generators/applications/app/app_generator.rb
index e8a3decffd..eee37be5c1 100644
--- a/railties/lib/rails_generator/generators/applications/app/app_generator.rb
+++ b/railties/lib/rails_generator/generators/applications/app/app_generator.rb
@@ -37,6 +37,7 @@ class AppGenerator < Rails::Generator::Base
m.template "configs/apache.conf", "public/.htaccess"
# Environments
+ m.file "environments/boot.rb", "config/boot.rb"
m.file "environments/environment.rb", "config/environment.rb"
m.file "environments/production.rb", "config/environments/production.rb"
m.file "environments/development.rb", "config/environments/development.rb"
diff --git a/railties/lib/tasks/databases.rake b/railties/lib/tasks/databases.rake
new file mode 100644
index 0000000000..470fc07d35
--- /dev/null
+++ b/railties/lib/tasks/databases.rake
@@ -0,0 +1,93 @@
+desc "Migrate the database according to the migrate scripts in db/migrate (only supported on PG/MySQL). A specific version can be targetted with VERSION=x"
+task :migrate => :environment do
+ ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/db/migrate/', ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
+end
+
+desc "Load fixtures into the current environment's database"
+task :load_fixtures => :environment do
+ require 'active_record/fixtures'
+ ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
+ Dir.glob(File.join(RAILS_ROOT, 'test', 'fixtures', '*.yml')).each do |fixture_file|
+ Fixtures.create_fixtures('test/fixtures', File.basename(fixture_file, '.*'))
+ end
+end
+
+desc "Recreate the test databases from the development structure"
+task :clone_structure_to_test => [ :db_structure_dump, :purge_test_database ] do
+ abcs = ActiveRecord::Base.configurations
+ case abcs["test"]["adapter"]
+ when "mysql"
+ ActiveRecord::Base.establish_connection(:test)
+ ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
+ IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
+ ActiveRecord::Base.connection.execute(table)
+ end
+ when "postgresql"
+ ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
+ ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
+ ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
+ `psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}`
+ when "sqlite", "sqlite3"
+ `#{abcs[RAILS_ENV]["adapter"]} #{abcs["test"]["dbfile"]} < db/#{RAILS_ENV}_structure.sql`
+ when "sqlserver"
+ `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql`
+ when "oci",
+ ActiveRecord::Base.establish_connection(:test)
+ IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl|
+ ActiveRecord::Base.connection.execute(ddl)
+ end
+ else
+ raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
+ end
+end
+
+desc "Dump the database structure to a SQL file"
+task :db_structure_dump => :environment do
+ abcs = ActiveRecord::Base.configurations
+ case abcs[RAILS_ENV]["adapter"]
+ when "mysql", "oci"
+ ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
+ File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
+ when "postgresql"
+ ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"]
+ ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"]
+ ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"]
+ `pg_dump -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{abcs[RAILS_ENV]["database"]}`
+ when "sqlite", "sqlite3"
+ `#{abcs[RAILS_ENV]["adapter"]} #{abcs[RAILS_ENV]["dbfile"]} .schema > db/#{RAILS_ENV}_structure.sql`
+ when "sqlserver"
+ `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r`
+ `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r`
+ else
+ raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
+ end
+end
+
+desc "Empty the test database"
+task :purge_test_database => :environment do
+ abcs = ActiveRecord::Base.configurations
+ case abcs["test"]["adapter"]
+ when "mysql"
+ ActiveRecord::Base.establish_connection(:test)
+ ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"])
+ when "postgresql"
+ ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
+ ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
+ ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
+ `dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
+ `createdb -T template0 -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
+ when "sqlite","sqlite3"
+ File.delete(abcs["test"]["dbfile"]) if File.exist?(abcs["test"]["dbfile"])
+ when "sqlserver"
+ dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-')
+ `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}`
+ `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql`
+ when "oci"
+ ActiveRecord::Base.establish_connection(:test)
+ ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
+ ActiveRecord::Base.connection.execute(ddl)
+ end
+ else
+ raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
+ end
+end \ No newline at end of file
diff --git a/railties/lib/tasks/documentation.rake b/railties/lib/tasks/documentation.rake
new file mode 100644
index 0000000000..9d8fc6383c
--- /dev/null
+++ b/railties/lib/tasks/documentation.rake
@@ -0,0 +1,44 @@
+desc "Generate documentation for the application"
+Rake::RDocTask.new("appdoc") { |rdoc|
+ rdoc.rdoc_dir = 'doc/app'
+ rdoc.title = "Rails Application Documentation"
+ rdoc.options << '--line-numbers --inline-source'
+ rdoc.rdoc_files.include('doc/README_FOR_APP')
+ rdoc.rdoc_files.include('app/**/*.rb')
+}
+
+desc "Generate documentation for the Rails framework"
+Rake::RDocTask.new("apidoc") { |rdoc|
+ rdoc.rdoc_dir = 'doc/api'
+ rdoc.template = "#{ENV['template']}.rb" if ENV['template']
+ rdoc.title = "Rails Framework Documentation"
+ rdoc.options << '--line-numbers --inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('CHANGELOG')
+ rdoc.rdoc_files.include('vendor/rails/railties/CHANGELOG')
+ rdoc.rdoc_files.include('vendor/rails/railties/MIT-LICENSE')
+ rdoc.rdoc_files.include('vendor/rails/activerecord/README')
+ rdoc.rdoc_files.include('vendor/rails/activerecord/CHANGELOG')
+ rdoc.rdoc_files.include('vendor/rails/activerecord/lib/active_record/**/*.rb')
+ rdoc.rdoc_files.exclude('vendor/rails/activerecord/lib/active_record/vendor/*')
+ rdoc.rdoc_files.include('vendor/rails/actionpack/README')
+ rdoc.rdoc_files.include('vendor/rails/actionpack/CHANGELOG')
+ rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_controller/**/*.rb')
+ rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_view/**/*.rb')
+ rdoc.rdoc_files.include('vendor/rails/actionmailer/README')
+ rdoc.rdoc_files.include('vendor/rails/actionmailer/CHANGELOG')
+ rdoc.rdoc_files.include('vendor/rails/actionmailer/lib/action_mailer/base.rb')
+ rdoc.rdoc_files.include('vendor/rails/actionwebservice/README')
+ rdoc.rdoc_files.include('vendor/rails/actionwebservice/CHANGELOG')
+ rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service.rb')
+ rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/*.rb')
+ rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/api/*.rb')
+ rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/client/*.rb')
+ rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/container/*.rb')
+ rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/dispatcher/*.rb')
+ rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/protocol/*.rb')
+ rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/support/*.rb')
+ rdoc.rdoc_files.include('vendor/rails/activesupport/README')
+ rdoc.rdoc_files.include('vendor/rails/activesupport/CHANGELOG')
+ rdoc.rdoc_files.include('vendor/rails/activesupport/lib/active_support/**/*.rb')
+} \ No newline at end of file
diff --git a/railties/lib/tasks/misc.rake b/railties/lib/tasks/misc.rake
new file mode 100644
index 0000000000..713f622547
--- /dev/null
+++ b/railties/lib/tasks/misc.rake
@@ -0,0 +1,15 @@
+desc "Run all the tests on a fresh test database"
+task :default => [ :test_units, :test_functional ]
+
+task :environment do
+ require(File.join(RAILS_ROOT, 'config', 'environment'))
+end
+
+
+desc "Clears all *.log files in log/"
+task :clear_logs do
+ FileList["log/*.log"].each do |log_file|
+ f = File.open(log_file, "w")
+ f.close
+ end
+end
diff --git a/railties/lib/tasks/rails.rb b/railties/lib/tasks/rails.rb
new file mode 100644
index 0000000000..28e4d8af80
--- /dev/null
+++ b/railties/lib/tasks/rails.rb
@@ -0,0 +1,7 @@
+$VERBOSE = nil
+
+# Load Rails rakefile extensions
+Dir["#{File.dirname(__FILE__)}/*.rake"].each { |ext| load ext }
+
+# Load any custom rakefile extensions
+Dir["./config/tasks/**/*.rake"].each { |ext| load ext } \ No newline at end of file
diff --git a/railties/lib/tasks/statistics.rake b/railties/lib/tasks/statistics.rake
new file mode 100644
index 0000000000..66b654725b
--- /dev/null
+++ b/railties/lib/tasks/statistics.rake
@@ -0,0 +1,13 @@
+desc "Report code statistics (KLOCs, etc) from the application"
+task :stats do
+ require 'code_statistics'
+ CodeStatistics.new(
+ ["Helpers", "app/helpers"],
+ ["Controllers", "app/controllers"],
+ ["APIs", "app/apis"],
+ ["Components", "components"],
+ ["Functionals", "test/functional"],
+ ["Models", "app/models"],
+ ["Units", "test/unit"]
+ ).to_s
+end
diff --git a/railties/lib/tasks/testing.rake b/railties/lib/tasks/testing.rake
new file mode 100644
index 0000000000..3542b74763
--- /dev/null
+++ b/railties/lib/tasks/testing.rake
@@ -0,0 +1,40 @@
+TEST_CHANGES_SINCE = Time.now - 600
+
+# Look up tests for recently modified sources.
+def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago)
+ FileList[source_pattern].map do |path|
+ if File.mtime(path) > touched_since
+ test = "#{test_path}/#{File.basename(path, '.rb')}_test.rb"
+ test if File.exists?(test)
+ end
+ end.compact
+end
+
+desc 'Test recent changes.'
+Rake::TestTask.new(:recent => [ :clone_structure_to_test ]) do |t|
+ since = TEST_CHANGES_SINCE
+ touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
+ recent_tests('app/models/*.rb', 'test/unit', since) +
+ recent_tests('app/controllers/*.rb', 'test/functional', since)
+
+ t.libs << 'test'
+ t.verbose = true
+ t.test_files = touched.uniq
+end
+task :test_recent => [ :clone_structure_to_test ]
+
+desc "Run the unit tests in test/unit"
+Rake::TestTask.new("test_units") { |t|
+ t.libs << "test"
+ t.pattern = 'test/unit/**/*_test.rb'
+ t.verbose = true
+}
+task :test_units => [ :clone_structure_to_test ]
+
+desc "Run the functional tests in test/functional"
+Rake::TestTask.new("test_functional") { |t|
+ t.libs << "test"
+ t.pattern = 'test/functional/**/*_test.rb'
+ t.verbose = true
+}
+task :test_functional => [ :clone_structure_to_test ] \ No newline at end of file