Last active
February 9, 2024 07:13
-
-
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
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
# 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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feel free to have a look to my latest project HERE
Thank you for taking the time to read this Gist !