Skip to content

Instantly share code, notes, and snippets.

@jbrains
Forked from CoryFoy/gist:9441665
Last active August 29, 2015 13:57
Show Gist options
  • Save jbrains/9451941 to your computer and use it in GitHub Desktop.
Save jbrains/9451941 to your computer and use it in GitHub Desktop.
GFILE = "Gemfile.lock"
list = File.read(GFILE)
gems = []
list.each_line do |line|
line = line.strip
break if line.include?("PLATFORMS")
next if line.include?("GEM")
next if line.include?("remote:")
next if line.include?("specs:")
next if line.empty?
gems.push(line.split(' ').first)
end
# SMELL Depends on globals like 'puts' and `` (execute process).
def count_lines_for_gem(gem)
puts "Processing #{gem}"
contents = `gem contents #{gem}`.split
line_count_for_this_gem = 0
contents.each do |file|
output = `wc -l #{file}`
amount = output.strip.split(' ').first.to_i
line_count_for_this_gem += amount
end
puts " LOC: #{line_count_for_this_gem}"
return line_count_for_this_gem
end
total = 0
gems.each do |gem|
# REFACTOR Extract this body as a function.
puts "Processing #{gem}"
contents = `gem contents #{gem}`.split
line_count_for_this_gem = 0
contents.each do |file|
output = `wc -l #{file}`
amount = output.strip.split(' ').first.to_i
line_count_for_this_gem += amount
end
puts " LOC: #{line_count_for_this_gem}"
total += line_count_for_this_gem
end
puts "Total Lines: #{total}"
@jbrains
Copy link
Author

jbrains commented Mar 9, 2014

If you don't care about the interim line count in each gem, then we can remove the duplicate count_lines concept, replacing the whole thing with this, in approximate Haskell, of course:

sum ((count_lines . filenames_in_gem . gems_in_gemfile_lock) gemfile_lock)

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