|
#!/usr/bin/ruby |
|
|
|
IVY = "/Users/daniel/.ivy2/cache" |
|
|
|
# the optional trailing schtuff means that snapshots, RCs, and other insanity will compare as equal to numbered versions |
|
NUM_VERSION = /^(\d+)(\.\d+)+?([-\.].*)?/ |
|
|
|
class Artifact |
|
CROSS_BUILT = /_2\.(9.*|10.*|11.*)$/ |
|
|
|
attr_reader :group_id, :artifact_id, :version |
|
|
|
def initialize(group_id, artifact_id, version, bundle) |
|
@group_id = group_id |
|
@artifact_id = artifact_id |
|
@version = version |
|
@bundle = bundle |
|
end |
|
|
|
def bundle? |
|
@bundle |
|
end |
|
|
|
def crossbuilt_211? |
|
md = artifact_id.match CROSS_BUILT |
|
md.nil? || (md[1] =~ /^11/) |
|
end |
|
|
|
def jar |
|
"#{IVY}/#{group_id}/#{artifact_id}/#{if bundle? then "bundles" else "jars" end}/#{artifact_id}-#{version}.jar" |
|
end |
|
|
|
def <=>(that) |
|
return nil if group_id != that.group_id || artifact_id != that.artifact_id |
|
|
|
if version =~ NUM_VERSION |
|
version_data = $~.to_a |
|
version_data.shift 1 |
|
|
|
if that.version =~ NUM_VERSION |
|
that_version_data = $~.to_a |
|
that_version_data.shift 1 |
|
|
|
version_data.select! { |e| !e.nil? } |
|
that_version_data.select! { |e| !e.nil? } |
|
|
|
version_data.map! do |e| |
|
if e =~ /^\.(.*)/ |
|
$~[1] |
|
else |
|
e |
|
end |
|
end |
|
|
|
that_version_data.map! do |e| |
|
if e =~ /^\.(.*)/ |
|
$~[1] |
|
else |
|
e |
|
end |
|
end |
|
|
|
# if both versions are non-snapshot, non-annotated, and basically not crazy, compare lexicographically |
|
version_data <=> that_version_data |
|
else |
|
nil |
|
end |
|
else |
|
# can't compare crazy versions (yet) |
|
nil |
|
end |
|
end |
|
|
|
def to_s |
|
"#{group_id}:#{artifact_id}:#{version}" |
|
end |
|
end |
|
|
|
groups = Dir.entries(IVY).select { |g| g != '.' && g != '..' } |
|
|
|
artifacts = groups.map do |group| |
|
artifacts = Dir.entries("#{IVY}/#{group}").select { |g| g != '.' && g != '..' } |
|
|
|
artifacts.map do |artifact| |
|
jar_path = "#{IVY}/#{group}/#{artifact}/jars" |
|
bundle_path = "#{IVY}/#{group}/#{artifact}/bundles" |
|
version_pat = /^#{artifact}(-bin|-src)?-(.*).jar$/ |
|
|
|
# TODO add support for locally-published artifacts |
|
jars = if File.exists? jar_path |
|
Dir.entries(jar_path).select { |g| g != '.' && g != '..' } |
|
else |
|
[] |
|
end |
|
|
|
bundles = if File.exists? bundle_path |
|
Dir.entries(bundle_path).select { |g| g != '.' && g != '..' } |
|
else |
|
[] |
|
end |
|
|
|
jar_arts = jars.map do |jar| |
|
md = jar.match version_pat |
|
|
|
if md |
|
version = if md.size == 2 |
|
md[1] |
|
else |
|
md[2] |
|
end |
|
|
|
[Artifact.new(group, artifact, version, false)] |
|
else |
|
[] |
|
end |
|
end |
|
|
|
bundle_arts = bundles.map do |jar| |
|
md = jar.match version_pat |
|
|
|
if md |
|
version = if md.size == 2 |
|
md[1] |
|
else |
|
md[2] |
|
end |
|
|
|
# TODO be less copy-pastey and fail |
|
[Artifact.new(group, artifact, version, true)] |
|
else |
|
[] |
|
end |
|
end |
|
|
|
(jar_arts + bundle_arts).uniq |
|
end |
|
end.flatten |
|
|
|
only_modern = artifacts.select { |a| a.crossbuilt_211? } |
|
|
|
jars = only_modern.group_by { |a| [a.group_id, a.artifact_id] }.map do |_, versions| |
|
versions.sort! |
|
|
|
versions.last.jar |
|
end |
|
|
|
jars.each { |j| puts j } |