Skip to content

Instantly share code, notes, and snippets.

@mehdi-farsi
Last active February 9, 2024 07:13
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mehdi-farsi/4e438e89792f6aee67536a41ea459ca6 to your computer and use it in GitHub Desktop.
Save mehdi-farsi/4e438e89792f6aee67536a41ea459ca6 to your computer and use it in GitHub Desktop.
Count the lines of code of a rails project - in pure Ruby
# Workflow:
#
# 1- if the path points to a directory
# 1.1- if the directory isn't in the exclusion list then: count LOC
# 1.2- else: prune directory
# 2- else
# 1.1- if the file extension is whitelisted then: count LOC
# 1.2- else: next
require 'find'
hidden_dir_char = '.'
total_loc = 0
DIRECTORIES_TO_IGNORE = %W(node_modules tmp log storage public .git .idea).freeze
EXTENSIONS = %W(erb rb css js html haml scss coffee yml).freeze
# rails path passed as argument or cwd path
rails_app_path = ARGV[0] || Dir.pwd
Find.find(rails_app_path) do |path|
basename = File.basename(path)
if FileTest.directory?(path)
if basename[0] == hidden_dir_char ||
DIRECTORIES_TO_IGNORE.include?(basename)
then
Find.prune
else
next
end
else
next unless EXTENSIONS.include?(basename.split('.').last)
end
total_loc += File.foreach(path).count
end
puts "#{total_loc} LOC"
@mehdi-farsi
Copy link
Author

mehdi-farsi commented Jan 31, 2019

?> ruby rails_project_loc.rb "path/to/your/rails/project"

Feel free to have a look to my latest project HERE

Thank you for taking the time to read this Gist !

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