-
Notifications
You must be signed in to change notification settings - Fork 11
/
find-jruby-head-url.rb
84 lines (69 loc) · 2.28 KB
/
find-jruby-head-url.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'open-uri'
require 'rubygems'
module MicroXMLParser
HEADER = %r{<\?xml\s.+\?>\n}
ATTRIBUTE = %r{(\w+)="([^"]+)"}
OPEN_TAG = %r{<(\w+)((?:\s+#{ATTRIBUTE})*)>}
CLOSE_TAG = %r{</(\w+)>}
TEXT = %r{([^<]*)}
def self.parse(xml)
xml = xml.strip
raise xml unless xml.start_with? HEADER
xml = xml[$&.size..-1].strip
value, rest = rec(xml)
raise rest unless rest.empty?
value
end
def self.rec(xml)
xml = xml.strip
if xml.start_with? OPEN_TAG
entries = []
while xml.start_with? OPEN_TAG
tag, attributes = $1, $2
xml = xml[$&.size..-1].strip
until attributes.empty?
attributes = attributes.strip
raise attributes unless attributes.start_with? ATTRIBUTE
key, value = $1, $2
attributes = attributes[$&.size..-1]
entries << { key.to_sym => value }
end
value, xml = rec(xml)
entries << { tag.to_sym => value }
close_tag = "</#{tag}>"
raise "Missing close tag #{close_tag}: #{xml}" unless xml.start_with? close_tag
xml = xml[close_tag.size..-1].strip
end
keys = entries.map { |entry| entry.first.first }
if keys == keys.uniq
entries = entries.inject({}, :merge)
end
[entries, xml]
elsif xml.start_with? TEXT
text = $1
xml = xml[$&.size..-1]
[text, xml]
else
raise xml
end
end
end
base_url = 'https://oss.sonatype.org/content/repositories/snapshots/org/jruby/jruby-dist'
index_url = "#{base_url}/maven-metadata.xml"
STDERR.puts index_url
xml = URI.open(index_url, &:read)
STDERR.puts xml
parsed = MicroXMLParser.parse(xml)
versions = parsed.dig(:metadata, :versioning, :versions).map { |e| e[:version] }
versions.delete('9000.dev-SNAPSHOT')
most_recent = versions.max_by { |v| Gem::Version.new(v) }
builds_url = "#{base_url}/#{most_recent}/maven-metadata.xml"
STDERR.puts builds_url
xml = URI.open(builds_url, &:read)
STDERR.puts xml
parsed = MicroXMLParser.parse(xml)
last_build = parsed.dig(:metadata, :versioning, :snapshotVersions).map { |e| e[:snapshotVersion] }.select { |node|
node[:classifier] == 'bin' and node[:extension] == 'tar.gz'
}.map { |node| node[:value] }.last
final_url = "#{base_url}/#{most_recent}/jruby-dist-#{last_build}-bin.tar.gz"
puts final_url