diff --git a/lib/syntax_tree/rake/check_task.rb b/lib/syntax_tree/rake/check_task.rb new file mode 100644 index 00000000..5fc4ce56 --- /dev/null +++ b/lib/syntax_tree/rake/check_task.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require "rake" +require "rake/tasklib" + +module SyntaxTree + module Rake + # A Rake task that runs check on a set of source files. + # + # Example: + # + # require 'syntax_tree/rake/check_task' + # + # SyntaxTree::Rake::CheckTask.new do |t| + # t.source_files = '{app,config,lib}/**/*.rb' + # end + # + # This will create task that can be run with: + # + # rake stree_check + # + class CheckTask < ::Rake::TaskLib + # Name of the task. + # Defaults to :stree_check. + attr_accessor :name + + # Glob pattern to match source files. + # Defaults to 'lib/**/*.rb'. + attr_accessor :source_files + + def initialize(name = :stree_check) + @name = name + @source_files = "lib/**/*.rb" + + yield self if block_given? + define_task + end + + private + + def define_task + desc "Runs `stree check` over source files" + task(name) { run_task } + end + + def run_task + SyntaxTree::CLI.run(["check", source_files].compact) + end + end + end +end diff --git a/lib/syntax_tree/rake/write_task.rb b/lib/syntax_tree/rake/write_task.rb new file mode 100644 index 00000000..6143d6b9 --- /dev/null +++ b/lib/syntax_tree/rake/write_task.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require "rake" +require "rake/tasklib" + +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 + + def initialize(name = :stree_write) + @name = name + @source_files = "lib/**/*.rb" + + 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 + SyntaxTree::CLI.run(["write", source_files].compact) + end + end + end +end diff --git a/test/check_task_test.rb b/test/check_task_test.rb new file mode 100644 index 00000000..33333241 --- /dev/null +++ b/test/check_task_test.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require_relative "test_helper" +require "syntax_tree/rake/check_task" + +module SyntaxTree + class CheckTaskTest < Minitest::Test + Invoke = Struct.new(:args) + + def test_task + source_files = "{app,config,lib}/**/*.rb" + + SyntaxTree::Rake::CheckTask.new { |t| t.source_files = source_files } + + invoke = nil + SyntaxTree::CLI.stub(:run, ->(args) { invoke = Invoke.new(args) }) do + ::Rake::Task["stree_check"].invoke + end + + assert_equal(["check", source_files], invoke.args) + end + end +end diff --git a/test/write_task_test.rb b/test/write_task_test.rb new file mode 100644 index 00000000..deb5acfd --- /dev/null +++ b/test/write_task_test.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require_relative "test_helper" +require "syntax_tree/rake/write_task" + +module SyntaxTree + class WriteTaskTest < Minitest::Test + Invoke = Struct.new(:args) + + def test_task + source_files = "{app,config,lib}/**/*.rb" + + SyntaxTree::Rake::WriteTask.new { |t| t.source_files = source_files } + + invoke = nil + SyntaxTree::CLI.stub(:run, ->(args) { invoke = Invoke.new(args) }) do + ::Rake::Task["stree_write"].invoke + end + + assert_equal(["write", source_files], invoke.args) + end + end +end