Sha256: 954092b1563398870c191be388225aeca9ffb0d63a2ebe1cae158f8de866f61c

Contents?: true

Size: 1.22 KB

Versions: 12

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

require 'rake'
require 'shellwords'
require_relative '../leftovers'

module Leftovers
  class RakeTask
    include ::Rake::DSL

    def self.generate_task(name = :leftovers, *default_argv) # rubocop:disable
      new(name, default_argv)

      name
    end

    def initialize(name, default_argv)
      @name = name
      @default_argv = default_argv

      describe_task
      define_task
    end

    private

    def escaped_argv(argv = @default_argv)
      return if argv.empty?

      Shellwords.shelljoin(argv)
    end

    def describe_task
      return desc('Run leftovers') if @default_argv.empty?

      desc("Run leftovers (default args: #{escaped_argv})")
    end

    def define_task
      task(@name, :'*args') do |_, task_argv|
        argv = argv_or_default(task_argv)
        write_cli_cmd(argv)
        run(argv)
      end
    end

    def write_cli_cmd(argv)
      $stdout.puts("\e[2mleftovers #{escaped_argv(argv)}\e[0m")
    end

    def run(argv)
      exitstatus = Leftovers::CLI.new(argv: argv).run

      exit exitstatus unless exitstatus == 0
    end

    def argv_or_default(task_argv)
      task_argv = task_argv.to_a.compact
      task_argv.empty? ? @default_argv : task_argv
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
leftovers-0.7.0 lib/leftovers/rake_task.rb
leftovers-0.6.0 lib/leftovers/rake_task.rb
leftovers-0.5.5 lib/leftovers/rake_task.rb
leftovers-0.5.4 lib/leftovers/rake_task.rb
leftovers-0.5.3 lib/leftovers/rake_task.rb
leftovers-0.5.2 lib/leftovers/rake_task.rb
leftovers-0.5.1 lib/leftovers/rake_task.rb
leftovers-0.5.0 lib/leftovers/rake_task.rb
leftovers-0.4.3 lib/leftovers/rake_task.rb
leftovers-0.4.2 lib/leftovers/rake_task.rb
leftovers-0.4.1 lib/leftovers/rake_task.rb
leftovers-0.4.0 lib/leftovers/rake_task.rb