Skip to content

Commit

Permalink
Added logic for performant update.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdotdesign committed Dec 24, 2016
1 parent e1cf58f commit 9cb9d16
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 23 deletions.
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ PATH
remote: .
specs:
elm_install (0.1.0)
colorize (~> 0.8.1)
commander (~> 4.4, >= 4.4.2)
git (~> 1.3)
git_clone_url (~> 2.0)
hashdiff (~> 0.3.1)
smart_colored (~> 1.1, >= 1.1.1)
solve (~> 3.1)

GEM
Expand All @@ -22,7 +23,6 @@ GEM
virtus (~> 1.0)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
colorize (0.8.1)
commander (4.4.2)
highline (~> 1.7.2)
descendants_tracker (0.0.4)
Expand All @@ -42,6 +42,7 @@ GEM
git (1.3.0)
git_clone_url (2.0.0)
uri-ssh_git (>= 2.0)
hashdiff (0.3.1)
highline (1.7.8)
ice_nine (0.11.2)
launchy (2.4.3)
Expand Down Expand Up @@ -90,6 +91,7 @@ GEM
virtus (~> 1.0)
semverse (2.0.0)
sexp_processor (4.7.0)
smart_colored (1.1.1)
solve (3.1.0)
molinillo (>= 0.5)
semverse (>= 1.1, < 3.0)
Expand Down
3 changes: 2 additions & 1 deletion elm_install.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Gem::Specification.new do |s|
s.add_dependency 'git_clone_url', '~> 2.0'
s.add_dependency 'solve', '~> 3.1'
s.add_dependency 'commander', '~> 4.4', '>= 4.4.2'
s.add_dependency 'colorize', '~> 0.8.1'
s.add_dependency 'smart_colored', '~> 1.1', '>= 1.1.1'
s.add_dependency 'hashdiff', '~> 0.3.1'

s.extra_rdoc_files = ['Readme.md']
end
3 changes: 2 additions & 1 deletion lib/elm_install.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'smart_colored/extend'
require 'git_clone_url'
require 'forwardable'
require 'fileutils'
require 'colorize'
require 'hashdiff'
require 'solve'
require 'json'
require 'git'
Expand Down
47 changes: 34 additions & 13 deletions lib/elm_install/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@ class Cache
# Initializes a new cache with the given options.
def initialize(options = {})
@options = options
@ref_cache = {}
@cache = {}
load
end

# Saves the cache into the json file.
def save
File.binwrite(ref_file, JSON.pretty_generate(@ref_cache))
File.binwrite(file, JSON.pretty_generate(@cache))
end

def clear
@cache = {}
end

# Loads a cache from the json file.
def load
@ref_cache = JSON.parse(File.read(ref_file))
@cache = JSON.parse(File.read(file))
rescue
clear
@ref_cache = {}
@cache = {}
end

def clear
@ref_cache = {}
end

# Returns the directory where the cache is stored.
Expand All @@ -42,7 +46,7 @@ def directory

# Returns if there is a package in the cache (with at least one version).
def package?(package)
@cache.key?(package)
@ref_cache.key?(package) && @cache.key?(package)
end

# Adds a new dependency to the cache for a given package & version
Expand Down Expand Up @@ -75,21 +79,38 @@ def repository(path)
if Dir.exist?(repo_path)
repo = Git.open(repo_path)
repo.reset_hard
unless package?(path)
Utils.log_with_dot "
Package: #{path} maybe not be up to date fetching changes...
".strip
repo.fetch

unless @ref_cache[path]
refs = refs_for(repo_path)

if HashDiff.diff(@ref_cache[path], refs).empty?
Utils.log_with_arrow "Package: #{path.bold} is outdated fetching changes..."
repo.fetch
end

@ref_cache[path] = refs
end

repo
else
Utils.log_with_dot "Package: #{path} not found in cache, cloning..."
Git.clone(path, repo_path)
Utils.log_with_arrow "Package: #{path.bold} not found in cache, cloning..."
repo = Git.clone(path, repo_path)
@ref_cache[path] = refs_for(repo_path)
repo
end
end

private

def refs_for(repo_path)
refs = Git.ls_remote(repo_path)
refs.delete('head')
end

def ref_file
File.join(directory, 'ref-cache.json')
end

# Returns the path to the json file.
def file
File.join(directory, 'cache.json')
Expand Down
26 changes: 20 additions & 6 deletions lib/elm_install/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,32 @@ def install
begin
populate_elm_stuff
rescue
puts ' ▶ Could not find a solution in local cache, refreshing packages...'

@cache.clear
resolver.add_constraints dependencies
populate_elm_stuff

begin
populate_elm_stuff
rescue Solve::Errors::NoSolutionError => e
puts 'Could not find a solution:'
puts indent(e.to_s)
end
end
end

puts 'Saving index cache...'
@cache.save
private

puts 'Packages configured successfully!'
def indent(str)
str.split("\n").map { |s| " #{s}" }.join("\n")
end

private
def end_sucessfully
puts 'Saving package cache...'
@cache.save

puts 'Packages configured successfully!'
end
# Populates the `elm-stuff` directory with the packages from
# the solution.
def populate_elm_stuff
Expand All @@ -45,6 +58,7 @@ def populate_elm_stuff
end

write_exact_dependencies
end_sucessfully
end

# Resolves and copies a package and it's version to `elm-stuff/packages`
Expand All @@ -60,7 +74,7 @@ def resolve_package(package, version)
@cache.repository(package).checkout(ref)

version_str = ref == version ? ref : "#{ref}(#{version})"
Utils.log_with_dot "#{package_name} - #{version_str}"
Utils.log_with_dot "#{package_name.bold} - #{version_str.bold}"

return if Dir.exist?(package_path)

Expand Down
4 changes: 4 additions & 0 deletions lib/elm_install/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def log_with_dot(message)
puts ' ● '.green + message
end

def log_with_arrow(message)
puts " ▶ #{message}"
end

def package_version_path(package, version)
package_name = GitCloneUrl.parse(package).path
[package_name, File.join('elm-stuff', 'packages', package_name, version)]
Expand Down

0 comments on commit 9cb9d16

Please sign in to comment.