deadweight_rails 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8f891d7c0bea2ab0c695d2a967320a65fd7a217ee60ef61afb11aae767626c56
4
+ data.tar.gz: 2295aae3602c2a1b502e60b733fbf200f612ad0fcdcb691a94ae662b76507d54
5
+ SHA512:
6
+ metadata.gz: '08ced7803426dce07aac21d80ad7bb4350b51d354c80d9342c8cac78bb1ec71676a70a6f1d2e7e40217da0bceb700eb131acd547a1484b1130bef18f5a7b6a63'
7
+ data.tar.gz: 20ffa08067e5998bf8d3636f48e81fd089462cdcfe341723756dafb410124c83fd3f78bf61a468f680478725b52235faa2f48de3893488a28dfdc3c481296b09
@@ -0,0 +1,26 @@
1
+ name: Ruby CI
2
+
3
+ on:
4
+ push:
5
+ branches: [ "main" ]
6
+ pull_request:
7
+ branches: [ "main" ]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@v3
15
+
16
+ - name: Set up Ruby
17
+ uses: ruby/setup-ruby@78c01b705fd9d5ad960d432d3a0cfa341d50e410 # v1.179.1
18
+ with:
19
+ ruby-version: 3.2.0
20
+ bundler-cache: true
21
+
22
+ - name: Install dependencies
23
+ run: bundle install
24
+
25
+ - name: Run RSpec tests
26
+ run: bundle exec rspec
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 mrmalvi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # DeadweightRails
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/deadweight_rails.svg)](https://badge.fury.io/rb/deadweight_rails)
4
+
5
+ DeadweightRails scans your Rails project for **unused assets and Ruby code**, helping you reduce bundle size, improve performance, and clean your codebase.
6
+
7
+ ---
8
+
9
+ ## Features
10
+
11
+ - Detect **unused CSS and JS** in Rails asset pipeline
12
+ - Detect **unused Ruby methods**
13
+ - Generate a **report** in terminal with colored output
14
+ - Works with standard Rails directories (`app/assets`, `app/views`, `app/models`)
15
+ - Simple Rake task integration
16
+
17
+ ---
18
+
19
+ ## Installation
20
+
21
+ Add this line to your Gemfile:
22
+
23
+ ```ruby
24
+ gem 'deadweight_rails'
25
+ ```
26
+
27
+ Then execute:
28
+
29
+ ```bash
30
+ bundle install
31
+ ```
32
+
33
+ Or install it yourself:
34
+
35
+ ```bash
36
+ gem install deadweight_rails
37
+ ```
38
+
39
+ ---
40
+
41
+ ## Usage
42
+
43
+ ### 1. Rake task
44
+
45
+ Add this task to `lib/tasks/deadweight.rake`:
46
+
47
+ ```ruby
48
+ require "rake"
49
+ require "deadweight_rails"
50
+
51
+ namespace :deadweight do
52
+ desc "Scan Rails project for unused assets and Ruby code"
53
+ task :scan do
54
+ DeadweightRails.run
55
+ end
56
+ end
57
+ ```
58
+
59
+ Run the task:
60
+
61
+ ```bash
62
+ bundle exec rake deadweight:scan
63
+ ```
64
+
65
+ ---
66
+
67
+ ### 2. Programmatically
68
+
69
+ ```ruby
70
+ require "deadweight_rails"
71
+
72
+ # Scan current Rails project
73
+ DeadweightRails.run
74
+
75
+ # Or scan a specific path
76
+ DeadweightRails.run(path: "/path/to/project")
77
+ ```
78
+
79
+ ---
80
+
81
+ ## Example Output
82
+
83
+ ```
84
+ 🔎 DEADWEIGHTRAILS REPORT
85
+
86
+ --- Assets ---
87
+ Unused CSS: old.css
88
+ Unused JS: legacy.js
89
+
90
+ --- Ruby ---
91
+ Unused Methods: old_helper
92
+ ```
93
+
94
+ ---
95
+
96
+ ## Development
97
+
98
+ After checking out the repo, run:
99
+
100
+ ```bash
101
+ bin/setup
102
+ rake spec
103
+ bin/console
104
+ ```
105
+
106
+ To install the gem locally:
107
+
108
+ ```bash
109
+ bundle exec rake install
110
+ ```
111
+
112
+ To release a new version:
113
+
114
+ ```bash
115
+ bundle exec rake release
116
+ ```
117
+
118
+ ---
119
+
120
+ ## Contributing
121
+
122
+ Bug reports and pull requests are welcome on GitHub:
123
+ gem "deadweight_rails", path: "../deadweight_rails"
124
+ https://github.com/[USERNAME]/deadwe
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,114 @@
1
+ require "fileutils"
2
+
3
+ module DeadweightRails
4
+ class AssetAnalyzer
5
+ MANIFEST_CSS = ["application.css", "application.scss", "application.sass"].freeze
6
+ MANIFEST_JS = ["application.js"].freeze
7
+
8
+ def initialize(path)
9
+ @path = path
10
+ end
11
+
12
+ def scan
13
+ views = Dir[File.join(@path, "app/views/**/*.{erb,haml,slim}")].map { |f| File.read(f) }
14
+
15
+ css_files = Dir[File.join(@path, "app/assets/stylesheets/**/*.{css,scss,sass}")]
16
+ js_files = Dir[File.join(@path, "app/assets/javascripts/**/*.js")]
17
+
18
+ used_css = track_used_css(css_files, views)
19
+ used_js = track_used_js(js_files, views)
20
+
21
+ unused_css = css_files.reject do |f|
22
+ MANIFEST_CSS.include?(File.basename(f)) ||
23
+ used_css.include?(File.basename(f, ".*"))
24
+ end
25
+
26
+ unused_js = js_files.reject do |f|
27
+ MANIFEST_JS.include?(File.basename(f)) ||
28
+ used_js.include?(File.basename(f, ".js"))
29
+ end
30
+
31
+ { unused_css: unused_css, unused_js: unused_js }
32
+ end
33
+
34
+ private
35
+
36
+ # Recursively track used CSS via @import and views
37
+ def track_used_css(files, views)
38
+ used = []
39
+ processed = []
40
+
41
+ traverse = lambda do |file|
42
+ return if file.nil? || processed.include?(file)
43
+ processed << file
44
+
45
+ content = File.read(file)
46
+ basename = File.basename(file, ".*")
47
+
48
+ # Mark as used if selectors match a view
49
+ used << basename if views.any? { |v| content_match_views?(content, v) }
50
+
51
+ # Follow imports
52
+ imports = content.scan(/@import\s+["']([\w\/-]+)["']/).flatten
53
+ imports.each do |import_name|
54
+ import_file = files.find { |f| File.basename(f, ".*") == import_name }
55
+ next unless import_file
56
+
57
+ used << File.basename(import_file, ".*")
58
+ traverse.call(import_file)
59
+
60
+ used << basename
61
+ end
62
+ end
63
+
64
+ # Always start from manifest files if they exist
65
+ manifest_files = files.select { |f| MANIFEST_CSS.include?(File.basename(f)) }
66
+ if manifest_files.any?
67
+ manifest_files.each { |f| traverse.call(f) }
68
+ else
69
+ files.each { |f| traverse.call(f) }
70
+ end
71
+
72
+ used.uniq
73
+ end
74
+
75
+
76
+ # Recursively track used JS via //= require and views
77
+ def track_used_js(files, views)
78
+ used = []
79
+ processed = []
80
+
81
+ traverse = lambda do |file|
82
+ return if file.nil? || processed.include?(file)
83
+ processed << file
84
+
85
+ content = File.read(file)
86
+ basename = File.basename(file, ".js")
87
+ used << basename
88
+
89
+ # Follow requires
90
+ requires = content.scan(/\/\/=\s*require\s+([\w\/-]+)/).flatten
91
+ requires.each do |req_name|
92
+ req_file = files.find { |f| File.basename(f, ".js") == req_name }
93
+ traverse.call(req_file)
94
+ end
95
+ end
96
+
97
+ # Always start from manifest files
98
+ manifest_files = files.select { |f| MANIFEST_JS.include?(File.basename(f)) }
99
+ manifest_files.each { |f| traverse.call(f) }
100
+
101
+ # Also check files referenced in views
102
+ files.each do |f|
103
+ traverse.call(f) if views.any? { |v| v.include?(File.basename(f, ".js")) }
104
+ end
105
+
106
+ used.uniq
107
+ end
108
+
109
+ def content_match_views?(content, view)
110
+ selectors = content.scan(/\.(\w[\w-]*)/).flatten
111
+ selectors.any? { |s| view.include?(s) }
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeadweightRails
4
+ class CSSClassAnalyzer
5
+ # Returns a hash of { "filename.css" => [unused_class1, unused_class2] }
6
+ def initialize(path)
7
+ @path = path
8
+ end
9
+
10
+ def scan
11
+ css_files = Dir[File.join(@path, "app/assets/stylesheets/**/*.{css,scss,sass}")]
12
+ views = Dir[File.join(@path, "app/views/**/*.{erb,haml,slim}")].map { |f| File.read(f) }
13
+
14
+ unused_classes = {}
15
+
16
+ css_files.each do |file|
17
+ content = File.read(file)
18
+ classes = content.scan(/\.(\w[\w-]*)/).flatten
19
+
20
+ unused = (classes || []).reject do |cls|
21
+ views.any? { |view| view.include?(cls) }
22
+ end
23
+
24
+ unused_classes[File.basename(file)] = unused unless unused.empty?
25
+ end
26
+
27
+ unused_classes
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "colorize"
4
+
5
+ module DeadweightRails
6
+ class Report
7
+ def initialize(asset_results, ruby_results, css_classes = {})
8
+ @assets = asset_results || {}
9
+ @ruby = ruby_results || {}
10
+ @css_classes = css_classes || {} # optional: per-file unused CSS classes
11
+ end
12
+
13
+ def print
14
+ print_assets
15
+ print_css_classes
16
+ print_ruby_methods
17
+ end
18
+
19
+ private
20
+
21
+ def print_assets
22
+ puts "\n--- Assets ---".colorize(:green)
23
+
24
+ unused_css = (@assets[:unused_css] || []).map { |f| File.basename(f) }
25
+ unused_js = (@assets[:unused_js] || []).map { |f| File.basename(f) }
26
+
27
+ puts "Unused CSS Files: #{unused_css.join(", ")}"
28
+ puts "Unused JS Files: #{unused_js.join(", ")}"
29
+ end
30
+
31
+ def print_css_classes
32
+ return if @css_classes.empty?
33
+
34
+ puts "\n--- Unused CSS Classes ---".colorize(:green)
35
+ @css_classes.each do |file, classes|
36
+ next if classes.nil? || classes.empty?
37
+
38
+ puts "#{file}: #{classes.join(", ")}"
39
+ end
40
+ end
41
+
42
+ def print_ruby_methods
43
+ unused_methods = @ruby[:unused_methods] || {}
44
+
45
+ return if unused_methods.empty?
46
+
47
+ puts "\n--- Ruby ---".colorize(:green)
48
+ unused_methods.each do |klass, methods|
49
+ next if methods.nil? || methods.empty?
50
+
51
+ puts "#{klass}: #{methods.join(", ")}"
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,92 @@
1
+ require "parser/current"
2
+
3
+ module DeadweightRails
4
+ class RubyAnalyzer
5
+ def initialize(path)
6
+ @path = path
7
+ @defined_methods = Hash.new { |h, k| h[k] = [] }
8
+ @called_methods = Hash.new { |h, k| h[k] = [] }
9
+ end
10
+
11
+ def scan
12
+ ruby_files = Dir[File.join(@path, "{app,lib}/**/*.rb")]
13
+
14
+ ruby_files.each do |file|
15
+ next unless File.file?(file)
16
+ next if File.basename(file).to_s.include? "controller.rb"
17
+ content = File.read(file)
18
+
19
+ # 🧭 Debug: stop when analyzing company.rb
20
+ begin
21
+ ast = Parser::CurrentRuby.parse(content)
22
+ walk(ast)
23
+ rescue Parser::SyntaxError => e
24
+ warn "⚠️ Skipping #{file}: #{e.message}"
25
+ end
26
+ end
27
+
28
+ # Compute unused methods per class
29
+ result = {}
30
+ @defined_methods.each do |klass, methods|
31
+ used = @called_methods[klass] || []
32
+ unused = methods - used
33
+ result[klass] = unused if unused.any?
34
+ end
35
+
36
+ puts "\n✅ Final detected methods:"
37
+ pp @defined_methods
38
+
39
+ result
40
+ end
41
+
42
+ private
43
+
44
+ def walk(node, current_class = nil)
45
+ return unless node.is_a?(Parser::AST::Node)
46
+
47
+ case node.type
48
+ when :class, :module
49
+ const_node = node.children[0]
50
+ class_name = extract_const_name(const_node)
51
+ body = node.children[2]
52
+ walk(body, class_name)
53
+
54
+ when :def
55
+ method_name = node.children[0]
56
+ if current_class
57
+ @defined_methods[current_class] << method_name
58
+ puts "🟢 Found instance method #{method_name} in #{current_class}"
59
+ else
60
+ puts "⚪ Found method #{method_name} outside any class"
61
+ end
62
+
63
+ when :defs
64
+ method_name = node.children[1]
65
+ if current_class
66
+ @defined_methods["#{current_class}.self"] << method_name
67
+ puts "🔵 Found class method #{method_name} in #{current_class}"
68
+ end
69
+ end
70
+
71
+ node.children.each do |child|
72
+ walk(child, current_class) if child.is_a?(Parser::AST::Node)
73
+ end
74
+ end
75
+
76
+ def extract_const_name(node)
77
+ return nil unless node
78
+
79
+ case node.type
80
+ when :const
81
+ parent_node, name_sym = node.children
82
+ parent_name = extract_const_name(parent_node)
83
+ name = name_sym.to_s
84
+ parent_name ? "#{parent_name}::#{name}" : name
85
+ when :cbase
86
+ "" # Handles leading ::
87
+ else
88
+ nil
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeadweightRails
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "deadweight_rails/version"
4
+ require "deadweight_rails/asset_analyzer"
5
+ require "deadweight_rails/ruby_analyzer"
6
+ require "deadweight_rails/css_class_analyzer"
7
+ require "deadweight_rails/report"
8
+
9
+ module DeadweightRails
10
+ def self.run(path: Dir.pwd)
11
+ puts "\n🔎 DEADWEIGHTRAILS REPORT".upcase.colorize(:cyan)
12
+ asset_results = AssetAnalyzer.new(path).scan
13
+ ruby_results = RubyAnalyzer.new(path).scan
14
+ css_class_results = CSSClassAnalyzer.new(path).scan # NEW
15
+
16
+ Report.new(asset_results, ruby_results, css_class_results).print
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ module DeadweightRails
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deadweight_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mrmalvi
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: parser
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: set
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: colorize
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rspec
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.12'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.12'
68
+ - !ruby/object:Gem::Dependency
69
+ name: rspec-rails
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '6.0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '6.0'
82
+ description: DeadweightRails scans your Rails project for dead assets and unused Ruby
83
+ code.
84
+ email:
85
+ - malviyak00@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".github/workflows/rubyonrails.yaml"
91
+ - ".ruby-version"
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/deadweight_rails.rb
96
+ - lib/deadweight_rails/asset_analyzer.rb
97
+ - lib/deadweight_rails/css_class_analyzer.rb
98
+ - lib/deadweight_rails/report.rb
99
+ - lib/deadweight_rails/ruby_analyzer.rb
100
+ - lib/deadweight_rails/version.rb
101
+ - sig/deadweight_rails.rbs
102
+ homepage: https://github.com/mrmalvi/deadweight_rails
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubygems_version: 3.6.9
121
+ specification_version: 4
122
+ summary: Detect unused CSS/JS assets and Ruby methods/classes in Rails apps
123
+ test_files: []