aboutsummaryrefslogtreecommitdiffstats
path: root/railties/bin/generate
blob: 55c55724b133c837b2fe165e4e1f65525e658ad7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/local/bin/ruby
require File.dirname(__FILE__) + '/../config/environment'
require 'rails_generator'

ARGV.shift unless ARGV.empty? or not ['--help', '-h'].include?(ARGV[0])

def find_synonyms(word)
  require 'open-uri'
  uri = "http://wordnet.princeton.edu/cgi-bin/webwn2.0?stage=2" +
    "&word=%s&posnumber=1&searchtypenumber=2&senses=&showglosses=1"

  open(uri % word) do |stream|
    data = stream.read.gsub("&nbsp;", " ").gsub("<BR>", "")
    data.scan(/^Sense \d+\n.+?\n\n/m)
  end
rescue Exception
  return nil
end

unless ARGV.empty?
  begin
    name = ARGV.shift
    generator = Rails::Generator.instance(name, ARGV)

    if msg = generator.collision_with_builtin? then
      $stderr.puts msg

      if synonyms = find_synonyms(generator.class_name) then
        $stderr.puts(
          "", "Here are a few synonyms from WordNet. Maybe they will help you find an alternative name.",
          "", synonyms
        )
      end
    else
      generator.generate
    end
  rescue Rails::Generator::UsageError => e
    puts e.message
  end
else
  builtin_generators = Rails::Generator.builtin_generators.join(', ')
  contrib_generators = Rails::Generator.contrib_generators.join(', ')

  $stderr.puts <<end_usage
  #{$0} generator [args]

  Rails comes with #{builtin_generators} generators.
    #{$0} controller Login login logout
    #{$0} model Account
    #{$0} mailer AccountMailer
    #{$0} scaffold Account action another_action

end_usage

  unless contrib_generators.empty?
    $stderr.puts "  Installed generators (in #{RAILS_ROOT}/script/generators):"
    $stderr.puts "    #{contrib_generators}"
    $stderr.puts
  end

  $stderr.puts <<end_usage
  More generators are available at http://rubyonrails.org/show/Generators
    1. Download, for example, login_generator.tar.gz
    2. Unzip to directory #{RAILS_ROOT}/script/generators/login
    3. Generate without args for usage information
         #{$0} login
end_usage
  exit 0
end