Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A script to search Objective-C and Swift files which outputs the number of lines in each file. I've used this to determine areas of an application which violate the Single Responsibility Principle. Output quickly identifies the largest classes in a project.
# Dependencies: sudo gem install colorize
# Usage: ruby SRPAnalyser.rb --hide-acceptable <Path to search>
require 'colorize'
def displayFileWithNumberOfLines (file, lines, hideAcceptable)
message = "#{file} has #{lines} lines."
maxLines = 250
acceptableLines = 150
if lines > maxLines
puts message.red
elsif lines > acceptableLines
puts message.yellow
elsif !hideAcceptable
puts message.green
end
end
directory = ARGV.last
hideAcceptable = false
ARGV.each do |a|
if a == "--hide-acceptable"
hideAcceptable = true
end
end
filesString = `grep --include=*.swift --include=*.m --exclude-dir=*Tests --exclude-dir=*Spec --exclude-dir=Pods --exclude-dir=Carthage -rc '' #{directory}`
files = filesString.split("\n")
files.each do |file|
values = file.split(":")
fileName = values[0].split("/").last
displayFileWithNumberOfLines fileName, values[1].to_i, hideAcceptable
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.