Sha256: 357228bc0acd987b1a1c75970c839c6a3eaff616fa9dd61fde69c658375105cc

Contents?: true

Size: 1.58 KB

Versions: 10

Compression:

Stored size: 1.58 KB

Contents

require "rake"

class <%= project_class_name %>::RakeCommand
  class << self
    # Same signature as <%= project_class_name %>::Command.perform.
    def perform(namespaced_command, thor_args)
      if thor_args.first == "help"
        puts help_message(namespaced_command)
        return
      end

      require_rake

      ARGV.unshift(namespaced_command) # Prepend the task, so Rake knows how to run it.

      rake.standard_exception_handling do
        rake.init("<%= project_name %>")
        rake.load_rakefile
        rake.top_level
      end
    end

    # Useful for CLI.lookup.
    def namespaced_commands
      formatted_rake_tasks(true).map(&:first)
    end

    def formatted_rake_tasks(all=false)
      rake_tasks(all).map do |t|
        comment = "# #{t.comment}" if t.comment
        [ t.name_with_args, comment ]
      end
    end

    def rake_tasks(all=false)
      require_rake

      Rake::TaskManager.record_task_metadata = true
      rake.instance_variable_set(:@name, "<%= project_name %>")
      load "Rakefile" if File.exist?("Rakefile")
      tasks = rake.tasks
      tasks = tasks.select(&:comment) unless all
      tasks
    end

    def help_message(namespaced_command)
      task = rake_tasks(true).find { |t| t.name == namespaced_command }
      message = "Help provided by rake task:\n\n"
      message << "<%= project_name %> #{task.name_with_args.dup}\n"
      message << "    #{task.full_comment}"
      message
    end

  private
    def rake
      Rake.application
    end

    def require_rake
      require "rake" # Defer booting Rake until we know it's needed.
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
cli-template-4.0.2 lib/templates/colon_namespaces/lib/%underscored_name%/rake_command.rb.tt
cli-template-4.0.1 lib/templates/colon_namespaces/lib/%underscored_name%/rake_command.rb.tt
cli-template-4.0.0 lib/templates/colon_namespaces/lib/%underscored_name%/rake_command.rb.tt
cli-template-3.5.1 lib/templates/colon_namespaces/lib/%underscored_name%/rake_command.rb.tt
cli-template-3.5.0 lib/templates/colon_namespaces/lib/%underscored_name%/rake_command.rb.tt
cli-template-3.4.0 lib/templates/colon_namespaces/lib/%underscored_name%/rake_command.rb.tt
cli-template-3.3.0 lib/templates/colon_namespaces/lib/%underscored_name%/rake_command.rb.tt
cli-template-3.2.0 lib/templates/colon_namespaces/lib/%underscored_name%/rake_command.rb.tt
cli-template-3.1.0 lib/templates/default/lib/%underscored_name%/rake_command.rb.tt
cli-template-3.0.0 lib/templates/skeleton/lib/%underscored_name%/rake_command.rb.tt