Skip to content

Commit

Permalink
Rubocop and Rubycritic.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdotdesign committed May 2, 2017
1 parent 7183164 commit da811e7
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 116 deletions.
4 changes: 4 additions & 0 deletions .reek
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ TooManyStatements:
max_statements: 7
InstanceVariableAssumption:
enabled: false
Attribute:
enabled: false
FeatureEnvy:
enabled: false
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ AllCops:
- 'vendor/**/*'
Style/FrozenStringLiteralComment:
Enabled: False
Metrics/MethodLength:
Enabled: False
Metrics/AbcSize:
Enabled: False
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ require 'bundler/setup'
Bundler::GemHelper.install_tasks

task :ci do
sh 'rspec'
sh 'rubocop'
sh 'rubycritic -m --no-browser -s 90 lib'
sh 'inch suggest lib --pedantic'
sh 'rspec'
end
3 changes: 2 additions & 1 deletion lib/elm_install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require_relative './elm_install/types'
require_relative './elm_install/source'
require_relative './elm_install/directory_source'
require_relative './elm_install/repository'
require_relative './elm_install/git_source'
require_relative './elm_install/dependency'
require_relative './elm_install/identifier'
Expand All @@ -37,6 +38,6 @@ module ElmInstall
#
# @return [void]
def install(options = { verbose: false })
Installer.new options
Installer.new(options).install
end
end
1 change: 1 addition & 0 deletions lib/elm_install/base.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module ElmInstall
# Base class that contains contracts.
class Base
include Contracts::Core
include Contracts::Builtin
Expand Down
7 changes: 6 additions & 1 deletion lib/elm_install/directory_source.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module ElmInstall
# This clas handles sources that point to a local directory.
class DirectorySource < Source
attr_reader :dir

Expand Down Expand Up @@ -35,7 +36,11 @@ def reset
# Returns the available versions for a repository
Contract ArrayOf[Solve::Constraint] => ArrayOf[Semverse::Version]
def versions(_)
[ identifier.version(fetch('')) ]
[identifier.version(fetch(''))]
end

def to_log
@dir.expand_path.to_s
end
end
end
1 change: 1 addition & 0 deletions lib/elm_install/ext.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Semverse
# Add utility functions
class Version
def to_simple
"#{major}.#{minor}.#{patch}"
Expand Down
70 changes: 24 additions & 46 deletions lib/elm_install/git_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,10 @@ def fetch(version)
when Branch::Just
@branch.ref
when Branch::Nothing
version
version.to_simple
end

repository.reset_hard
repository.checkout ref
repository_directory
end

# Returns the directory of the repository
Contract None => Dir
def repository_directory
# This removes the .git from filename
Dir.new(File.dirname(repository.repo.path))
end

# Copies the version into the given directory
Expand All @@ -59,38 +50,31 @@ def copy_to(version, directory)
def versions(constraints)
case @branch
when Branch::Just
[ identifier.version(fetch(@branch.ref)) ]
[identifier.version(fetch(@branch.ref))]
when Branch::Nothing
matches = matching_versions constraints
return matches if matches.any?
Logger.arrow "Could not find matching versions for: #{package_name.bold} in cache. Fetching updates."
Logger.arrow(
"Could not find matching versions for: #{package_name.bold}"\
' in cache. Fetching updates.'
)
repository.fetch
matching_versions constraints
end
end

def all_versions
repository
.tags
.map(&:name)
.map { |tag| Semverse::Version.try_new tag }
.compact
end

def matching_versions(constraints)
all_versions
.select { |version| constraints.all? { |c| c.satisfies?(version) } }
repository
.versions
.select do |version|
constraints.all? { |constraint| constraint.satisfies?(version) }
end
end

# Returns the url for the repository
Contract None => String
def url
case @uri
when Uri::Github
"https://github.com/#{@uri.name}"
else
@uri.uri.to_s
end
@uri.to_s
end

# Returns the temporary path for the repository
Expand All @@ -111,27 +95,21 @@ def package_name
end

# Returns the local repository
Contract None => Git::Base
Contract None => Repository
def repository
return clone unless Dir.exist?(path)
repo = Git.open path
repo.reset_hard
repo
@repository ||= Repository.new url, path
end

Contract None => nil
def reset
Logger.arrow "Getting updates for: #{url.bold}..."
repository.fetch
nil
end

# Clonse the repository
Contract None => Git::Base
def clone
Logger.arrow "Package: #{url.bold} not found in cache, cloning..."
FileUtils.mkdir_p path
Git.clone(url, path)
def to_log
case @uri
when Uri::Ssh, Uri::Http
case @branch
when Branch::Just
"#{url} at #{@branch.ref}"
else
url
end
end
end
end
end
8 changes: 4 additions & 4 deletions lib/elm_install/identifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ def identify(directory)

dependency_sources =
raw['dependency-sources']
.to_h
.merge(@dependency_sources)
.to_h
.merge(@dependency_sources)

dependencies.map do |package, constraint|
constraints = Utils.transform_constraint constraint

type =
if dependency_sources.key?(package) then
if dependency_sources.key?(package)
source = dependency_sources[package]
case source
when Hash
uri_type source['url'], Branch::Just(source['ref'])
when String
if File.exists?(source)
if File.exist?(source)
Type::Directory(Pathname.new(source))
else
uri_type source, Branch::Just('master')
Expand Down
41 changes: 22 additions & 19 deletions lib/elm_install/installer.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
module ElmInstall
# Installer class
class Installer < Base
def initialize(options = {})
@identifier = Identifier.new Dir.new(Dir.pwd), options
@resolver = Resolver.new @identifier
end

puts "Resolving packages..."

def install
puts 'Resolving packages...'
@graph = @resolver.resolve

initial = @identifier.initial_dependencies.map do |dependency|
puts 'Solving dependencies...'
(Populator.new results).populate

puts 'Packages configured successfully!'
end

def results
Solve
.it!(@graph, initial_solve_constraints)
.map do |name, version|
dep = @resolver.dependencies[name]
dep.version = Semverse::Version.new(version)
dep
end
end

def initial_solve_constraints
@identifier.initial_dependencies.flat_map do |dependency|
dependency.constraints.map do |constraint|
[dependency.name, constraint]
end
end
.flatten(1)

puts "Solving dependencies..."

results =
Solve
.it!(@graph, initial)
.map { |name, version|
dep = @resolver.dependencies[name]
dep.version = Semverse::Version.new(version)
dep
}

(Populator.new results).populate

puts "Packages configured successfully!"
end
end
end
38 changes: 11 additions & 27 deletions lib/elm_install/populator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,21 @@ def exact_dependencies
def copy_dependencies
@dependencies.each do |dependency|
path =
File.join(
'elm-stuff',
'packages',
dependency.name,
dependency.version.to_simple
)
File.join('elm-stuff', 'packages', dependency.name,
dependency.version.to_simple)

log = "#{dependency.name} - "

case dependency.source
when DirectorySource
log += "#{dependency.source.dir.expand_path} (#{dependency.version.to_simple})"
when GitSource
case dependency.source.uri
when Uri::Github
log += dependency.version.to_simple
else
log += dependency.source.url
case dependency.source.branch
when Branch::Just
log += " (#{dependency.source.branch.ref})"
else
log += " (#{dependency.version.to_simple})"
end
end
end

Logger.dot log
log_dependency dependency

dependency.source.copy_to(dependency.version, Pathname.new(path))
end
end

def log_dependency(dependency)
log = "#{dependency.name} - "
log += dependency.source.to_log.to_s
log += " (#{dependency.version.to_simple})"

Logger.dot log
end
end
end
53 changes: 53 additions & 0 deletions lib/elm_install/repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module ElmInstall
# Handles git repositories.
class Repository < Base
extend Forwardable

attr_reader :url, :path

def_delegators :repo, :fetch

def initialize(url, path)
@path = path
@url = url
end

# Downloads the version into a temporary directory
Contract String => Dir
def checkout(ref)
repo.reset_hard
repo.checkout ref
directory
end

# Returns the directory of the repository
Contract None => Dir
def directory
# This removes the .git from filename
Dir.new(File.dirname(repo.repo.path))
end

def versions
repo
.tags
.map(&:name)
.map { |tag| Semverse::Version.try_new tag }
.compact
end

def repo
return clone unless Dir.exist?(path)
repo = Git.open path
repo.reset_hard
repo
end

# Clonse the repository
Contract None => Git::Base
def clone
Logger.arrow "Package: #{url.bold} not found in cache, cloning..."
FileUtils.mkdir_p path
Git.clone(url, path)
end
end
end
1 change: 1 addition & 0 deletions lib/elm_install/source.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module ElmInstall
# Abstract class for a source
class Source < Base
attr_accessor :identifier, :options
end
Expand Down
Loading

0 comments on commit da811e7

Please sign in to comment.