Skip to content

Instantly share code, notes, and snippets.

@CoryFoy
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save CoryFoy/9441665 to your computer and use it in GitHub Desktop.

Select an option

Save CoryFoy/9441665 to your computer and use it in GitHub Desktop.
require 'set'
GEM_FILE_TO_PROCESS = "Gemfile.lock"
def gem_list_finished?(line)
line.include?("PLATFORMS")
end
def non_gem_line?(line)
line.include?("GEM") ||
line.include?("remote:") ||
line.include?("specs:") ||
line.empty?
end
def gem_name_without_version(line)
line.split(' ').first
end
def get_files_in_gem(gem)
#gem contents returns the files
#as a line break delimited string
`gem contents #{gem}`.split
end
def line_count_for_file(file)
output = `wc -l #{file}`
#line count is the first column from
#the returned value
output.strip.split(' ').first.to_i
end
def log(message)
puts message
end
gemfile_list = File.read(GEM_FILE_TO_PROCESS)
gems_to_process = Set.new
total_line_count = 0
gemfile_list.each_line do |gem_line|
gem_line = gem_line.strip
break if gem_list_finished?(gem_line)
next if non_gem_line?(gem_line)
gems_to_process.add(gem_name_without_version(gem_line))
end
log "TOTAL GTP: #{gems_to_process.count}"
gems_to_process.each do |gem|
log "Processing #{gem}"
contents = get_files_in_gem(gem)
gem_line_count = 0
contents.each do |file|
gem_line_count += line_count_for_file(file)
end
log " LOC: #{gem_line_count}"
total_line_count += gem_line_count
end
log "Total Lines: #{total_line_count}"
@CoryFoy
Copy link
Author

CoryFoy commented Mar 9, 2014

It should be noted that JB's post above was in response to version 2 of this Gist. The above version attempts to take into account better code principles, and I blogged about it at: Put Your Best Code Forward http://blog.coryfoy.com/2014/03/putting-your-best-code-forward/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment