Skip to content

Commit cff7953

Browse files
committed
Document rake task
1 parent 6d72cfb commit cff7953

File tree

10 files changed

+129
-75
lines changed

10 files changed

+129
-75
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
ruby-version: '3.1'
3939
- name: Check
4040
run: |
41-
bundle exec rake check
41+
bundle exec rake stree:check
4242
bundle exec rubocop
4343
4444
automerge:

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- [#74](https://github.com/ruby-syntax-tree/syntax_tree/pull/74) - Add Rake test to run check and format commands.
12+
913
## [2.5.0] - 2022-05-13
1014

1115
### Added

README.md

+41
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ It is built with only standard library dependencies. It additionally ships with
3838
- [syntaxTree/visualizing](#syntaxtreevisualizing)
3939
- [Plugins](#plugins)
4040
- [Integration](#integration)
41+
- [Rake](#rake)
4142
- [RuboCop](#rubocop)
4243
- [VSCode](#vscode)
4344
- [Contributing](#contributing)
@@ -436,6 +437,46 @@ Below are listed all of the "official" language plugins hosted under the same Gi
436437

437438
Syntax Tree's goal is to seemlessly integrate into your workflow. To this end, it provides a couple of additional tools beyond the CLI and the Ruby library.
438439

440+
### Rake
441+
442+
Syntax Tree ships with the ability to define [rake](https://github.com/ruby/rake) tasks that will trigger runs of the CLI. To define them in your application, add the following configuration to your `Rakefile`:
443+
444+
```ruby
445+
require "syntax_tree/rake_tasks"
446+
SyntaxTree::Rake::CheckTask.new
447+
SyntaxTree::Rake::WriteTask.new
448+
```
449+
450+
These calls will define `rake stree:check` and `rake stree:write` (equivalent to calling `stree check` and `stree write` respectively). You can configure them by either passing arguments to the `new` method or by using a block.
451+
452+
#### `name`
453+
454+
If you'd like to change the default name of the rake task, you can pass that as the first optioon, as in:
455+
456+
```ruby
457+
SyntaxTree::Rake::WriteTask.new(:format)
458+
```
459+
460+
#### `source_files`
461+
462+
If you wanted to configure Syntax Tree to check or write different files than the default (`lib/**/*.rb`), you can set the `source_files` field, as in:
463+
464+
```ruby
465+
SyntaxTree::Rake::WriteTask.new do |t|
466+
t.source_files = FileList[%w[Gemfile Rakefile lib/**/*.rb test/**/*.rb]]
467+
end
468+
```
469+
470+
#### `plugins`
471+
472+
If you're running Syntax Tree with plugins (either your own or the pre-built ones), you can pass that to the `plugins` field, as in:
473+
474+
```ruby
475+
SyntaxTree::Rake::WriteTask.new do |t|
476+
t.plugins = ["plugin/single_quotes"]
477+
end
478+
```
479+
439480
### RuboCop
440481

441482
RuboCop and Syntax Tree serve different purposes, but there is overlap with some of RuboCop's functionality. Syntax Tree provides a RuboCop configuration file to disable rules that are redundant with Syntax Tree. To use this configuration file, add the following snippet to the top of your project's `.rubocop.yml`:

Rakefile

+5-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "bundler/gem_tasks"
44
require "rake/testtask"
5+
require "syntax_tree/rake_tasks"
56

67
Rake::TestTask.new(:test) do |t|
78
t.libs << "test"
@@ -11,24 +12,8 @@ end
1112

1213
task default: :test
1314

14-
FILEPATHS = %w[
15-
Gemfile
16-
Rakefile
17-
syntax_tree.gemspec
18-
lib/**/*.rb
19-
test/*.rb
20-
].freeze
15+
SOURCE_FILES =
16+
FileList[%w[Gemfile Rakefile syntax_tree.gemspec lib/**/*.rb test/*.rb]]
2117

22-
task :syntax_tree do
23-
$:.unshift File.expand_path("lib", __dir__)
24-
require "syntax_tree"
25-
require "syntax_tree/cli"
26-
end
27-
28-
task check: :syntax_tree do
29-
exit SyntaxTree::CLI.run(["check"] + FILEPATHS)
30-
end
31-
32-
task format: :syntax_tree do
33-
exit SyntaxTree::CLI.run(["write"] + FILEPATHS)
34-
end
18+
SyntaxTree::Rake::CheckTask.new { |t| t.source_files = SOURCE_FILES }
19+
SyntaxTree::Rake::WriteTask.new { |t| t.source_files = SOURCE_FILES }

lib/syntax_tree/rake/check_task.rb

+19-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
require "rake"
44
require "rake/tasklib"
55

6+
require "syntax_tree"
7+
require "syntax_tree/cli"
8+
69
module SyntaxTree
710
module Rake
811
# A Rake task that runs check on a set of source files.
@@ -21,16 +24,25 @@ module Rake
2124
#
2225
class CheckTask < ::Rake::TaskLib
2326
# Name of the task.
24-
# Defaults to :stree_check.
27+
# Defaults to :"stree:check".
2528
attr_accessor :name
2629

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

31-
def initialize(name = :stree_check)
34+
# The set of plugins to require.
35+
# Defaults to [].
36+
attr_accessor :plugins
37+
38+
def initialize(
39+
name = :"stree:check",
40+
source_files = ::Rake::FileList["lib/**/*.rb"],
41+
plugins = []
42+
)
3243
@name = name
33-
@source_files = "lib/**/*.rb"
44+
@source_files = source_files
45+
@plugins = plugins
3446

3547
yield self if block_given?
3648
define_task
@@ -44,7 +56,10 @@ def define_task
4456
end
4557

4658
def run_task
47-
SyntaxTree::CLI.run(["check", source_files].compact)
59+
arguments = ["check"]
60+
arguments << "--plugins=#{plugins.join(",")}" if plugins.any?
61+
62+
SyntaxTree::CLI.run(arguments + Array(source_files))
4863
end
4964
end
5065
end

lib/syntax_tree/rake/write_task.rb

+19-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
require "rake"
44
require "rake/tasklib"
55

6+
require "syntax_tree"
7+
require "syntax_tree/cli"
8+
69
module SyntaxTree
710
module Rake
811
# A Rake task that runs format on a set of source files.
@@ -21,16 +24,25 @@ module Rake
2124
#
2225
class WriteTask < ::Rake::TaskLib
2326
# Name of the task.
24-
# Defaults to :stree_write.
27+
# Defaults to :"stree:write".
2528
attr_accessor :name
2629

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

31-
def initialize(name = :stree_write)
34+
# The set of plugins to require.
35+
# Defaults to [].
36+
attr_accessor :plugins
37+
38+
def initialize(
39+
name = :"stree:write",
40+
source_files = ::Rake::FileList["lib/**/*.rb"],
41+
plugins = []
42+
)
3243
@name = name
33-
@source_files = "lib/**/*.rb"
44+
@source_files = source_files
45+
@plugins = plugins
3446

3547
yield self if block_given?
3648
define_task
@@ -44,7 +56,10 @@ def define_task
4456
end
4557

4658
def run_task
47-
SyntaxTree::CLI.run(["write", source_files].compact)
59+
arguments = ["write"]
60+
arguments << "--plugins=#{plugins.join(",")}" if plugins.any?
61+
62+
SyntaxTree::CLI.run(arguments + Array(source_files))
4863
end
4964
end
5065
end

lib/syntax_tree/rake_tasks.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "rake/check_task"
4+
require_relative "rake/write_task"

test/check_task_test.rb

-23
This file was deleted.

test/rake_test.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "test_helper"
4+
require "syntax_tree/rake_tasks"
5+
6+
module SyntaxTree
7+
module Rake
8+
class CheckTaskTest < Minitest::Test
9+
Invoke = Struct.new(:args)
10+
11+
def test_check_task
12+
source_files = "{app,config,lib}/**/*.rb"
13+
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+
23+
def test_write_task
24+
source_files = "{app,config,lib}/**/*.rb"
25+
WriteTask.new { |t| t.source_files = source_files }
26+
27+
invoke = nil
28+
SyntaxTree::CLI.stub(:run, ->(args) { invoke = Invoke.new(args) }) do
29+
::Rake::Task["stree:write"].invoke
30+
end
31+
32+
assert_equal(["write", source_files], invoke.args)
33+
end
34+
end
35+
end
36+
end

test/write_task_test.rb

-23
This file was deleted.

0 commit comments

Comments
 (0)