Sha256: 1f879f5f7135f78e23cb15065a285d10c2ac8c7315c8db321ae903746ee448fd

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents


module Ing
  module Commands
  
    # This is the boot command invoked from `ing generate ...`
    class Generate < Boot

      DEFAULTS = {
         namespace: 'object',
         ing_file:  'ing.rb',
         gen_root:  ENV.fetch('ING_GENERATOR_ROOT', '~/.ing/generators')
      }
    
      def self.specify_options(parser)
        parser.text "Run a generator task"
        parser.opt :debug, "Display debug messages"
        parser.opt :namespace, "Top-level namespace for generators",
                   :type => :string, :default => DEFAULTS[:namespace]
        parser.opt :gen_root, "Generators root directory", 
                   :type => :string, :short => 'r', 
                   :default => DEFAULTS[:gen_root]
        parser.opt :ing_file, "Default generator file (ruby)", 
                   :type => :string, :short => 'f', 
                   :default => DEFAULTS[:ing_file]
        parser.stop_on_unknown
      end

      def generator_root
        @generator_root ||= File.expand_path(options[:gen_root])
      end
      
      # Locate and require the generator ruby file identified by the first arg,
      # before dispatching to it.
      def before(*args)
        require_generator args.first
      end
      
      private
          
      def require_generator(name)
        path = File.expand_path(generator_name_to_path(name), generator_root)
        f = if File.directory?(path)
          File.join(path, options[:ing_file])
        else
          path
        end
        debug "#{__FILE__}:#{__LINE__} :: require #{f.inspect}" 
        require f
      rescue LoadError
        raise LoadError, 
          "No generator found named `#{name}`. Check that you have set the generator root directory correctly (looking for `#{f}`)"
      end
      
      def generator_name_to_path(name)
        name.split(":").join("/")
      end
      
    end
    
    # alias
    G = Generate

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ing-0.1.1 lib/ing/commands/generate.rb