Sha256: f7c7a672469db46223fdc212178d1df61c438ffbf9bb372401ea1801f947c02f

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

require 'thor/group'
require 'active_support/inflector'

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

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

      add_runtime_options!

      class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false,
        :desc => "Skip Git ignores and keeps"

      # 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

3 entries across 3 versions & 1 rubygems

Version Path
couch-0.2.0 lib/couch/generators/base.rb
couch-0.1.2 lib/couch/generators/base.rb
couch-0.1.1 lib/couch/generators/base.rb