-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunusedfiles.rb
97 lines (74 loc) · 2.11 KB
/
unusedfiles.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'find'
XCODE_PROJECT_FILE = "/project.pbxproj"
class ProjectParser
def initialize
assetFileTypes = ["wav", "png", "pvr", "caf"]
sourceFileTypes = ["h", "m", "c", "cpp"]
@excludeDirs = [".svn", ".git"]
@assetsPattern = assetFileTypes.join("|")
@sourcesPattern = sourceFileTypes.join("|")
end
def allProjectFiles(path)
inReferencesSection = false
projectFiles = []
file = File.new(path, "r")
while(line = file.gets)
if(line =~ /\/\*\s*Begin PBXFileReference/)
inReferencesSection = true
next
elsif(line =~ /\/\*\s*End PBXFileReference/)
break;
end
if(inReferencesSection)
line = parseLine(line)
projectFiles << line unless line == nil
end
end
return projectFiles
end
def parseLine(line)
match = line.scan /.*?path\s+=\s+"?([^.]+)\.(#{@assetsPattern})/
match[0][0] unless match[0] == nil
end
def unusedFiles(path)
filesInProject = allProjectFiles(path+XCODE_PROJECT_FILE)
inUse = []
dir = File.dirname(File.expand_path(path));
Find.find(dir) do |file|
if FileTest.directory?(file)
if @excludeDirs.include?(File.basename(file))
Find.prune
else
next
end
else
if(file =~ /[^\.]+\.(#{@sourcesPattern})$/)
data = File.open(file).read
filesInProject.each do |projectFile|
projectFileInclude = '"'+projectFile+'"'
match = data.scan(projectFileInclude)
if(match && match[0])
inUse << projectFile
end
end
end
end
end
return filesInProject-inUse
end
end
if(!ARGV[0])
puts "Usage: #{File.basename(__FILE__)} [PROJECT_FILE]"
exit
end
if(!File.exists?(ARGV[0]))
puts "#{File.basename(__FILE__)}: #{ARGV[0]}: No such file or directory."
exit
end
if(!File.exists?(ARGV[0]+XCODE_PROJECT_FILE))
puts "#{File.basename(__FILE__)}: #{ARGV[0]}: Is not a valid Xcode project."
exit
end
p = ProjectParser.new
files = p.unusedFiles(ARGV[0])
puts files