rails / thor

Thor is a toolkit for building powerful command-line interfaces.
http://whatisthor.com/
MIT License
5.13k stars 552 forks source link

No tasks shown in thor list with thor/rake_compat for RSpec #619

Open FilBot3 opened 6 years ago

FilBot3 commented 6 years ago

Using the example found here: https://www.rubydoc.info/github/wycats/thor/Thor/RakeCompat

I am unable to run the RSpec task, or have it show when running thor list.

user01@M1711732 [17:26:32] ~/Documents/development/
$ thor list --debug
user01@M1711732 [17:26:42] ~/Documents/development/
$ thor list --debug --all
user01@M1711732 [17:26:46] ~/Documents/development/
$ cat Thorfile
require 'thor/rake_compat'
require 'rspec/core/rake_task'

class Default < Thor
  include Thor::RakeCompat

  RSpec::Core::RakeTask.new(:spec) do |t|
    t.spec_opts = ['--options', './.rspec']
    t.spec_files = FileList['spec/**/*_spec.rb']
  end
end

user01@M1711732 [17:26:54] ~/Documents/development/
$ thor version
Thor 0.20.0
FilBot3 commented 6 years ago

From playing around with this a bit and looking at the spec's and the Thorfile included with the repository, you need to create a Thor task to invoke the Rake task.

require 'thor/rake_compat'
require 'rspec/core/rake_task'

class Default < Thor
  include Thor::RakeCompat

  RSpec::Core::RakeTask.new(:spec) do |t|
    t.spec_opts = ['--options', './.rspec']
    t.spec_files = FileList['spec/**/*_spec.rb']
  end

  desc 'spec', 'Run RSpec tests'
  def spec
    Rake::Task['spec'].invoke
  end
end

Even taking from the rake_compat_spec.rb

require 'thor/rake_compat'
require "rake/tasklib"

class RakeTask < Rake::TaskLib
  def initialize
    define
  end

  def define
    instance_eval do
      desc "Say it's cool"
      task :cool do
        puts "COOL"
      end

      namespace :hiper_mega do
        task :super do
          puts "HIPER MEGA SUPER"
        end
      end
    end
  end
end

class ThorTask < Thor
  include Thor::RakeCompat
  RakeTask.new

  desc 'cool', 'say cool'
  def cool
    Rake::Task['cool'].invoke
    puts ThorTask.tasks['cool'].description
  end
end

The result is:

thor thor_task:cool
COOL
say cool
FilBot3 commented 6 years ago

Would it be helpful to make this into a Wiki page as well?