chusaku 1.4.0 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/linting.yml +7 -3
- data/.github/workflows/testing.yml +8 -3
- data/chusaku.gemspec +5 -6
- data/lib/chusaku/routes.rb +66 -14
- data/lib/chusaku/version.rb +1 -1
- data/lib/chusaku.rb +32 -10
- metadata +12 -15
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 91762a5b8314f0486e73067fd338cad5b440ca263d77c90322787ba86f318e58
|
|
4
|
+
data.tar.gz: c9dcc279a4401864a946b1c97115d9f759152166eea11fa633517474529b8f56
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 96bad9906074a576eefd8ec2e1bb30c3a3f6774f42127107feeb2a7414db41e493417bde9f97d8b7dacc32a8f71302abb47c4ffcf3250503e4c26b3e2975de9c
|
|
7
|
+
data.tar.gz: 278543a4dbe3f74fbb1de39ab06ab455896f1f255a9410e52902955180d2d02a4a15e9cce075f6db444acaccecfd1018cc38cba2e117725e1b43ed4d287a199b
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
name: Linting
|
|
2
2
|
on: [pull_request]
|
|
3
3
|
jobs:
|
|
4
|
-
|
|
4
|
+
lint:
|
|
5
5
|
runs-on: ubuntu-latest
|
|
6
6
|
steps:
|
|
7
|
-
-
|
|
7
|
+
- name: Checkout
|
|
8
|
+
uses: actions/checkout@v4
|
|
9
|
+
|
|
8
10
|
- name: Set up Ruby
|
|
9
11
|
uses: ruby/setup-ruby@v1
|
|
10
12
|
with:
|
|
11
|
-
ruby-version:
|
|
13
|
+
ruby-version: 4.0
|
|
14
|
+
bundler: 4
|
|
12
15
|
bundler-cache: true
|
|
16
|
+
|
|
13
17
|
- name: Run Standard
|
|
14
18
|
run: bundle exec standardrb --fail-level A
|
|
@@ -5,13 +5,18 @@ jobs:
|
|
|
5
5
|
runs-on: ubuntu-latest
|
|
6
6
|
strategy:
|
|
7
7
|
matrix:
|
|
8
|
-
ruby-version: ["3.
|
|
8
|
+
ruby-version: ["3.2", "3.3", "3.4", "4.0"]
|
|
9
|
+
|
|
9
10
|
steps:
|
|
10
|
-
-
|
|
11
|
+
- name: Checkout
|
|
12
|
+
uses: actions/checkout@v4
|
|
13
|
+
|
|
11
14
|
- name: Set up Ruby
|
|
12
15
|
uses: ruby/setup-ruby@v1
|
|
13
16
|
with:
|
|
14
17
|
ruby-version: ${{ matrix.ruby-version }}
|
|
18
|
+
bundler: 4
|
|
15
19
|
bundler-cache: true
|
|
20
|
+
|
|
16
21
|
- name: Run tests
|
|
17
|
-
run: bundle exec rake
|
|
22
|
+
run: bundle exec rake test
|
data/chusaku.gemspec
CHANGED
|
@@ -23,8 +23,7 @@ Gem::Specification.new do |spec|
|
|
|
23
23
|
spec.metadata["source_code_uri"] = spec.homepage
|
|
24
24
|
spec.metadata["changelog_uri"] = spec.homepage
|
|
25
25
|
else
|
|
26
|
-
raise "RubyGems 2.0 or newer is required to protect against "
|
|
27
|
-
"public gem pushes."
|
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
|
28
27
|
end
|
|
29
28
|
|
|
30
29
|
# Specify which files should be added to the gem when it is released.
|
|
@@ -39,10 +38,10 @@ Gem::Specification.new do |spec|
|
|
|
39
38
|
spec.executables = "chusaku"
|
|
40
39
|
spec.require_paths = ["lib"]
|
|
41
40
|
|
|
42
|
-
spec.add_development_dependency "bundler", "~>
|
|
43
|
-
spec.add_development_dependency "minitest", "~> 5
|
|
44
|
-
spec.add_development_dependency "rake", "~> 13
|
|
45
|
-
spec.add_development_dependency "
|
|
41
|
+
spec.add_development_dependency "bundler", "~> 4"
|
|
42
|
+
spec.add_development_dependency "minitest", "~> 5"
|
|
43
|
+
spec.add_development_dependency "rake", "~> 13"
|
|
44
|
+
spec.add_development_dependency "standard", "~> 1"
|
|
46
45
|
|
|
47
46
|
spec.add_dependency "railties", ">= 3.0"
|
|
48
47
|
end
|
data/lib/chusaku/routes.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require "active_support/inflector"
|
|
2
|
+
|
|
1
3
|
module Chusaku
|
|
2
4
|
# Handles extracting information about the Rails project's routes.
|
|
3
5
|
class Routes
|
|
@@ -9,16 +11,40 @@ module Chusaku
|
|
|
9
11
|
# {
|
|
10
12
|
# "users" => {
|
|
11
13
|
# "edit" => [
|
|
12
|
-
# {
|
|
14
|
+
# {
|
|
15
|
+
# verb: "GET",
|
|
16
|
+
# path: "/users/:id",
|
|
17
|
+
# name: "edit_user",
|
|
18
|
+
# defaults: {},
|
|
19
|
+
# source_path: "/path/to/users_controller.rb"
|
|
20
|
+
# }
|
|
13
21
|
# ],
|
|
14
22
|
# "update" => [
|
|
15
|
-
# {
|
|
16
|
-
#
|
|
23
|
+
# {
|
|
24
|
+
# verb: "PATCH",
|
|
25
|
+
# path: "/users",
|
|
26
|
+
# name: "edit_user",
|
|
27
|
+
# defaults: {},
|
|
28
|
+
# source_path: "/path/to/users_controller.rb"
|
|
29
|
+
# },
|
|
30
|
+
# {
|
|
31
|
+
# verb: "PUT",
|
|
32
|
+
# path: "/users",
|
|
33
|
+
# name: "edit_user",
|
|
34
|
+
# defaults: {},
|
|
35
|
+
# source_path: "/path/to/users_controller.rb"
|
|
36
|
+
# }
|
|
17
37
|
# ]
|
|
18
38
|
# },
|
|
19
39
|
# "empanadas" => {
|
|
20
40
|
# "create" => [
|
|
21
|
-
# {
|
|
41
|
+
# {
|
|
42
|
+
# verb: "POST",
|
|
43
|
+
# path: "/empanadas",
|
|
44
|
+
# name: nil,
|
|
45
|
+
# defaults: {},
|
|
46
|
+
# source_path: "/path/to/empanadas_controller.rb"
|
|
47
|
+
# }
|
|
22
48
|
# ]
|
|
23
49
|
# }
|
|
24
50
|
# }
|
|
@@ -33,6 +59,12 @@ module Chusaku
|
|
|
33
59
|
|
|
34
60
|
private
|
|
35
61
|
|
|
62
|
+
# Recursively populate the routes hash with information from the given Rails
|
|
63
|
+
# application. Accounts for Rails engines.
|
|
64
|
+
#
|
|
65
|
+
# @param app [Rails::Application] Result of `Rails.application`
|
|
66
|
+
# @param routes [Hash] Collection of all route info
|
|
67
|
+
# @return [void]
|
|
36
68
|
def populate_routes(app, routes)
|
|
37
69
|
app.routes.routes.each do |route|
|
|
38
70
|
if route.app.engine?
|
|
@@ -40,7 +72,7 @@ module Chusaku
|
|
|
40
72
|
next
|
|
41
73
|
end
|
|
42
74
|
|
|
43
|
-
controller, action, defaults = extract_data_from(route)
|
|
75
|
+
controller, action, defaults, source_path = extract_data_from(route)
|
|
44
76
|
routes[controller] ||= {}
|
|
45
77
|
routes[controller][action] ||= []
|
|
46
78
|
|
|
@@ -49,7 +81,8 @@ module Chusaku
|
|
|
49
81
|
routes: routes,
|
|
50
82
|
controller: controller,
|
|
51
83
|
action: action,
|
|
52
|
-
defaults: defaults
|
|
84
|
+
defaults: defaults,
|
|
85
|
+
source_path: source_path
|
|
53
86
|
end
|
|
54
87
|
end
|
|
55
88
|
|
|
@@ -60,11 +93,17 @@ module Chusaku
|
|
|
60
93
|
# @param controller [String] Controller key
|
|
61
94
|
# @param action [String] Action key
|
|
62
95
|
# @param defaults [Hash] Default parameters for route
|
|
96
|
+
# @param source_path [String] Path to controller file
|
|
63
97
|
# @return [void]
|
|
64
|
-
def add_info_for(route:, routes:, controller:, action:, defaults:)
|
|
98
|
+
def add_info_for(route:, routes:, controller:, action:, defaults:, source_path:)
|
|
65
99
|
verbs_for(route).each do |verb|
|
|
66
|
-
routes[controller][action]
|
|
67
|
-
|
|
100
|
+
routes[controller][action].push \
|
|
101
|
+
format(
|
|
102
|
+
route: route,
|
|
103
|
+
verb: verb,
|
|
104
|
+
defaults: defaults,
|
|
105
|
+
source_path: source_path
|
|
106
|
+
)
|
|
68
107
|
routes[controller][action].uniq!
|
|
69
108
|
end
|
|
70
109
|
end
|
|
@@ -88,13 +127,15 @@ module Chusaku
|
|
|
88
127
|
# @param route [ActionDispatch::Journey::Route] Route given by Rails
|
|
89
128
|
# @param verb [String] HTTP verb
|
|
90
129
|
# @param defaults [Hash] Default parameters for route
|
|
130
|
+
# @param source_path [String] Path to controller file
|
|
91
131
|
# @return [Hash] { verb => String, path => String, name => String }
|
|
92
|
-
def format(route:, verb:, defaults:)
|
|
132
|
+
def format(route:, verb:, defaults:, source_path:)
|
|
93
133
|
{
|
|
94
134
|
verb: verb,
|
|
95
135
|
path: route.path.spec.to_s.gsub("(.:format)", ""),
|
|
96
136
|
name: route.name,
|
|
97
|
-
defaults: defaults
|
|
137
|
+
defaults: defaults,
|
|
138
|
+
source_path: source_path
|
|
98
139
|
}
|
|
99
140
|
end
|
|
100
141
|
|
|
@@ -143,16 +184,27 @@ module Chusaku
|
|
|
143
184
|
routes
|
|
144
185
|
end
|
|
145
186
|
|
|
146
|
-
# Given a route, extract the controller
|
|
187
|
+
# Given a route, extract the controller & action strings as well as defaults
|
|
188
|
+
# hash and source path.
|
|
147
189
|
#
|
|
148
190
|
# @param route [ActionDispatch::Journey::Route] Route instance
|
|
149
|
-
# @return [Array<Object>] (String, String, Hash)
|
|
191
|
+
# @return [Array<Object>] (String, String, Hash, String)
|
|
150
192
|
def extract_data_from(route)
|
|
151
193
|
defaults = route.defaults.dup
|
|
152
194
|
controller = defaults.delete(:controller)
|
|
153
195
|
action = defaults.delete(:action)
|
|
154
196
|
|
|
155
|
-
|
|
197
|
+
controller_name = ActiveSupport::Inflector.camelize(ActiveSupport::Inflector.underscore(controller))
|
|
198
|
+
controller_class = controller ? ActiveSupport::Inflector.constantize("#{controller_name}Controller") : nil
|
|
199
|
+
action_method_name = action&.to_sym
|
|
200
|
+
source_path =
|
|
201
|
+
if !action_method_name.nil? && controller_class&.method_defined?(action_method_name)
|
|
202
|
+
controller_class.instance_method(action_method_name).source_location&.[](0)
|
|
203
|
+
else
|
|
204
|
+
""
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
[controller, action, defaults, source_path]
|
|
156
208
|
end
|
|
157
209
|
end
|
|
158
210
|
end
|
data/lib/chusaku/version.rb
CHANGED
data/lib/chusaku.rb
CHANGED
|
@@ -29,14 +29,7 @@ module Chusaku
|
|
|
29
29
|
.new(Rails.root.join(controllers_pattern))
|
|
30
30
|
.exclude(Rails.root.join(exclusion_pattern))
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
next unless controller
|
|
34
|
-
|
|
35
|
-
controller_class = "#{controller.underscore.camelize}Controller".constantize
|
|
36
|
-
action_method_name = actions.keys.first&.to_sym
|
|
37
|
-
next unless !action_method_name.nil? && controller_class.method_defined?(action_method_name)
|
|
38
|
-
|
|
39
|
-
source_path = controller_class.instance_method(action_method_name).source_location&.[](0)
|
|
32
|
+
source_paths_map.each do |source_path, actions|
|
|
40
33
|
next unless controllers_paths.include?(source_path)
|
|
41
34
|
|
|
42
35
|
annotate_file(path: source_path, actions: actions)
|
|
@@ -56,6 +49,34 @@ module Chusaku
|
|
|
56
49
|
|
|
57
50
|
private
|
|
58
51
|
|
|
52
|
+
# Maps source paths to their respective routes.
|
|
53
|
+
#
|
|
54
|
+
# Example output:
|
|
55
|
+
#
|
|
56
|
+
# {
|
|
57
|
+
# "/path/to/users_controller.rb" => {
|
|
58
|
+
# "edit" => [...],
|
|
59
|
+
# "update" => [...]
|
|
60
|
+
# }
|
|
61
|
+
# }
|
|
62
|
+
#
|
|
63
|
+
# @return [Hash] Source paths mapped to their respective routes
|
|
64
|
+
def source_paths_map
|
|
65
|
+
map = {}
|
|
66
|
+
|
|
67
|
+
@routes.each do |controller, actions|
|
|
68
|
+
actions.each do |action, data|
|
|
69
|
+
data.each do |datum|
|
|
70
|
+
map[datum[:source_path]] ||= {}
|
|
71
|
+
map[datum[:source_path]][action] ||= []
|
|
72
|
+
map[datum[:source_path]][action].push(datum)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
map
|
|
78
|
+
end
|
|
79
|
+
|
|
59
80
|
# Adds annotations to the given file.
|
|
60
81
|
#
|
|
61
82
|
# @param path [String] Path to file
|
|
@@ -110,8 +131,9 @@ module Chusaku
|
|
|
110
131
|
# @param path [String] Rails path for route
|
|
111
132
|
# @param name [String] Name used in route helpers
|
|
112
133
|
# @param defaults [Hash] Default parameters for route
|
|
134
|
+
# @param source_path [String] Path to controller file
|
|
113
135
|
# @return [String] "@route <verb> <path> {<defaults>} (<name>)"
|
|
114
|
-
def annotate_route(verb:, path:, name:, defaults:)
|
|
136
|
+
def annotate_route(verb:, path:, name:, defaults:, source_path:)
|
|
115
137
|
annotation = "@route #{verb} #{path}"
|
|
116
138
|
if defaults&.any?
|
|
117
139
|
defaults_str =
|
|
@@ -166,7 +188,7 @@ module Chusaku
|
|
|
166
188
|
#
|
|
167
189
|
# @return [String] 'r' or 'w'
|
|
168
190
|
def file_mode
|
|
169
|
-
File.
|
|
191
|
+
File.method_defined?(:test_write) ? "r" : "w"
|
|
170
192
|
end
|
|
171
193
|
|
|
172
194
|
# Output results to user.
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: chusaku
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.4.
|
|
4
|
+
version: 1.4.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Nishiki Liu
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: bundler
|
|
@@ -16,56 +15,56 @@ dependencies:
|
|
|
16
15
|
requirements:
|
|
17
16
|
- - "~>"
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
18
|
+
version: '4'
|
|
20
19
|
type: :development
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
23
|
- - "~>"
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
25
|
+
version: '4'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
27
|
name: minitest
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
30
29
|
requirements:
|
|
31
30
|
- - "~>"
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '5
|
|
32
|
+
version: '5'
|
|
34
33
|
type: :development
|
|
35
34
|
prerelease: false
|
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
37
|
- - "~>"
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '5
|
|
39
|
+
version: '5'
|
|
41
40
|
- !ruby/object:Gem::Dependency
|
|
42
41
|
name: rake
|
|
43
42
|
requirement: !ruby/object:Gem::Requirement
|
|
44
43
|
requirements:
|
|
45
44
|
- - "~>"
|
|
46
45
|
- !ruby/object:Gem::Version
|
|
47
|
-
version: '13
|
|
46
|
+
version: '13'
|
|
48
47
|
type: :development
|
|
49
48
|
prerelease: false
|
|
50
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
50
|
requirements:
|
|
52
51
|
- - "~>"
|
|
53
52
|
- !ruby/object:Gem::Version
|
|
54
|
-
version: '13
|
|
53
|
+
version: '13'
|
|
55
54
|
- !ruby/object:Gem::Dependency
|
|
56
|
-
name:
|
|
55
|
+
name: standard
|
|
57
56
|
requirement: !ruby/object:Gem::Requirement
|
|
58
57
|
requirements:
|
|
59
58
|
- - "~>"
|
|
60
59
|
- !ruby/object:Gem::Version
|
|
61
|
-
version: '1
|
|
60
|
+
version: '1'
|
|
62
61
|
type: :development
|
|
63
62
|
prerelease: false
|
|
64
63
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
64
|
requirements:
|
|
66
65
|
- - "~>"
|
|
67
66
|
- !ruby/object:Gem::Version
|
|
68
|
-
version: '1
|
|
67
|
+
version: '1'
|
|
69
68
|
- !ruby/object:Gem::Dependency
|
|
70
69
|
name: railties
|
|
71
70
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -114,7 +113,6 @@ metadata:
|
|
|
114
113
|
homepage_uri: https://github.com/nshki/chusaku
|
|
115
114
|
source_code_uri: https://github.com/nshki/chusaku
|
|
116
115
|
changelog_uri: https://github.com/nshki/chusaku
|
|
117
|
-
post_install_message:
|
|
118
116
|
rdoc_options: []
|
|
119
117
|
require_paths:
|
|
120
118
|
- lib
|
|
@@ -129,8 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
129
127
|
- !ruby/object:Gem::Version
|
|
130
128
|
version: '0'
|
|
131
129
|
requirements: []
|
|
132
|
-
rubygems_version:
|
|
133
|
-
signing_key:
|
|
130
|
+
rubygems_version: 4.0.3
|
|
134
131
|
specification_version: 4
|
|
135
132
|
summary: Annotate your Rails controllers with route info.
|
|
136
133
|
test_files: []
|