Module: Spitball::Repo

Extended by:
Repo
Included in:
Repo
Defined in:
lib/spitball/repo.rb

Constant Summary collapse

WORKING_DIR =
ENV['SPITBALL_CACHE'] || "/tmp/spitball-#{ENV['USER']}"

Instance Method Summary collapse

Instance Method Details

#bundle_path(digest, extension = nil) ⇒ Object



6
7
8
9
# File 'lib/spitball/repo.rb', line 6

def bundle_path(digest, extension = nil)
  extension = ".#{extension}" unless extension.nil? or extension.empty?
  File.join WORKING_DIR, "bundle_#{digest}#{extension}"
end

#cached_digestsObject



27
28
29
30
31
# File 'lib/spitball/repo.rb', line 27

def cached_digests
  Dir[File.join(WORKING_DIR, 'bundle_*.tgz')].map do |path|
    path.match(/bundle_(.*?)\.tgz$/)[1]
  end.compact.uniq.sort
end

#clean_up_unused(access_window) ⇒ Object



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

def clean_up_unused(access_window)
  cutoff_time = Time.now - access_window

  cached_digests.each do |digest|
    stat = File.stat(tarball(digest))
    access_time = [stat.atime, stat.mtime].max

    if access_time < cutoff_time
      File.unlink(tarball(digest)) if File.exist? tarball(digest)
      File.unlink(gemfile(digest)) if File.exist? gemfile(digest)
    end
  end
end

#exist?(digest) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/spitball/repo.rb', line 19

def exist?(digest)
  File.exist? tarball(digest)
end

#gemcache_pathObject



11
12
13
# File 'lib/spitball/repo.rb', line 11

def gemcache_path
  File.join(WORKING_DIR, "gemcache")
end

#gemfile(digest) ⇒ Object



15
16
17
# File 'lib/spitball/repo.rb', line 15

def gemfile(digest)
  bundle_path digest, 'gemfile'
end

#make_cache_dirsObject



33
34
35
# File 'lib/spitball/repo.rb', line 33

def make_cache_dirs
  FileUtils.mkdir_p File.join(WORKING_DIR, "gemcache")
end

#tarball(digest) ⇒ Object



23
24
25
# File 'lib/spitball/repo.rb', line 23

def tarball(digest)
  bundle_path(digest, 'tgz')
end