Updated README.rdoc again
[feedcatcher.git] / vendor / rails / railties / lib / rails_generator / generators / applications / app / app_generator.rb
1 require 'rbconfig'
2 require File.dirname(__FILE__) + '/template_runner'
3 require 'digest/md5'
4 require 'active_support/secure_random'
5
6 class AppGenerator < Rails::Generator::Base
7 DEFAULT_SHEBANG = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
8
9 DATABASES = %w( mysql oracle postgresql sqlite2 sqlite3 frontbase ibm_db )
10 DEFAULT_DATABASE = 'sqlite3'
11
12 mandatory_options :source => "#{File.dirname(__FILE__)}/../../../../.."
13 default_options :db => (ENV["RAILS_DEFAULT_DATABASE"] || DEFAULT_DATABASE),
14 :shebang => DEFAULT_SHEBANG, :with_dispatchers => false, :freeze => false
15
16
17 def initialize(runtime_args, runtime_options = {})
18 super
19
20 usage if args.empty?
21 usage("Databases supported for preconfiguration are: #{DATABASES.join(", ")}") if (options[:db] && !DATABASES.include?(options[:db]))
22
23 @destination_root = args.shift
24 @app_name = File.basename(File.expand_path(@destination_root))
25 end
26
27 def manifest
28 record do |m|
29 create_directories(m)
30 create_root_files(m)
31 create_app_files(m)
32 create_config_files(m)
33 create_script_files(m)
34 create_test_files(m)
35 create_public_files(m)
36 create_documentation_file(m)
37 create_log_files(m)
38 end
39 end
40
41 def after_generate
42 if options[:template]
43 Rails::TemplateRunner.new(options[:template], @destination_root)
44 end
45 end
46
47 protected
48 def banner
49 "Usage: #{$0} /path/to/your/app [options]"
50 end
51
52 def add_options!(opt)
53 opt.separator ''
54 opt.separator 'Options:'
55 opt.on("-r", "--ruby=path", String,
56 "Path to the Ruby binary of your choice (otherwise scripts use env, dispatchers current path).",
57 "Default: #{DEFAULT_SHEBANG}") { |v| options[:shebang] = v }
58
59 opt.on("-d", "--database=name", String,
60 "Preconfigure for selected database (options: #{DATABASES.join('/')}).",
61 "Default: #{DEFAULT_DATABASE}") { |v| options[:db] = v }
62
63 opt.on("-D", "--with-dispatchers",
64 "Add CGI/FastCGI/mod_ruby dispatches code to generated application skeleton",
65 "Default: false") { |v| options[:with_dispatchers] = v }
66
67 opt.on("-f", "--freeze",
68 "Freeze Rails in vendor/rails from the gems generating the skeleton",
69 "Default: false") { |v| options[:freeze] = v }
70
71 opt.on("-m", "--template=path", String,
72 "Use an application template that lives at path (can be a filesystem path or URL).",
73 "Default: (none)") { |v| options[:template] = v }
74
75 end
76
77
78 private
79 def create_directories(m)
80 m.directory ''
81
82 # Intermediate directories are automatically created so don't sweat their absence here.
83 %w(
84 app/controllers
85 app/helpers
86 app/models
87 app/views/layouts
88 config/environments
89 config/initializers
90 config/locales
91 db
92 doc
93 lib
94 lib/tasks
95 log
96 public/images
97 public/javascripts
98 public/stylesheets
99 script/performance
100 test/fixtures
101 test/functional
102 test/integration
103 test/performance
104 test/unit
105 vendor
106 vendor/plugins
107 tmp/sessions
108 tmp/sockets
109 tmp/cache
110 tmp/pids
111 ).each { |path| m.directory(path) }
112 end
113
114 def create_root_files(m)
115 m.file "fresh_rakefile", "Rakefile"
116 m.file "README", "README"
117 end
118
119 def create_app_files(m)
120 m.file "helpers/application_controller.rb", "app/controllers/application_controller.rb"
121 m.file "helpers/application_helper.rb", "app/helpers/application_helper.rb"
122 end
123
124 def create_config_files(m)
125 create_database_configuration_file(m)
126 create_routes_file(m)
127 create_locale_file(m)
128 create_initializer_files(m)
129 create_environment_files(m)
130 end
131
132 def create_documentation_file(m)
133 m.file "doc/README_FOR_APP", "doc/README_FOR_APP"
134 end
135
136 def create_log_files(m)
137 %w( server production development test ).each do |file|
138 m.file "configs/empty.log", "log/#{file}.log", :chmod => 0666
139 end
140 end
141
142 def create_public_files(m)
143 create_dispatch_files(m)
144 create_error_files(m)
145 create_welcome_file(m)
146 create_browser_convention_files(m)
147 create_rails_image(m)
148 create_javascript_files(m)
149 end
150
151 def create_script_files(m)
152 %w(
153 about console dbconsole destroy generate runner server plugin
154 performance/benchmarker performance/profiler
155 ).each do |file|
156 m.file "bin/#{file}", "script/#{file}", {
157 :chmod => 0755,
158 :shebang => options[:shebang] == DEFAULT_SHEBANG ? nil : options[:shebang]
159 }
160 end
161 end
162
163 def create_test_files(m)
164 m.file "helpers/test_helper.rb", "test/test_helper.rb"
165 m.file "helpers/performance_test.rb", "test/performance/browsing_test.rb"
166 end
167
168
169 def create_database_configuration_file(m)
170 m.template "configs/databases/#{options[:db]}.yml", "config/database.yml", :assigns => {
171 :app_name => @app_name,
172 :socket => options[:db] == "mysql" ? mysql_socket_location : nil }
173 end
174
175 def create_routes_file(m)
176 m.file "configs/routes.rb", "config/routes.rb"
177 end
178
179 def create_initializer_files(m)
180 %w(
181 backtrace_silencers
182 inflections
183 mime_types
184 new_rails_defaults
185 ).each do |initializer|
186 m.file "configs/initializers/#{initializer}.rb", "config/initializers/#{initializer}.rb"
187 end
188
189 m.template "configs/initializers/session_store.rb", "config/initializers/session_store.rb",
190 :assigns => { :app_name => @app_name, :app_secret => ActiveSupport::SecureRandom.hex(64) }
191 end
192
193 def create_locale_file(m)
194 m.file "configs/locales/en.yml", "config/locales/en.yml"
195 end
196
197 def create_environment_files(m)
198 m.template "environments/environment.rb", "config/environment.rb",
199 :assigns => { :freeze => options[:freeze] }
200
201 m.file "environments/boot.rb", "config/boot.rb"
202 m.file "environments/production.rb", "config/environments/production.rb"
203 m.file "environments/development.rb", "config/environments/development.rb"
204 m.file "environments/test.rb", "config/environments/test.rb"
205 end
206
207
208 def create_dispatch_files(m)
209 if options[:with_dispatchers]
210 dispatcher_options = { :chmod => 0755, :shebang => options[:shebang] }
211
212 m.file "dispatches/config.ru", "config.ru"
213 m.file "dispatches/dispatch.rb", "public/dispatch.rb", dispatcher_options
214 m.file "dispatches/dispatch.rb", "public/dispatch.cgi", dispatcher_options
215 m.file "dispatches/dispatch.fcgi", "public/dispatch.fcgi", dispatcher_options
216 end
217 end
218
219 def create_error_files(m)
220 %w( 404 422 500 ).each do |file|
221 m.file "html/#{file}.html", "public/#{file}.html"
222 end
223 end
224
225 def create_welcome_file(m)
226 m.file 'html/index.html', 'public/index.html'
227 end
228
229 def create_browser_convention_files(m)
230 m.file "html/favicon.ico", "public/favicon.ico"
231 m.file "html/robots.txt", "public/robots.txt"
232 end
233
234 def create_rails_image(m)
235 m.file "html/images/rails.png", "public/images/rails.png"
236 end
237
238 def create_javascript_files(m)
239 %w( prototype effects dragdrop controls application ).each do |javascript|
240 m.file "html/javascripts/#{javascript}.js", "public/javascripts/#{javascript}.js"
241 end
242 end
243
244
245 def mysql_socket_location
246 [
247 "/tmp/mysql.sock", # default
248 "/var/run/mysqld/mysqld.sock", # debian/gentoo
249 "/var/tmp/mysql.sock", # freebsd
250 "/var/lib/mysql/mysql.sock", # fedora
251 "/opt/local/lib/mysql/mysql.sock", # fedora
252 "/opt/local/var/run/mysqld/mysqld.sock", # mac + darwinports + mysql
253 "/opt/local/var/run/mysql4/mysqld.sock", # mac + darwinports + mysql4
254 "/opt/local/var/run/mysql5/mysqld.sock", # mac + darwinports + mysql5
255 "/opt/lampp/var/mysql/mysql.sock" # xampp for linux
256 ].find { |f| File.exist?(f) } unless RUBY_PLATFORM =~ /(:?mswin|mingw)/
257 end
258 end