From f38e89cfbad6886a2812b1176e0c47b7d95cda43 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 20 Mar 2010 12:34:21 -0500 Subject: Move railties/builtin into lib --- railties/lib/rails/application.rb | 1 - railties/lib/rails/application/configuration.rb | 4 +- railties/lib/rails/application/finisher.rb | 4 +- railties/lib/rails/info.rb | 147 ++++++++++++++++++++++++ railties/lib/rails/info_controller.rb | 15 +++ railties/lib/rails/info_routes.rb | 3 + railties/lib/rails/railties_path.rb | 1 - railties/lib/rails/tasks/documentation.rake | 7 +- railties/lib/rails/tasks/misc.rake | 7 +- 9 files changed, 176 insertions(+), 13 deletions(-) create mode 100644 railties/lib/rails/info.rb create mode 100644 railties/lib/rails/info_controller.rb create mode 100644 railties/lib/rails/info_routes.rb delete mode 100644 railties/lib/rails/railties_path.rb (limited to 'railties/lib') diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index f43e8847ac..8b355dd76c 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -1,5 +1,4 @@ require 'fileutils' -require 'rails/railties_path' require 'rails/plugin' require 'rails/engine' diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index d3a0ecb243..52f869b877 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -78,7 +78,7 @@ module Rails end def builtin_controller - File.join(RAILTIES_PATH, "builtin", "rails_info") if Rails.env.development? + File.expand_path('../info_routes', __FILE__) if Rails.env.development? end def log_level @@ -96,4 +96,4 @@ module Rails end end end -end \ No newline at end of file +end diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index cb38d5a5db..978490f25f 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -23,7 +23,7 @@ module Rails initializer :add_builtin_route do |app| if Rails.env.development? - app.routes_reloader.paths << File.join(RAILTIES_PATH, 'builtin', 'routes.rb') + app.routes_reloader.paths << File.expand_path('../../info_routes.rb', __FILE__) end end @@ -45,4 +45,4 @@ module Rails end end end -end \ No newline at end of file +end diff --git a/railties/lib/rails/info.rb b/railties/lib/rails/info.rb new file mode 100644 index 0000000000..5a496f6536 --- /dev/null +++ b/railties/lib/rails/info.rb @@ -0,0 +1,147 @@ +require "active_support/core_ext/object/misc" +require "cgi" +require "active_support/core_ext/cgi" + +module Rails + module Info + mattr_accessor :properties + class << (@@properties = []) + def names + map {|val| val.first } + end + + def value_for(property_name) + if property = assoc(property_name) + property.last + end + end + end + + class << self #:nodoc: + def property(name, value = nil) + value ||= yield + properties << [name, value] if value + rescue Exception + end + + def frameworks + %w( active_record action_pack active_resource action_mailer active_support ) + end + + def framework_version(framework) + if Object.const_defined?(framework.classify) + require "#{framework}/version" + "#{framework.classify}::VERSION::STRING".constantize + end + end + + def edge_rails_revision(info = git_info) + info[/commit ([a-z0-9-]+)/, 1] || freeze_edge_version + end + + def freeze_edge_version + if File.exist?(rails_vendor_root) + begin + Dir[File.join(rails_vendor_root, 'REVISION_*')].first.scan(/_(\d+)$/).first.first + rescue + Dir[File.join(rails_vendor_root, 'TAG_*')].first.scan(/_(.+)$/).first.first rescue 'unknown' + end + end + end + + def to_s + column_width = properties.names.map {|name| name.length}.max + info = properties.map do |name, value| + value = value.join(", ") if value.is_a?(Array) + "%-#{column_width}s %s" % [name, value] + end + info.unshift "About your application's environment" + info * "\n" + end + + alias inspect to_s + + def to_html + (table = '').tap do + properties.each do |(name, value)| + table << %() + formatted_value = if value.kind_of?(Array) + "" + else + CGI.escapeHTML(value.to_s) + end + table << %() + end + table << '
#{CGI.escapeHTML(name.to_s)}#{formatted_value}
' + end + end + + protected + def rails_vendor_root + @rails_vendor_root ||= "#{Rails.root}/vendor/rails" + end + + def git_info + env_lang, ENV['LC_ALL'] = ENV['LC_ALL'], 'C' + Dir.chdir(rails_vendor_root) do + silence_stderr { `git log -n 1` } + end + ensure + ENV['LC_ALL'] = env_lang + end + end + + # The Ruby version and platform, e.g. "1.8.2 (powerpc-darwin8.2.0)". + property 'Ruby version', "#{RUBY_VERSION} (#{RUBY_PLATFORM})" + + # The RubyGems version, if it's installed. + property 'RubyGems version' do + Gem::RubyGemsVersion + end + + property 'Rack version' do + ::Rack.release + end + + # The Rails version. + property 'Rails version' do + Rails::VERSION::STRING + end + + # Versions of each Rails framework (Active Record, Action Pack, + # Active Resource, Action Mailer, and Active Support). + frameworks.each do |framework| + property "#{framework.titlecase} version" do + framework_version(framework) + end + end + + property 'Middleware' do + Rails.configuration.middleware.active.map(&:inspect) + end + + # The Rails Git revision, if it's checked out into vendor/rails. + property 'Edge Rails revision' do + edge_rails_revision + end + + # The application's location on the filesystem. + property 'Application root' do + File.expand_path(Rails.root) + end + + # The current Rails environment (development, test, or production). + property 'Environment' do + Rails.env + end + + # The name of the database adapter for the current environment. + property 'Database adapter' do + ActiveRecord::Base.configurations[Rails.env]['adapter'] + end + + property 'Database schema version' do + ActiveRecord::Migrator.current_version rescue nil + end + end +end diff --git a/railties/lib/rails/info_controller.rb b/railties/lib/rails/info_controller.rb new file mode 100644 index 0000000000..196eeb4a6c --- /dev/null +++ b/railties/lib/rails/info_controller.rb @@ -0,0 +1,15 @@ +class Rails::InfoController < ActionController::Base + def properties + if consider_all_requests_local? || local_request? + render :inline => Rails::Info.to_html + else + render :text => '

For security purposes, this information is only available to local requests.

', :status => :forbidden + end + end + + protected + + def consider_all_requests_local? + Rails.application.config.consider_all_requests_local + end +end diff --git a/railties/lib/rails/info_routes.rb b/railties/lib/rails/info_routes.rb new file mode 100644 index 0000000000..bd58034d8f --- /dev/null +++ b/railties/lib/rails/info_routes.rb @@ -0,0 +1,3 @@ +Rails.application.routes.draw do |map| + match '/rails/info/properties' => "rails/info#properties" +end diff --git a/railties/lib/rails/railties_path.rb b/railties/lib/rails/railties_path.rb deleted file mode 100644 index e291fc23ea..0000000000 --- a/railties/lib/rails/railties_path.rb +++ /dev/null @@ -1 +0,0 @@ -RAILTIES_PATH = File.expand_path(File.join(File.dirname(__FILE__), '..', '..')) diff --git a/railties/lib/rails/tasks/documentation.rake b/railties/lib/rails/tasks/documentation.rake index abf9b33ae5..f2fee45594 100644 --- a/railties/lib/rails/tasks/documentation.rake +++ b/railties/lib/rails/tasks/documentation.rake @@ -66,13 +66,14 @@ namespace :doc do task :plugins => plugins.collect { |plugin| "doc:plugins:#{plugin}" } desc "Remove plugin documentation" - task :clobber_plugins do + task :clobber_plugins do rm_rf 'doc/plugins' rescue nil end desc "Generate Rails guides" task :guides do - require File.join(RAILTIES_PATH, "guides/rails_guides") + # FIXME: Reaching outside lib directory is a bad idea + require File.expand_path('../../../../guides/rails_guides', __FILE__) RailsGuides::Generator.new(Rails.root.join("doc/guides")).generate end @@ -92,7 +93,7 @@ namespace :doc do files.include("#{plugin_base}/lib/**/*.rb") if File.exist?("#{plugin_base}/README") - files.include("#{plugin_base}/README") + files.include("#{plugin_base}/README") options << "--main '#{plugin_base}/README'" end files.include("#{plugin_base}/CHANGELOG") if File.exist?("#{plugin_base}/CHANGELOG") diff --git a/railties/lib/rails/tasks/misc.rake b/railties/lib/rails/tasks/misc.rake index 48fce92215..2309c91d83 100644 --- a/railties/lib/rails/tasks/misc.rake +++ b/railties/lib/rails/tasks/misc.rake @@ -15,7 +15,6 @@ end desc 'Explain the current environment' task :about do - $LOAD_PATH.unshift "#{RAILTIES_PATH}/builtin/rails_info" require 'rails/info' puts Rails::Info end @@ -26,12 +25,12 @@ namespace :time do task :all do build_time_zone_list(:all) end - + desc 'Displays names of US time zones recognized by the Rails TimeZone class, grouped by offset. Results can be filtered with optional OFFSET parameter, e.g., OFFSET=-6' task :us do build_time_zone_list(:us_zones) end - + desc 'Displays names of time zones recognized by the Rails TimeZone class with the same offset as the system local time' task :local do require 'active_support' @@ -41,7 +40,7 @@ namespace :time do offset = jan_offset < jul_offset ? jan_offset : jul_offset build_time_zone_list(:all, offset) end - + # to find UTC -06:00 zones, OFFSET can be set to either -6, -6:00 or 21600 def build_time_zone_list(method, offset = ENV['OFFSET']) require 'active_support' -- cgit v1.2.3