Class: LgPodPlugin::LCache
- Inherits:
-
Object
- Object
- LgPodPlugin::LCache
- Defined in:
- lib/lg_pod_plugin/l_cache.rb
Class Method Summary collapse
-
.cache_pod(name, target, options = {}) ⇒ Object
拷贝 pod 缓存文件到 sandbox.
- .cached_spec(request) ⇒ Object
- .copy_and_clean(source, destination, spec) ⇒ Object
- .download_request(name, params) ⇒ Object
- .get_local_spec(request, target) ⇒ Object
- .group_sub_specs_by_platform(spec) ⇒ Object
- .lock(location, lock_type) ⇒ Object
- .path_for_pod(request, slug_opts = {}) ⇒ Object
- .path_for_spec(request, slug_opts = {}) ⇒ Object
- .root_path ⇒ Object
- .valid_lock?(file, filename) ⇒ Boolean
- .write_lock(location, &block) ⇒ Object
- .write_spec(spec, path) ⇒ Object
Instance Method Summary collapse
-
#find_pod_cache(name, git, branch, tag, commit, is_update) ⇒ Object
判断缓存是否存在且有效命中缓存.
-
#get_download_path(name) ⇒ Object
根据下载参数生产缓存的路径.
-
#get_request_params(git, branch, tag, commit) ⇒ Object
根据git branch commit 返回请求参数用来获取缓存 path.
-
#initialize(workspace) ⇒ LCache
constructor
A new instance of LCache.
-
#slug(params, spec) ⇒ Object
根据下载参数生产缓存目录.
Constructor Details
#initialize(workspace) ⇒ LCache
Returns a new instance of LCache.
23 24 25 26 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 23 def initialize(workspace) self.workspace = workspace self.cache_root = LFileManager.cache_workspace(self.workspace) end |
Class Method Details
.cache_pod(name, target, options = {}) ⇒ Object
拷贝 pod 缓存文件到 sandbox
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 182 def self.cache_pod(name, target, = {}) request = LCache.download_request(name, ) result, pods_pecs = get_local_spec(request, target) result.location = nil pods_pecs.each do |s_name, s_spec| destination = path_for_pod(request, {}) unless File.exist?(destination) LgPodPlugin.log_green "Copying #{name} from `#{target}` to `#{destination}` " copy_and_clean(target, destination, s_spec) end cache_pod_spec = path_for_spec(request, {}) unless File.exist?(cache_pod_spec) write_spec(s_spec, cache_pod_spec) end if request.name == s_name result.location = destination end end end |
.cached_spec(request) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 90 def self.cached_spec(request) path = path_for_spec(request) path.file? && Specification.from_file(path) rescue JSON::ParserError nil end |
.copy_and_clean(source, destination, spec) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 130 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) ⇒ Object
75 76 77 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 75 def self.download_request(name, params) Pod::Downloader::Request.new(spec: nil, released: false, name: name, params: params) end |
.get_local_spec(request, target) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 97 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
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 119 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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 145 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
79 80 81 82 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 79 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
84 85 86 87 88 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 84 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_path ⇒ Object
70 71 72 73 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 70 def self.root_path path = File.join(Dir.home, "Library/Caches/CocoaPods/Pods") Pathname(path) end |
.valid_lock?(file, filename) ⇒ Boolean
168 169 170 171 172 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 168 def self.valid_lock?(file, filename) file.stat.ino == File.stat(filename).ino rescue Errno::ENOENT false end |
.write_lock(location, &block) ⇒ Object
141 142 143 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 141 def self.write_lock(location, &block) self.lock(location, File::LOCK_EX, &block) end |
.write_spec(spec, path) ⇒ Object
174 175 176 177 178 179 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 174 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, git, branch, tag, commit, is_update) ⇒ Object
判断缓存是否存在且有效命中缓存
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 47 def find_pod_cache(name, git, branch, tag, commit, is_update) if is_update hash_map = self.get_request_params(git, branch, tag, commit) else if LRequest.shared.lock_params lock_tag = LRequest.shared.lock_params[:tag] lock_branch = LRequest.shared.lock_params[:branch] lock_commit = LRequest.shared.lock_params[:commit] hash_map = self.get_request_params(git, lock_branch, lock_tag, lock_commit) else hash_map = self.get_request_params(git, branch, tag, commit) end end request = LCache.download_request(name, hash_map) destination = LCache.path_for_pod(request, {}) cache_pod_spec = LCache.path_for_spec(request, {}) if File.exist?(destination) && File.exist?(cache_pod_spec) false else true end end |
#get_download_path(name) ⇒ Object
根据下载参数生产缓存的路径
205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 205 def get_download_path(name) # hash_map = {:git => git} # if git && tag # hash_map[:tag] = tag # elsif git && commit # hash_map[:commit] = commit # elsif git && branch # hash_map[:commit] = commit # end # request = LCache.download_request(name, hash_map) # self.slug(name, request.params, nil) self.cache_root.join(name) end |
#get_request_params(git, branch, tag, commit) ⇒ Object
根据git branch commit 返回请求参数用来获取缓存 path
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 29 def get_request_params(git, branch, tag, commit) = { :git => git } if git && tag [:tag] = tag [:commit] = commit elsif git && branch if commit [:commit] = commit else new_commit_id = LGitUtil.git_ls_remote_refs(git, branch, nil, commit) [:commit] = new_commit_id end elsif git && commit [:commit] = commit end end |
#slug(params, spec) ⇒ Object
根据下载参数生产缓存目录
220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/lg_pod_plugin/l_cache.rb', line 220 def slug(params, spec) path = "" checksum = spec&.checksum && '-' << spec.checksum[0, 5] opts = params.to_a.sort_by(&:first).map { |k, v| "#{k}=#{v}" }.join('-') digest = Digest::MD5.hexdigest(opts) if digest path += "#{digest}" end if checksum path += "#{checksum}" end path end |