Skip to content

Commit f27590c

Browse files
committed
Split "check_and_format" task into two different files "check" and "write"
1 parent b39b1bf commit f27590c

File tree

5 files changed

+114
-39
lines changed

5 files changed

+114
-39
lines changed

lib/syntax_tree/rake/task.rb renamed to lib/syntax_tree/rake/check_task.rb

+15-12
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,31 @@
55

66
module SyntaxTree
77
module Rake
8-
# A Rake task that runs check and format on a set of source files.
8+
# A Rake task that runs check on a set of source files.
99
#
1010
# Example:
1111
#
12-
# require 'syntax_tree/rake/task'
12+
# require 'syntax_tree/rake/check_task'
1313
#
14-
# SyntaxTree::Rake::Task.new do |t|
14+
# SyntaxTree::Rake::CheckTask.new do |t|
1515
# t.source_files = '{app,config,lib}/**/*.rb'
1616
# end
1717
#
1818
# This will create task that can be run with:
1919
#
20-
# rake syntax_tree:check_and_format
21-
class Task < ::Rake::TaskLib
20+
# rake stree_check
21+
#
22+
class CheckTask < ::Rake::TaskLib
23+
# Name of the task.
24+
# Defaults to :stree_check.
25+
attr_accessor :name
26+
2227
# Glob pattern to match source files.
2328
# Defaults to 'lib/**/*.rb'.
2429
attr_accessor :source_files
2530

26-
def initialize
31+
def initialize(name = :stree_check)
32+
@name = name
2733
@source_files = "lib/**/*.rb"
2834

2935
yield self if block_given?
@@ -33,16 +39,13 @@ def initialize
3339
private
3440

3541
def define_task
36-
desc "Runs syntax_tree over source files"
37-
task(:check_and_format) { run_task }
42+
desc "Runs `stree check` over source files"
43+
task(name) { run_task }
3844
end
3945

4046
def run_task
41-
%w[check format].each do |command|
42-
SyntaxTree::CLI.run([command, source_files].compact)
43-
end
47+
SyntaxTree::CLI.run(["check", source_files].compact)
4448

45-
# TODO: figure this out
4649
# exit($?.exitstatus) if $?&.exited?
4750
end
4851
end

lib/syntax_tree/rake/write_task.rb

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
require "rake"
4+
require "rake/tasklib"
5+
6+
module SyntaxTree
7+
module Rake
8+
# A Rake task that runs format on a set of source files.
9+
#
10+
# Example:
11+
#
12+
# require 'syntax_tree/rake/write_task'
13+
#
14+
# SyntaxTree::Rake::WriteTask.new do |t|
15+
# t.source_files = '{app,config,lib}/**/*.rb'
16+
# end
17+
#
18+
# This will create task that can be run with:
19+
#
20+
# rake stree_write
21+
#
22+
class WriteTask < ::Rake::TaskLib
23+
# Name of the task.
24+
# Defaults to :stree_write.
25+
attr_accessor :name
26+
27+
# Glob pattern to match source files.
28+
# Defaults to 'lib/**/*.rb'.
29+
attr_accessor :source_files
30+
31+
def initialize(name = :stree_write)
32+
@name = name
33+
@source_files = "lib/**/*.rb"
34+
35+
yield self if block_given?
36+
define_task
37+
end
38+
39+
private
40+
41+
def define_task
42+
desc "Runs `stree write` over source files"
43+
task(name) { run_task }
44+
end
45+
46+
def run_task
47+
SyntaxTree::CLI.run(["write", source_files].compact)
48+
49+
# exit($?.exitstatus) if $?&.exited?
50+
end
51+
end
52+
end
53+
end

test/check_task_test.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "test_helper"
4+
require "syntax_tree/rake/check_task"
5+
6+
module SyntaxTree
7+
class CheckTaskTest < Minitest::Test
8+
Invoke = Struct.new(:args)
9+
10+
def test_task
11+
source_files = "{app,config,lib}/**/*.rb"
12+
13+
SyntaxTree::Rake::CheckTask.new { |t| t.source_files = source_files }
14+
15+
invoke = nil
16+
SyntaxTree::CLI.stub(:run, ->(args) { invoke = Invoke.new(args) }) do
17+
::Rake::Task["stree_check"].invoke
18+
end
19+
20+
assert_equal(["check", source_files], invoke.args)
21+
end
22+
end
23+
end

test/task_test.rb

-27
This file was deleted.

test/write_task_test.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "test_helper"
4+
require "syntax_tree/rake/write_task"
5+
6+
module SyntaxTree
7+
class WriteTaskTest < Minitest::Test
8+
Invoke = Struct.new(:args)
9+
10+
def test_task
11+
source_files = "{app,config,lib}/**/*.rb"
12+
13+
SyntaxTree::Rake::WriteTask.new { |t| t.source_files = source_files }
14+
15+
invoke = nil
16+
SyntaxTree::CLI.stub(:run, ->(args) { invoke = Invoke.new(args) }) do
17+
::Rake::Task["stree_write"].invoke
18+
end
19+
20+
assert_equal(["write", source_files], invoke.args)
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)