-
-
Save jbrains/9451941 to your computer and use it in GitHub Desktop.
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
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 | |
# Guess what? Now independent of "gem" concept. More reusable. MAGIC! | |
# Guess what? Almost dead-simple wrapper around command line tool. Cohesive. SRP. Nice. | |
def count_lines_in_file(file) | |
# SMELL 'output' and 'amount' are both purely structural names (don't reveal intent) | |
# SMELL 'output' and 'amount' names don't reflect their relationship to each other | |
# (both related to the same gem file) | |
output = `wc -l #{file}` | |
amount = output.strip.split(' ').first.to_i | |
amount | |
end | |
# SMELL Depends on globals like 'puts' and `` (execute process). | |
def count_lines_for_gem(gem) | |
puts "Processing #{gem}" | |
contents = `gem contents #{gem}`.split | |
# REFACTOR Remove duplication between this name and the function's name. | |
# REFACTOR Let's reduce, shall we? | |
line_count_for_this_gem = 0 | |
contents.each do |file| | |
line_count_for_this_gem += count_lines_in_file(file) | |
end | |
puts " LOC: #{line_count_for_this_gem}" | |
return line_count_for_this_gem | |
end | |
total = 0 | |
gems.each do |gem| | |
total += count_lines_for_gem(gem) | |
end | |
puts "Total Lines: #{total}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: