aboutsummaryrefslogtreecommitdiffstats
path: root/lib/bundler/gem_helpers.rb
diff options
context:
space:
mode:
authorhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-08 08:45:41 +0000
committerhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-08 08:45:41 +0000
commit8598f8c2dc78c6d1ae87cb6ae19c34ba2cb29241 (patch)
tree0bbd28f684e745cb212761b7c74fe08668f85cc8 /lib/bundler/gem_helpers.rb
parentf2e04b77aa8a363d7e36ce5a9cdb60714a537a3c (diff)
downloadruby-8598f8c2dc78c6d1ae87cb6ae19c34ba2cb29241.tar.gz
Merge bundler to standard libraries.
rubygems 2.7.x depends bundler-1.15.x. This is preparation for rubygems and bundler migration. * lib/bundler.rb, lib/bundler/*: files of bundler-1.15.4 * spec/bundler/*: rspec examples of bundler-1.15.4. I applied patches. * https://github.com/bundler/bundler/pull/6007 * Exclude not working examples on ruby repository. * Fake ruby interpriter instead of installed ruby. * Makefile.in: Added test task named `test-bundler`. This task is only working macOS/linux yet. I'm going to support Windows environment later. * tool/sync_default_gems.rb: Added sync task for bundler. [Feature #12733][ruby-core:77172] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59779 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/bundler/gem_helpers.rb')
-rw-r--r--lib/bundler/gem_helpers.rb100
1 files changed, 100 insertions, 0 deletions
diff --git a/lib/bundler/gem_helpers.rb b/lib/bundler/gem_helpers.rb
new file mode 100644
index 0000000000..955834ff01
--- /dev/null
+++ b/lib/bundler/gem_helpers.rb
@@ -0,0 +1,100 @@
+# frozen_string_literal: true
+module Bundler
+ module GemHelpers
+ GENERIC_CACHE = {} # rubocop:disable MutableConstant
+ GENERICS = [
+ [Gem::Platform.new("java"), Gem::Platform.new("java")],
+ [Gem::Platform.new("mswin32"), Gem::Platform.new("mswin32")],
+ [Gem::Platform.new("mswin64"), Gem::Platform.new("mswin64")],
+ [Gem::Platform.new("universal-mingw32"), Gem::Platform.new("universal-mingw32")],
+ [Gem::Platform.new("x64-mingw32"), Gem::Platform.new("x64-mingw32")],
+ [Gem::Platform.new("x86_64-mingw32"), Gem::Platform.new("x64-mingw32")],
+ [Gem::Platform.new("mingw32"), Gem::Platform.new("x86-mingw32")]
+ ].freeze
+
+ def generic(p)
+ return p if p == Gem::Platform::RUBY
+
+ GENERIC_CACHE[p] ||= begin
+ _, found = GENERICS.find do |match, _generic|
+ p.os == match.os && (!match.cpu || p.cpu == match.cpu)
+ end
+ found || Gem::Platform::RUBY
+ end
+ end
+ module_function :generic
+
+ def generic_local_platform
+ generic(Bundler.local_platform)
+ end
+ module_function :generic_local_platform
+
+ def platform_specificity_match(spec_platform, user_platform)
+ spec_platform = Gem::Platform.new(spec_platform)
+ return PlatformMatch::EXACT_MATCH if spec_platform == user_platform
+ return PlatformMatch::WORST_MATCH if spec_platform.nil? || spec_platform == Gem::Platform::RUBY || user_platform == Gem::Platform::RUBY
+
+ PlatformMatch.new(
+ PlatformMatch.os_match(spec_platform, user_platform),
+ PlatformMatch.cpu_match(spec_platform, user_platform),
+ PlatformMatch.platform_version_match(spec_platform, user_platform)
+ )
+ end
+ module_function :platform_specificity_match
+
+ def select_best_platform_match(specs, platform)
+ specs.select {|spec| spec.match_platform(platform) }.
+ min_by {|spec| platform_specificity_match(spec.platform, platform) }
+ end
+ module_function :select_best_platform_match
+
+ PlatformMatch = Struct.new(:os_match, :cpu_match, :platform_version_match)
+ class PlatformMatch
+ def <=>(other)
+ return nil unless other.is_a?(PlatformMatch)
+
+ m = os_match <=> other.os_match
+ return m unless m.zero?
+
+ m = cpu_match <=> other.cpu_match
+ return m unless m.zero?
+
+ m = platform_version_match <=> other.platform_version_match
+ m
+ end
+
+ EXACT_MATCH = new(-1, -1, -1).freeze
+ WORST_MATCH = new(1_000_000, 1_000_000, 1_000_000).freeze
+
+ def self.os_match(spec_platform, user_platform)
+ if spec_platform.os == user_platform.os
+ 0
+ else
+ 1
+ end
+ end
+
+ def self.cpu_match(spec_platform, user_platform)
+ if spec_platform.cpu == user_platform.cpu
+ 0
+ elsif spec_platform.cpu == "arm" && user_platform.cpu.to_s.start_with?("arm")
+ 0
+ elsif spec_platform.cpu.nil? || spec_platform.cpu == "universal"
+ 1
+ else
+ 2
+ end
+ end
+
+ def self.platform_version_match(spec_platform, user_platform)
+ if spec_platform.version == user_platform.version
+ 0
+ elsif spec_platform.version.nil?
+ 1
+ else
+ 2
+ end
+ end
+ end
+ end
+end