Sha256: 9a9ab1a32555d04ff47fa2a8ad8e46ef1e660d891524a73bd4e49d8df27d8ff8

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents


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

      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 :gen_root, "Generators root directory", 
                   :type => :string, :short => 'r', 
                   :default => DEFAULTS[:gen_root]
        parser.stop_on_unknown
      end

      include Ing::Boot
      include Ing::CommonOptions
      
      def generator_root
        @generator_root ||= File.expand_path(options[:gen_root])
      end
      
      # Require libs and ing_file, then
      # locate and require the generator ruby file identified by the first arg,
      # before dispatching to it.
      def before(*args)
        require_libs
        require_ing_file      
        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

2 entries across 2 versions & 1 rubygems

Version Path
ing-0.1.5 lib/ing/commands/generate.rb
ing-0.1.2 lib/ing/commands/generate.rb