/vendor/radiant/lib/generators/instance/instance_generator.rb

https://github.com/nullset/physical_therapy · Ruby · 117 lines · 85 code · 23 blank · 9 comment · 6 complexity · 6fc70d7fcf2e2e40de1e0793e02223a0 MD5 · raw file

  1. require 'rbconfig'
  2. class InstanceGenerator < Rails::Generator::Base
  3. DEFAULT_SHEBANG = File.join(Config::CONFIG['bindir'],
  4. Config::CONFIG['ruby_install_name'])
  5. DATABASES = %w( mysql postgresql sqlite3 sqlserver db2 )
  6. MYSQL_SOCKET_LOCATIONS = [
  7. "/tmp/mysql.sock", # default
  8. "/var/run/mysqld/mysqld.sock", # debian/gentoo
  9. "/var/tmp/mysql.sock", # freebsd
  10. "/var/lib/mysql/mysql.sock", # fedora
  11. "/opt/local/lib/mysql/mysql.sock", # fedora
  12. "/opt/local/var/run/mysqld/mysqld.sock", # mac + darwinports + mysql
  13. "/opt/local/var/run/mysql4/mysqld.sock", # mac + darwinports + mysql4
  14. "/opt/local/var/run/mysql5/mysqld.sock" # mac + darwinports + mysql5
  15. ]
  16. default_options :db => "mysql", :shebang => DEFAULT_SHEBANG, :freeze => false
  17. def initialize(runtime_args, runtime_options = {})
  18. super
  19. usage if args.empty?
  20. usage("Databases supported for preconfiguration are: #{DATABASES.join(", ")}") if (options[:db] && !DATABASES.include?(options[:db]))
  21. @destination_root = args.shift
  22. end
  23. def manifest
  24. # The absolute location of the Radiant files
  25. root = File.expand_path(RADIANT_ROOT)
  26. # Use /usr/bin/env if no special shebang was specified
  27. script_options = { :chmod => 0755, :shebang => options[:shebang] == DEFAULT_SHEBANG ? nil : options[:shebang] }
  28. dispatcher_options = { :chmod => 0755, :shebang => options[:shebang] }
  29. record do |m|
  30. # Root directory
  31. m.directory ""
  32. # Standard files and directories
  33. base_dirs = %w(config config/environments db log script public vendor/plugins vendor/extensions)
  34. text_files = %w(CHANGELOG CONTRIBUTORS LICENSE INSTALL README)
  35. environments = Dir["#{root}/config/environments/*.rb"]
  36. scripts = Dir["#{root}/script/**/*"].reject { |f| f =~ /(destroy|generate|plugin)$/ }
  37. public_files = ["public/.htaccess"] + Dir["#{root}/public/**/*"]
  38. files = base_dirs + text_files + environments + scripts + public_files
  39. files.map! { |f| f = $1 if f =~ %r{^#{root}/(.+)$}; f }
  40. files.sort!
  41. files.each do |file|
  42. case
  43. when File.directory?("#{root}/#{file}")
  44. m.directory file
  45. when file =~ %r{^script/}
  46. m.file radiant_root(file), file, script_options
  47. when file =~ %r{^public/dispatch}
  48. m.file radiant_root(file), file, dispatcher_options
  49. else
  50. m.file radiant_root(file), file
  51. end
  52. end
  53. # script/generate
  54. m.file "instance_generate", "script/generate", script_options
  55. # database.yml and .htaccess
  56. m.template "databases/#{options[:db]}.yml", "config/database.yml", :assigns => {
  57. :app_name => File.basename(File.expand_path(@destination_root)),
  58. :socket => options[:db] == "mysql" ? mysql_socket_location : nil
  59. }
  60. # Instance Rakefile
  61. m.file "instance_rakefile", "Rakefile"
  62. # Instance Configurations
  63. m.file "instance_routes.rb", "config/routes.rb"
  64. m.template "instance_environment.rb", "config/environment.rb", :assigns => {
  65. :radiant_environment => File.join(File.dirname(__FILE__), 'templates', radiant_root("config/environment.rb")),
  66. :app_name => File.basename(File.expand_path(@destination_root))
  67. }
  68. m.template "instance_boot.rb", "config/boot.rb"
  69. # Install Readme
  70. m.readme radiant_root("INSTALL")
  71. end
  72. end
  73. protected
  74. def banner
  75. "Usage: #{$0} /path/to/radiant/app [options]"
  76. end
  77. def add_options!(opt)
  78. opt.separator ''
  79. opt.separator 'Options:'
  80. opt.on("-r", "--ruby=path", String,
  81. "Path to the Ruby binary of your choice (otherwise scripts use env, dispatchers current path).",
  82. "Default: #{DEFAULT_SHEBANG}") { |v| options[:shebang] = v }
  83. opt.on("-d", "--database=name", String,
  84. "Preconfigure for selected database (options: #{DATABASES.join(", ")}).",
  85. "Default: mysql") { |v| options[:db] = v }
  86. end
  87. def mysql_socket_location
  88. RUBY_PLATFORM =~ /mswin32/ ? MYSQL_SOCKET_LOCATIONS.find { |f| File.exists?(f) } : nil
  89. end
  90. private
  91. def radiant_root(filename = '')
  92. File.join("..", "..", "..", "..", filename)
  93. end
  94. end