Commit deaffc9c authored by Samuel E. Giddins's avatar Samuel E. Giddins

First go at caching

parent 0f562c02
...@@ -82,6 +82,10 @@ module Pod ...@@ -82,6 +82,10 @@ module Pod
@cache_root @cache_root
end end
def download_cache
@download_cache ||= Downloader::Cache.new(cache_root + 'Pods')
end
public public
#-------------------------------------------------------------------------# #-------------------------------------------------------------------------#
......
...@@ -3,6 +3,10 @@ require 'claide/informative_error' ...@@ -3,6 +3,10 @@ require 'claide/informative_error'
module Pod module Pod
module Downloader module Downloader
require 'cocoapods/downloader/analyzer'
require 'cocoapods/downloader/cache'
require 'cocoapods/downloader/cleaner'
class DownloaderError; include CLAide::InformativeError; end class DownloaderError; include CLAide::InformativeError; end
class Base class Base
......
module Pod
module Downloader
class Analyzer
attr_reader :root
def initialize(root)
@root = root
end
end
end
end
require 'fileutils'
require 'tmpdir'
module Pod
module Downloader
class Cache
class MockSandbox
attr_accessor :spec, :target, :checkout_options
def initialize(target)
@target = target
@specs = []
end
def pod_dir(_name)
target
end
def store_podspec(_name, podspec, _external_source = false, json = false)
if podspec.is_a? String
podspec = Specification.from_string(podspec, "pod.podspec#{'.json' if json}")
elsif podspec.is_a? Pathname
podspec = Specification.from_file(podspec)
end
spec = podspec
end
def store_pre_downloaded_pod(*_); end
def store_checkout_source(_name, source)
checkout_options = source
end
def local?(_); false end
end
attr_accessor :root
def initialize(root)
@root = root
root.mkpath unless root.exist?
end
def cache_key(pod_name, version = nil, downloader_opts = nil)
raise ArgumentError unless pod_name || (!version && !downloader_opts)
pod_name =
if version
"Release/#{pod_name}/#{version}"
elsif downloader_opts
opts = downloader_opts.to_a.sort_by(&:first).map { |k, v| "#{k}=#{v}" }.join('-')
"External/#{pod_name}/#{opts}"
end
end
def path_for_pod(name, version = nil, downloader_opts = nil)
root + cache_key(name, version, downloader_opts)
end
def download_pod(name_or_spec, downloader_opts = nil, head = false)
if name_or_spec.is_a? Pod::Specification
spec = name_or_spec.root
name, version, downloader_opts = spec.name, spec.version, spec.source.dup
else
name = Specification.root_name(name_or_spec.to_s)
end
destination = path_for_pod(name, version, downloader_opts)
return destination if destination.directory? && !head
source = ExternalSources::DownloaderSource.new(name, downloader_opts, nil)
Dir.mktmpdir do |tmpdir|
tmpdir = Pathname(tmpdir)
mock_sandbox = MockSandbox.new(tmpdir)
source.fetch(mock_sandbox)
if spec
destination = path_for_pod(name, version)
else
spec = mock_sandbox.spec
checkout_options = mock_sandbox.checkout_options
destination = path_for_pod(found_spec.name, nil, mock_sandbox.checkout_options)
end
copy_and_clean(tmpdir, destination, spec)
FileUtils.touch(tmpdir)
[destination, checkout_options]
end
end
def copy_and_clean(source, destination, spec)
specs_by_platform = {}
spec.available_platforms.each do |platform|
specs_by_platform[platform] = spec.recursive_subspecs.select { |ss| ss.supported_on_platform?(platform) }
end
pod_source_installer = Installer::PodSourceInstaller.new(MockSandbox.new(source), specs_by_platform)
pod_source_installer.clean!
destination.parent.mkpath unless destination.parent.exist?
FileUtils.move(source, destination)
end
end
end
end
module Pod
module Downloader
class Cleaner
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!
clean_paths.each { |path| FileUtils.rm_rf(path) } if root.exist?
end
private
# @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).
#
# @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
glob_options = File::FNM_DOTMATCH | File::FNM_CASEFOLD
files = Pathname.glob(root + '**/*', glob_options).map(&:to_s)
files.reject! do |candidate|
candidate = candidate.downcase
candidate.end_with?('.', '..') || cached_used.any? do |path|
path = path.downcase
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(&:vendored_frameworks),
file_accessors.map(&:vendored_libraries),
file_accessors.map(&:resource_bundle_files),
file_accessors.map(&:license),
file_accessors.map(&:prefix_header),
file_accessors.map(&:preserve_paths),
file_accessors.map(&:readme),
file_accessors.map(&:resources),
file_accessors.map(&:source_files),
]
files.flatten.compact.map(&:to_s).uniq
end
end
end
end
...@@ -298,6 +298,8 @@ module Pod ...@@ -298,6 +298,8 @@ module Pod
end end
end end
config.download_cache.download_pod(specs_by_platform.values.flatten.first.root)
@pod_installers ||= [] @pod_installers ||= []
pod_installer = PodSourceInstaller.new(sandbox, specs_by_platform) pod_installer = PodSourceInstaller.new(sandbox, specs_by_platform)
pod_installer.install! pod_installer.install!
......
...@@ -135,7 +135,8 @@ module Pod ...@@ -135,7 +135,8 @@ module Pod
# @return [void] # @return [void]
# #
def clean_installation def clean_installation
clean_paths.each { |path| FileUtils.rm_rf(path) } if root.exist? cleaner = Downloader::Cleaner.new(root, specs_by_platform)
cleaner.clean!
end end
#-----------------------------------------------------------------------# #-----------------------------------------------------------------------#
...@@ -195,78 +196,6 @@ module Pod ...@@ -195,78 +196,6 @@ module Pod
end 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).
#
# @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
glob_options = File::FNM_DOTMATCH | File::FNM_CASEFOLD
files = Pathname.glob(root + '**/*', glob_options).map(&:to_s)
files.reject! do |candidate|
candidate = candidate.downcase
candidate.end_with?('.', '..') || cached_used.any? do |path|
path = path.downcase
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(&:vendored_frameworks),
file_accessors.map(&:vendored_libraries),
file_accessors.map(&:resource_bundle_files),
file_accessors.map(&:license),
file_accessors.map(&:prefix_header),
file_accessors.map(&:preserve_paths),
file_accessors.map(&:readme),
file_accessors.map(&:resources),
file_accessors.map(&:source_files),
]
files.flatten.compact.map(&:to_s).uniq
end
#-----------------------------------------------------------------------#
end end
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment