/
pod_source_installer.rb
291 lines (239 loc) · 8.3 KB
/
pod_source_installer.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
module Pod
class Installer
# Controller class responsible of installing the activated specifications
# of a single Pod.
#
# @note This class needs to consider all the activated specs of a Pod.
#
class PodSourceInstaller
# @return [Sandbox]
#
attr_reader :sandbox
# @return [Hash{Symbol=>Array}] The specifications that need to be
# installed grouped by platform.
#
attr_reader :specs_by_platform
# @param [Sandbox] sandbox @see sandbox
# @param [Hash{Symbol=>Array}] specs_by_platform @see specs_by_platform
#
def initialize(sandbox, specs_by_platform)
@sandbox = sandbox
@specs_by_platform = specs_by_platform
@clean = true
@generate_docs = false
@install_docs = false
@aggressive_cache = false
end
# @return [String] A string suitable for debugging.
#
def inspect
"<#{self.class} sandbox=#{sandbox.root} pod=#{root_spec.name}"
end
#-----------------------------------------------------------------------#
public
# @!group Configuration
# @return [Bool] whether the file not used by CocoaPods should be
# removed.
#
attr_accessor :clean
alias_method :clean?, :clean
# @return [Bool] whether the documentation should be generated for the
# Pod.
#
attr_accessor :generate_docs
alias_method :generate_docs?, :generate_docs
# @return [Bool] whether the generated documentation should be installed
# in Xcode.
#
attr_accessor :install_docs
alias_method :install_docs?, :install_docs
# @return [Bool] whether the downloader should always check against the
# remote if issues might be generated (mostly useful to speed up
# testing).
#
# @note This might be removed in future.
#
attr_accessor :aggressive_cache
alias_method :aggressive_cache?, :aggressive_cache
#-----------------------------------------------------------------------#
public
# @!group Installation
# Creates the target in the Pods project and the relative support files.
#
# @return [void]
#
def install!
download_source unless predownloaded? || local?
generate_docs if generate_docs?
end
# Cleans the installations if appropriate.
#
# @todo As the pre install hooks need to run before cleaning this
# method should be refactored.
#
# @return [void]
#
def clean!
clean_installation if clean? && !local?
end
# @return [Hash]
#
attr_reader :specific_source
#-----------------------------------------------------------------------#
private
# @!group Installation Steps
# Downloads the source of the Pod. It also stores the specific options
# needed to recreate the same exact installation if needed in
# `#specific_source`.
#
# @return [void]
#
def download_source
root.rmtree if root.exist?
if root_spec.version.head?
downloader.download_head
@specific_source = downloader.checkout_options
else
downloader.download
unless downloader.options_specific?
@specific_source = downloader.checkout_options
end
end
if specific_source
sandbox.store_checkout_source(root_spec.name, specific_source)
end
end
# Generates the documentation for the Pod.
#
# @return [void]
#
def generate_docs
if @cleaned
raise Informative, "Attempt to generate the documentation from a cleaned Pod."
end
if documentation_generator.already_installed?
UI.section " > Using existing documentation"
else
UI.section " > Installing documentation" do
documentation_generator.generate(install_docs?)
end
end
end
# Removes all the files not needed for the installation according to the
# specs by platform.
#
# @return [void]
#
def clean_installation
clean_paths.each { |path| FileUtils.rm_rf(path) }
@cleaned = true
end
#-----------------------------------------------------------------------#
public
# @!group Dependencies
# @return [Downloader] The downloader to use for the retrieving the
# source.
#
def downloader
return @downloader if @downloader
@downloader = Downloader.for_target(root, root_spec.source.dup)
@downloader.cache_root = CACHE_ROOT
@downloader.max_cache_size = MAX_CACHE_SIZE
@downloader.aggressive_cache = aggressive_cache?
@downloader
end
# @return [Generator::Documentation] The documentation generator to use
# for generating the documentation.
#
def documentation_generator
@documentation_generator ||= Generator::Documentation.new(sandbox, root_spec, path_list)
end
#-----------------------------------------------------------------------#
private
# @!group Convenience methods.
# @return [Array<Specifications>] the specification of the Pod used in
# this installation.
#
def specs
specs_by_platform.values.flatten
end
# @return [Specification] the root specification of the Pod.
#
def root_spec
specs.first.root
end
# @return [Pathname] the folder where the source of the Pod is located.
#
def root
sandbox.pod_dir(root_spec.name)
end
# @return [Boolean] whether the source has been pre downloaded in the
# resolution process to retrieve its podspec.
#
def predownloaded?
sandbox.predownloaded_pods.include?(root_spec.name)
end
# @return [Boolean] whether the pod uses the local option and thus
# CocoaPods should not interfere with the files of the user.
#
def local?
sandbox.local?(root_spec.name)
end
#-----------------------------------------------------------------------#
private
# @!group Private helpers
# @return [Array<Sandbox::FileAccessor>] the file accessors for all the
# specifications on their respective platform.
#
def file_accessors
return @file_accessors if @file_accessors
@file_accessors = []
specs_by_platform.each do |platform, specs|
specs.each do |spec|
@file_accessors << Sandbox::FileAccessor.new(path_list, spec.consumer(platform))
end
end
@file_accessors
end
# @return [Sandbox::PathList] The path list for this Pod.
#
def path_list
@path_list ||= Sandbox::PathList.new(root)
end
# Finds the absolute paths, including hidden ones, of the files
# that are not used by the pod and thus can be safely deleted.
#
# @note Implementation detail: Don't use `Dir#glob` as there is an
# unexplained issue (#568, #572 and #602).
#
# @return [Array<Strings>] The paths that can be deleted.
#
def clean_paths
cached_used = used_files
glob_options = File::FNM_DOTMATCH | File::FNM_CASEFOLD
files = Pathname.glob(root + "**/*", glob_options).map(&:to_s)
files.reject! do |candidate|
candidate.end_with?('.', '..') || cached_used.any? do |path|
path.include?(candidate) || candidate.include?(path)
end
end
files
end
# @return [Array<String>] The absolute path of all the files used by the
# specifications (according to their platform) of this Pod.
#
def used_files
files = [
file_accessors.map(&:source_files),
file_accessors.map(&:resources),
file_accessors.map(&:preserve_paths),
file_accessors.map(&:prefix_header),
file_accessors.map(&:readme),
file_accessors.map(&:license),
]
files.flatten.compact.map{ |path| path.to_s }.uniq
end
#-----------------------------------------------------------------------#
end
end
end