Skip to content

Instantly share code, notes, and snippets.

@willhbr
Last active September 30, 2017 04:50
Show Gist options
  • Save willhbr/a56d4aa5ff2029ec4b49e3ec5888c070 to your computer and use it in GitHub Desktop.
Save willhbr/a56d4aa5ff2029ec4b49e3ec5888c070 to your computer and use it in GitHub Desktop.
Number of words in reports vs number of words in code
#!/usr/bin/env ruby
DETAILED_MODE = !ARGV[2].nil?
begin
require 'yomu'
rescue LoadError
msg = '(Looks like this is you)'
lab = `hostname`.strip.start_with? 'cs'
puts <<-EOF
You need the 'yomu' gem to use this script.
Install it on your own computer: #{lab ? '' : msg}
gem install yomu
Install it on a lab computer: #{lab ? msg : ''}
gem install yomu --user-install
EOF
exit 2
end
if ARGV.length < 2
puts <<-EOF
That's not how you do this!
useage: ruby seng_stats.rb <report_folder> <repo_file> <detailed mode?>
report_folder is a folder of files (PDF, DOCX, etc) that you wrote this year.
repo_file is a list of git-clonable URLs (either HTTPS or SSH) one per line
add a third option to enable detailed/ debug mode
If the thing was part of a group project, either rename the files to include the
percentage (my_report_thing50%.docx), or change the repo_file line to have the
percentage after the url (space separated), like 'gitlab.com/me/project.git 50%'
EOF
exit 1
end
require 'fileutils'
def col(text, color)
"\033[#{color.to_i}m#{text}\033[0m"
end
def do_reports(folder)
Dir[folder + '/*'].map do |file|
print "Processing #{file}... "
ratio = 1
if match = file.match(/(\d{2})%/)
ratio = match[1].to_i / 100.0
end
yom = Yomu.new file
text = yom.text
size = text.scan(/\w+/).length
puts "#{size} * #{ratio}" if DETAILED_MODE
puts col('done.', 32)
size * ratio
end.sum
end
def do_repos(file='repos.txt', dest='repos')
FileUtils::mkdir_p dest
File.read(file).lines.map do |line|
url, percent = line.split /\s+/
url.strip!
ratio = (((percent || '').match(/(\d{2})%/) || [])[1] || 100).to_i / 100.0
puts col("Processing #{url}...", 32)
path = dest + "/#{url}"
unless File.exists? path
system 'git', 'clone', url, path
end
size = Dir[path + '/**/*'].map do |file|
if File.file? file
content = File.read(file)
if content.valid_encoding?
size = content.scan(/\w+/).length
puts "#{file}: #{size}" if DETAILED_MODE
size
else
0
end
else
0
end
end.sum
puts "#{size} * #{ratio}" if DETAILED_MODE
puts '... done.'
size * ratio
end.sum
end
report_folder = ARGV[0]
repo_file = ARGV[1]
puts col('Processing reports in ' + report_folder, 33)
report_size = do_reports(report_folder)
puts col('Processing git repos from ' + repo_file, 33)
repo_size = do_repos(repo_file)
puts col('Done!', 123)
puts '-' * 50
total = (repo_size + report_size).to_f / 100
puts <<-EOF
Report words: #{report_size}
'Words' in code: #{repo_size}
Ratio: reports / code = #{(report_size.to_f / repo_size).round(4)}
Reports make up #{(report_size / total).round(1)}%
Code makes up #{(repo_size / total).round(1)}%
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment