<NME> gembag.rb
<BEF> #!/usr/bin/env ruby

require "rubygems/package"

gemspec = ARGV.shift


require "rubygems/specification"
require "rubygems/commands/install_command"

target = "vendor/bundle/jruby/1.9/"

deps.each do |dep|
  cmd = "gem install --install-dir #{target} #{dep.name} -v '#{dep.requirement}'"
  puts cmd
  system(cmd)
end

#specs_and_sources, errors = Gem::SpecFetcher.fetcher.fetch_with_errors(deps.first, true, true, false)
#require "pry"; binding.pry




gemspec = ARGV.shift || "logstash-kafka.gemspec"

spec = Gem::Specification.load(gemspec)
deps = [spec.runtime_dependencies].flatten

deps.each do |dep|
  # TODO(sissel): Hack for now
  next if "#{dep}" == "addressable (~> 2.2.6)" 

  begin
    # Check if the gem is available
    # 'gem' returns 'true' if it loaded it, false if already loaded,
    # and raises a Gem::LoadError exception on failure.
    # Skip downloading/installing it if it's already here.
    gem(dep.name, dep.requirement)

    # If we get here, we have the gem.
    puts "Gem found matching: #{dep}"
  rescue Gem::LoadError => e
    # Not installed, continue.
    message = e.to_s

    # Sometimes we failed to load because gembag installs too
    # many things. Like 'shoulda' fails to load because two
    # conflicting versions of 'mocha' were installed.
    # Fundamentally, gembag should build a dependency graph and
    # resolve all version requirements to single nodes to prevent
    # this madness.
    #
    # Possible we can steal bundler's implementation of this,
    # or just use bundler to do it, but only if bundler doesn't
    # bite me in the ass again :)
    case message
      when /Unable to activate/
        puts "Gem found, but funky: #{dep} (#{e})"
      when /Could not find/
        puts "Gem not found: #{dep}"
        install_gem(dep.name, dep.requirement, target)
      else
        puts "Unexpected error: #{e}"
        exit 1
    end # case message
  end # begin / rescue Gem::LoadError
end # deps.each




<MSG> Run the installer from ruby instead of using system(); otherwise we risk running the wrong ruby

<DFF> @@ -1,6 +1,7 @@
 #!/usr/bin/env ruby
 
-require "rubygems/package"
+require "rubygems/specification"
+require "rubygems/commands/install_command"
 
 gemspec = ARGV.shift
 
@@ -11,13 +12,21 @@ deps = [spec.development_dependencies, spec.runtime_dependencies].flatten
 target = "vendor/bundle/jruby/1.9/"
 
 deps.each do |dep|
-  cmd = "gem install --install-dir #{target} #{dep.name} -v '#{dep.requirement}'"
-  puts cmd
-  system(cmd)
+  #cmd = "gem install --install-dir #{target} #{dep.name} -v '#{dep.requirement}'"
+  installer = Gem::Commands::InstallCommand.new
+  installer.options[:generate_rdoc] = false
+  installer.options[:generate_ri] = false
+  installer.options[:version] = dep.requirement
+  installer.options[:args] = [dep.name]
+  begin
+    installer.execute
+  rescue Gem::SystemExitException => e
+    if e.exit_code != 0
+      puts "Installation of #{dep.to_s} failed"
+      raise
+    end
+  end
 end
 
-#specs_and_sources, errors = Gem::SpecFetcher.fetcher.fetch_with_errors(deps.first, true, true, false)
-#require "pry"; binding.pry
-
 
 

Run the installer from ruby instead of using system(); otherwise we risk running the wrong ruby