Class: ElmInstall::GitResolver

Inherits:
Base
  • Object
show all
Defined in:
lib/elm_install/git_resolver.rb

Overview

This class if for cloning and fetching repositories based on a cache.

Instance Attribute Summary

Attributes inherited from Base

#cache

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#directory, #file, #load, #save

Constructor Details

#initialize(options) ⇒ GitResolver

Returns a new instance of GitResolver.



7
8
9
10
11
12
# File 'lib/elm_install/git_resolver.rb', line 7

def initialize(options)
  @file = 'ref-cache.json'
  super options
  @check_cache =
    @cache.keys.each_with_object({}) { |key, memo| memo[key] = true }
end

Class Method Details

.refs(url) ⇒ Object



22
23
24
25
26
# File 'lib/elm_install/git_resolver.rb', line 22

def self.refs(url)
  refs = Git.ls_remote url
  refs.delete 'head'
  JSON.parse(refs.to_json)
end

Instance Method Details

#clearObject



18
19
20
# File 'lib/elm_install/git_resolver.rb', line 18

def clear
  @check_cache = {}
end

#clone(url, path) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/elm_install/git_resolver.rb', line 71

def clone(url, path)
  Logger.arrow "Package: #{url.bold} not found in cache, cloning..."
  FileUtils.mkdir_p path
  repo = Git.clone(url, path)
  @check_cache[path] = true
  cache[path] = refs url
  repo
end

#open(url) {|repo| ... } ⇒ Object

Yields:

  • (repo)


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/elm_install/git_resolver.rb', line 58

def open(url)
  path = repository_path(url)

  return clone url, path unless Dir.exist?(path)

  repo = Git.open path
  repo.reset_hard

  yield repo unless @check_cache[path]

  repo
end

#package?(url) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/elm_install/git_resolver.rb', line 28

def package?(url)
  @check_cache.key?(repository_path(url))
end

#refs(url) ⇒ Object



14
15
16
# File 'lib/elm_install/git_resolver.rb', line 14

def refs(url)
  self.class.refs(url)
end

#repository(url) ⇒ Object



38
39
40
41
42
# File 'lib/elm_install/git_resolver.rb', line 38

def repository(url)
  open(url) do |repo|
    update_cache repo
  end
end

#repository_path(url) ⇒ Object

:reek:FeatureEnvy



33
34
35
36
# File 'lib/elm_install/git_resolver.rb', line 33

def repository_path(url)
  uri = GitCloneUrl.parse(url)
  File.join(directory, uri.host, uri.path)
end

#update_cache(repo) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/elm_install/git_resolver.rb', line 44

def update_cache(repo)
  directory = File.dirname(repo.repo.path)
  url = repo.remote.url
  refs = refs(url)

  unless HashDiff.diff(cache[directory], refs).empty?
    Logger.arrow "Package: #{url.bold} is outdated, fetching changes..."
    repo.fetch
  end

  @check_cache[directory] = true
  cache[directory] = refs
end