module Buildr::ArtifactSearch

Search best artifact version from remote repositories

Public Instance Methods

best_version(spec, *methods) click to toggle source

TODO: return the url for best matching repo

# File lib/buildr/packaging/artifact_search.rb, line 33
def best_version(spec, *methods)
  spec = Artifact.to_hash(spec)
  spec[:version] = requirement = VersionRequirement.create(spec[:version])
  select = lambda do |candidates|
    candidates.find { |candidate| requirement.satisfied_by?(candidate) }
  end
  result = nil
  methods = search_methods if methods.empty?
  if requirement.composed?
    until result || methods.empty?
      method = methods.shift
      type = method.keys.first
      from = method[type]
      if (include.empty? || !(include & [:all, type, from]).empty?) &&
          (exclude & [:all, type, from]).empty?
        if from.respond_to?(:call)
          versions = from.call(spec.dup)
        else
          versions = send("#{type}_versions", spec.dup, *from)
        end
        result = select[versions]
      end
    end
  end
  result ||= requirement.default
  raise "Could not find #{Artifact.to_spec(spec)}"  +
    "\n You may need to use an specific version instead of a requirement" unless result
  spec.merge :version => result
end
exclude(method = nil) click to toggle source
# File lib/buildr/packaging/artifact_search.rb, line 28
def exclude(method = nil)
  (@excludes ||= []).tap { push method if method }
end
include(method = nil) click to toggle source
# File lib/buildr/packaging/artifact_search.rb, line 24
def include(method = nil)
  (@includes ||= []).tap { push method if method }
end
requirement?(spec) click to toggle source
# File lib/buildr/packaging/artifact_search.rb, line 63
def requirement?(spec)
  VersionRequirement.requirement?(spec[:version])
end

Private Instance Methods

depend_version(spec) click to toggle source
# File lib/buildr/packaging/artifact_search.rb, line 77
def depend_version(spec)
  spec[:version][/[\w\.]+/]
end
local_versions(spec, repo) click to toggle source
# File lib/buildr/packaging/artifact_search.rb, line 92
def local_versions(spec, repo)
  path = (spec[:group].split(/\./) + [spec[:id]]).flatten.join('/')
  Dir[File.expand_path(path + "/*", repo)].map { |d| d.pathmap("%f") }.sort.reverse
end
mvnrepository_versions(art) click to toggle source
# File lib/buildr/packaging/artifact_search.rb, line 125
def mvnrepository_versions(art)
  uri = "http://www.mvnrepository.com/artifact/#{art[:group]}/#{art[:id]}"
  xml = begin
          URI.read(uri)
        rescue URI::NotFoundError => e
          puts e.class, e
          return []
        end
  doc = Hpricot(xml)
  doc.search("table.grid/tr/td[1]/a").map(&:innerHTML)
end
remote_versions(art, base, from = :metadata, fallback = true) click to toggle source
# File lib/buildr/packaging/artifact_search.rb, line 97
def remote_versions(art, base, from = :metadata, fallback = true)
  path = (art[:group].split(/\./) + [art[:id]]).flatten.join('/')
  base ||= "https://repo1.maven.org/maven2"
  uris = {:metadata => "#{base}/#{path}/maven-metadata.xml"}
  uris[:listing] = "#{base}/#{path}/" if base =~ /^https?:/
    xml = nil
  until xml || uris.empty?
    begin
      xml = URI.read(uris.delete(from))
    rescue URI::NotFoundError => e
      from = fallback ? uris.keys.first : nil
    end
  end
  return [] unless xml
  doc = Hpricot(xml)
  case from
  when :metadata then
    doc.search("versions/version").map(&:innerHTML).reverse
  when :listing then
    doc.search("a[@href]").inject([]) { |vers, a|
      vers << a.innerHTML.chop if a.innerHTML[-1..-1] == '/'
      vers
    }.sort.reverse
  else
    fail "Don't know how to parse #{from}: \n#{xml.inspect}"
  end
end
runtime_versions(spec, artifacts) click to toggle source
# File lib/buildr/packaging/artifact_search.rb, line 81
def runtime_versions(spec, artifacts)
  spec_classif = spec.values_at(:group, :id, :type)
  artifacts.inject([]) do |in_memory, str|
    candidate = Artifact.to_hash(str)
    if spec_classif == candidate.values_at(:group, :id, :type)
      in_memory << candidate[:version]
    end
    in_memory
  end
end
search_methods() click to toggle source
# File lib/buildr/packaging/artifact_search.rb, line 68
def search_methods
  [].tap do
    push :runtime => [Artifact.list]
    push :local => Buildr.repositories.local
    Buildr.repositories.remote.each { |remote| push :remote => remote }
    push :mvnrepository => []
  end
end