#!/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'
require 'timeout'
uri = "http://wordnet.princeton.edu/cgi-bin/webwn2.0?stage=2" +
"&word=%s&posnumber=1&searchtypenumber=2&senses=&showglosses=1"
timeout(5) do
open(uri % word) do |stream|
data = stream.read.gsub(" ", " ").gsub("
", "")
data.scan(/^Sense \d+\n.+?\n\n/m)
end
end
rescue Timeout::Error, 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 <