5a348bcdcd72e7f41deed2e3c4b422550de9e2e7
[depot.git] / vendor / rails / railties / lib / rails_generator / generators / applications / app / app_generator.rb
1 require 'rbconfig'
2 require 'digest/md5'
3 require 'active_support/secure_random'
4
5 class AppGenerator < Rails::Generator::Base
6 DEFAULT_SHEBANG = File.join(Config::CONFIG['bindir'],
7 Config::CONFIG['ruby_install_name'])
8
9 DATABASES = %w(mysql oracle postgresql sqlite2 sqlite3 frontbase ibm_db)
10 DEFAULT_DATABASE = 'sqlite3'
11
12 default_options :db => (ENV["RAILS_DEFAULT_DATABASE"] || DEFAULT_DATABASE),
13 :shebang => DEFAULT_SHEBANG, :freeze => false
14 mandatory_options :source => "#{File.dirname(__FILE__)}/../../../../.."
15
16 def initialize(runtime_args, runtime_options = {})
17 super
18 usage if args.empty?
19 usage("Databases supported for preconfiguration are: #{DATABASES.join(", ")}") if (options[:db] && !DATABASES.include?(options[:db]))
20 @destination_root = args.shift
21 @app_name = File.basename(File.expand_path(@destination_root))
22 end
23
24 def manifest
25 # Use /usr/bin/env if no special shebang was specified
26 script_options = { :chmod => 0755, :shebang => options[:shebang] == DEFAULT_SHEBANG ? nil : options[:shebang] }
27 dispatcher_options = { :chmod => 0755, :shebang => options[:shebang] }
28
29 # duplicate CGI::Session#generate_unique_id
30 md5 = Digest::MD5.new
31 now = Time.now
32 md5 << now.to_s
33 md5 << String(now.usec)
34 md5 << String(rand(0))
35 md5 << String($$)
36 md5 << @app_name
37
38 # Do our best to generate a secure secret key for CookieStore
39 secret = ActiveSupport::SecureRandom.hex(64)
40
41 record do |m|
42 # Root directory and all subdirectories.
43 m.directory ''
44 BASEDIRS.each { |path| m.directory path }
45
46 # Root
47 m.file "fresh_rakefile", "Rakefile"
48 m.file "README", "README"
49
50 # Application
51 m.template "helpers/application.rb", "app/controllers/application.rb", :assigns => { :app_name => @app_name, :app_secret => md5.hexdigest }
52 m.template "helpers/application_helper.rb", "app/helpers/application_helper.rb"
53 m.template "helpers/test_helper.rb", "test/test_helper.rb"
54 m.template "helpers/performance_test.rb", "test/performance/browsing_test.rb"
55
56 # database.yml and routes.rb
57 m.template "configs/databases/#{options[:db]}.yml", "config/database.yml", :assigns => {
58 :app_name => @app_name,
59 :socket => options[:db] == "mysql" ? mysql_socket_location : nil
60 }
61 m.template "configs/routes.rb", "config/routes.rb"
62
63 # Initializers
64 m.template "configs/initializers/inflections.rb", "config/initializers/inflections.rb"
65 m.template "configs/initializers/mime_types.rb", "config/initializers/mime_types.rb"
66 m.template "configs/initializers/new_rails_defaults.rb", "config/initializers/new_rails_defaults.rb"
67
68 # Locale
69 m.template "configs/locales/en.yml", "config/locales/en.yml"
70
71 # Environments
72 m.file "environments/boot.rb", "config/boot.rb"
73 m.template "environments/environment.rb", "config/environment.rb", :assigns => { :freeze => options[:freeze], :app_name => @app_name, :app_secret => secret }
74 m.file "environments/production.rb", "config/environments/production.rb"
75 m.file "environments/development.rb", "config/environments/development.rb"
76 m.file "environments/test.rb", "config/environments/test.rb"
77
78 # Scripts
79 %w( about console dbconsole destroy generate performance/benchmarker performance/profiler performance/request process/reaper process/spawner process/inspector runner server plugin ).each do |file|
80 m.file "bin/#{file}", "script/#{file}", script_options
81 end
82
83 # Dispatches
84 m.file "dispatches/dispatch.rb", "public/dispatch.rb", dispatcher_options
85 m.file "dispatches/dispatch.rb", "public/dispatch.cgi", dispatcher_options
86 m.file "dispatches/dispatch.fcgi", "public/dispatch.fcgi", dispatcher_options
87
88 # HTML files
89 %w(404 422 500 index).each do |file|
90 m.template "html/#{file}.html", "public/#{file}.html"
91 end
92
93 m.template "html/favicon.ico", "public/favicon.ico"
94 m.template "html/robots.txt", "public/robots.txt"
95 m.file "html/images/rails.png", "public/images/rails.png"
96
97 # Javascripts
98 m.file "html/javascripts/prototype.js", "public/javascripts/prototype.js"
99 m.file "html/javascripts/effects.js", "public/javascripts/effects.js"
100 m.file "html/javascripts/dragdrop.js", "public/javascripts/dragdrop.js"
101 m.file "html/javascripts/controls.js", "public/javascripts/controls.js"
102 m.file "html/javascripts/application.js", "public/javascripts/application.js"
103
104 # Docs
105 m.file "doc/README_FOR_APP", "doc/README_FOR_APP"
106
107 # Logs
108 %w(server production development test).each { |file|
109 m.file "configs/empty.log", "log/#{file}.log", :chmod => 0666
110 }
111 end
112 end
113
114 protected
115 def banner
116 "Usage: #{$0} /path/to/your/app [options]"
117 end
118
119 def add_options!(opt)
120 opt.separator ''
121 opt.separator 'Options:'
122 opt.on("-r", "--ruby=path", String,
123 "Path to the Ruby binary of your choice (otherwise scripts use env, dispatchers current path).",
124 "Default: #{DEFAULT_SHEBANG}") { |v| options[:shebang] = v }
125
126 opt.on("-d", "--database=name", String,
127 "Preconfigure for selected database (options: #{DATABASES.join('/')}).",
128 "Default: #{DEFAULT_DATABASE}") { |v| options[:db] = v }
129
130 opt.on("-f", "--freeze",
131 "Freeze Rails in vendor/rails from the gems generating the skeleton",
132 "Default: false") { |v| options[:freeze] = v }
133 end
134
135 def mysql_socket_location
136 MYSQL_SOCKET_LOCATIONS.find { |f| File.exist?(f) } unless RUBY_PLATFORM =~ /(:?mswin|mingw)/
137 end
138
139
140 # Installation skeleton. Intermediate directories are automatically
141 # created so don't sweat their absence here.
142 BASEDIRS = %w(
143 app/controllers
144 app/helpers
145 app/models
146 app/views/layouts
147 config/environments
148 config/initializers
149 config/locales
150 db
151 doc
152 lib
153 lib/tasks
154 log
155 public/images
156 public/javascripts
157 public/stylesheets
158 script/performance
159 script/process
160 test/fixtures
161 test/functional
162 test/integration
163 test/performance
164 test/unit
165 vendor
166 vendor/plugins
167 tmp/sessions
168 tmp/sockets
169 tmp/cache
170 tmp/pids
171 )
172
173 MYSQL_SOCKET_LOCATIONS = [
174 "/tmp/mysql.sock", # default
175 "/var/run/mysqld/mysqld.sock", # debian/gentoo
176 "/var/tmp/mysql.sock", # freebsd
177 "/var/lib/mysql/mysql.sock", # fedora
178 "/opt/local/lib/mysql/mysql.sock", # fedora
179 "/opt/local/var/run/mysqld/mysqld.sock", # mac + darwinports + mysql
180 "/opt/local/var/run/mysql4/mysqld.sock", # mac + darwinports + mysql4
181 "/opt/local/var/run/mysql5/mysqld.sock", # mac + darwinports + mysql5
182 "/opt/lampp/var/mysql/mysql.sock" # xampp for linux
183 ]
184 end