Skip to content

Commit

Permalink
Establish the DSL for defining maven "gems" and sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
ANithian committed Feb 8, 2012
1 parent a29d455 commit be925d3
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 5 deletions.
36 changes: 31 additions & 5 deletions lib/bundler/dsl.rb
@@ -1,5 +1,4 @@
require 'bundler/dependency' require 'bundler/dependency'

module Bundler module Bundler
class Dsl class Dsl
def self.evaluate(gemfile, lockfile, unlock) def self.evaluate(gemfile, lockfile, unlock)
Expand Down Expand Up @@ -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." raise InvalidOption, "There are multiple gemspecs at #{path}. Please use the :name option to specify which one."
end end
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) def gem(name, *args)
if name.is_a?(Symbol) if name.is_a?(Symbol)
raise GemfileError, %{You need to specify gem names as Strings. Use 'gem "#{name.to_s}"' instead.} raise GemfileError, %{You need to specify gem names as Strings. Use 'gem "#{name.to_s}"' instead.}
Expand Down Expand Up @@ -85,7 +108,11 @@ def gem(name, *args)
end end
end end
end end


if !@source.nil? && @source.is_a?(Bundler::Source::Maven)
@source.add_dependency(name,version[0])
end

@dependencies << dep @dependencies << dep
end end


Expand Down Expand Up @@ -185,7 +212,7 @@ def _normalize_hash(opts)
def _normalize_options(name, version, opts) def _normalize_options(name, version, opts)
_normalize_hash(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? if invalid_keys.any?
plural = invalid_keys.size > 1 plural = invalid_keys.size > 1
message = "You passed #{invalid_keys.map{|k| ':'+k }.join(", ")} " message = "You passed #{invalid_keys.map{|k| ':'+k }.join(", ")} "
Expand Down Expand Up @@ -215,8 +242,7 @@ def _normalize_options(name, version, opts)
github = "#{github}/#{github}" unless github.include?("/") github = "#{github}/#{github}" unless github.include?("/")
opts["git"] = "git://github.com/#{github}.git" opts["git"] = "git://github.com/#{github}.git"
end end

["git", "path","mvn"].each do |type|
["git", "path"].each do |type|
if param = opts[type] if param = opts[type]
if version.first && version.first =~ /^\s*=?\s*(\d[^\s]*)\s*$/ if version.first && version.first =~ /^\s*=?\s*(\d[^\s]*)\s*$/
options = opts.merge("name" => name, "version" => $1) options = opts.merge("name" => name, "version" => $1)
Expand Down
63 changes: 63 additions & 0 deletions lib/bundler/source.rb
Expand Up @@ -5,9 +5,72 @@
require "rubygems/format" require "rubygems/format"
require "digest/sha1" require "digest/sha1"
require "open3" require "open3"
require 'bundler/maven_gemify2'
require 'set'


module Bundler module Bundler
module Source 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 # TODO: Refactor this class
class Rubygems class Rubygems
FORCE_MODERN_INDEX_LIMIT = 100 # threshold for switching back to the modern index instead of fetching every spec FORCE_MODERN_INDEX_LIMIT = 100 # threshold for switching back to the modern index instead of fetching every spec
Expand Down
16 changes: 16 additions & 0 deletions spec/bundler/dsl_spec.rb
Expand Up @@ -18,5 +18,21 @@
github_uri = "git://github.com/rails/rails.git" github_uri = "git://github.com/rails/rails.git"
subject.dependencies.first.source.uri.should == github_uri subject.dependencies.first.source.uri.should == github_uri
end 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
end end
15 changes: 15 additions & 0 deletions spec/install/mvn_spec.rb
@@ -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.