forked from smirn0v/xcode-same-targets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxcode-same-targets.rb
177 lines (138 loc) · 4.71 KB
/
xcode-same-targets.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
require 'colorize'
require 'json'
require 'pathname'
require 'set'
require 'xcodeproj'
CONFIG_FILE_NAME=".xcode-same-targets"
def report_error(msg)
puts "["+"-".red+"] #{msg}"
end
def report_fail(msg)
report_error(msg)
exit(1)
end
def report_ok(msg)
puts "["+"+".green+"] #{msg}"
end
module Xcodeproj
class Project
module Object
class AbstractObject
def recursive(&block)
if self.respond_to?('recursive_children')
recursive_children.each { |chld|
block.call(chld) unless chld.is_a?(PBXGroup) || chld.wrong_parent
}
else
block.call(self)
end
end
# corrupted target
def wrong_parent
parent=Xcodeproj::Project::Object::GroupableHelper.parent(self)
return parent.is_a?(Xcodeproj::Project::Object::PBXBuildFile)
end
end
end
end
end
project_paths = Pathname.pwd.children.select { |pn| pn.extname == '.xcodeproj' }
if project_paths.empty?
report_fail("No xcode projects found inside current directory.")
end
report_ok("#{project_paths.first}")
config = nil
unless File.exist?(CONFIG_FILE_NAME)
report_ok("No configuration file '#{CONFIG_FILE_NAME}' found, will compare all targets.") unless File.exist?(CONFIG_FILE_NAME)
else
config = JSON.parse(IO.read(CONFIG_FILE_NAME))
end
project = Xcodeproj::Project.open(project_paths.first)
if !config
config = { "compare-sets" => [ {"targets" => project.targets.map{|t| t.name}} ] }
end
target_files = {}
project.targets.each { |target|
name = target.name
target_files[name] = [].to_set
target.build_phases.each { |bp|
bp.files.each { |bf|
target_files[name] << bf.file_ref
}
}
}
extra_target_files = {}
report_fail("No compare sets found.") unless config.key?("compare-sets")
should_be_ignored = ->(path, ignores) {
ignored = false
ignores.each { |iregx|
ignored = path[/#{iregx}/] != nil
break if ignored
}
return ignored
}
exclusive_except = ->(name,local_exclusive) {
exclusive = []
if config.key?('exclusive')
config['exclusive'].each { |t_name, value|
next if t_name == name
exclusive.concat(value)
}
if config['exclusive'].key?(name)
exclusive = exclusive - config['exclusive'][name]
end
end
local_exclusive.each { |t_name, value|
next if t_name == name
exclusive.concat(value)
}
if local_exclusive.key?(name)
exclusive = exclusive - local_exclusive[name]
end
return exclusive
}
success = true
config['compare-sets'].each { |cs|
report_fail("All compare sets must contain targets array.") unless cs.key?('targets') && cs['targets'].kind_of?(Array)
extra = [].to_set
cs['targets'].permutation(2).each { |t1_name, t2_name|
report_fail("'#{t1_name}' target not found") unless project.targets.select { |t| t1_name == t.name }.length == 1
report_fail("'#{t2_name}' target not found") unless project.targets.select { |t| t2_name == t.name }.length == 1
ignores = []
ignores.concat(cs['exclusive'][t1_name]) if cs.key?('exclusive') && cs['exclusive'].key?(t1_name)
ignores.concat(config['exclusive'][t1_name]) if config.key?('exclusive') && config['exclusive'].key?(t1_name)
ignores.concat(config['exclusive']['*']) if config.key?('exclusive') && config['exclusive'].key?('*')
foreign_exclusive = exclusive_except[t1_name, cs.key?('exclusive')?cs['exclusive']:{}]
t1_extra = target_files[t1_name] - target_files[t2_name]
target_files[t1_name].each { |fr|
conflicting = false
fr.recursive { |cfr|
foreign_exclusive.each { |iregx|
break if conflicting = (cfr.real_path.to_s[/#{iregx}/] != nil)
}
break if conflicting
}
t1_extra << fr if conflicting
}
t1_extra.reject! { |fr|
need_reject = false
fr.recursive { |cfr|
need_reject = should_be_ignored[cfr.real_path.to_s, ignores]
break if need_reject
}
need_reject
}
extra.merge(t1_extra)
}
unless extra.empty?
success = false
report_error("Conflicting files for [#{cs['targets'].join(', ')}]")
extra.each { |fr|
fr.recursive { |cfr|
puts(" #{cfr.real_path}")
}
}
end
}
report_ok("No conflicts found.") if success
exit(success ? 0 : 1)