Class: Pod::Prebuild::SharedCache

Inherits:
Object
  • Object
show all
Extended by:
Config::Mixin
Defined in:
lib/cocoapods-binary-gcp/helper/shared_cache.rb

Class Method Summary collapse

Class Method Details

.cache(target, input_path, options) ⇒ Object

Copies input_path to target’s cache



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cocoapods-binary-gcp/helper/shared_cache.rb', line 49

def self.cache(target, input_path, options)
    if not Podfile::DSL.shared_cache_enabled
        return
    end
    cache_path = framework_cache_path_for(target, options)
    cache_path.mkpath unless cache_path.exist?
    FileUtils.cp_r "#{input_path}/.", cache_path

    if Podfile::DSL.shared_gcp_cache_enabled
        cloud_path = cloud_framework_path_for(target, options)
        storage = Google::Cloud::Storage.new
        bucket = storage.bucket(Podfile::DSL.gcp_options[:bucket])

        Pod::UI.puts "Save to remote cache"
        Dir.mktmpdir {|dir|
            zip(cache_path, "#{dir}/framework.zip")
            bucket.create_file "#{dir}/framework.zip",
                                "#{cloud_path}"     
        }
    end
end

.cloud_framework_path_for(target, options) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/cocoapods-binary-gcp/helper/shared_cache.rb', line 82

def self.cloud_framework_path_for(target, options) 
    cloud_cache_path = Pathname.new('').to_s + xcode_version
    cloud_cache_path = cloud_cache_path + target.name
    cloud_cache_path = cloud_cache_path + target.version
    options_with_platform = options + [target.platform.name]
    cloud_cache_path = cloud_cache_path + Digest::MD5.hexdigest(options_with_platform.to_s).to_s
end

.framework_cache_path_for(target, options) ⇒ Pathname

Path of the target’s cache

Returns:

  • (Pathname)


74
75
76
77
78
79
80
# File 'lib/cocoapods-binary-gcp/helper/shared_cache.rb', line 74

def self.framework_cache_path_for(target, options)
    framework_cache_path = cache_root + xcode_version
    framework_cache_path = framework_cache_path + target.name
    framework_cache_path = framework_cache_path + target.version
    options_with_platform = options + [target.platform.name]
    framework_cache_path = framework_cache_path + Digest::MD5.hexdigest(options_with_platform.to_s).to_s
end

.has?(target, options) ⇒ Boolean

‘true` if there is cache for the target `false` otherwise

Returns:

  • (Boolean)


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
# File 'lib/cocoapods-binary-gcp/helper/shared_cache.rb', line 15

def self.has?(target, options)
    if Podfile::DSL.shared_cache_enabled
        path = framework_cache_path_for(target, options)
        if path.exist?
            Pod::UI.puts "Local cache for #{target.name} found"
            true
        else
            Pod::UI.puts "Local cache for #{target.name} not found"
            if Podfile::DSL.shared_gcp_cache_enabled
                cloud_path = cloud_framework_path_for(target, options)
                storage = Google::Cloud::Storage.new
                bucket = storage.bucket(Podfile::DSL.gcp_options[:bucket])
                file = bucket.file "#{cloud_path}"
                if not file.nil? 
                    Pod::UI.puts "GCP cache for #{target.name} found, downloading..."
                    Dir.mktmpdir {|dir|
                        file.download "#{dir}/framework.zip"
                        unzip("#{dir}/framework.zip", path)
                    }
                    true
                else
                    Pod::UI.puts "GCP cache for #{target.name} not found"
                    false
                end
            else
                false
            end
        end
    else
        false
    end
end

.unzip(zip, unzip_dir, remove_after = false) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/cocoapods-binary-gcp/helper/shared_cache.rb', line 104

def self.unzip(zip, unzip_dir, remove_after = false)
    Zip::File.open(zip) do |zip_file|
        zip_file.each do |f|
            f_path=File.join(unzip_dir, f.name)
            FileUtils.mkdir_p(File.dirname(f_path))
            zip_file.extract(f, f_path) unless File.exist?(f_path)
        end
    end
    FileUtils.rm(zip) if remove_after
end

.zip(dir, zip_dir) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cocoapods-binary-gcp/helper/shared_cache.rb', line 90

def self.zip(dir, zip_dir)
    Zip::File.open(zip_dir, Zip::File::CREATE)do |zipfile|
        Find.find(dir) do |path|
            Find.prune if File.basename(path)[0] == ?.
            dest = /#{dir}\/(\w.*)/.match(path)
            # Skip files if they exists
            begin
                zipfile.add(dest[1],path) if dest
            rescue Zip::ZipEntryExistsError
            end
        end
    end
end