Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Have bundler generate executable stubs into ~/.bundler/bin that will …

…automatically call bundler/setup if in bundle directory
  • Loading branch information...
commit 5f26a035a498681dcadf31c7711dc862508ade93 1 parent cd8a563
Carl Lerche authored
View
8 lib/bundler.rb
@@ -70,6 +70,14 @@ def bundle_path
end
end
+ def bin_path
+ @bin_path ||= begin
+ path = settings[:bin] || "#{Gem.user_home}/.bundler/bin"
+ FileUtils.mkdir_p(path)
+ Pathname.new(path).expand_path
+ end
+ end
+
def setup(*groups)
return @setup if @setup
View
12 lib/bundler/installer.rb
@@ -36,9 +36,21 @@ def run(unlocked_gems, options)
end
spec.source.install(spec)
+ generate_bundler_executable_stubs(spec)
end
lock
end
+
+ private
+
+ def generate_bundler_executable_stubs(spec)
+ spec.executables.each do |executable|
+ File.open "#{Bundler.bin_path}/#{executable}", 'w', 0755 do |f|
+ f.puts File.read(File.expand_path('../templates/Executable', __FILE__))
+ end
+ end
+ end
+
end
end
View
5 lib/bundler/source.rb
@@ -149,7 +149,9 @@ def fetch_remote_specs(index)
spec.source = self
# Temporary hack until this can be figured out better
@spec_fetch_map[[name, version, platform]] = lambda do
- download_gem_from_uri(spec, uri)
+ path = download_gem_from_uri(spec, uri)
+ s = Gem::Format.from_file_by_path(path).spec
+ spec.__swap__(s)
end
index << spec
end
@@ -173,6 +175,7 @@ def fetch_all_remote_specs(&blk)
def download_gem_from_uri(spec, uri)
spec.fetch_platform
Gem::RemoteFetcher.fetcher.download(spec, uri, Gem.dir)
+ "#{Gem.dir}/cache/#{spec.full_name}.gem"
end
end
View
28 lib/bundler/templates/Executable
@@ -0,0 +1,28 @@
+#!/usr/bin/env ruby
+# This is not actually required by the actual library
+# loads the bundled environment
+require 'rubygems'
+
+begin
+ require 'bundler/setup'
+rescue LoadError
+ # Let's not complain if bundler isn't around
+end
+
+base = File.basename($0)
+paths = ENV['PATH'].split(File::PATH_SEPARATOR)
+here = File.expand_path(File.dirname(__FILE__))
+
+gem_stub = paths.find do |path|
+ path = File.expand_path(path)
+
+ next if path == here
+
+ File.exist?("#{path}/#{base}")
+end
+
+if gem_stub
+ load "#{gem_stub}/#{base}"
+else
+ abort "The gem stub #{base} could not be found"
+end
View
69 spec/runtime/executable_spec.rb
@@ -0,0 +1,69 @@
+require "spec_helper"
+
+describe "Running commands" do
+ it "runs the bundled command when in the bundle" do
+ install_gemfile <<-G
+ source "file://#{gem_repo1}"
+ gem "rack"
+ G
+
+ build_gem "rack", "2.0", :to_system => true do |s|
+ s.executables = "rackup"
+ end
+
+ gembin "rackup"
+ out.should == "1.0.0"
+ end
+
+ it "runs the system command when out of the bundle" do
+ install_gemfile <<-G
+ source "file://#{gem_repo1}"
+ gem "rack"
+ G
+
+ build_gem "rack", "2.0", :to_system => true do |s|
+ s.executables = "rackup"
+ end
+
+ Dir.chdir(tmp) do
+ gembin "rackup"
+ out.should == "2.0"
+ end
+ end
+
+ it "works with gems in path" do
+ build_lib "rack", :path => lib_path("rack") do |s|
+ s.executables = 'rackup'
+ end
+
+ install_gemfile <<-G
+ gem "rack", :path => "#{lib_path('rack')}"
+ G
+
+ build_gem 'rack', '2.0', :to_system => true do |s|
+ s.executables = 'rackup'
+ end
+
+ gembin "rackup"
+ out.should == '1.0'
+ end
+
+ it "blows up when running outside of the directory" do
+ build_lib "rack", :path => lib_path("rack") do |s|
+ s.executables = 'rackup'
+ end
+
+ install_gemfile <<-G
+ gem "rack", :path => "#{lib_path('rack')}"
+ G
+
+ build_gem 'rack', '2.0', :to_system => true do |s|
+ s.executables = 'rackup'
+ end
+
+ Dir.chdir(tmp) do
+ gembin "rackup"
+ out.should == '2.0'
+ end
+ end
+end
View
9 spec/support/helpers.rb
@@ -64,6 +64,14 @@ def ruby(ruby, options = {})
sys_exec(%'#{Gem.ruby} -I#{lib} -e "#{ruby}"', expect_err)
end
+ def gembin(cmd)
+ lib = File.expand_path("../../../lib", __FILE__)
+ old, ENV['RUBYOPT'] = ENV['RUBYOPT'], "#{ENV['RUBYOPT']} -I#{lib}"
+ sys_exec(cmd)
+ ensure
+ ENV['RUBYOPT'] = old
+ end
+
def sys_exec(cmd, expect_err = false)
require "open3"
@in, @out, @err = Open3.popen3(cmd)
@@ -151,7 +159,6 @@ def system_gems(*gems)
gem_home, gem_path, path = ENV['GEM_HOME'], ENV['GEM_PATH'], ENV['PATH']
ENV['GEM_HOME'], ENV['GEM_PATH'] = system_gem_path.to_s, system_gem_path.to_s
- ENV['PATH'] = "#{system_gem_path}/bin:#{ENV['PATH']}"
install_gems(*gems)
if block_given?
View
2  spec/support/rubygems_ext.rb
@@ -5,7 +5,7 @@ def self.setup
ENV['BUNDLE_PATH'] = nil
ENV['GEM_HOME'] = ENV['GEM_PATH'] = Path.base_system_gems.to_s
- ENV['PATH'] = "#{Path.system_gem_path}/bin:#{ENV['PATH']}"
+ ENV['PATH'] = "#{Path.home}/.bundler/bin:#{Path.system_gem_path}/bin:#{ENV['PATH']}"
unless File.exist?("#{Path.base_system_gems}")
FileUtils.mkdir_p(Path.base_system_gems)

1 comment on commit 5f26a03

@joshbuddy
Collaborator

Thank you Carl.

Please sign in to comment.
Something went wrong with that request. Please try again.