From 885ed215c21575bcb264038543c9834bb4a98744 Mon Sep 17 00:00:00 2001 From: Terence Lee Date: Thu, 27 Dec 2012 22:47:00 -0500 Subject: handle the case where the binstub already exists, also --force --- lib/bundler/cli.rb | 4 +++- lib/bundler/installer.rb | 17 ++++++++++++++--- spec/other/binstubs_spec.rb | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index c26fe1f2..3167afdb 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -338,6 +338,8 @@ module Bundler D method_option "binstubs", :type => :string, :lazy_default => "bin", :banner => "Generate bin stubs for bundled gems to ./bin" + method_option "force", :type => :boolean, :default => false, :banner => + "forces clean even if --path is not set" def binstubs(gem_name) Bundler.definition.validate_ruby! Bundler.settings[:bin] = options["binstubs"] if options["binstubs"] @@ -346,7 +348,7 @@ module Bundler spec = installer.specs.find{|s| s.name == gem_name } raise GemNotFound, not_found_message(name, Bundler.load.specs) unless spec - installer.generate_bundler_executable_stubs(spec) + installer.generate_bundler_executable_stubs(spec, :force => options[:force]) end desc "outdated [GEM]", "list installed gems with newer versions available" diff --git a/lib/bundler/installer.rb b/lib/bundler/installer.rb index 2073b443..ce069a14 100644 --- a/lib/bundler/installer.rb +++ b/lib/bundler/installer.rb @@ -126,7 +126,7 @@ module Bundler raise Bundler::InstallError, msg end - def generate_bundler_executable_stubs(spec) + def generate_bundler_executable_stubs(spec, options = {}) # double-assignment to avoid warnings about variables that will be used by ERB bin_path = bin_path = Bundler.bin_path template = template = File.read(File.expand_path('../templates/Executable', __FILE__)) @@ -134,9 +134,20 @@ module Bundler ruby_command = ruby_command = Thor::Util.ruby_command spec.executables.each do |executable| + write = true + binstub_path = "#{bin_path}/#{executable}" next if executable == "bundle" - File.open "#{bin_path}/#{executable}", 'w', 0755 do |f| - f.puts ERB.new(template, nil, '-').result(binding) + if File.exists?(binstub_path) && !options[:force] + write = false + Bundler.ui.warn <<-MSG + Skipping #{executable}, since it already exists. + MSG + end + + if write + File.open binstub_path, 'w', 0755 do |f| + f.puts ERB.new(template, nil, '-').result(binding) + end end end end diff --git a/spec/other/binstubs_spec.rb b/spec/other/binstubs_spec.rb index faab6c0a..2a14241f 100644 --- a/spec/other/binstubs_spec.rb +++ b/spec/other/binstubs_spec.rb @@ -92,4 +92,43 @@ describe "bundle binstubs " do expect(bundled_app("exec/rails")).to exist end end + + context "when the bin already exists" do + it "don't override it and warn" do + FileUtils.mkdir_p(bundled_app("bin")) + File.open(bundled_app("bin/rackup"), 'wb') do |file| + file.print "OMG" + end + + install_gemfile <<-G + source "file://#{gem_repo1}" + gem "rack" + G + + bundle "binstubs rack" + + expect(bundled_app("bin/rackup")).to exist + expect(File.read(bundled_app("bin/rackup"))).to eq("OMG") + expect(out).to eq("Skipping rackup, since it already exists.") + end + + context "when using --force" do + it "overrides the binstub" do + FileUtils.mkdir_p(bundled_app("bin")) + File.open(bundled_app("bin/rackup"), 'wb') do |file| + file.print "OMG" + end + + install_gemfile <<-G + source "file://#{gem_repo1}" + gem "rack" + G + + bundle "binstubs rack --force" + + expect(bundled_app("bin/rackup")).to exist + expect(File.read(bundled_app("bin/rackup"))).not_to eq("OMG") + end + end + end end -- cgit v1.2.3