Sha256: 3608c24e08d492cbb4fac57864b01a7ba30f7752eaa149ab1b6dc56da1dcf16e

Contents?: true

Size: 1.43 KB

Versions: 10

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require "rake"
require "rake/tasklib"

require "syntax_tree"
require "syntax_tree/cli"

module SyntaxTree
  module Rake
    # A Rake task that runs format on a set of source files.
    #
    # Example:
    #
    #   require 'syntax_tree/rake/write_task'
    #
    #   SyntaxTree::Rake::WriteTask.new do |t|
    #     t.source_files = '{app,config,lib}/**/*.rb'
    #   end
    #
    # This will create task that can be run with:
    #
    #   rake stree_write
    #
    class WriteTask < ::Rake::TaskLib
      # Name of the task.
      # Defaults to :"stree:write".
      attr_accessor :name

      # Glob pattern to match source files.
      # Defaults to 'lib/**/*.rb'.
      attr_accessor :source_files

      # The set of plugins to require.
      # Defaults to [].
      attr_accessor :plugins

      def initialize(
        name = :"stree:write",
        source_files = ::Rake::FileList["lib/**/*.rb"],
        plugins = []
      )
        @name = name
        @source_files = source_files
        @plugins = plugins

        yield self if block_given?
        define_task
      end

      private

      def define_task
        desc "Runs `stree write` over source files"
        task(name) { run_task }
      end

      def run_task
        arguments = ["write"]
        arguments << "--plugins=#{plugins.join(",")}" if plugins.any?

        SyntaxTree::CLI.run(arguments + Array(source_files))
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
syntax_tree-3.2.1 lib/syntax_tree/rake/write_task.rb
syntax_tree-3.2.0 lib/syntax_tree/rake/write_task.rb
syntax_tree-3.1.0 lib/syntax_tree/rake/write_task.rb
syntax_tree-3.0.1 lib/syntax_tree/rake/write_task.rb
syntax_tree-3.0.0 lib/syntax_tree/rake/write_task.rb
syntax_tree-2.9.0 lib/syntax_tree/rake/write_task.rb
syntax_tree-2.8.0 lib/syntax_tree/rake/write_task.rb
syntax_tree-2.7.1 lib/syntax_tree/rake/write_task.rb
syntax_tree-2.7.0 lib/syntax_tree/rake/write_task.rb
syntax_tree-2.6.0 lib/syntax_tree/rake/write_task.rb