-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
pod_dir_cleaner.rb
71 lines (64 loc) · 2.34 KB
/
pod_dir_cleaner.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
module Pod
class Sandbox
class PodDirCleaner
attr_reader :root
attr_reader :specs_by_platform
def initialize(root, specs_by_platform)
@root = root
@specs_by_platform = specs_by_platform
end
# Removes all the files not needed for the installation according to the
# specs by platform.
#
# @return [void]
#
def clean!
FileUtils.rm_rf(clean_paths) if root.exist?
end
private
# @return [Array<Sandbox::FileAccessor>] the file accessors for all the
# specifications on their respective platform.
#
def file_accessors
@file_accessors ||= specs_by_platform.flat_map do |platform, specs|
specs.flat_map { |spec| Sandbox::FileAccessor.new(path_list, spec.consumer(platform)) }
end
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).
#
# @todo The paths are down-cased for the comparison as issues similar
# to #602 lead the files not being matched and so cleaning all
# the files. This solution might create side effects.
#
# @return [Array<Strings>] The paths that can be deleted.
#
def clean_paths
cached_used = used_files.map(&:downcase)
glob_options = File::FNM_DOTMATCH | File::FNM_CASEFOLD
files = Pathname.glob(root + '**/*', glob_options).map(&:to_s)
cached_used_set = cached_used.to_set
files.reject do |candidate|
candidate = candidate.downcase
candidate.end_with?('.', '..') || cached_used_set.include?(candidate) || cached_used.any? do |path|
path.include?(candidate) || candidate.include?(path)
end
end
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
FileAccessor.all_files(file_accessors).map(&:to_s)
end
end
end
end