2
require File.dirname(__FILE__) + '/template_runner'
4
require 'active_support/secure_random'
6
class AppGenerator < Rails::Generator::Base
7
DEFAULT_SHEBANG = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
9
DATABASES = %w( mysql oracle postgresql sqlite2 sqlite3 frontbase ibm_db )
10
DEFAULT_DATABASE = 'sqlite3'
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
17
def initialize(runtime_args, runtime_options = {})
21
usage("Databases supported for preconfiguration are: #{DATABASES.join(", ")}") if (options[:db] && !DATABASES.include?(options[:db]))
23
@destination_root = args.shift
24
@app_name = File.basename(File.expand_path(@destination_root))
32
create_config_files(m)
33
create_script_files(m)
35
create_public_files(m)
36
create_documentation_file(m)
43
Rails::TemplateRunner.new(options[:template], @destination_root)
49
"Usage: #{$0} /path/to/your/app [options]"
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 }
59
opt.on("-d", "--database=name", String,
60
"Preconfigure for selected database (options: #{DATABASES.join('/')}).",
61
"Default: #{DEFAULT_DATABASE}") { |v| options[:db] = v }
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 }
67
opt.on("-f", "--freeze",
68
"Freeze Rails in vendor/rails from the gems generating the skeleton",
69
"Default: false") { |v| options[:freeze] = v }
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 }
79
def create_directories(m)
82
# Intermediate directories are automatically created so don't sweat their absence here.
111
).each { |path| m.directory(path) }
114
def create_root_files(m)
115
m.file "fresh_rakefile", "Rakefile"
116
m.file "README", "README"
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"
124
def create_config_files(m)
125
create_database_configuration_file(m)
126
create_routes_file(m)
127
create_locale_file(m)
129
create_initializer_files(m)
130
create_environment_files(m)
133
def create_documentation_file(m)
134
m.file "doc/README_FOR_APP", "doc/README_FOR_APP"
137
def create_log_files(m)
138
%w( server production development test ).each do |file|
139
m.file "configs/empty.log", "log/#{file}.log", :chmod => 0666
143
def create_public_files(m)
144
create_dispatch_files(m)
145
create_error_files(m)
146
create_welcome_file(m)
147
create_browser_convention_files(m)
148
create_rails_image(m)
149
create_javascript_files(m)
152
def create_script_files(m)
154
about console dbconsole destroy generate runner server plugin
155
performance/benchmarker performance/profiler
157
m.file "bin/#{file}", "script/#{file}", {
159
:shebang => options[:shebang] == DEFAULT_SHEBANG ? nil : options[:shebang]
164
def create_test_files(m)
165
m.file "helpers/test_helper.rb", "test/test_helper.rb"
166
m.file "helpers/performance_test.rb", "test/performance/browsing_test.rb"
170
def create_database_configuration_file(m)
171
m.template "configs/databases/#{options[:db]}.yml", "config/database.yml", :assigns => {
172
:app_name => @app_name,
173
:socket => options[:db] == "mysql" ? mysql_socket_location : nil }
176
def create_routes_file(m)
177
m.file "configs/routes.rb", "config/routes.rb"
180
def create_seeds_file(m)
181
m.file "configs/seeds.rb", "db/seeds.rb"
184
def create_initializer_files(m)
190
).each do |initializer|
191
m.file "configs/initializers/#{initializer}.rb", "config/initializers/#{initializer}.rb"
194
m.template "configs/initializers/session_store.rb", "config/initializers/session_store.rb",
195
:assigns => { :app_name => @app_name, :app_secret => ActiveSupport::SecureRandom.hex(64) }
198
def create_locale_file(m)
199
m.file "configs/locales/en.yml", "config/locales/en.yml"
202
def create_environment_files(m)
203
m.template "environments/environment.rb", "config/environment.rb",
204
:assigns => { :freeze => options[:freeze] }
206
m.file "environments/boot.rb", "config/boot.rb"
207
m.file "environments/production.rb", "config/environments/production.rb"
208
m.file "environments/development.rb", "config/environments/development.rb"
209
m.file "environments/test.rb", "config/environments/test.rb"
213
def create_dispatch_files(m)
214
if options[:with_dispatchers]
215
dispatcher_options = { :chmod => 0755, :shebang => options[:shebang] }
217
m.file "dispatches/config.ru", "config.ru"
218
m.file "dispatches/dispatch.rb", "public/dispatch.rb", dispatcher_options
219
m.file "dispatches/dispatch.rb", "public/dispatch.cgi", dispatcher_options
220
m.file "dispatches/dispatch.fcgi", "public/dispatch.fcgi", dispatcher_options
224
def create_error_files(m)
225
%w( 404 422 500 ).each do |file|
226
m.file "html/#{file}.html", "public/#{file}.html"
230
def create_welcome_file(m)
231
m.file 'html/index.html', 'public/index.html'
234
def create_browser_convention_files(m)
235
m.file "html/favicon.ico", "public/favicon.ico"
236
m.file "html/robots.txt", "public/robots.txt"
239
def create_rails_image(m)
240
m.file "html/images/rails.png", "public/images/rails.png"
243
def create_javascript_files(m)
244
%w( prototype effects dragdrop controls application ).each do |javascript|
245
m.file "html/javascripts/#{javascript}.js", "public/javascripts/#{javascript}.js"
250
def mysql_socket_location
252
"/tmp/mysql.sock", # default
253
"/var/run/mysqld/mysqld.sock", # debian/gentoo
254
"/var/tmp/mysql.sock", # freebsd
255
"/var/lib/mysql/mysql.sock", # fedora
256
"/opt/local/lib/mysql/mysql.sock", # fedora
257
"/opt/local/var/run/mysqld/mysqld.sock", # mac + darwinports + mysql
258
"/opt/local/var/run/mysql4/mysqld.sock", # mac + darwinports + mysql4
259
"/opt/local/var/run/mysql5/mysqld.sock", # mac + darwinports + mysql5
260
"/opt/lampp/var/mysql/mysql.sock" # xampp for linux
261
].find { |f| File.exist?(f) } unless RUBY_PLATFORM =~ /(:?mswin|mingw)/
b'\\ No newline at end of file'