From d8713e31bd4e3f630ca07db7f157680b538a8568 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Fri, 12 Aug 2016 13:16:20 -0500 Subject: [Lock] Allow removing platforms --- lib/bundler/cli.rb | 2 ++ lib/bundler/cli/lock.rb | 16 ++++++++++++++-- lib/bundler/definition.rb | 5 +++++ spec/commands/lock_spec.rb | 22 ++++++++++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index a20c76b30f..75cceee80d 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -454,6 +454,8 @@ module Bundler "Fall back to using the single-file index of all gems" method_option "add-platform", :type => :array, :default => [], :banner => "add a new platform to the lockfile" + method_option "remove-platform", :type => :array, :default => [], :banner => + "remove a platform from the lockfile" def lock require "bundler/cli/lock" Lock.new(options).run diff --git a/lib/bundler/cli/lock.rb b/lib/bundler/cli/lock.rb index ba9a32655d..a6a95f895c 100644 --- a/lib/bundler/cli/lock.rb +++ b/lib/bundler/cli/lock.rb @@ -26,11 +26,23 @@ module Bundler definition = Bundler.definition(true) end - options["add-platform"].each do |platform| - platform = Gem::Platform.new(platform) + options["remove-platform"].each do |platform| + definition.remove_platform(platform) + end + + options["add-platform"].each do |platform_string| + platform = Gem::Platform.new(platform_string) + if platform.to_a.compact == %w(unknown) + Bundler.ui.warn "The platform `#{platform_string}` is unknown to RubyGems " \ + "and adding it will likely lead to resolution errors" + end definition.add_platform(platform) end + if definition.platforms.empty? + raise InvalidOption, "Removing all platforms from the bundle is not allowed" + end + definition.resolve_remotely! unless options[:local] if print diff --git a/lib/bundler/definition.rb b/lib/bundler/definition.rb index 726e9d261b..ee966e7164 100644 --- a/lib/bundler/definition.rb +++ b/lib/bundler/definition.rb @@ -481,6 +481,11 @@ module Bundler @platforms |= [platform] end + def remove_platform(platform) + return if @platforms.delete(Gem::Platform.new(platform)) + raise InvalidOption, "Unable to remove the platform `#{platform}` since the only platforms are #{@platforms.join ", "}" + end + attr_reader :sources private :sources diff --git a/spec/commands/lock_spec.rb b/spec/commands/lock_spec.rb index 98eb98a789..1e189a9659 100644 --- a/spec/commands/lock_spec.rb +++ b/spec/commands/lock_spec.rb @@ -110,4 +110,26 @@ describe "bundle lock" do lockfile = Bundler::LockfileParser.new(read_lockfile) expect(lockfile.platforms).to eq([java, local, mingw]) end + + it "warns when adding an unknown platform" do + bundle "lock --add-platform foobarbaz" + expect(out).to include("The platform `foobarbaz` is unknown to RubyGems and adding it will likely lead to resolution errors") + end + + it "allows removing platforms" do + bundle! "lock --add-platform java x86-mingw32" + + lockfile = Bundler::LockfileParser.new(read_lockfile) + expect(lockfile.platforms).to eq([java, local, mingw]) + + bundle! "lock --remove-platform java" + + lockfile = Bundler::LockfileParser.new(read_lockfile) + expect(lockfile.platforms).to eq([local, mingw]) + end + + it "errors when removing all platforms" do + bundle "lock --remove-platform #{local}" + expect(out).to include("Removing all platforms from the bundle is not allowed") + end end -- cgit v1.2.1