Class: LgPodPlugin::LCache

Inherits:
Object
  • Object
show all
Defined in:
lib/lg_pod_plugin/downloader/l_cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLCache

Returns a new instance of LCache.



11
12
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 11

def initialize
end

Class Method Details

.cache_pod(name, target, options = {}, spec = nil, released_pod = false) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 164

def self.cache_pod(name, target, options = {}, spec = nil, released_pod = false)
  checkout_options = Hash.new.deep_merge(options).reject do |key, val|
    !key || !val
  end
  request = LCache.download_request(name, checkout_options, spec, released_pod)
  _, pods_pecs = get_local_spec(request, target)
  pods_pecs.each do |_, s_spec|
    destination = path_for_pod(request, :name => name, :params => checkout_options)
    if !File.exist?(destination) || destination.children.empty?
      LgPodPlugin.log_green "Copying #{name} from `#{target}` to `#{destination}` "
      copy_and_clean(target, destination, s_spec)
    end
    cache_pod_spec = path_for_spec(request, :name => name, :params => checkout_options)
    unless File.exist?(cache_pod_spec)
      write_spec(s_spec, cache_pod_spec)
    end
  end

end

.cached_spec(request) ⇒ Object



61
62
63
64
65
66
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 61

def self.cached_spec(request)
  path = path_for_spec(request)
  path.file? && Specification.from_file(path)
rescue JSON::ParserError
  nil
end

.clean_pod_unused_files(destination, spec) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 111

def self.clean_pod_unused_files(destination, spec)
  specs_by_platform = group_sub_specs_by_platform(spec)
  self.write_lock(destination) do
    Pod::Installer::PodSourcePreparer.new(spec, destination).prepare!
    Pod::Sandbox::PodDirCleaner.new(destination, specs_by_platform).clean!
  end
end

.copy_and_clean(source, destination, spec) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 100

def self.copy_and_clean(source, destination, spec)
  specs_by_platform = group_sub_specs_by_platform(spec)
  destination.parent.mkpath
  self.write_lock(destination) do
    FileUtils.rm_rf(destination)
    FileUtils.cp_r(source, destination)
    Pod::Installer::PodSourcePreparer.new(spec, destination).prepare!
    Pod::Sandbox::PodDirCleaner.new(destination, specs_by_platform).clean!
  end
end

.download_request(name, params, spec = nil, released_pod = false) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 42

def self.download_request(name, params, spec = nil, released_pod = false)
  if released_pod
    Pod::Downloader::Request.new(spec: spec, released: true , name: name, params: params)
  else
    Pod::Downloader::Request.new(spec: nil, released: false , name: name, params: params)
  end
end

.get_local_spec(request, target) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 68

def self.get_local_spec(request, target)
  result = Pod::Downloader::Response.new
  result.location = target
  if request.released_pod?
    result.spec = request.spec
    local_specs = { request.name => request.spec }
    return [request, local_specs]
  else
    local_specs = {}
    pods_pecs = Pod::Sandbox::PodspecFinder.new(target).podspecs
    pods_pecs[request.name] = request.spec if request.spec
    pods_pecs.each do |name, spec|
      if request.name == name
        result.spec = spec
        local_specs[request.name] = spec
      end
    end
  end
  [result, local_specs]
end

.group_sub_specs_by_platform(spec) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 89

def self.group_sub_specs_by_platform(spec)
  specs_by_platform = {}
  [spec, *spec.recursive_subspecs].each do |ss|
    ss.available_platforms.each do |platform|
      specs_by_platform[platform] ||= []
      specs_by_platform[platform] << ss
    end
  end
  specs_by_platform
end

.lock(location, lock_type) ⇒ Object

Raises:

  • (ArgumentError)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 123

def self.lock(location, lock_type)
  raise ArgumentError, 'no block given' unless block_given?
  lockfile = "#{location}.lock"
  f = nil
  loop do
    f.close if f
    f = File.open(lockfile, File::CREAT, 0o644)
    f.flock(lock_type)
    break if self.valid_lock?(f, lockfile)
  end
  begin
    yield location
  ensure
    if lock_type == File::LOCK_SH
      f.flock(File::LOCK_EX)
      File.delete(lockfile) if self.valid_lock?(f, lockfile)
    else
      File.delete(lockfile)
    end
    f.close
  end
end

.path_for_pod(request, slug_opts = {}) ⇒ Object

MARK - 缓存方法



50
51
52
53
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 50

def self.path_for_pod(request, slug_opts = {})
  root = self.root_path
  root + request.slug(**slug_opts)
end

.path_for_spec(request, slug_opts = {}) ⇒ Object



55
56
57
58
59
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 55

def self.path_for_spec(request, slug_opts = {})
  root = self.root_path
  path = root + 'Specs' + request.slug(**slug_opts)
  Pathname.new(path.to_path + '.podspec.json')
end

.root_pathObject



37
38
39
40
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 37

def self.root_path
  path = File.join(Dir.home, "Library/Caches/CocoaPods/Pods")
  Pathname(path)
end

.valid_lock?(file, filename) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 146

def self.valid_lock?(file, filename)
  file.stat.ino == File.stat(filename).ino
rescue Errno::ENOENT
  false
end

.write_lock(location, &block) ⇒ Object



119
120
121
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 119

def self.write_lock(location, &block)
  self.lock(location, File::LOCK_EX, &block)
end

.write_spec(spec, path) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 153

def self.write_spec(spec, path)
  path.dirname.mkpath
  Pod::Downloader::Cache.write_lock(path) do
    path.open('w') { |f|
      f.write spec.to_pretty_json
    }
  end
end

Instance Method Details

#find_pod_cache(name, options, spec = nil, released_pod = false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 26

def find_pod_cache(name, options, spec = nil, released_pod = false)
  hash_map = Hash.new.merge!(options)
  if hash_map.has_key?(:version)
    hash_map.delete(:version)
  end
  request = LCache.download_request(name, hash_map, spec, released_pod)
  destination = LCache.path_for_pod(request, {})
  cache_pod_spec = LCache.path_for_spec(request, {})
  [destination, cache_pod_spec]
end

#pod_cache_exist(name, options, spec = nil, released_pod = false) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/lg_pod_plugin/downloader/l_cache.rb', line 15

def pod_cache_exist(name, options, spec = nil, released_pod = false)
  destination, cache_pod_spec = self.find_pod_cache name, options, spec, released_pod
  if (File.exist?(destination) && !destination.children.empty?) && cache_pod_spec.exist?
    return [true, destination, cache_pod_spec]
  else
    [false, destination, cache_pod_spec]
  end
end