3 class InstanceGenerator < Rails::Generator::Base
4 DEFAULT_SHEBANG = File.join(Config::CONFIG['bindir'],
5 Config::CONFIG['ruby_install_name'])
7 DATABASES = %w( mysql postgresql sqlite3 sqlserver )
9 MYSQL_SOCKET_LOCATIONS = [
10 "/tmp/mysql.sock", # default
11 "/var/run/mysqld/mysqld.sock", # debian/gentoo
12 "/var/tmp/mysql.sock", # freebsd
13 "/var/lib/mysql/mysql.sock", # fedora
14 "/opt/local/lib/mysql/mysql.sock", # fedora
15 "/opt/local/var/run/mysqld/mysqld.sock", # mac + darwinports + mysql
16 "/opt/local/var/run/mysql4/mysqld.sock", # mac + darwinports + mysql4
17 "/opt/local/var/run/mysql5/mysqld.sock" # mac + darwinports + mysql5
20 default_options :db => "mysql", :shebang => DEFAULT_SHEBANG, :freeze => false
22 def initialize(runtime_args, runtime_options = {})
25 usage("Databases supported for preconfiguration are: #{DATABASES.join(", ")}") if (options[:db] && !DATABASES.include?(options[:db]))
26 @destination_root = args.shift
30 # The absolute location of the Radiant files
31 root = File.expand_path(RADIANT_ROOT)
33 # Use /usr/bin/env if no special shebang was specified
34 script_options = { :chmod => 0755, :shebang => options[:shebang] == DEFAULT_SHEBANG ? nil : options[:shebang] }
35 dispatcher_options = { :chmod => 0755, :shebang => options[:shebang] }
41 # Standard files and directories
42 base_dirs = %w(config config/environments db log script public vendor/plugins vendor/extensions)
43 text_files = %w(CHANGELOG CONTRIBUTORS LICENSE INSTALL README)
44 environments = Dir["#{root}/config/environments/*.rb"]
45 scripts = Dir["#{root}/script/**/*"].reject { |f| f =~ /(destroy|generate|plugin)$/ }
46 public_files = ["public/.htaccess"] + Dir["#{root}/public/**/*"]
48 files = base_dirs + text_files + environments + scripts + public_files
49 files.map! { |f| f = $1 if f =~ %r{^#{root}/(.+)$}; f }
54 when File.directory?("#{root}/#{file}")
56 when file =~ %r{^script/}
57 m.file radiant_root(file), file, script_options
58 when file =~ %r{^public/dispatch}
59 m.file radiant_root(file), file, dispatcher_options
61 m.file radiant_root(file), file
66 m.file "instance_generate", "script/generate", script_options
68 # database.yml and .htaccess
69 m.template "databases/#{options[:db]}.yml", "config/database.yml", :assigns => {
70 :app_name => File.basename(File.expand_path(@destination_root)),
71 :socket => options[:db] == "mysql" ? mysql_socket_location : nil
75 m.file "instance_rakefile", "Rakefile"
77 # Instance Configurations
78 m.file "instance_routes.rb", "config/routes.rb"
79 m.template "instance_environment.rb", "config/environment.rb", :assigns => {
80 :radiant_environment => File.join(File.dirname(__FILE__), 'templates', radiant_root("config/environment.rb"))
82 m.template "instance_boot.rb", "config/boot.rb"
85 m.readme radiant_root("INSTALL")
92 "Usage: #{$0} /path/to/radiant/app [options]"
97 opt.separator 'Options:'
98 opt.on("-r", "--ruby=path", String,
99 "Path to the Ruby binary of your choice (otherwise scripts use env, dispatchers current path).",
100 "Default: #{DEFAULT_SHEBANG}") { |v| options[:shebang] = v }
101 opt.on("-d", "--database=name", String,
102 "Preconfigure for selected database (options: #{DATABASES.join(", ")}).",
103 "Default: mysql") { |v| options[:db] = v }
106 def mysql_socket_location
107 RUBY_PLATFORM =~ /mswin32/ ? MYSQL_SOCKET_LOCATIONS.find { |f| File.exists?(f) } : nil
112 def radiant_root(filename = '')
113 File.join("..", "..", "..", "..", filename)