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

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

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

spec = Gem::Specification.load(gemspec)
      puts "Installation of #{name} failed"
      raise
    end
target = "vendor/bundle/jruby/1.9/"

deps.each do |dep|
  begin
    # Check if the gem is available
    # 'gem' returns true if successful
    # Skip downloading/installing it if it's already here.
    if gem(dep.name, dep.requirement)
      puts "Gem found matching: #{dep.name} #{dep.requirement}"
      next
    end
  rescue Gem::LoadError
    # Not installed, continue.
  end

  puts "Fetching and installing gem: #{dep.name} #{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]
  installer.options[:install_dir] = target
  begin
    installer.execute
  rescue Gem::SystemExitException => e
    if e.exit_code != 0
      puts "Installation of #{dep.to_s} failed"
      raise
    end
  end
end



    #
    # 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> - factor installation into a separate method
- gem() returns true and false if loaded successfully, only
  indicates failure to load if it raises a Gem::LoadError
- only download gems that are actually missing locally.

<DFF> @@ -3,6 +3,25 @@
 require "rubygems/specification"
 require "rubygems/commands/install_command"
 
+def install_gem(name, requirement, target)
+  puts "Fetching and installing gem: #{name} (#{requirement})"
+
+  installer = Gem::Commands::InstallCommand.new
+  installer.options[:generate_rdoc] = false
+  installer.options[:generate_ri] = false
+  installer.options[:version] = requirement
+  installer.options[:args] = [name]
+  installer.options[:install_dir] = target
+  begin
+    installer.execute
+  rescue Gem::SystemExitException => e
+    if e.exit_code != 0
+      puts "Installation of #{name} failed"
+      raise
+    end
+  end
+end # def install_gem
+
 gemspec = ARGV.shift || "logstash.gemspec"
 
 spec = Gem::Specification.load(gemspec)
@@ -12,35 +31,31 @@ deps = [spec.development_dependencies, spec.runtime_dependencies].flatten
 target = "vendor/bundle/jruby/1.9/"
 
 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 successful
+    # '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.
-    if gem(dep.name, dep.requirement)
-      puts "Gem found matching: #{dep.name} #{dep.requirement}"
-      next
-    end
-  rescue Gem::LoadError
-    # Not installed, continue.
-  end
+    gem(dep.name, dep.requirement)
 
-  puts "Fetching and installing gem: #{dep.name} #{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]
-  installer.options[:install_dir] = target
-  begin
-    installer.execute
-  rescue Gem::SystemExitException => e
-    if e.exit_code != 0
-      puts "Installation of #{dep.to_s} failed"
-      raise
-    end
-  end
-end
+    # If we get here, we have the gem.
+    puts "Gem found matching: #{dep}"
+  rescue Gem::LoadError => e
+    # Not installed, continue.
+    message = e.to_s
+
+    case message
+      when /Unable to activate/
+        puts "Gem found, but funky: #{dep} (#{e})"
+      else
+        puts "Gem not found: #{dep} (#{e})"
+        install_gem(dep.name, dep.requirement, target)
+    end # case message
+  end # begin / rescue Gem::LoadError
+end # deps.each
 
 
 

- factor installation into a separate method - gem() returns true and false if loaded successfully, only   indicates failure to load if it raises a Gem::LoadError - only download gems that are actually missing locally.