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

Initializes a git resolver with the given options.

Parameters:

  • options (Hash)

    The options



10
11
12
13
14
15
# File 'lib/elm_install/git_resolver.rb', line 10

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) ⇒ Hash

Returns the refs for a given url.

Parameters:

  • url (String)

    the url

Returns:

  • (Hash)

    The refs



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

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

Instance Method Details

#clearvoid

This method returns an undefined value.

Clears the cache



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

def clear
  @check_cache = {}
end

#clone(url, path) ⇒ Git::Base

Clones the repostiry from the given url to the given path.

Parameters:

  • url (String)

    The url

  • path (String)

    The path

Returns:

  • (Git::Base)

    The repository



120
121
122
123
124
125
126
127
# File 'lib/elm_install/git_resolver.rb', line 120

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| ... } ⇒ Git::Base

Opens a git repository cloning if it’s not exists.

Parameters:

  • url (String)

    The url

Yields:

  • (repo)

Returns:

  • (Git::Base)

    The repository



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

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 if the given package (url) is in the cache.

Parameters:

  • url (String)

    The url

Returns:

  • (Boolean)

    True if exists false if not



49
50
51
# File 'lib/elm_install/git_resolver.rb', line 49

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

#refs(url) ⇒ Hash

Returns the refs for a given url.

Parameters:

  • url (String)

    the url

Returns:

  • (Hash)

    The refs



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

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

#repository(url) ⇒ Git::Base

Returns a git repository object for the given url, cloning it if it does not exists.

Parameters:

  • url (String)

    The url

Returns:

  • (Git::Base)

    The repository



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

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

#repository_path(url) ⇒ String

Returns the path of the repository for a given url.

:reek:FeatureEnvy

Parameters:

  • url (String)

    The url

Returns:

  • (String)

    The path



60
61
62
63
# File 'lib/elm_install/git_resolver.rb', line 60

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

#update_cache(repo) ⇒ void

This method returns an undefined value.

Updates a repository checking it’s refs and fetching changes if needed.

Parameters:

  • repo (Git::Base)

    The repository



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

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