Permalink
Browse files

Establish the DSL for defining maven "gems" and sources.

  • Loading branch information...
ANithian committed Feb 8, 2012
1 parent a29d455 commit be925d3ea913999b0985814c737ced7a5ce52760
Showing with 125 additions and 5 deletions.
  1. +31 −5 lib/bundler/dsl.rb
  2. +63 −0 lib/bundler/source.rb
  3. +16 −0 spec/bundler/dsl_spec.rb
  4. +15 −0 spec/install/mvn_spec.rb
View
@@ -1,5 +1,4 @@
require 'bundler/dependency'
-
module Bundler
class Dsl
def self.evaluate(gemfile, lockfile, unlock)
@@ -45,7 +44,31 @@ def gemspec(opts = nil)
raise InvalidOption, "There are multiple gemspecs at #{path}. Please use the :name option to specify which one."
end
end
+
+ #START MAVEN STUFF
+
+ #Influenced from https://github.com/jkutner/bundler/blob/master/lib/bundler/dsl.rb
+ def mvn(repo, options={}, source_options={}, &blk)
+ if (options['name'].nil? || options['version'].nil?) and !block_given?
+ raise InvalidOption, 'Must specify a dependency name+version, or block of dependencies.'
+ end
+ puts "MVN REPO=#{repo} OPTS = #{options.inspect} SOPTS = #{source_options.inspect}"
+ remotes = Array === repo ? repo : [repo]
+ local_source = source Source::Maven.new(_normalize_hash(options).merge('remotes' => remotes)), source_options, &blk
+
+ if options['name'] && options['version']
+ puts "ADD DEP"
+ local_source.add_dependency(options['name'], options['version'])
+ end
+ local_source
+ end
+
+ #TODO: ADD mvn_repo "repo" {
+ #mvn <group_id>,<artifact_id>,<name>
+ #}
+
+ #END MAVEN STUFF
def gem(name, *args)
if name.is_a?(Symbol)
raise GemfileError, %{You need to specify gem names as Strings. Use 'gem "#{name.to_s}"' instead.}
@@ -85,7 +108,11 @@ def gem(name, *args)
end
end
end
-
+
+ if !@source.nil? && @source.is_a?(Bundler::Source::Maven)
+ @source.add_dependency(name,version[0])
+ end
+
@dependencies << dep
end
@@ -185,7 +212,7 @@ def _normalize_hash(opts)
def _normalize_options(name, version, opts)
_normalize_hash(opts)
- invalid_keys = opts.keys - %w(group groups git github path name branch ref tag require submodules platform platforms type)
+ invalid_keys = opts.keys - %w(group groups git github path name branch ref tag require submodules platform platforms type mvn)
if invalid_keys.any?
plural = invalid_keys.size > 1
message = "You passed #{invalid_keys.map{|k| ':'+k }.join(", ")} "
@@ -215,8 +242,7 @@ def _normalize_options(name, version, opts)
github = "#{github}/#{github}" unless github.include?("/")
opts["git"] = "git://github.com/#{github}.git"
end
-
- ["git", "path"].each do |type|
+ ["git", "path","mvn"].each do |type|
if param = opts[type]
if version.first && version.first =~ /^\s*=?\s*(\d[^\s]*)\s*$/
options = opts.merge("name" => name, "version" => $1)
View
@@ -5,9 +5,72 @@
require "rubygems/format"
require "digest/sha1"
require "open3"
+require 'bundler/maven_gemify2'
+require 'set'
module Bundler
module Source
+
+ class Maven
+ def initialize(options = {})
+ @maven_gemify = Gem::Maven::Gemify2.new
+ @dependencies = []
+ repos = options['remotes']
+ if repos
+ repos.each {|repo|
+ @maven_gemify.add_repository(repo) unless repo == "default"
+ }
+ end
+ end
+
+ def add_repository(repo_url)
+ @maven_gemify.add_repository(repo_url)
+ end
+
+ def add_dependency(gemname, version)
+ @dependencies << [gemname,version]
+ end
+
+ def install(spec)
+ #Use maven_gemify to generate the gem here
+ Bundler.ui.info "Installing #{spec.name} (#{spec.version}) "
+ @maven_gemify.generate_gem(spec.name,spec.version)
+ end
+
+ def specs
+ @specs ||= download_specs
+ end
+
+ def remote!
+
+ end
+
+ def to_lock
+ out = "MAVEN\n"
+ out << @maven_gemify.repositories.map {|r| " remotes: #{r}\n" }.join
+ out << " specs:\n"
+ end
+
+ def self.from_lock(options)
+ if options['remotes']
+ options['remotes']=Array(options['remotes'])
+ end
+
+ new(options)
+ end
+ private
+
+ def download_specs
+ return_specs = Index.new
+ @dependencies.each {|dep|
+ spec = @maven_gemify.generate_spec(dep[0],dep[1])
+ spec.source = self
+ spec.loaded_from = "#{Bundler.rubygems.gem_dir}/specifications/#{@maven_gemify.maven_name(spec.name)}-#{spec.version.to_s}.gemspec"
+ return_specs << spec
+ }
+ return_specs
+ end
+ end
# TODO: Refactor this class
class Rubygems
FORCE_MODERN_INDEX_LIMIT = 100 # threshold for switching back to the modern index instead of fetching every spec
View
@@ -18,5 +18,21 @@
github_uri = "git://github.com/rails/rails.git"
subject.dependencies.first.source.uri.should == github_uri
end
+
+ it "should work with maven as an option" do
+ subject.gem("mvn:commons-lang:commons-lang","2.6.1",:mvn=>"default")
+ source = subject.dependencies.first.source
+ puts "SOURCE=#{source}"
+ puts "SPECS = #{source.specs.inspect}"
+ end
+
+ it "should work with maven as a block" do
+ subject.mvn("default") do
+ subject.gem("mvn:commons-lang:commons-lang","2.6.1")
+ end
+ source = subject.dependencies.first.source
+ puts "SOURCE=#{source}"
+ puts "SPECS = #{source.specs.inspect}"
+ end
end
end
View
@@ -0,0 +1,15 @@
+require "spec_helper"
+
+describe "bundle install with maven" do
+ it "fetches gems" do
+ build_lib "foo"
+
+ install_gemfile <<-G
+ mvn "default"
+ gem 'mvn:commons-lang:commons-lang','2.3'
+ G
+
+ should_be_installed("foo 1.0")
+ end
+
+end

0 comments on commit be925d3

Please sign in to comment.