Sha256: 2be37baa70e050f98e0c782dec83f0e20fe8924409ce5b71fdda325266fed897

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

require 'couch/core_ext/string/inflections'

require 'rubygems'
require 'thor/group'

module Couch
  module Generators
    class Error < Thor::Error
    end

    class Base < Thor::Group
      include Thor::Actions

      add_runtime_options!

      # Automatically sets the source root based on the class name.
      #
      def self.source_root
        @_couch_source_root ||= begin
          if generator_name
            File.expand_path(File.join("../generators", generator_name, 'templates'), File.dirname(__FILE__))
          end
        end
      end

      # Tries to get the description from a USAGE file one folder above the source
      # root otherwise uses a default description.
      #
      def self.desc(description=nil)
        return super if description
        usage = File.expand_path(File.join(source_root, "..", "USAGE"))

        @desc ||= if File.exist?(usage)
          File.read(usage)
        else
          "Description:\n    Create files for #{generator_name} generator."
        end
      end

      def self.info
        desc.split(/\n+/)[1].strip
      end

      protected

      # Use Couch default banner.
      #
      def self.banner
        "couch generate|destroy #{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
      end

      # Removes the namespaces and get the generator name. For example,
      # Couch::Generators::MetalGenerator will return "metal" as generator name.
      #
      def self.generator_name
        @generator_name ||= begin
          if generator = name.to_s.split('::').last
            generator.sub!(/Generator$/, '')
            generator.underscore
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
couch-0.0.2 lib/couch/generators/base.rb
couch-0.0.1 lib/couch/generators/base.rb