ordit 0.1.0 → 0.1.2

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: 573d6f3d0536da9b396366f3c794b28b5c74161792ed2b107b9da4b4edc5c268
4
- data.tar.gz: a806b40aaefc894a2cfea8caebc4f2c94f19ce87dcab0be01671bf9fd00f8c74
3
+ metadata.gz: fdefa2e20e2bd7fa34e8c898fc6e54e934a35e6c9dc3eb5e9ce9f22bf0403b87
4
+ data.tar.gz: 704929dfdbee426f4d358512b18d217f2ddf1521f8e376cdb839993066b229a6
5
5
  SHA512:
6
- metadata.gz: 4c3a5d96f70d1423f748db50052b37767eec553b5b6577ba18866040cb25d78457835aacbc478ed6169bcf379dea64ed4f743426144ce7e3288a3e854f9097ce
7
- data.tar.gz: 0e30d715b13e47c0dd7c3362ee5917f6b1dfd40e9bdbffe1b590352af65791fd53383ddf222ebf982e41ae28d90ca3852557ff83fa03e873f9d44a757d15e73b
6
+ metadata.gz: 980a3d0f8ba5d373577b55594350962f784775de06f2c0b288271929a71a68998799fc60b1611293a68ce15102168923377d0dccde9699f2348775c5cf4dc180
7
+ data.tar.gz: 3e83e18491bb40508a8f65a8210b29e1c31e94c4e87c67c64c033ed5a3682768c39cae36c794363d37a8d2bdb3f3a457e161a52001584e0b1eabd8349891a0cc
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.2] - 2024-12-12
4
+
5
+ - refactor, clean up
6
+
7
+
8
+ ## [0.1.1] - 2024-12-12
9
+
10
+ - Fix namespace issue in rake task
11
+
3
12
  ## [0.1.0] - 2024-12-12
4
13
 
5
14
  - Initial release
data/README.md CHANGED
@@ -56,13 +56,13 @@ Example output:
56
56
  Find all uses of a specific controller:
57
57
 
58
58
  ```bash
59
- rails audit:scan[controller_name]
59
+ rails ordit:scan[controller_name]
60
60
  ```
61
61
 
62
62
  Example:
63
63
  ```bash
64
- rails audit:scan[products]
65
- rails audit:scan[users--name] # For namespaced controllers
64
+ rails ordit:scan[products]
65
+ rails ordit:scan[users--name] # For namespaced controllers
66
66
  ```
67
67
 
68
68
  ### Configuration
data/lib/ordit/auditor.rb CHANGED
@@ -2,27 +2,27 @@
2
2
 
3
3
  module Ordit
4
4
  class Auditor
5
- def initialize(config = Ordit.configuration)
6
- @config = config
5
+ def self.run
6
+ new.run
7
7
  end
8
8
 
9
- def audit
10
- defined_controllers = find_defined_controllers
11
- used_controllers = find_used_controllers
12
-
13
- AuditResult.new(
14
- defined_controllers: defined_controllers,
15
- used_controllers: used_controllers,
16
- controller_locations: find_controller_locations,
17
- usage_locations: find_usage_locations
9
+ def run
10
+ ResultGenerator.new(
11
+ defined_controllers:,
12
+ used_controllers:,
13
+ controller_locations:,
14
+ usage_locations:
18
15
  )
19
16
  end
20
17
 
21
18
  private
22
19
 
23
- def find_defined_controllers
24
- controllers = Set.new
25
- @config.controller_paths.each do |path|
20
+ def config
21
+ @config ||= Ordit.configuration
22
+ end
23
+
24
+ def defined_controllers
25
+ config.controller_paths.each_with_object(Set.new) do |path, controllers|
26
26
  Dir.glob(path.to_s).each do |file|
27
27
  # Extract relative path from controllers directory
28
28
  full_path = Pathname.new(file)
@@ -32,6 +32,8 @@ module Ordit
32
32
 
33
33
  # Get path components after 'controllers'
34
34
  controller_path = full_path.each_filename.to_a[(controllers_dir + 1)..]
35
+ next unless controller_path[-1].include?('_controller')
36
+
35
37
  # Remove _controller.js from the last component
36
38
  controller_path[-1] = controller_path[-1].sub(/_controller\.(js|ts)$/, "")
37
39
  # Join with -- for namespacing and convert underscores to hyphens
@@ -39,17 +41,10 @@ module Ordit
39
41
  controllers << name
40
42
  end
41
43
  end
42
- controllers
43
44
  end
44
45
 
45
- def find_used_controllers
46
- controllers = Set.new
47
- patterns = [
48
- /data-controller=["']([^"']+)["']/, # HTML attribute syntax
49
- /data:\s*{\s*(?:controller:|:controller\s*=>)\s*["']([^"']+)["']/ # Both hash syntaxes
50
- ]
51
-
52
- @config.view_paths.each do |path|
46
+ def used_controllers
47
+ config.view_paths.each_with_object(Set.new) do |path, controllers|
53
48
  Dir.glob(path.to_s).each do |file|
54
49
  content = File.read(file)
55
50
  patterns.each do |pattern|
@@ -64,12 +59,10 @@ module Ordit
64
59
  end
65
60
  end
66
61
  end
67
- controllers
68
62
  end
69
63
 
70
- def find_controller_locations
71
- locations = {}
72
- @config.controller_paths.each do |path_pattern|
64
+ def controller_locations
65
+ config.controller_paths.each_with_object({}) do |path_pattern, locations|
73
66
  Dir.glob(path_pattern).each do |file|
74
67
  relative_path = Pathname.new(file).relative_path_from(Dir.pwd)
75
68
  controller_path = relative_path.to_s.gsub(%r{^app/javascript/controllers/|_controller\.(js|ts)$}, "")
@@ -77,18 +70,10 @@ module Ordit
77
70
  locations[name] = relative_path
78
71
  end
79
72
  end
80
- locations
81
73
  end
82
74
 
83
- def find_usage_locations
84
- locations = Hash.new { |h, k| h[k] = {} }
85
- patterns = [
86
- /data-controller=["']([^"']+)["']/,
87
- /data:\s*{(?:[^}]*\s)?controller:\s*["']([^"']+)["']/,
88
- /data:\s*{(?:[^}]*\s)?controller\s*=>\s*["']([^"']+)["']/
89
- ]
90
-
91
- @config.view_paths.each do |path_pattern|
75
+ def usage_locations
76
+ config.view_paths.each_with_object(Hash.new { |h, k| h[k] = {} }) do |path_pattern, locations|
92
77
  Dir.glob(path_pattern).each do |file|
93
78
  File.readlines(file).each_with_index do |line, index|
94
79
  patterns.each do |pattern|
@@ -103,73 +88,16 @@ module Ordit
103
88
  end
104
89
  end
105
90
  end
106
- locations
107
- end
108
- end
109
-
110
- class AuditResult
111
- attr_reader :defined_controllers, :used_controllers,
112
- :controller_locations, :usage_locations
113
-
114
- def initialize(defined_controllers:, used_controllers:,
115
- controller_locations:, usage_locations:)
116
- @defined_controllers = defined_controllers
117
- @used_controllers = used_controllers
118
- @controller_locations = controller_locations
119
- @usage_locations = usage_locations
120
- end
121
-
122
- def unused_controllers
123
- defined_controllers - used_controllers
124
- end
125
-
126
- def undefined_controllers
127
- used_controllers - defined_controllers
128
91
  end
129
92
 
130
- def active_controllers
131
- defined_controllers & used_controllers
132
- end
133
-
134
- def to_console
135
- puts "\n📊 Stimulus Controller Audit\n"
136
-
137
- if unused_controllers.any?
138
- puts "\n❌ Defined but unused controllers:"
139
- unused_controllers.sort.each do |controller|
140
- puts " #{controller}"
141
- puts " └─ #{controller_locations[controller]}"
142
- end
143
- end
144
-
145
- if undefined_controllers.any?
146
- puts "\n⚠️ Used but undefined controllers:"
147
- undefined_controllers.sort.each do |controller|
148
- puts " #{controller}"
149
- usage_locations[controller].each do |file, lines|
150
- puts " └─ #{file} (lines: #{lines.join(", ")})"
151
- end
152
- end
153
- end
154
-
155
- if active_controllers.any?
156
- puts "\n✅ Active controllers:"
157
- active_controllers.sort.each do |controller|
158
- puts " #{controller}"
159
- puts " └─ Defined in: #{controller_locations[controller]}"
160
- puts " └─ Used in:"
161
- usage_locations[controller].each do |file, lines|
162
- puts " └─ #{file} (lines: #{lines.join(", ")})"
163
- end
164
- end
165
- end
166
-
167
- puts "\n📈 Summary:"
168
- puts " Total controllers defined: #{defined_controllers.size}"
169
- puts " Total controllers in use: #{used_controllers.size}"
170
- puts " Unused controllers: #{unused_controllers.size}"
171
- puts " Undefined controllers: #{undefined_controllers.size}"
172
- puts " Properly paired: #{active_controllers.size}"
93
+ def patterns
94
+ @patterns ||= [
95
+ /data-controller=["']([^"']+)["']/, # HTML attribute syntax
96
+ # Ruby 1.9+ hash syntax - covers both with and without symbol prefix
97
+ /data:\s*{(?:[^}]*\s)?(?::)?controller:\s*["']([^"']+)["']/,
98
+ # Hash rocket syntax - covers both with and without symbol prefix
99
+ /data:\s*{(?:[^}]*\s)?(?::)?controller\s*=>\s*["']([^"']+)["']/
100
+ ]
173
101
  end
174
102
  end
175
103
  end
@@ -0,0 +1,67 @@
1
+ module Ordit
2
+ class ResultGenerator
3
+ attr_reader :defined_controllers, :used_controllers,
4
+ :controller_locations, :usage_locations
5
+
6
+ def initialize(defined_controllers:, used_controllers:,
7
+ controller_locations:, usage_locations:)
8
+ @defined_controllers = defined_controllers
9
+ @used_controllers = used_controllers
10
+ @controller_locations = controller_locations
11
+ @usage_locations = usage_locations
12
+ end
13
+
14
+ def unused_controllers
15
+ defined_controllers - used_controllers
16
+ end
17
+
18
+ def undefined_controllers
19
+ used_controllers - defined_controllers
20
+ end
21
+
22
+ def active_controllers
23
+ defined_controllers & used_controllers
24
+ end
25
+
26
+ def to_console
27
+ puts "\n📊 Stimulus Controller Audit\n"
28
+
29
+ if unused_controllers.any?
30
+ puts "\n❌ Defined but unused controllers:"
31
+ unused_controllers.sort.each do |controller|
32
+ puts " #{controller}"
33
+ puts " └─ #{controller_locations[controller]}"
34
+ end
35
+ end
36
+
37
+ if undefined_controllers.any?
38
+ puts "\n⚠️ Used but undefined controllers:"
39
+ undefined_controllers.sort.each do |controller|
40
+ puts " #{controller}"
41
+ usage_locations[controller].each do |file, lines|
42
+ puts " └─ #{file} (lines: #{lines.join(", ")})"
43
+ end
44
+ end
45
+ end
46
+
47
+ if active_controllers.any?
48
+ puts "\n✅ Active controllers:"
49
+ active_controllers.sort.each do |controller|
50
+ puts " #{controller}"
51
+ puts " └─ Defined in: #{controller_locations[controller]}"
52
+ puts " └─ Used in:"
53
+ usage_locations[controller].each do |file, lines|
54
+ puts " └─ #{file} (lines: #{lines.join(", ")})"
55
+ end
56
+ end
57
+ end
58
+
59
+ puts "\n📈 Summary:"
60
+ puts " Total controllers defined: #{defined_controllers.size}"
61
+ puts " Total controllers in use: #{used_controllers.size}"
62
+ puts " Unused controllers: #{unused_controllers.size}"
63
+ puts " Undefined controllers: #{undefined_controllers.size}"
64
+ puts " Properly paired: #{active_controllers.size}"
65
+ end
66
+ end
67
+ end
data/lib/ordit/scanner.rb CHANGED
@@ -2,25 +2,22 @@
2
2
 
3
3
  module Ordit
4
4
  class Scanner
5
- def initialize(config = Ordit.configuration)
6
- @config = config
5
+ def self.run(controller)
6
+ new(controller).run
7
7
  end
8
8
 
9
- def scan(controller)
10
- matches = find_matches(controller)
11
- print_results(controller, matches)
12
- end
9
+ attr_reader :controller
13
10
 
14
- private
11
+ def initialize(controller)
12
+ @controller = controller
13
+ end
15
14
 
16
- def find_matches(controller)
17
- matches = []
18
- patterns = [
19
- /data-controller=["'](?:[^"']*\s)?#{Regexp.escape(controller)}(?:\s[^"']*)?["']/, # HTML attribute
20
- /data:\s*{\s*(?:controller:|:controller\s*=>)\s*["'](?:[^"']*\s)?#{Regexp.escape(controller)}(?:\s[^"']*)?["']/ # Both hash syntaxes
21
- ]
15
+ def run
16
+ print
17
+ end
22
18
 
23
- @config.view_paths.each do |path|
19
+ def results
20
+ config.view_paths.each_with_object([]) do |path, matches|
24
21
  Dir.glob(path.to_s).each do |file|
25
22
  content = File.readlines(file)
26
23
  content.each_with_index do |line, index|
@@ -34,19 +31,31 @@ module Ordit
34
31
  end
35
32
  end
36
33
  end
37
- matches
38
34
  end
39
35
 
40
- def print_results(controller, matches)
36
+ private
37
+
38
+ def config
39
+ @config ||= Ordit.configuration
40
+ end
41
+
42
+ def patterns
43
+ @patterns ||= [
44
+ /data-controller=["'](?:[^"']*\s)?#{Regexp.escape(controller)}(?:\s[^"']*)?["']/, # HTML attribute
45
+ /data:\s*{\s*(?:controller:|:controller\s*=>)\s*["'](?:[^"']*\s)?#{Regexp.escape(controller)}(?:\s[^"']*)?["']/ # Both hash syntaxes
46
+ ]
47
+ end
48
+
49
+ def print
41
50
  puts "\nSearching for stimulus controller: '#{controller}'\n\n"
42
51
 
43
- if matches.empty?
52
+ if results.empty?
44
53
  puts "No matches found."
45
54
  return
46
55
  end
47
56
 
48
57
  current_file = nil
49
- matches.each do |match|
58
+ results.each do |match|
50
59
  if current_file != match[:file]
51
60
  puts "📁 #{match[:file]}"
52
61
  current_file = match[:file]
data/lib/ordit/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ordit
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/ordit.rb CHANGED
@@ -6,6 +6,7 @@ require_relative "ordit/version"
6
6
  require_relative "ordit/configuration"
7
7
  require_relative "ordit/auditor"
8
8
  require_relative "ordit/scanner"
9
+ require_relative "ordit/result_generator"
9
10
 
10
11
  if defined?(Rails)
11
12
  require "rails"
data/lib/tasks/ordit.rake CHANGED
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- namespace :audit do
3
+ namespace :ordit do
4
4
  desc "Audit Stimulus controllers usage and find orphaned controllers"
5
5
  task stimulus: :environment do
6
- StimulusAudit::Auditor.new.audit.to_console
6
+ Ordit::Auditor.run
7
7
  end
8
8
 
9
9
  desc "Scan files for stimulus controller usage (e.g., rake audit:scan[products])"
@@ -11,10 +11,10 @@ namespace :audit do
11
11
  controller = args[:controller]
12
12
 
13
13
  if controller.nil? || controller.empty?
14
- puts "Please provide a controller name: rake audit:scan[controller_name]"
14
+ puts "Please provide a controller name: rake ordit:scan[controller_name]"
15
15
  next
16
16
  end
17
17
 
18
- StimulusAudit::Scanner.new.scan(controller)
18
+ Ordit::Scanner.run(controller)
19
19
  end
20
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ordit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toby
@@ -53,6 +53,7 @@ files:
53
53
  - lib/ordit/auditor.rb
54
54
  - lib/ordit/configuration.rb
55
55
  - lib/ordit/railtie.rb
56
+ - lib/ordit/result_generator.rb
56
57
  - lib/ordit/scanner.rb
57
58
  - lib/ordit/version.rb
58
59
  - lib/tasks/ordit.rake