| 1 |
require 'delegate' |
|---|
| 2 |
require 'optparse' |
|---|
| 3 |
require 'fileutils' |
|---|
| 4 |
require 'tempfile' |
|---|
| 5 |
require 'erb' |
|---|
| 6 |
|
|---|
| 7 |
module Rails |
|---|
| 8 |
module Generator |
|---|
| 9 |
module Commands |
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
def self.instance(command, generator) |
|---|
| 14 |
const_get(command.to_s.camelize).new(generator) |
|---|
| 15 |
end |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
def self.included(base) |
|---|
| 21 |
base.send(:define_method, :command) do |command| |
|---|
| 22 |
Commands.instance(command, self) |
|---|
| 23 |
end |
|---|
| 24 |
end |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
class Base < DelegateClass(Rails::Generator::Base) |
|---|
| 40 |
|
|---|
| 41 |
def invoke! |
|---|
| 42 |
manifest.replay(self) |
|---|
| 43 |
end |
|---|
| 44 |
|
|---|
| 45 |
def dependency(generator_name, args, runtime_options = {}) |
|---|
| 46 |
logger.dependency(generator_name) do |
|---|
| 47 |
self.class.new(instance(generator_name, args, full_options(runtime_options))).invoke! |
|---|
| 48 |
end |
|---|
| 49 |
end |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
def class_collisions(*class_names) |
|---|
| 53 |
end |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
def readme(*args) |
|---|
| 57 |
end |
|---|
| 58 |
|
|---|
| 59 |
protected |
|---|
| 60 |
def migration_directory(relative_path) |
|---|
| 61 |
directory(@migration_directory = relative_path) |
|---|
| 62 |
end |
|---|
| 63 |
|
|---|
| 64 |
def existing_migrations(file_name) |
|---|
| 65 |
Dir.glob("#{@migration_directory}/[0-9]*_*.rb").grep(/[0-9]+_ |
|---|
| 66 |
end |
|---|
| 67 |
|
|---|
| 68 |
def migration_exists?(file_name) |
|---|
| 69 |
not existing_migrations(file_name).empty? |
|---|
| 70 |
end |
|---|
| 71 |
|
|---|
| 72 |
def next_migration_string(padding = 3) |
|---|
| 73 |
Time.now.utc.strftime("%Y%m%d%H%M%S") |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
def gsub_file(relative_destination, regexp, *args, &block) |
|---|
| 77 |
path = destination_path(relative_destination) |
|---|
| 78 |
content = File.read(path).gsub(regexp, *args, &block) |
|---|
| 79 |
File.open(path, 'wb') { |file| file.write(content) } |
|---|
| 80 |
end |
|---|
| 81 |
|
|---|
| 82 |
private |
|---|
| 83 |
|
|---|
| 84 |
def force_file_collision?(destination, src, dst, file_options = {}, &block) |
|---|
| 85 |
$stdout.print "overwrite #{destination}? (enter \"h\" for help) [Ynaqdh] " |
|---|
| 86 |
case $stdin.gets.chomp |
|---|
| 87 |
when /\Ad\z/i |
|---|
| 88 |
Tempfile.open(File.basename(destination), File.dirname(dst)) do |temp| |
|---|
| 89 |
temp.write render_file(src, file_options, &block) |
|---|
| 90 |
temp.rewind |
|---|
| 91 |
$stdout.puts ` |
|---|
| 92 |
end |
|---|
| 93 |
puts "retrying" |
|---|
| 94 |
raise 'retry diff' |
|---|
| 95 |
when /\Aa\z/i |
|---|
| 96 |
$stdout.puts "forcing #{spec.name}" |
|---|
| 97 |
options[:collision] = :force |
|---|
| 98 |
when /\Aq\z/i |
|---|
| 99 |
$stdout.puts "aborting #{spec.name}" |
|---|
| 100 |
raise SystemExit |
|---|
| 101 |
when /\An\z/i then :skip |
|---|
| 102 |
when /\Ay\z/i then :force |
|---|
| 103 |
else |
|---|
| 104 |
$stdout.puts <<-HELP |
|---|
| 105 |
Y - yes, overwrite |
|---|
| 106 |
n - no, do not overwrite |
|---|
| 107 |
a - all, overwrite this and all others |
|---|
| 108 |
q - quit, abort |
|---|
| 109 |
d - diff, show the differences between the old and the new |
|---|
| 110 |
h - help, show this help |
|---|
| 111 |
HELP |
|---|
| 112 |
raise 'retry' |
|---|
| 113 |
end |
|---|
| 114 |
rescue |
|---|
| 115 |
retry |
|---|
| 116 |
end |
|---|
| 117 |
|
|---|
| 118 |
def diff_cmd |
|---|
| 119 |
ENV['RAILS_DIFF'] || 'diff -u' |
|---|
| 120 |
end |
|---|
| 121 |
|
|---|
| 122 |
def render_template_part(template_options) |
|---|
| 123 |
|
|---|
| 124 |
part_binding = template_options[:sandbox].call.sandbox_binding |
|---|
| 125 |
part_rel_path = template_options[:insert] |
|---|
| 126 |
part_path = source_path(part_rel_path) |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
rendered_part = ERB.new(File.readlines(part_path).join, nil, '-').result(part_binding) |
|---|
| 130 |
begin_mark = template_part_mark(template_options[:begin_mark], template_options[:mark_id]) |
|---|
| 131 |
end_mark = template_part_mark(template_options[:end_mark], template_options[:mark_id]) |
|---|
| 132 |
begin_mark + rendered_part + end_mark |
|---|
| 133 |
end |
|---|
| 134 |
|
|---|
| 135 |
def template_part_mark(name, id) |
|---|
| 136 |
"<!--[#{name}:#{id}]-->\n" |
|---|
| 137 |
end |
|---|
| 138 |
end |
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
class RewindBase < Base |
|---|
| 142 |
|
|---|
| 143 |
def invoke! |
|---|
| 144 |
manifest.rewind(self) |
|---|
| 145 |
end |
|---|
| 146 |
end |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
class Create < Base |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
def class_collisions(*class_names) |
|---|
| 157 |
class_names.flatten.each do |class_name| |
|---|
| 158 |
|
|---|
| 159 |
class_name = class_name.to_s |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
next if class_name.strip.empty? |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
nesting = class_name.split('::') |
|---|
| 166 |
name = nesting.pop |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
last = nesting.inject(Object) { |last, nest| |
|---|
| 170 |
break unless last.const_defined?(nest) |
|---|
| 171 |
last.const_get(nest) |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
if last and last.const_defined?(name.camelize) |
|---|
| 177 |
raise_class_collision(class_name) |
|---|
| 178 |
end |
|---|
| 179 |
end |
|---|
| 180 |
end |
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
def file(relative_source, relative_destination, file_options = {}, &block) |
|---|
| 196 |
|
|---|
| 197 |
source = source_path(relative_source) |
|---|
| 198 |
destination = destination_path(relative_destination) |
|---|
| 199 |
destination_exists = File.exist?(destination) |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
if destination_exists and identical?(source, destination, &block) |
|---|
| 203 |
return logger.identical(relative_destination) |
|---|
| 204 |
end |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
if destination_exists |
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
choice = case (file_options[:collision] || options[:collision]).to_sym |
|---|
| 212 |
when :ask then force_file_collision?(relative_destination, source, destination, file_options, &block) |
|---|
| 213 |
when :force then :force |
|---|
| 214 |
when :skip then :skip |
|---|
| 215 |
else raise "Invalid collision option: #{options[:collision].inspect}" |
|---|
| 216 |
end |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
case choice |
|---|
| 221 |
when :force then logger.force(relative_destination) |
|---|
| 222 |
when :skip then return(logger.skip(relative_destination)) |
|---|
| 223 |
else raise "Invalid collision choice: #{choice}.inspect" |
|---|
| 224 |
end |
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
else |
|---|
| 228 |
logger.create relative_destination |
|---|
| 229 |
end |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
return if options[:pretend] |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
File.open(destination, 'wb') do |dest| |
|---|
| 239 |
dest.write render_file(source, file_options, &block) |
|---|
| 240 |
end |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
if file_options[:chmod] |
|---|
| 244 |
FileUtils.chmod(file_options[:chmod], destination) |
|---|
| 245 |
end |
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
system("svn add #{destination}") if options[:svn] |
|---|
| 249 |
system("git add -v #{relative_destination}") if options[:git] |
|---|
| 250 |
end |
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
def identical?(source, destination, &block) |
|---|
| 256 |
return false if File.directory? destination |
|---|
| 257 |
source = block_given? ? File.open(source) {|sf| yield(sf)} : IO.read(source) |
|---|
| 258 |
destination = IO.read(destination) |
|---|
| 259 |
source == destination |
|---|
| 260 |
end |
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
def template(relative_source, relative_destination, template_options = {}) |
|---|
| 277 |
file(relative_source, relative_destination, template_options) do |file| |
|---|
| 278 |
|
|---|
| 279 |
vars = template_options[:assigns] || {} |
|---|
| 280 |
b = binding |
|---|
| 281 |
vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b } |
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
ERB.new(file.read, nil, '-').result(b) |
|---|
| 285 |
end |
|---|
| 286 |
end |
|---|
| 287 |
|
|---|
| 288 |
def complex_template(relative_source, relative_destination, template_options = {}) |
|---|
| 289 |
options = template_options.dup |
|---|
| 290 |
options[:assigns] ||= {} |
|---|
| 291 |
options[:assigns]['template_for_inclusion'] = render_template_part(template_options) |
|---|
| 292 |
template(relative_source, relative_destination, options) |
|---|
| 293 |
end |
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
def directory(relative_path) |
|---|
| 298 |
path = destination_path(relative_path) |
|---|
| 299 |
if File.exist?(path) |
|---|
| 300 |
logger.exists relative_path |
|---|
| 301 |
else |
|---|
| 302 |
logger.create relative_path |
|---|
| 303 |
unless options[:pretend] |
|---|
| 304 |
FileUtils.mkdir_p(path) |
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
if options[:svn] |
|---|
| 313 |
stack = [relative_path] |
|---|
| 314 |
until File.dirname(stack.last) == stack.last |
|---|
| 315 |
stack.push File.dirname(stack.last) |
|---|
| 316 |
end |
|---|
| 317 |
stack.reverse_each do |rel_path| |
|---|
| 318 |
svn_path = destination_path(rel_path) |
|---|
| 319 |
system("svn add -N #{svn_path}") unless File.directory?(File.join(svn_path, '.svn')) |
|---|
| 320 |
end |
|---|
| 321 |
end |
|---|
| 322 |
end |
|---|
| 323 |
end |
|---|
| 324 |
end |
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 |
def readme(*relative_sources) |
|---|
| 328 |
relative_sources.flatten.each do |relative_source| |
|---|
| 329 |
logger.readme relative_source |
|---|
| 330 |
puts File.read(source_path(relative_source)) unless options[:pretend] |
|---|
| 331 |
end |
|---|
| 332 |
end |
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
def migration_template(relative_source, relative_destination, template_options = {}) |
|---|
| 336 |
migration_directory relative_destination |
|---|
| 337 |
migration_file_name = template_options[:migration_file_name] || file_name |
|---|
| 338 |
raise "Another migration is already named #{migration_file_name}: #{existing_migrations(migration_file_name).first}" if migration_exists?(migration_file_name) |
|---|
| 339 |
template(relative_source, "#{relative_destination}/#{next_migration_string}_#{migration_file_name}.rb", template_options) |
|---|
| 340 |
end |
|---|
| 341 |
|
|---|
| 342 |
def route_resources(*resources) |
|---|
| 343 |
resource_list = resources.map { |r| r.to_sym.inspect }.join(', ') |
|---|
| 344 |
sentinel = 'ActionController::Routing::Routes.draw do |map|' |
|---|
| 345 |
|
|---|
| 346 |
logger.route "map.resources #{resource_list}" |
|---|
| 347 |
unless options[:pretend] |
|---|
| 348 |
gsub_file 'config/routes.rb', /( |
|---|
| 349 |
"#{match}\n map.resources #{resource_list}\n" |
|---|
| 350 |
end |
|---|
| 351 |
end |
|---|
| 352 |
end |
|---|
| 353 |
|
|---|
| 354 |
private |
|---|
| 355 |
def render_file(path, options = {}) |
|---|
| 356 |
File.open(path, 'rb') do |file| |
|---|
| 357 |
if block_given? |
|---|
| 358 |
yield file |
|---|
| 359 |
else |
|---|
| 360 |
content = '' |
|---|
| 361 |
if shebang = options[:shebang] |
|---|
| 362 |
content << "#!#{shebang}\n" |
|---|
| 363 |
if line = file.gets |
|---|
| 364 |
content << "line\n" if line !~ /^ |
|---|
| 365 |
end |
|---|
| 366 |
end |
|---|
| 367 |
content << file.read |
|---|
| 368 |
end |
|---|
| 369 |
end |
|---|
| 370 |
end |
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
def raise_class_collision(class_name) |
|---|
| 375 |
message = <<end_message |
|---|
| 376 |
The name '#{class_name}' is reserved by Ruby on Rails. |
|---|
| 377 |
Please choose an alternative and run this generator again. |
|---|
| 378 |
end_message |
|---|
| 379 |
if suggest = find_synonyms(class_name) |
|---|
| 380 |
message << "\n Suggestions: \n\n" |
|---|
| 381 |
message << suggest.join("\n") |
|---|
| 382 |
end |
|---|
| 383 |
raise UsageError, message |
|---|
| 384 |
end |
|---|
| 385 |
|
|---|
| 386 |
SYNONYM_LOOKUP_URI = "http://wordnet.princeton.edu/perl/webwn?s=%s" |
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
def find_synonyms(word) |
|---|
| 390 |
require 'open-uri' |
|---|
| 391 |
require 'timeout' |
|---|
| 392 |
timeout(5) do |
|---|
| 393 |
open(SYNONYM_LOOKUP_URI % word) do |stream| |
|---|
| 394 |
|
|---|
| 395 |
data = stream.read.gsub(" ", " ").scan(/<a href="webwn.*?">([\w ]*?)<\/a>/s).uniq |
|---|
| 396 |
end |
|---|
| 397 |
end |
|---|
| 398 |
rescue Exception |
|---|
| 399 |
return nil |
|---|
| 400 |
end |
|---|
| 401 |
end |
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
class Destroy < RewindBase |
|---|
| 407 |
|
|---|
| 408 |
def file(relative_source, relative_destination, file_options = {}) |
|---|
| 409 |
destination = destination_path(relative_destination) |
|---|
| 410 |
if File.exist?(destination) |
|---|
| 411 |
logger.rm relative_destination |
|---|
| 412 |
unless options[:pretend] |
|---|
| 413 |
if options[:svn] |
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 |
if options[:svn][relative_destination] |
|---|
| 417 |
system("svn revert #{destination}") |
|---|
| 418 |
FileUtils.rm(destination) |
|---|
| 419 |
else |
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
system("svn rm #{destination}") |
|---|
| 423 |
end |
|---|
| 424 |
elsif options[:git] |
|---|
| 425 |
if options[:git][:new][relative_destination] |
|---|
| 426 |
|
|---|
| 427 |
system("git reset HEAD #{relative_destination}") |
|---|
| 428 |
FileUtils.rm(destination) |
|---|
| 429 |
elsif options[:git][:modified][relative_destination] |
|---|
| 430 |
|
|---|
| 431 |
system("git rm -f #{relative_destination}") |
|---|
| 432 |
else |
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
system("git rm #{relative_destination}") |
|---|
| 436 |
end |
|---|
| 437 |
else |
|---|
| 438 |
FileUtils.rm(destination) |
|---|
| 439 |
end |
|---|
| 440 |
end |
|---|
| 441 |
else |
|---|
| 442 |
logger.missing relative_destination |
|---|
| 443 |
return |
|---|
| 444 |
end |
|---|
| 445 |
end |
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
alias_method :template, :file |
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
def directory(relative_path) |
|---|
| 454 |
parts = relative_path.split('/') |
|---|
| 455 |
until parts.empty? |
|---|
| 456 |
partial = File.join(parts) |
|---|
| 457 |
path = destination_path(partial) |
|---|
| 458 |
if File.exist?(path) |
|---|
| 459 |
if Dir[File.join(path, '*')].empty? |
|---|
| 460 |
logger.rmdir partial |
|---|
| 461 |
unless options[:pretend] |
|---|
| 462 |
if options[:svn] |
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
if options[:svn][relative_path] |
|---|
| 466 |
system("svn revert #{path}") |
|---|
| 467 |
FileUtils.rmdir(path) |
|---|
| 468 |
else |
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
system("svn rm #{path}") |
|---|
| 472 |
end |
|---|
| 473 |
|
|---|
| 474 |
|
|---|
| 475 |
else |
|---|
| 476 |
FileUtils.rmdir(path) |
|---|
| 477 |
end |
|---|
| 478 |
end |
|---|
| 479 |
else |
|---|
| 480 |
logger.notempty partial |
|---|
| 481 |
end |
|---|
| 482 |
else |
|---|
| 483 |
logger.missing partial |
|---|
| 484 |
end |
|---|
| 485 |
parts.pop |
|---|
| 486 |
end |
|---|
| 487 |
end |
|---|
| 488 |
|
|---|
| 489 |
def complex_template(*args) |
|---|
| 490 |
|
|---|
| 491 |
end |
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 |
def migration_template(relative_source, relative_destination, template_options = {}) |
|---|
| 495 |
migration_directory relative_destination |
|---|
| 496 |
|
|---|
| 497 |
migration_file_name = template_options[:migration_file_name] || file_name |
|---|
| 498 |
unless migration_exists?(migration_file_name) |
|---|
| 499 |
puts "There is no migration named #{migration_file_name}" |
|---|
| 500 |
return |
|---|
| 501 |
end |
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
existing_migrations(migration_file_name).each do |file_path| |
|---|
| 505 |
file(relative_source, file_path, template_options) |
|---|
| 506 |
end |
|---|
| 507 |
end |
|---|
| 508 |
|
|---|
| 509 |
def route_resources(*resources) |
|---|
| 510 |
resource_list = resources.map { |r| r.to_sym.inspect }.join(', ') |
|---|
| 511 |
look_for = "\n map.resources #{resource_list}\n" |
|---|
| 512 |
logger.route "map.resources #{resource_list}" |
|---|
| 513 |
gsub_file 'config/routes.rb', /( |
|---|
| 514 |
end |
|---|
| 515 |
end |
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 |
class List < Base |
|---|
| 520 |
def dependency( |
|---|