forked from CocoaPods/CocoaPods
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sandbox_analyzer.rb
268 lines (235 loc) · 8.17 KB
/
sandbox_analyzer.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
module Pod
class Installer
class Analyzer
# Analyze the sandbox to detect which Pods should be removed, and which
# ones should be reinstalled.
#
# The logic is the following:
#
# Added
# - If not present in the sandbox lockfile.
# - The directory of the Pod doesn't exits.
#
# Changed
# - The version of the Pod changed.
# - The SHA of the specification file changed.
# - The specific installed (sub)specs of the same Pod changed.
# - The specification is from an external source and the
# installation process is in update mode.
# - The directory of the Pod is empty.
# - The Pod has been pre-downloaded.
#
# Removed
# - If a specification is present in the lockfile but not in the resolved
# specs.
#
# Unchanged
# - If none of the above conditions match.
#
class SandboxAnalyzer
# @return [Sandbox] The sandbox to analyze.
#
attr_reader :sandbox
# @return [Podfile] The Podfile to analyze dependencies.
#
attr_reader :podfile
# @return [Array<Specifications>] The specifications returned by the
# resolver.
#
attr_reader :specs
# @return [Bool] Whether the installation is performed in update mode.
#
attr_reader :update_mode
alias_method :update_mode?, :update_mode
# Init a new SandboxAnalyzer
#
# @param [Sandbox] sandbox @see sandbox
# @param [Podfile] podfile @see podfile
# @param [Array<Specifications>] specs @see specs
# @param [Bool] update_mode @see update_mode
#
def initialize(sandbox, podfile, specs, update_mode)
@sandbox = sandbox
@podfile = podfile
@specs = specs
@update_mode = update_mode
end
# Performs the analysis to the detect the state of the sandbox respect
# to the resolved specifications.
#
# @return [void]
#
def analyze
state = SpecsState.new
if sandbox_manifest
all_names = (resolved_pods + sandbox_pods).uniq.sort
all_names.sort.each do |name|
state.add_name(name, pod_state(name))
end
else
state.added.merge(resolved_pods)
end
state
end
#---------------------------------------------------------------------#
private
# @!group Pod state
# Returns the state of the Pod with the given name.
#
# @param [String] pod
# the name of the Pod.
#
# @return [Symbol] The state
#
def pod_state(pod)
return :added if pod_added?(pod)
return :deleted if pod_deleted?(pod)
return :changed if pod_changed?(pod)
:unchanged
end
# Returns whether the Pod with the given name should be installed.
#
# @note A Pod whose folder doesn't exists is considered added.
#
# @param [String] pod
# the name of the Pod.
#
# @return [Bool] Whether the Pod is added.
#
def pod_added?(pod)
return true if resolved_pods.include?(pod) && !sandbox_pods.include?(pod)
return true if !sandbox.local?(pod) && !folder_exist?(pod)
false
end
# Returns whether the Pod with the given name should be removed from
# the installation.
#
# @param [String] pod
# the name of the Pod.
#
# @return [Bool] Whether the Pod is deleted.
#
def pod_deleted?(pod)
return true if !resolved_pods.include?(pod) && sandbox_pods.include?(pod)
false
end
# Returns whether the Pod with the given name should be considered
# changed and thus should be reinstalled.
#
# @note In update mode, as there is no way to know if a remote source
# hash changed the Pods from external
# sources are always marked as changed.
#
# @note A Pod whose folder is empty is considered changed.
#
# @param [String] pod
# the name of the Pod.
#
# @return [Bool] Whether the Pod is changed.
#
def pod_changed?(pod)
spec = root_spec(pod)
return true if spec.version != sandbox_version(pod)
return true if spec.checksum != sandbox_checksum(pod)
return true if resolved_spec_names(pod) != sandbox_spec_names(pod)
podfile_dep = podfile_dependency(pod)&.tap { |dep| dep.podspec_repo = nil }
return true if podfile_dep != sandbox_dependency(pod)
return true if sandbox.predownloaded?(pod)
return true if folder_empty?(pod)
false
end
#---------------------------------------------------------------------#
private
# @!group Private helpers
# @return [Lockfile] The manifest to use for the sandbox.
#
def sandbox_manifest
sandbox.manifest
end
#--------------------------------------#
# @return [Array<String>] The name of the resolved Pods.
#
def resolved_pods
@resolved_pods ||= specs.map { |spec| spec.root.name }.uniq
end
# @return [Array<String>] The name of the Pods stored in the sandbox
# manifest.
#
def sandbox_pods
@sandbox_pods ||= sandbox_manifest.pod_names.map { |name| Specification.root_name(name) }.uniq
end
# @return [Array<String>] The name of the resolved specifications
# (includes subspecs).
#
# @param [String] pod
# the name of the Pod.
#
def resolved_spec_names(pod)
specs.select { |s| s.root.name == pod }.map(&:name).uniq.sort
end
# @return [Array<String>] The name of the specifications stored in the
# sandbox manifest (includes subspecs).
#
# @param [String] pod
# the name of the Pod.
#
def sandbox_spec_names(pod)
sandbox_manifest.pod_names.select { |name| Specification.root_name(name) == pod }.uniq.sort
end
# @return [Specification] The root specification for the Pod with the
# given name.
#
# @param [String] pod
# the name of the Pod.
#
def root_spec(pod)
specs.find { |s| s.root.name == pod }.root
end
#--------------------------------------#
# @return [Version] The version of Pod with the given name stored in
# the sandbox.
#
# @param [String] pod
# the name of the Pod.
#
def sandbox_version(pod)
sandbox_manifest.version(pod)
end
# @return [String] The checksum of the specification of the Pod with
# the given name stored in the sandbox.
#
# @param [String] pod
# the name of the Pod.
#
def sandbox_checksum(pod)
sandbox_manifest.checksum(pod)
end
# @return [Dependency, nil] The dependency with the given name stored in the sandbox.
#
# @param [String] pod
# the name of the Pod.
#
def sandbox_dependency(pod)
sandbox_manifest.dependencies.find { |d| d.name == pod }
end
#--------------------------------------#
# @return [Dependency, nil] The dependency with the given name from the podfile.
#
# @param [String] pod
# the name of the Pod.
#
def podfile_dependency(pod)
podfile.dependencies.find { |d| d.name == pod }
end
#--------------------------------------#
def folder_exist?(pod)
sandbox.pod_dir(pod).exist?
end
def folder_empty?(pod)
Dir.glob(sandbox.pod_dir(pod) + '*').empty?
end
#---------------------------------------------------------------------#
end
end
end
end