require 'couch' require 'couch/generators/base' module Couch module Generators # Receives a name, arguments and the behavior to invoke the generator. # It's used as the default entry point for generate and destroy commands. def self.invoke(name, args = ARGV, config = {}) if klass = lookup(name.to_s) args << "--help" if args.empty? && klass.arguments.any? { |a| a.required? } klass.start(args, config) else puts "Could not find generator #{name}." end end # Show help message with available generators. def self.help(command = 'generate') path = File.expand_path("../generators/*/*_generator.rb", __FILE__) generators = Dir.glob(path) generators.sort! generators.map! { |f| File.basename(f) } generators.map! { |n| n.sub!(/_generator\.rb$/, '') } longest_name_size = generators.map { |g| g.size }.sort.last generators.map! { |g| "%s # %s" % [g.ljust(longest_name_size), lookup(g).info] } puts "Usage: couch #{command} GENERATOR [args] [options]" puts puts "General options:" puts " -h, [--help] # Print generators options and usage" puts " -p, [--pretend] # Run but do not make any changes" puts " -f, [--force] # Overwrite files that already exist" puts " -s, [--skip] # Skip files that already exist" puts " -q, [--quiet] # Supress status output" puts puts "Please choose a generator below:" puts puts generators end protected def self.lookup(name) # real path path = File.expand_path("../generators/#{name}/#{name}_generator.rb", __FILE__) # no matches? unless File.exists?(path) # try to find by prefix found = Dir.glob(File.expand_path("../generators/#{name}*/#{name}*_generator.rb", __FILE__)) if found.size == 1 path = found.first name = File.basename(path).sub(/_generator\.rb$/, '') end end require path const_get "#{name.classify}Generator" rescue LoadError => e raise unless e.message =~ /#{Regexp.escape(path)}$/ rescue Exception => e warn "[WARNING] Could not load generator #{path.inspect}. Error: #{e.message}.\n#{e.backtrace.join("\n")}" end end end