Skip to content

Instantly share code, notes, and snippets.

@imryan
Forked from Orangenhain/import_minimizer.rb
Created January 11, 2017 16:33
Show Gist options
  • Save imryan/b64e8e2dc0696b96793bcba0f0133fb5 to your computer and use it in GitHub Desktop.
Save imryan/b64e8e2dc0696b96793bcba0f0133fb5 to your computer and use it in GitHub Desktop.
Script that takes a ObjC .m file and tries to find unused (or duplicate) import statements by commenting out each #import line in turn and seeing if the project still compiles. You will have to change BUILD_DIR & BUILD_CMD.
#!/usr/bin/env ruby -w
class String
def starts_with?(prefix)
prefix.respond_to?(:to_str) && self[0, prefix.length] == prefix
end
end
HEADER_REGEX = /^#import\s+["<](.*)[">]/
BUILD_DIR = File.expand_path("~/Code/project_xyz")
BUILD_CMD = 'xcodebuild -target "Project XYZ" -configuration "Debug" build > /tmp/import_minimizer-build.log 2>&1'
FILE_TO_MINIMIZE = ARGV.shift
unless File.readable?(FILE_TO_MINIMIZE)
puts "Usage: import_minimizer.rb FileToMinimize.m"
puts "\t file path must be relative to #{BUILD_DIR} (or an absolute path)"
exit 1
end
Dir.chdir(BUILD_DIR)
lines = open(FILE_TO_MINIMIZE, "r").each_line.to_a
line_no = 0
counter = 0
puts "testing #{FILE_TO_MINIMIZE}"
printf ". checking for duplicates"
seen_headers = []
while line_no < lines.length
line = lines[line_no]
line_no += 1
next unless line.starts_with? "#import "
header = line[HEADER_REGEX, 1]
if seen_headers.include?(header)
lines[line_no-1] = "// #{line.strip} // -- duplicate\n"
counter += 1
end
seen_headers << header
end
puts " - found: #{counter}"
line_no = 0
while line_no < lines.length
line = lines[line_no]
unless line.starts_with? "#import "
line_no += 1
next
end
orig_line = line
header = line[HEADER_REGEX, 1]
printf ". checking import: #{header}"
lines[line_no] = "// #{orig_line.strip} // -- not needed\n"
open(FILE_TO_MINIMIZE, "w+") { |f| f.write(lines.join) }
`#{BUILD_CMD}`
if ($?.exitstatus != 0)
lines[line_no] = orig_line
puts " - needed"
else
puts " - NOT needed"
counter += 1
end
line_no += 1
end
puts "\nFound #{counter} #import's that are not needed / duplicates"
open(FILE_TO_MINIMIZE, "w+") { |f| f.write(lines.join) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment