chusaku 1.0.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8295890eb74a89ebc38a8651d4a5d5c8f87112a7336658e8dea1117d525a1a1a
4
- data.tar.gz: 6cb5e53d089e0996c0b6b7e9e0b7128902fa60bc54c3f458d737e56e5c5a7a2c
3
+ metadata.gz: f73db691fb40f399f999f495736dca02bd8c372934aa4122824d086a723d0479
4
+ data.tar.gz: dea436d47f3101f5d3112c3722b04c00b317fc32c7ad3ee2c2594ca12fbe3c01
5
5
  SHA512:
6
- metadata.gz: 885022690966e86943484029fc383572ea254487d18f4b9f43ac1fa225e99b5cfd75f8e7f1787929cf7bbccae872ce63149a260ec5c5ef00f8e1e61added54db
7
- data.tar.gz: 2d6151abb44a539dbd662af25f8f961a21ec72c2bd012268eff9a749da4affbe02589f38036a3926dadb430340c6fa4d84a2ceff052e4c1ca1045723bd1b8600
6
+ metadata.gz: 03bff26c3f0a2de5ed0b4c64feb3c2a198c4704f53f3683d5bbcf0699f2c36081672324ea871ed664836d0a6d09f9314b1b4c23d886885597c7e9f03bcb13821
7
+ data.tar.gz: 8d4def4efa795ac278608c7d9c8f19dd361e361b4e1ff4da7a9434fa85d865acb3de28e183d67f74bf244023220ad84464735da275516a236576c29dde9d9d64
@@ -6,9 +6,9 @@ jobs:
6
6
  steps:
7
7
  - uses: actions/checkout@v2
8
8
  - name: Set up Ruby
9
- uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
9
+ uses: ruby/setup-ruby@v1
10
10
  with:
11
- ruby-version: 3.0.0
11
+ ruby-version: 3.3
12
12
  bundler-cache: true
13
13
  - name: Run Standard
14
14
  run: bundle exec standardrb --fail-level A
@@ -5,11 +5,11 @@ jobs:
5
5
  runs-on: ubuntu-latest
6
6
  strategy:
7
7
  matrix:
8
- ruby-version: ["2.6", "2.7", "3.0"]
8
+ ruby-version: ["3.1", "3.2", "3.3"]
9
9
  steps:
10
10
  - uses: actions/checkout@v2
11
11
  - name: Set up Ruby
12
- uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
12
+ uses: ruby/setup-ruby@v1
13
13
  with:
14
14
  ruby-version: ${{ matrix.ruby-version }}
15
15
  bundler-cache: true
data/.gitignore CHANGED
@@ -7,4 +7,5 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
 
10
+ .ruby-version
10
11
  Gemfile.lock
data/README.md CHANGED
@@ -26,16 +26,14 @@ Add this line to your Rails application's Gemfile:
26
26
 
27
27
  ```ruby
28
28
  group :development do
29
- # ...
30
29
  gem "chusaku", require: false
31
- # ...
32
30
  end
33
31
  ```
34
32
 
35
33
  And then execute:
36
34
 
37
- ```
38
- $ bundle install
35
+ ```sh
36
+ bundle install
39
37
  ```
40
38
 
41
39
 
@@ -43,8 +41,8 @@ $ bundle install
43
41
 
44
42
  From the root of your Rails application, run:
45
43
 
46
- ```
47
- $ bundle exec chusaku
44
+ ```sh
45
+ bundle exec chusaku
48
46
  ```
49
47
 
50
48
  Chusaku has some flags available for use as well:
@@ -52,25 +50,40 @@ Chusaku has some flags available for use as well:
52
50
  ```
53
51
  $ bundle exec chusaku --help
54
52
  Usage: chusaku [options]
55
- --dry-run Run without file modifications
56
- --exit-with-error-on-annotation
57
- Fail if any file was annotated
58
- --verbose Print all annotations
59
- -v, --version Show Chusaku version number and quit
60
- -h, --help Show this help message and quit
53
+ --dry-run Run without file modifications
54
+ --exit-with-error-on-annotation Fail if any file was annotated
55
+ -c, --controllers-pattern=GLOB Specify alternative controller files glob pattern
56
+ --verbose Print all annotated files
57
+ -v, --version Show Chusaku version number and quit
58
+ -h, --help Show this help message and quit
59
+ ```
60
+
61
+ ### Rake usage
62
+
63
+ If you'd like to use Chusaku as a Rake task, add the following line to your `Rakefile`:
64
+
65
+ ```ruby
66
+ require "chusaku"
67
+ Chusaku.load_tasks
68
+ ```
69
+
70
+ This will then allow you to call:
71
+
72
+ ```sh
73
+ bin/rake chusaku
74
+ ```
75
+
76
+ To pass flags, pass them like you would from the CLI executable:
77
+
78
+ ```sh
79
+ bin/rake chusaku -- --dry-run --exit-with-error-on-annotation
61
80
  ```
62
81
 
63
82
 
64
83
  ## Development
65
84
 
66
- Read the blog post explaining how the gem works at a high level:
67
- https://nshki.com/chusaku-a-controller-annotation-gem.
85
+ Read the blog post explaining how the gem works at a high level: https://nshki.com/chusaku-a-controller-annotation-gem.
68
86
 
69
- After checking out the repo, run `bin/setup` to install dependencies. Then, run
70
- `bundle exec rake test` to run the tests. You can also run `bin/console` for an
71
- interactive prompt that will allow you to experiment.
87
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
72
88
 
73
- To release a new version, update the version number in `version.rb`, and then
74
- run `bundle exec rake release`, which will create a git tag for the version,
75
- git commits and tags, and push the `.gem` file to
76
- [rubygems.org](https://rubygems.org).
89
+ To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
data/lib/chusaku/cli.rb CHANGED
@@ -52,8 +52,10 @@ module Chusaku
52
52
  def optparser
53
53
  OptionParser.new do |opts|
54
54
  opts.banner = "Usage: chusaku [options]"
55
+ opts.set_summary_width(35)
55
56
  add_dry_run_flag(opts)
56
57
  add_error_on_annotation_flag(opts)
58
+ add_controllers_pattern_flag(opts)
57
59
  add_verbose_flag(opts)
58
60
  add_version_flag(opts)
59
61
  add_help_flag(opts)
@@ -80,12 +82,22 @@ module Chusaku
80
82
  end
81
83
  end
82
84
 
85
+ # Adds `--controllers-pattern` flag.
86
+ #
87
+ # @param opts [OptionParser] OptionParser instance
88
+ # @return [void]
89
+ def add_controllers_pattern_flag(opts)
90
+ opts.on("-c", "--controllers-pattern", "=GLOB", "Specify alternative controller files glob pattern") do |value|
91
+ @options[:controllers_pattern] = value
92
+ end
93
+ end
94
+
83
95
  # Adds `--verbose` flag.
84
96
  #
85
97
  # @param opts [OptionParser] OptionParser instance
86
98
  # @return [void]
87
99
  def add_verbose_flag(opts)
88
- opts.on("--verbose", "Print all annotations") do
100
+ opts.on("--verbose", "Print all annotated files") do
89
101
  @options[:verbose] = true
90
102
  end
91
103
  end
@@ -1,3 +1,3 @@
1
1
  module Chusaku
2
- VERSION = "1.0.1"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/chusaku.rb CHANGED
@@ -18,9 +18,8 @@ module Chusaku
18
18
  def call(flags = {})
19
19
  @flags = flags
20
20
  @routes = Chusaku::Routes.call
21
- @changes = []
22
21
  @changed_files = []
23
- controllers_pattern = "app/controllers/**/*_controller.rb"
22
+ controllers_pattern = @flags[:controllers_pattern] || "app/controllers/**/*_controller.rb"
24
23
 
25
24
  Dir.glob(Rails.root.join(controllers_pattern)).each do |path|
26
25
  controller = %r{controllers/(.*)_controller\.rb}.match(path)[1]
@@ -33,6 +32,15 @@ module Chusaku
33
32
  output_results
34
33
  end
35
34
 
35
+ # Load Rake tasks for Chusaku. Should be called in your project's `Rakefile`.
36
+ #
37
+ # @return [void]
38
+ def load_tasks
39
+ Dir[File.join(File.dirname(__FILE__), "tasks", "**/*.rake")].each do |task|
40
+ load(task)
41
+ end
42
+ end
43
+
36
44
  private
37
45
 
38
46
  # Adds annotations to the given file.
@@ -44,46 +52,18 @@ module Chusaku
44
52
  def annotate_file(path:, controller:, actions:)
45
53
  parsed_file = Chusaku::Parser.call(path: path, actions: actions)
46
54
  parsed_file[:groups].each_cons(2) do |prev, curr|
47
- record_change(group: prev, type: :clean, path: path)
55
+ clean_group(prev)
48
56
  next unless curr[:type] == :action
49
57
 
50
58
  route_data = @routes[controller][curr[:action]]
51
59
  next unless route_data.any?
52
60
 
53
- record_change(group: curr, type: :annotate, route_data: route_data, path: path)
61
+ annotate_group(group: curr, route_data: route_data)
54
62
  end
55
63
 
56
64
  write_to_file(path: path, parsed_file: parsed_file)
57
65
  end
58
66
 
59
- # Clean or annotate a group and track the group as changed if applicable.
60
- #
61
- # @param group [Hash] { type => Symbol, body => String }
62
- # @param type [Symbol] [:clean, :annotate]
63
- # @param path [String] File path
64
- # @param route_data [Array<Hash>] [{
65
- # verb: String,
66
- # path: String,
67
- # name: String }]
68
- # @return [void]
69
- def record_change(group:, type:, path:, route_data: [])
70
- old_body = group[:body]
71
-
72
- case type
73
- when :clean
74
- clean_group(group)
75
- when :annotate
76
- annotate_group(group: group, route_data: route_data)
77
- end
78
- return if old_body == group[:body]
79
-
80
- @changes.push \
81
- old_body: old_body,
82
- new_body: group[:body],
83
- path: path,
84
- line_number: group[:line_number]
85
- end
86
-
87
67
  # Given a parsed group, clean out its contents.
88
68
  #
89
69
  # @param group [Hash] { type => Symbol, body => String }
@@ -191,36 +171,22 @@ module Chusaku
191
171
  #
192
172
  # @return [String] Copy to be outputted to user
193
173
  def output_copy
194
- return "Nothing to annotate." if @changed_files.empty?
174
+ return "Controller files unchanged." if @changed_files.empty?
195
175
 
196
176
  copy = changes_copy
197
- copy += "\nChusaku has finished running."
177
+ copy += "Chusaku has finished running."
198
178
  copy += "\nThis was a dry run so no files were changed." if @flags.include?(:dry)
199
179
  copy += "\nExited with status code 1." if @flags.include?(:error_on_annotation)
200
180
  copy
201
181
  end
202
182
 
203
- # Returns the copy for recorded changes if `--verbose` flag is passed.
183
+ # Returns the copy for changed files if `--verbose` flag is passed.
204
184
  #
205
- # @return [String] Copy of recorded changes
185
+ # @return [String] Copy for changed files
206
186
  def changes_copy
207
187
  return "" unless @flags.include?(:verbose)
208
188
 
209
- @changes.map do |change|
210
- <<~CHANGE_OUTPUT
211
- [#{change[:path]}:#{change[:line_number]}]
212
-
213
- Before:
214
- ```ruby
215
- #{change[:old_body].chomp}
216
- ```
217
-
218
- After:
219
- ```ruby
220
- #{change[:new_body].chomp}
221
- ```
222
- CHANGE_OUTPUT
223
- end.join("\n")
189
+ @changed_files.map { |file| "Annotated #{file}" }.join("\n") + "\n"
224
190
  end
225
191
  end
226
192
  end
@@ -0,0 +1,8 @@
1
+ chusaku_lib = File.expand_path(File.dirname(__FILE__, 2))
2
+
3
+ desc "Add route annotations to your Rails actions"
4
+ task chusaku: :environment do
5
+ require "#{chusaku_lib}/chusaku/cli"
6
+
7
+ Chusaku::CLI.new.call(ARGV[2...] || [])
8
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chusaku
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nishiki Liu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-08 00:00:00.000000000 Z
11
+ date: 2024-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -105,6 +105,7 @@ files:
105
105
  - lib/chusaku/parser.rb
106
106
  - lib/chusaku/routes.rb
107
107
  - lib/chusaku/version.rb
108
+ - lib/tasks/chusaku.rake
108
109
  homepage: https://github.com/nshki/chusaku
109
110
  licenses:
110
111
  - MIT
@@ -128,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
129
  - !ruby/object:Gem::Version
129
130
  version: '0'
130
131
  requirements: []
131
- rubygems_version: 3.4.17
132
+ rubygems_version: 3.5.1
132
133
  signing_key:
133
134
  specification_version: 4
134
135
  summary: Annotate your Rails controllers with route info.