Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / lib / generators / instance / .svn / text-base / instance_generator.rb.svn-base
blobc09431b7e13736a94eede33837736f28694ac748
1 require 'rbconfig'
3 class InstanceGenerator < Rails::Generator::Base
4   DEFAULT_SHEBANG = File.join(Config::CONFIG['bindir'],
5                               Config::CONFIG['ruby_install_name'])
6   
7   DATABASES = %w( mysql postgresql sqlite3 sqlserver )
8   
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
18   ]
19     
20   default_options :db => "mysql", :shebang => DEFAULT_SHEBANG, :freeze => false
22   def initialize(runtime_args, runtime_options = {})
23     super
24     usage if args.empty?
25     usage("Databases supported for preconfiguration are: #{DATABASES.join(", ")}") if (options[:db] && !DATABASES.include?(options[:db]))
26     @destination_root = args.shift
27   end
29   def manifest
30     # The absolute location of the Radiant files
31     root = File.expand_path(RADIANT_ROOT) 
32     
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] }
36     
37     record do |m|
38       # Root directory
39       m.directory ""
40       
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/**/*"]
47       
48       files = base_dirs + text_files + environments + scripts + public_files
49       files.map! { |f| f = $1 if f =~ %r{^#{root}/(.+)$}; f }
50       files.sort!
51       
52       files.each do |file|
53         case
54         when File.directory?("#{root}/#{file}")
55           m.directory 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
60         else
61           m.file radiant_root(file), file
62         end
63       end
64       
65       # script/generate
66       m.file "instance_generate", "script/generate", script_options
67       
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
72       }
74       # Instance Rakefile
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"))
81       }
82       m.template "instance_boot.rb", "config/boot.rb"
83       
84       # Install Readme
85       m.readme radiant_root("INSTALL")
86     end
87   end
89   protected
91     def banner
92       "Usage: #{$0} /path/to/radiant/app [options]"
93     end
95     def add_options!(opt)
96       opt.separator ''
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 }
104     end
105     
106     def mysql_socket_location
107       RUBY_PLATFORM =~ /mswin32/ ? MYSQL_SOCKET_LOCATIONS.find { |f| File.exists?(f) } : nil
108     end
110   private
112     def radiant_root(filename = '')
113       File.join("..", "..", "..", "..", filename)
114     end
115