Module: GitBinaryCache

Defined in:
lib/git_binary_cache.rb,
lib/git_binary_cache/manager.rb

Defined Under Namespace

Classes: CacheCorruptedError, CacheError, CacheMissingError, Error, Manager, OutdatedCacheError, RevisionError, UnknownRevisionError

Class Method Summary collapse

Class Method Details

.check_cache_for_config(cache_name, cache_config) ⇒ Object


Checks if specific cache exists and in a state that meets the demands listed in the config

If there are any issues with the cache, throws an exception explaning the problem.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/git_binary_cache.rb', line 101

def self.check_cache_for_config(cache_name, cache_config)
  log("Checking binary cache status for cache: #{cache_name}")
  manager = manager_for_config(cache_name, cache_config)

  # Check cache status (either a specific revision or the cache existence)
  rev = cache_config['revision']
  if rev
    log("Checking if the cache has revision #{rev} has already been applied...")
    manager.need_revision!(rev)
  else
    log("Checking if the cache exists...")
    manager.need_cache!
  end
end

.check_cache_for_target(target) ⇒ Object




37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/git_binary_cache.rb', line 37

def self.check_cache_for_target(target)
  cache_config_file = config_file_for_target(target)
  return unless File.readable?(cache_config_file)

  config = load_yaml_config(cache_config_file)
  config.each do |cache_name, cache_config|
    check_cache_for_config(cache_name, cache_config)
  end

  project_dir = File.dirname(cache_config_file)
  create_symlinks_for_project(project_dir, config)
end

.config_file_for_target(target) ⇒ Object


Raises:

  • (ArgumentError)


30
31
32
33
34
# File 'lib/git_binary_cache.rb', line 30

def self.config_file_for_target(target)
  return target if File.file?(target)
  return File.join(target, 'git-binary-cache.yml') if File.directory?(target)
  raise ArgumentError, "Invalid TARGET value: should be a yaml file or a directory!"
end



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/git_binary_cache.rb', line 65

def self.create_symlinks_for_project(project_dir, config)
  config.each do |cache_name, cache_config|
    create_link = cache_config['create_link'] || cache_config.has_key?('link_name')
    next unless create_link

    link_name = cache_config['link_name'] || cache_name
    link_path = File.join(project_dir, link_name)
    manager = manager_for_config(cache_name, cache_config)

    # If something exists with the name of our link
    if File.exists?(link_path)
      # Name collision
      unless File.symlink?(link_path)
        error = "Could not create link '#{link_name}' for cache '#{cache_name}' because of a name collision with an existing file or directory!"
        raise Error, error
      end

      # Link exists and is correct
      next if File.readlink(link_path) == manager.cache_dir

      # Drop existing link
      log("Link '#{link_name}' for cache '#{cache_name}' exists, but is pointing to a wrong place, fixing!")
      File.unlink(link_path)
    end

    # Create the link
    log("Creating cache link: #{link_name}...")
    File.symlink(manager.cache_dir, link_path)
  end
end

.log(message) ⇒ Object



24
25
26
27
# File 'lib/git_binary_cache.rb', line 24

def self.log(message)
  return unless logger
  logger.info("git-binary-cache: #{message}")
end

.loggerObject



20
21
22
# File 'lib/git_binary_cache.rb', line 20

def self.logger
  @logger
end

.logger=(logger) ⇒ Object




16
17
18
# File 'lib/git_binary_cache.rb', line 16

def self.logger=(logger)
  @logger = logger
end

.update_cache_for_config(cache_name, cache_config) ⇒ Object


Makes sure a specific cache exists and in a state that meets the demands listed in the config



118
119
120
121
122
123
# File 'lib/git_binary_cache.rb', line 118

def self.update_cache_for_config(cache_name, cache_config)
  log("Updating binary cache: #{cache_name}")
  manager = manager_for_config(cache_name, cache_config)
  manager.update_cache!(cache_config['revision'])
  log("Done updating binary cache: #{cache_name}")
end

.update_cache_for_target(target) ⇒ Object




51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/git_binary_cache.rb', line 51

def self.update_cache_for_target(target)
  cache_config_file = config_file_for_target(target)
  return unless File.readable?(cache_config_file)

  config = load_yaml_config(cache_config_file)
  config.each do |cache_name, cache_config|
    update_cache_for_config(cache_name, cache_config)
  end

  project_dir = File.dirname(cache_config_file)
  create_symlinks_for_project(project_dir, config)
end