From 9b0dc0ac0373d125d9c9b89504982eb87482a1f9 Mon Sep 17 00:00:00 2001 From: "Samuel E. Giddins" Date: Fri, 12 Sep 2014 17:08:35 -0700 Subject: [PATCH] Initial commit! --- .gitignore | 34 +++++++ .gitmodules | 3 + .kick | 30 ++++++ .rubocop.yml | 8 ++ .rubocop_cocoapods.yml | 89 ++++++++++++++++++ Gemfile | 19 ++++ Gemfile.lock | 91 ++++++++++++++++++ LICENSE | 9 ++ README.md | 0 Rakefile | 67 ++++++++++++++ lib/resolver.rb | 1 + lib/resolver/gem_metadata.rb | 3 + lib/resolver/resolver.rb | 122 +++++++++++++++++++++++++ lib/resolver/result.rb | 17 ++++ lib/resolver/specification.rb | 11 +++ lib/resolver/specification_provider.rb | 7 ++ lib/resolver/specification_set.rb | 10 ++ lib/resolver/state.rb | 13 +++ lib/resolver/ui.rb | 15 +++ resolver.gemspec | 21 +++++ spec/resolver_integration_specs | 1 + spec/resolver_spec.rb | 67 ++++++++++++++ spec/spec_helper.rb | 32 +++++++ 23 files changed, 670 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 .kick create mode 100644 .rubocop.yml create mode 100644 .rubocop_cocoapods.yml create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 LICENSE create mode 100644 README.md create mode 100644 Rakefile create mode 100644 lib/resolver.rb create mode 100644 lib/resolver/gem_metadata.rb create mode 100644 lib/resolver/resolver.rb create mode 100644 lib/resolver/result.rb create mode 100644 lib/resolver/specification.rb create mode 100644 lib/resolver/specification_provider.rb create mode 100644 lib/resolver/specification_set.rb create mode 100644 lib/resolver/state.rb create mode 100644 lib/resolver/ui.rb create mode 100644 resolver.gemspec create mode 160000 spec/resolver_integration_specs create mode 100644 spec/resolver_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2c1360 --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +*.gem +*.rbc +/.config +/coverage/ +/InstalledFiles +/pkg/ +/spec/reports/ +/test/tmp/ +/test/version_tmp/ +/tmp/ + +## Specific to RubyMotion: +.dat* +.repl_history +build/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +## Environment normalisation: +/.bundle/ +/lib/bundler/man/ + +# for a library or gem, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# Gemfile.lock +# .ruby-version +# .ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: +.rvmrc diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..6453cf7 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "spec/resolver_integration_specs"] + path = spec/resolver_integration_specs + url = https://github.com/CocoaPods/Resolver-Integration-Specs.git diff --git a/.kick b/.kick new file mode 100644 index 0000000..91ed9ed --- /dev/null +++ b/.kick @@ -0,0 +1,30 @@ +recipe :ruby + +Kicker::Recipes::Ruby.runner_bin = 'bacon --quiet' + +process do |files| + specs = files.take_and_map do |file| + if file =~ %r{lib/[^/]*/(.+?)\.rb$} + s = Dir.glob("spec/**/#{File.basename(file, '.rb')}_spec.rb") + s.uniq unless s.empty? + end + end + Kicker::Recipes::Ruby.run_tests(specs) +end + +# Have written this so many times, probably should make a recipe out of it. +process do |files| + files.each do |file| + case file + when 'Gemfile' + files.delete(file) + execute 'bundle install' + end + end +end + +recipe :ignore +ignore(/.*\/?tags/) +ignore(/.*\/?\.git/) +ignore(/^tmp/) + diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..146aac9 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,8 @@ +inherit_from: .rubocop_cocoapods.yml + +Metrics/LineLength: + Max: 120 + +AllCops: + Exclude: + - 'vendor/**/*' diff --git a/.rubocop_cocoapods.yml b/.rubocop_cocoapods.yml new file mode 100644 index 0000000..5467468 --- /dev/null +++ b/.rubocop_cocoapods.yml @@ -0,0 +1,89 @@ +AllCops: + Include: + - ./Rakefile + - ./Gemfile + - ./*.gemspec + Exclude: + - ./spec/fixtures/**/* + +# At the moment not ready to be used +# https://github.com/bbatsov/rubocop/issues/947 +Documentation: + Enabled: false + +#- CocoaPods -----------------------------------------------------------------# + +# We adopted raise instead of fail. +SignalException: + EnforcedStyle: only_raise + +# They are idiomatic +AssignmentInCondition: + Enabled: false + +# Allow backticks +AsciiComments: + Enabled: false + +# Indentation clarifies logic branches in implementations +IfUnlessModifier: + Enabled: false + +# No enforced convention here. +SingleLineBlockParams: + Enabled: false + +# We only add the comment when needed. +Encoding: + Enabled: false + +# Having these make it easier to *not* forget to add one when adding a new +# value and you can simply copy the previous line. +TrailingComma: + EnforcedStyleForMultiline: comma + +# Clashes with CLAide Command#validate! +GuardClause: + Enabled: false + +# Not always desirable: lib/claide/command/plugins_helper.rb:12:15 +Next: + Enabled: false + +#- CocoaPods support for Ruby 1.8.7 ------------------------------------------# + +HashSyntax: + EnforcedStyle: hash_rockets + +Lambda: + Enabled: false + +DotPosition: + EnforcedStyle: trailing + +EachWithObject: + Enabled: false + +Style/SpecialGlobalVars: + Enabled: false + +#- CocoaPods specs -----------------------------------------------------------# + +# Allow for `should.match /regexp/`. +AmbiguousRegexpLiteral: + Exclude: + - spec/**/* + +# Allow `object.should == object` syntax. +Void: + Exclude: + - spec/**/* + +ClassAndModuleChildren: + Exclude: + - spec/**/* + +UselessComparison: + Exclude: + - spec/**/* + diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..e06cd91 --- /dev/null +++ b/Gemfile @@ -0,0 +1,19 @@ +source 'https://rubygems.org' + +gemspec + +group :development do + gem 'bacon' + gem 'coveralls', :require => false + gem 'mocha', '~> 0.11.4' + gem 'mocha-on-bacon' + gem 'prettybacon' + gem 'kicker' + gem 'version_kit', :path => '../VersionKit'#:git => 'https://github.com/CocoaPods/VersionKit.git' + + # Ruby 1.8.7 fixes + gem 'mime-types', '< 2.0' + if RUBY_VERSION >= '1.9.3' + gem 'rubocop' + end +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..030b17d --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,91 @@ +PATH + remote: . + specs: + resolver (0.0.1) + +PATH + remote: ../VersionKit + specs: + version_kit (0.0.1) + +GEM + remote: https://rubygems.org/ + specs: + ast (2.0.0) + astrolabe (1.3.0) + parser (>= 2.2.0.pre.3, < 3.0) + bacon (1.2.0) + coveralls (0.7.1) + multi_json (~> 1.3) + rest-client + simplecov (>= 0.7) + term-ansicolor + thor + docile (1.1.5) + ffi (1.9.3) + kicker (3.0.0) + listen (~> 1.3.0) + notify (~> 0.5.2) + listen (1.3.1) + rb-fsevent (>= 0.9.3) + rb-inotify (>= 0.9) + rb-kqueue (>= 0.2) + metaclass (0.0.4) + mime-types (1.25.1) + mocha (0.11.4) + metaclass (~> 0.0.1) + mocha-on-bacon (0.2.1) + mocha (>= 0.9.8) + multi_json (1.10.1) + netrc (0.7.7) + notify (0.5.2) + parser (2.2.0.pre.4) + ast (>= 1.1, < 3.0) + slop (~> 3.4, >= 3.4.5) + powerpack (0.0.9) + prettybacon (0.0.2) + bacon (~> 1.2) + rainbow (2.0.0) + rake (10.3.2) + rb-fsevent (0.9.4) + rb-inotify (0.9.5) + ffi (>= 0.5.0) + rb-kqueue (0.2.3) + ffi (>= 0.5.0) + rest-client (1.7.2) + mime-types (>= 1.16, < 3.0) + netrc (~> 0.7) + rubocop (0.26.0) + astrolabe (~> 1.3) + parser (>= 2.2.0.pre.4, < 3.0) + powerpack (~> 0.0.6) + rainbow (>= 1.99.1, < 3.0) + ruby-progressbar (~> 1.4) + ruby-progressbar (1.5.1) + simplecov (0.9.0) + docile (~> 1.1.0) + multi_json + simplecov-html (~> 0.8.0) + simplecov-html (0.8.0) + slop (3.6.0) + term-ansicolor (1.3.0) + tins (~> 1.0) + thor (0.19.1) + tins (1.3.2) + +PLATFORMS + ruby + +DEPENDENCIES + bacon + bundler (~> 1.5) + coveralls + kicker + mime-types (< 2.0) + mocha (~> 0.11.4) + mocha-on-bacon + prettybacon + rake + resolver! + rubocop + version_kit! diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..01feffa --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +This project is licensed under the MIT license. + +Copyright (c) 2014 Samuel E. Giddins segiddins@segiddins.me + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..2ccbbc5 --- /dev/null +++ b/Rakefile @@ -0,0 +1,67 @@ +# encoding: utf-8 + +#-- Bootstrap --------------------------------------------------------------# + +desc 'Initializes your working copy to run the specs' +task :bootstrap do + if system('which bundle') + title 'Installing gems' + sh 'bundle install' + else + $stderr.puts "\033[0;31m" \ + "[!] Please install the bundler gem manually:\n" \ + ' $ [sudo] gem install bundler' + "\e[0m" + exit 1 + end +end + +begin + require 'bundler/gem_tasks' + task :default => :spec + + #-- Specs ------------------------------------------------------------------# + + desc 'Run specs' + task :spec do + title 'Running Unit Tests' + files = FileList['spec/**/*_spec.rb'].shuffle.join(' ') + sh "bundle exec bacon #{files}" + + Rake::Task['rubocop'].invoke if RUBY_VERSION >= '1.9.3' + end + + #-- Kick -------------------------------------------------------------------# + + desc 'Automatically run specs for updated files' + task :kick do + exec 'bundle exec kicker -c' + end + + #-- RuboCop ----------------------------------------------------------------# + + if RUBY_VERSION >= '1.9.3' + require 'rubocop/rake_task' + RuboCop::RakeTask.new + end + +rescue LoadError + $stderr.puts "\033[0;31m" \ + '[!] Some Rake tasks haven been disabled because the environment' \ + ' couldn’t be loaded. Be sure to run `rake bootstrap` first.' \ + "\e[0m" + $stderr.puts e.message + $stderr.puts e.backtrace + $stderr.puts +end + +#-- Helpers ------------------------------------------------------------------# + +def title(title) + cyan_title = "\033[0;36m#{title}\033[0m" + puts + puts '-' * 80 + puts cyan_title + puts '-' * 80 + puts +end diff --git a/lib/resolver.rb b/lib/resolver.rb new file mode 100644 index 0000000..0d40c52 --- /dev/null +++ b/lib/resolver.rb @@ -0,0 +1 @@ +require 'resolver/gem_metadata' diff --git a/lib/resolver/gem_metadata.rb b/lib/resolver/gem_metadata.rb new file mode 100644 index 0000000..33e9140 --- /dev/null +++ b/lib/resolver/gem_metadata.rb @@ -0,0 +1,3 @@ +module Resolver + VERSION = '0.0.1' +end diff --git a/lib/resolver/resolver.rb b/lib/resolver/resolver.rb new file mode 100644 index 0000000..a6abbdb --- /dev/null +++ b/lib/resolver/resolver.rb @@ -0,0 +1,122 @@ +require 'version_kit/resolver/result' +require 'version_kit/resolver/state' +require 'version_kit/resolver/ui' +require 'version_kit/resolver/specification_provider' +require 'version_kit/resolver/specification' + +module VersionKit + # Features + # + # - Supports multiple resolution groups with the limitation of only one + # version activated for a given library among them. + # + class Resolver + attr_reader :specification_provider, :resolver_ui + + def initialize(specification_provider, resolver_ui) + @specification_provider = specification_provider + @resolver_ui = resolver_ui + end + + class Resolution + require 'set' + + attr_reader :specification_provider, :resolver_ui, :requested, :base + + def initialize(specification_provider, resolver_ui, requested, base) + @specification_provider = specification_provider + @resolver_ui = resolver_ui + @requested = requested + @base = base + @errors = [] + @conflicts = Set.new + end + + def resolve + @started_at = Time.now + activated = {} + + until requirements.empty? + indicate_progress + + requested = requested.sort + + attempt_to_activate(requested.shift, activated) + end + + @ended_at = Time.now + + Result.new([], {}) + end + + private + + attr_accessor :errors + attr_accessor :dependencies_for + attr_accessor :conflicts + attr_accessor :missing_specs + attr_accessor :iteration_rate + attr_accessor :iteration_counter + attr_accessor :started_at + attr_accessor :ended_at + + def indicate_progress + iteration_count + + + if iteration_rate.nil? + if ((Time.now - started_at) % 3600).round >= 1 + iteration_rate = iteration_counter + end + else + if ((iteration_counter % iteration_rate) == 0) + resolver_ui.indicate_progress + end + end + end + + def resolve_conflict(_current_state, _states) + end + + def find_conflict_state(_current_state, _states) + end + + def unwind_for_conflict(_conflict_state) + end + + def attempt_to_activate(requested_spec) + existing_spec = activated[requested_spec.name] + is_resolver_specification = + specification_provider.is_resolver_specification?(requested_spec.name) + if existing_spec || is_resolver_specification + + if !existing_spec && is_resolver_specification + # force the requested + + existing_spec = search_for(requested_spec.name).first + + ## raise unless existing_spec + existing_spec.resolver_required_by = existing_spec + end + end + end + + def activate_spec(_spec_to_activate, _activated) + end + + def search_for(spec_name) + specification_provider.search_for(spec_name) + end + + def possibilities_for_state?(state) + state && state.possibilities.any? + end + end + + def resolve(requested, base) + Resolution.new(specification_provider, + resolver_ui, + requested, + base). + resolve + end + end +end diff --git a/lib/resolver/result.rb b/lib/resolver/result.rb new file mode 100644 index 0000000..ccdfb9a --- /dev/null +++ b/lib/resolver/result.rb @@ -0,0 +1,17 @@ +module VersioKit + class Resolver + class Result + attr_reader :conflicts, :dependency_graph + + def initialize(dependency_graph, conflicts) + @dependency_graph = dependency_graph + @conflicts = conflicts + end + + def ==(other) + conflicts == other.conflicts && + dependency_graph == other.dependency_graph + end + end + end +end diff --git a/lib/resolver/specification.rb b/lib/resolver/specification.rb new file mode 100644 index 0000000..fe09f0b --- /dev/null +++ b/lib/resolver/specification.rb @@ -0,0 +1,11 @@ +module Resolver + module Specification + def name + inspect + end + + def version_compare(other) + self <=> other + end + end +end diff --git a/lib/resolver/specification_provider.rb b/lib/resolver/specification_provider.rb new file mode 100644 index 0000000..9027249 --- /dev/null +++ b/lib/resolver/specification_provider.rb @@ -0,0 +1,7 @@ +module Resolver + module SpecificationProvider + def search_for(_specification_name) + [] + end + end +end diff --git a/lib/resolver/specification_set.rb b/lib/resolver/specification_set.rb new file mode 100644 index 0000000..74d0d0c --- /dev/null +++ b/lib/resolver/specification_set.rb @@ -0,0 +1,10 @@ +module Resolver + class SpecificationSet + attr_reader :specifications, :name + + def initialize(name, specifications) + @name = name + @specifications = specifications + end + end +end diff --git a/lib/resolver/state.rb b/lib/resolver/state.rb new file mode 100644 index 0000000..759485d --- /dev/null +++ b/lib/resolver/state.rb @@ -0,0 +1,13 @@ +module Resolver + class ResolutionState < Struct.new(:requirements, + :activated, + :requirement, + :possibilities, + :depth, + :conflicts) + + def name + requirement.name + end + end +end diff --git a/lib/resolver/ui.rb b/lib/resolver/ui.rb new file mode 100644 index 0000000..7d8b3eb --- /dev/null +++ b/lib/resolver/ui.rb @@ -0,0 +1,15 @@ +module Resolver + module UI + def indicate_progress + STDOUT.print '.' + end + + def debug + if ENV['DEBUG_RESOLVER'] + debug_info = yield + debug_info = debug_info.inspect unless debug_info.is_a?(String) + STDERR.puts debug_info + end + end + end +end diff --git a/resolver.gemspec b/resolver.gemspec new file mode 100644 index 0000000..b964ad0 --- /dev/null +++ b/resolver.gemspec @@ -0,0 +1,21 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'resolver/gem_metadata' + +Gem::Specification.new do |spec| + spec.name = 'resolver' + spec.version = Resolver::VERSION + spec.authors = ['Samuel E. Giddins'] + spec.email = ['segiddins@segiddins.me'] + spec.summary = 'Provides support for dependency resolution' + spec.homepage = 'https://github.com/CocoaPods/Resolver' + spec.license = 'MIT' + + spec.files = Dir['lib/**/*.rb'] + %w(README.md LICENSE) + spec.test_files = Dir['spec/**/*.rb'] + spec.require_paths = ['lib'] + + spec.add_development_dependency 'bundler', '~> 1.5' + spec.add_development_dependency 'rake' +end diff --git a/spec/resolver_integration_specs b/spec/resolver_integration_specs new file mode 160000 index 0000000..b6b3e84 --- /dev/null +++ b/spec/resolver_integration_specs @@ -0,0 +1 @@ +Subproject commit b6b3e8411ae8f41e68b651b052d76ccd70ab3daf diff --git a/spec/resolver_spec.rb b/spec/resolver_spec.rb new file mode 100644 index 0000000..d1768aa --- /dev/null +++ b/spec/resolver_spec.rb @@ -0,0 +1,67 @@ +require File.expand_path('../spec_helper', __FILE__) +require 'json' +require 'pathname' + +module VersionKit + describe Resolver do + + FIXTURE_DIR = Pathname.new('resolver_integration_specs') + FIXTURE_INDEX_DIR = FIXTURE_DIR + 'index' + FIXTURE_CASE_DIR = FIXTURE_DIR + 'case' + + class TestSpecification + attr_accessor :name, :version, :dependencies, :platform + def initialize(hash) + self.name = hash['name'] + self.version = hash['version'] + self.dependencies = hash['dependencies'] + self.platform = hash['platform'] + end + end + + class TestIndex + attr_accessor :specs + + def initialize(fixture_name) + File.open(FIXTURE_INDEX_DIR + (fixture_name + '.json'), 'r') do |fixture| + self.specs = JSON.load(fixture).reduce({}) do |specs_by_name, (name, versions)| + specs_by_name.tap do |specs| + specs[name] = versions.map { |s| TestSpecification.new s } + end + end + end + end + end + + class TestCase + attr_accessor :name, :requested, :resolver, :result, :index + + # rubocop:disable Metrics/MethodLength + def initialize(fixture_path) + File.open(fixture_path) do |fixture| + JSON.load(fixture).tap do |test_case| + self.name = test_case['name'] + self.requested = test_case['requested'].map do |(name, reqs)| + Dependency.new name, reqs.split(/\w/) + end + self.result = Resolver::Result.new(test_case['resolved'], test_case['conflicts']) + self.index = TestIndex.new(test_case['index'] || 'awesome') + end + end + + self.resolver = Resolver.new + end + # rubocop:enable Metrics/MethodLength + end + + describe 'dependency resolution' do + Dir.glob(FIXTURE_CASE_DIR + '**/*.json').map do |fixture| + test_case = TestCase.new(fixture) + it test_case.name do + test_case.resolver.resolve.should.equal test_case.result + end + end + end + + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..730fb3e --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,32 @@ + +# Set up coverage analysis +#-----------------------------------------------------------------------------# + +if ENV['CI'] || ENV['GENERATE_COVERAGE'] + require 'simplecov' + require 'coveralls' + + if ENV['CI'] + SimpleCov.formatter = Coveralls::SimpleCov::Formatter + elsif ENV['GENERATE_COVERAGE'] + SimpleCov.formatter = SimpleCov::Formatter::HTMLFormatter + end + SimpleCov.start do + add_filter '/vendor/' + end +end + +# Set up +#-----------------------------------------------------------------------------# + +require 'pathname' +ROOT = Pathname.new(File.expand_path('../../', __FILE__)) +$LOAD_PATH.unshift((ROOT + 'lib').to_s) +$LOAD_PATH.unshift((ROOT + 'spec').to_s) + +require 'bundler/setup' +require 'bacon' +require 'mocha-on-bacon' +require 'pretty_bacon' +require 'version_kit' +require 'resolver'