-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
config.rb
366 lines (304 loc) · 9.63 KB
/
config.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
require 'active_support/multibyte/unicode'
module Pod
# Stores the global configuration of CocoaPods.
#
class Config
# The default settings for the configuration.
#
# Users can specify custom settings in `~/.cocoapods/config.yaml`.
# An example of the contents of this file might look like:
#
# ---
# skip_repo_update: true
# new_version_message: false
#
DEFAULTS = {
:verbose => false,
:silent => false,
:skip_download_cache => !ENV['COCOAPODS_SKIP_CACHE'].nil?,
:new_version_message => ENV['COCOAPODS_SKIP_UPDATE_MESSAGE'].nil?,
:cache_root => Pathname.new(Dir.home) + 'Library/Caches/CocoaPods',
}
# Applies the given changes to the config for the duration of the given
# block.
#
# @param [Hash<#to_sym,Object>] changes
# the changes to merge temporarily with the current config
#
# @yield [] is called while the changes are applied
#
def with_changes(changes)
old = {}
changes.keys.each do |key|
key = key.to_sym
old[key] = send(key) if respond_to?(key)
end
configure_with(changes)
yield if block_given?
ensure
configure_with(old)
end
public
#-------------------------------------------------------------------------#
# @!group UI
# @return [Boolean] Whether CocoaPods should provide detailed output about the
# performed actions.
#
attr_accessor :verbose
alias_method :verbose?, :verbose
# @return [Boolean] Whether CocoaPods should produce not output.
#
attr_accessor :silent
alias_method :silent?, :silent
# @return [Boolean] Whether CocoaPods is allowed to run as root.
#
attr_accessor :allow_root
alias_method :allow_root?, :allow_root
# @return [Boolean] Whether a message should be printed when a new version of
# CocoaPods is available.
#
attr_accessor :new_version_message
alias_method :new_version_message?, :new_version_message
#-------------------------------------------------------------------------#
# @!group Installation
# @return [Boolean] Whether the installer should skip the download cache.
#
attr_accessor :skip_download_cache
alias_method :skip_download_cache?, :skip_download_cache
public
#-------------------------------------------------------------------------#
# @!group Cache
# @return [Pathname] The directory where CocoaPods should cache remote data
# and other expensive to compute information.
#
attr_accessor :cache_root
def cache_root
@cache_root.mkpath unless @cache_root.exist?
@cache_root
end
public
#-------------------------------------------------------------------------#
# @!group Initialization
def initialize(use_user_settings = true)
configure_with(DEFAULTS)
unless ENV['CP_HOME_DIR'].nil?
@cache_root = home_dir + 'cache'
end
if use_user_settings && user_settings_file.exist?
require 'yaml'
user_settings = YAML.load_file(user_settings_file)
configure_with(user_settings)
end
unless ENV['CP_CACHE_DIR'].nil?
@cache_root = Pathname.new(ENV['CP_CACHE_DIR']).expand_path
end
end
def verbose
@verbose && !silent
end
public
#-------------------------------------------------------------------------#
# @!group Paths
# @return [Pathname] the directory where repos, templates and configuration
# files are stored.
#
def home_dir
@home_dir ||= Pathname.new(ENV['CP_HOME_DIR'] || '~/.cocoapods').expand_path
end
# @return [Pathname] the directory where the CocoaPods sources are stored.
#
def repos_dir
@repos_dir ||= Pathname.new(ENV['CP_REPOS_DIR'] || (home_dir + 'repos')).expand_path
end
attr_writer :repos_dir
# @return [Source::Manager] the source manager for the spec repos in `repos_dir`
#
def sources_manager
return @sources_manager if @sources_manager && @sources_manager.repos_dir == repos_dir
@sources_manager = Source::Manager.new(repos_dir)
end
# @return [Pathname] the directory where the CocoaPods templates are stored.
#
def templates_dir
@templates_dir ||= Pathname.new(ENV['CP_TEMPLATES_DIR'] || (home_dir + 'templates')).expand_path
end
# @return [Pathname] the root of the CocoaPods installation where the
# Podfile is located.
#
def installation_root
@installation_root ||= begin
current_dir = Pathname.new(Dir.pwd.unicode_normalize(:nfkc))
current_path = current_dir
until current_path.root?
if podfile_path_in_dir(current_path)
installation_root = current_path
unless current_path == current_dir
UI.puts("[in #{current_path}]")
end
break
else
current_path = current_path.parent
end
end
installation_root || current_dir
end
end
attr_writer :installation_root
alias_method :project_root, :installation_root
# @return [Pathname] The root of the sandbox.
#
def sandbox_root
@sandbox_root ||= installation_root + 'Pods'
end
attr_writer :sandbox_root
alias_method :project_pods_root, :sandbox_root
# @return [Sandbox] The sandbox of the current project.
#
def sandbox
@sandbox ||= Sandbox.new(sandbox_root)
end
# @return [Podfile] The Podfile to use for the current execution.
# @return [Nil] If no Podfile is available.
#
def podfile
@podfile ||= Podfile.from_file(podfile_path) if podfile_path
end
attr_writer :podfile
# @return [Lockfile] The Lockfile to use for the current execution.
# @return [Nil] If no Lockfile is available.
#
def lockfile
@lockfile ||= Lockfile.from_file(lockfile_path) if lockfile_path
end
# Returns the path of the Podfile.
#
# @note The Podfile can be named either `CocoaPods.podfile.yaml`,
# `CocoaPods.podfile` or `Podfile`. The first two are preferred as
# they allow to specify an OS X UTI.
#
# @return [Pathname]
# @return [Nil]
#
def podfile_path
@podfile_path ||= podfile_path_in_dir(installation_root)
end
# Returns the path of the Lockfile.
#
# @note The Lockfile is named `Podfile.lock`.
#
def lockfile_path
@lockfile_path ||= installation_root + 'Podfile.lock'
end
# Returns the path of the default Podfile pods.
#
# @note The file is expected to be named Podfile.default
#
# @return [Pathname]
#
def default_podfile_path
@default_podfile_path ||= templates_dir + 'Podfile.default'
end
# Returns the path of the default Podfile test pods.
#
# @note The file is expected to be named Podfile.test
#
# @return [Pathname]
#
def default_test_podfile_path
@default_test_podfile_path ||= templates_dir + 'Podfile.test'
end
# @return [Pathname] The file to use to cache the search data.
#
def search_index_file
cache_root + 'search_index.json'
end
private
#-------------------------------------------------------------------------#
# @!group Private helpers
# @return [Pathname] The path of the file which contains the user settings.
#
def user_settings_file
home_dir + 'config.yaml'
end
# Sets the values of the attributes with the given hash.
#
# @param [Hash{String,Symbol => Object}] values_by_key
# The values of the attributes grouped by key.
#
# @return [void]
#
def configure_with(values_by_key)
return unless values_by_key
values_by_key.each do |key, value|
if key.to_sym == :cache_root
value = Pathname.new(value).expand_path
end
instance_variable_set("@#{key}", value)
end
end
# @return [Array<String>] The filenames that the Podfile can have ordered
# by priority.
#
PODFILE_NAMES = [
'CocoaPods.podfile.yaml',
'CocoaPods.podfile',
'Podfile',
'Podfile.rb',
].freeze
public
# Returns the path of the Podfile in the given dir if any exists.
#
# @param [Pathname] dir
# The directory where to look for the Podfile.
#
# @return [Pathname] The path of the Podfile.
# @return [Nil] If not Podfile was found in the given dir
#
def podfile_path_in_dir(dir)
PODFILE_NAMES.each do |filename|
candidate = dir + filename
if candidate.file?
return candidate
end
end
nil
end
# Excludes the given dir from Time Machine backups.
#
# @param [Pathname] dir
# The directory to exclude from Time Machine backups.
#
# @return [void]
#
def exclude_from_backup(dir)
return if Gem.win_platform?
system('tmutil', 'addexclusion', dir.to_s, %i(out err) => File::NULL)
end
public
#-------------------------------------------------------------------------#
# @!group Singleton
# @return [Config] the current config instance creating one if needed.
#
def self.instance
@instance ||= new
end
# Sets the current config instance. If set to nil the config will be
# recreated when needed.
#
# @param [Config, Nil] the instance.
#
# @return [void]
#
class << self
attr_writer :instance
end
# Provides support for accessing the configuration instance in other
# scopes.
#
module Mixin
def config
Config.instance
end
end
end
end