Permalink
Browse files

slimmed down bundle bin to pull out ruby DSL info from Gemfile

  • Loading branch information...
1 parent 13da67a commit 78e3eb9385f943a314863388a8652e3f1e737121 @hone hone committed Dec 27, 2012
Showing with 194 additions and 0 deletions.
  1. +56 −0 bin/bundle_ruby
  2. +112 −0 spec/other/bundle_ruby_spec.rb
  3. +26 −0 spec/support/helpers.rb
View
@@ -0,0 +1,56 @@
+#!/usr/bin/env ruby
+
+Signal.trap("INT") { exit 1 }
+
+require 'bundler/ruby_version'
+require 'bundler/ruby_dsl'
+require 'bundler/shared_helpers'
+
+module Bundler
+ class GemfileError < RuntimeError; end
+ class Dsl
+ include RubyDsl
+
+ attr_accessor :ruby_version
+
+ def initialize
+ @ruby_version = nil
+ end
+
+ def eval_gemfile(gemfile, contents = nil)
+ contents ||= File.open(gemfile, "rb") { |f| f.read }
+ instance_eval(contents, gemfile.to_s, 1)
+ rescue SyntaxError => e
+ bt = e.message.split("\n")[1..-1]
+ raise GemfileError, ["Gemfile syntax error:", *bt].join("\n")
+ rescue ScriptError, RegexpError, NameError, ArgumentError => e
+ e.backtrace[0] = "#{e.backtrace[0]}: #{e.message} (#{e.class})"
+ STDERR.puts e.backtrace.join("\n ")
+ raise GemfileError, "There was an error in your Gemfile," \
+ " and Bundler cannot continue."
+ end
+
+ def source(source, options = {})
+ end
+
+ def gem(name, *args)
+ end
+
+ def group(*args, &blk)
+ end
+ end
+end
+
+dsl = Bundler::Dsl.new
+begin
+ dsl.eval_gemfile(Bundler::SharedHelpers.default_gemfile)
+ ruby_version = dsl.ruby_version
+ if ruby_version
+ puts ruby_version
+ else
+ puts "No ruby version specified"
+ end
+rescue Bundler::GemfileError => e
+ puts e.message
+ exit(-1)
+end
@@ -0,0 +1,112 @@
+require "spec_helper"
+
+describe "bundle_ruby" do
+ it "returns the ruby version" do
+ gemfile <<-G
+ source "file://#{gem_repo1}"
+ ruby "1.9.3", :engine => 'ruby', :engine_version => '1.9.3'
+
+ gem "foo"
+ G
+
+ bundle_ruby
+
+ expect(out).to eq("ruby 1.9.3")
+ end
+
+ it "engine defaults to MRI" do
+ gemfile <<-G
+ source "file://#{gem_repo1}"
+ ruby "1.9.3"
+
+ gem "foo"
+ G
+
+ bundle_ruby
+
+ expect(out).to eq("ruby 1.9.3")
+ end
+
+ it "handles jruby" do
+ gemfile <<-G
+ source "file://#{gem_repo1}"
+ ruby "1.8.7", :engine => 'jruby', :engine_version => '1.6.5'
+
+ gem "foo"
+ G
+
+ bundle_ruby
+
+ expect(out).to eq("ruby 1.8.7 (jruby 1.6.5)")
+ end
+
+ it "handles rbx" do
+ gemfile <<-G
+ source "file://#{gem_repo1}"
+ ruby "1.8.7", :engine => 'rbx', :engine_version => '1.2.4'
+
+ gem "foo"
+ G
+
+ bundle_ruby
+
+ expect(out).to eq("ruby 1.8.7 (rbx 1.2.4)")
+ end
+
+ it "raises an error if engine is used but engine version is not" do
+ gemfile <<-G
+ source "file://#{gem_repo1}"
+ ruby "1.8.7", :engine => 'rbx'
+
+ gem "foo"
+ G
+
+ bundle_ruby :exitstatus => true
+ expect(exitstatus).not_to eq(0)
+
+ bundle_ruby
+ expect(out).to eq("Please define :engine_version")
+ end
+
+ it "raises an error if engine_version is used but engine is not" do
+ gemfile <<-G
+ source "file://#{gem_repo1}"
+ ruby "1.8.7", :engine_version => '1.2.4'
+
+ gem "foo"
+ G
+
+ bundle_ruby :exitstatus => true
+ expect(exitstatus).not_to eq(0)
+
+ bundle_ruby
+ expect(out).to eq("Please define :engine")
+ end
+
+ it "raises an error if engine version doesn't match ruby version for mri" do
+ gemfile <<-G
+ source "file://#{gem_repo1}"
+ ruby "1.8.7", :engine => 'ruby', :engine_version => '1.2.4'
+
+ gem "foo"
+ G
+
+ bundle_ruby :exitstatus => true
+ expect(exitstatus).not_to eq(0)
+
+ bundle_ruby
+ expect(out).to eq("ruby_version must match the :engine_version for MRI")
+ end
+
+ it "should print if no ruby version is specified" do
+ gemfile <<-G
+ source "file://#{gem_repo1}"
+
+ gem "foo"
+ G
+
+ bundle_ruby
+
+ expect(out).to eq("No ruby version specified")
+ end
+end
View
@@ -81,6 +81,32 @@ def bundle(cmd, options = {})
end
end
+ def bundle_ruby(options = {})
+ expect_err = options.delete(:expect_err)
+ exitstatus = options.delete(:exitstatus)
+ options["no-color"] = true unless options.key?("no-color")
+
+ bundle_bin = File.expand_path('../../../bin/bundle_ruby', __FILE__)
+
+ requires = options.delete(:requires) || []
+ requires << File.expand_path('../fakeweb/'+options.delete(:fakeweb)+'.rb', __FILE__) if options.key?(:fakeweb)
+ requires << File.expand_path('../artifice/'+options.delete(:artifice)+'.rb', __FILE__) if options.key?(:artifice)
+ requires_str = requires.map{|r| "-r#{r}"}.join(" ")
+
+ env = (options.delete(:env) || {}).map{|k,v| "#{k}='#{v}' "}.join
+ args = options.map do |k,v|
+ v == true ? " --#{k}" : " --#{k} #{v}" if v
+ end.join
+
+ cmd = "#{env}#{Gem.ruby} -I#{lib} #{requires_str} #{bundle_bin}"
+
+ if exitstatus
+ sys_status(cmd)
+ else
+ sys_exec(cmd, expect_err){|i| yield i if block_given? }
+ end
+ end
+
def ruby(ruby, options = {})
expect_err = options.delete(:expect_err)
env = (options.delete(:env) || {}).map{|k,v| "#{k}='#{v}' "}.join

0 comments on commit 78e3eb9

Please sign in to comment.