Class: ElmInstall::GitResolver
- 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
Class Method Summary collapse
-
.refs(url) ⇒ Hash
Returns the refs for a given url.
Instance Method Summary collapse
-
#clear ⇒ void
Clears the cache.
-
#clone(url, path) ⇒ Git::Base
Clones the repostiry from the given url to the given path.
-
#initialize(options) ⇒ GitResolver
constructor
Initializes a git resolver with the given options.
-
#open(url) {|repo| ... } ⇒ Git::Base
Opens a git repository cloning if it’s not exists.
-
#package?(url) ⇒ Boolean
Returns if the given package (url) is in the cache.
-
#refs(url) ⇒ Hash
Returns the refs for a given url.
-
#repository(url) ⇒ Git::Base
Returns a git repository object for the given url, cloning it if it does not exists.
-
#repository_path(url) ⇒ String
Returns the path of the repository for a given url.
-
#update_cache(repo) ⇒ void
Updates a repository checking it’s refs and fetching changes if needed.
Methods inherited from Base
#directory, #file, #load, #save
Constructor Details
#initialize(options) ⇒ GitResolver
Initializes a git resolver with the given options.
10 11 12 13 14 15 |
# File 'lib/elm_install/git_resolver.rb', line 10 def initialize() @file = 'ref-cache.json' super @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.
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
#clear ⇒ void
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.
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.
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.
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.
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.
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
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.
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 |