Created
April 27, 2019 22:33
-
-
Save jnraine/b0f7bb5a4a5b10ff07274389e2de83ad to your computer and use it in GitHub Desktop.
Count lines of code per gem in your Gemfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gem_loc.rb | |
# | |
# Output require-able lines of code gems in your Gemfile | |
# | |
# Usage: rails runner <path to this file> | |
require "rails/code_statistics_calculator" | |
require "csv" | |
gem_specs = Bundler::LockfileParser.new(Rails.root.join("Gemfile.lock").read).specs.each(&:__materialize__) | |
gem_file_paths = gem_specs.map do |gem_spec| | |
file_paths = gem_spec.full_require_paths.flat_map do |require_path| | |
Dir.glob("#{require_path}/**/*.rb").to_a | |
end | |
[gem_spec.name, file_paths] | |
end.to_h | |
stats_by_gem = Hash.new {|hash, key| hash[key] = CodeStatisticsCalculator.new } | |
gem_file_paths.each do |gem_name, file_paths| | |
file_paths.each do |file_path| | |
begin | |
stats_by_gem[gem_name].add_by_file_path(file_path) | |
rescue | |
STDERR.puts("#{$!.class} raised. Skipping #{file_path}") | |
end | |
end | |
end | |
include ActionView::Helpers::NumberHelper | |
csv_string = CSV.generate do |csv| | |
csv << ["Gem Name", "Lines of code"] | |
stats_by_gem.sort_by {|gem_name, stats| stats.code_lines }.each do |gem_name, stats| | |
csv << [gem_name, number_with_delimiter(stats.code_lines)] | |
end | |
rails_gems = ["actioncable", "actionmailbox", "actionmailer", "actionpack", "actiontext", "actionview", "activejob", "activemodel", "activerecord", "activestorage", "activesupport", "railties"] | |
csv << ["rails/rails", stats_by_gem.sum {|gem_name, stats| rails_gems.include?(gem_name) ? stats.code_lines : 0 }] | |
csv << ["Total", stats_by_gem.sum {|_gem_name, stats| stats.code_lines }] | |
end | |
puts csv_string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment