Sha256: 6f057df476c5f3bfc827b13222b9519a90d0a8a1e52e93b8a3024d0771fe45d7

Contents?: true

Size: 1.75 KB

Versions: 7

Compression:

Stored size: 1.75 KB

Contents

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

module Rid
  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
        @_rid_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 Rid default banner.
      #
      def self.banner
        "rid generate|destroy #{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
      end

      # Removes the namespaces and get the generator name. For example,
      # Rid::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

7 entries across 7 versions & 1 rubygems

Version Path
rid-0.5.2 lib/rid/generators/base.rb
rid-0.5.1 lib/rid/generators/base.rb
rid-0.5.0 lib/rid/generators/base.rb
rid-0.4.1 lib/rid/generators/base.rb
rid-0.4.0 lib/rid/generators/base.rb
rid-0.3.1 lib/rid/generators/base.rb
rid-0.3.0 lib/rid/generators/base.rb