Skip to content

Instantly share code, notes, and snippets.

@electronicbites
Created September 15, 2010 15:29
Show Gist options
  • Save electronicbites/580898 to your computer and use it in GitHub Desktop.
Save electronicbites/580898 to your computer and use it in GitHub Desktop.
scan unused images and globals in a xcode project
#!/usr/bin/env ruby
#TODO
# find all files not in project file
# => list these files
globalsfile = "Classes/Globals.h"
allfiles = `git ls-files`.split(/\n/)
#allfiles.each{|f| f.gsub!(/\"/, "")} #not needed when files have no unicode chars inf filenames
sourcefileextensions = "\.(m|h)$"
allsourcefileextensions = "\.(m|h|xib|html|css|js)$"
projectfilesextensions = "\.(xib)$"
resourcefilesextensions = "\.(png|gif|jpg)$"
sourcefiles = allfiles.grep /#{allsourcefileextensions}/
projectfiles = allfiles.grep /#{projectfilesextensions}/
resourcefiles = allfiles.grep /#{resourcefilesextensions}/
def check_file( file, string )
File.open( file ) do |io|
io.each {|line| line.chomp! ; return true if line.include? string}
end
nil
end
#find all resourefiles that are in no source file or nib file
# => they are not used anymore, even if they are in projectfiles
# this doesnt work if the image filesnames are generated
danglingresources = resourcefiles.find_all do |rf|
sourcefiles.find {|sf| check_file(sf, File.basename(rf))}.nil?
end
danglingresources.each {|f| File.delete(f)}
def scan_globals(globalsfile)
reg = /#define (\w+)/
globals = []
File.open( globalsfile ) do |io|
io.each do |line|
line.chomp!
m = reg.match(line)
globals << m[1] unless m.nil?
end
end
globals
end
sourcefiles_without_globals = sourcefiles.delete(globalsfile)
dangling_globals = scan_globals(globalsfile).find_all do |s|
sourcefiles.find {|sf| check_file(sf, s)}.nil?
end
def delete_all_lines_with_dangling_globals(globalsfile, dangling_globals)
file_content = ""
File.open( globalsfile ) do |io|
io.each { |line| file_content += line }
end
dangling_globals.each do |glob|
out = ""
file_content.lines.each do |line|
out += line unless line =~ / #{glob} /
end
file_content = out
end
file_content
end
out = File.open("Globals_new.h","w")
out.puts delete_all_lines_with_dangling_globals(globalsfile, dangling_globals)
out.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment