Class: Pod::Downloader::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/bd_pod_cache_lock.rb

Overview

The class responsible for managing Pod downloads, transparently caching them in a cache directory.

Instance Method Summary collapse

Instance Method Details

#flock(file, mode) ⇒ Boolean

Returns The result of lock specific file and mode.

Parameters:

  • file (File)

    should be locked file.

  • mode (mode)

    the file lock mode.

Returns:

  • (Boolean)

    The result of lock specific file and mode.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bd_pod_cache_lock.rb', line 82

def flock(file, mode)
  success = file.flock(mode)
  if success
    begin
      yield file
    ensure
      file.flock(File::LOCK_UN)
    end
  end
  success
end

#lock_path_for_pod(request, slug_opts = {}) ⇒ String

Returns The lock file path for the Pod downloaded from the given ‘request`.

Parameters:

  • request (Request)

    the request to be downloaded.

  • slug_opts (Hash<Symbol,String>) (defaults to: {})

    the download options that should be used in constructing the cache slug for this request.

Returns:

  • (String)

    The lock file path for the Pod downloaded from the given ‘request`.



34
35
36
37
38
39
# File 'lib/bd_pod_cache_lock.rb', line 34

def lock_path_for_pod(request, slug_opts = {})
  lock_dir = root + "CacheLock"
  Dir.mkdir($pod_cache_lock_dir) unless Dir.exist?($pod_cache_lock_dir)
  request_sha1 = Digest::SHA1.hexdigest(request.slug(slug_opts))
  return ($pod_cache_lock_dir + request_sha1).to_s + '.lock'
end

#uncached_pod(request) ⇒ Response

Returns The download response for the given ‘request` that was not found in the download cache.

Parameters:

  • request (Request)

    the request to be downloaded.

Returns:

  • (Response)

    The download response for the given ‘request` that was not found in the download cache.



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
72
# File 'lib/bd_pod_cache_lock.rb', line 47

def uncached_pod(request)
  in_tmpdir do |target|
    result, podspecs = download(request, target)
    result.location = nil

    podspecs.each do |name, spec|
      destination = path_for_pod(request, :name => name, :params => result.checkout_options)

      destination.parent.mkpath
      lock_file_path = lock_path_for_pod(request, :name => name, :params => result.checkout_options)

      puts lock_file_path
      File.new(lock_file_path, File::CREAT) unless File.exist? lock_file_path
      File.open(lock_file_path) do |file|
        flock(file, File::LOCK_EX) { copy_and_clean(target, destination, spec) }
      end

      write_spec(spec, path_for_spec(request, :name => name, :params => result.checkout_options))
      if request.name == name
        result.location = destination
      end
    end

    result
  end
end