1#!/usr/bin/env ruby
2
3DOWNLOADS_FILE = 'pages/download.md'
4
5def need_pages_submodule
6    unless File.exists?(DOWNLOADS_FILE)
7        raise "Robolectric pages submodule isn't present.  Run git submodule update --init"
8    end
9end
10
11def fill_index_downloads
12    require 'digest/sha1'
13
14    download_html = "<!-- START_DOWNLOADS -->\n"
15    Dir.glob('pages/downloads/*.jar').sort.reverse.each do |f|
16        sha1 = Digest::SHA1.hexdigest File.read(f)
17
18        fn = f.sub(/^pages\//, '')
19        match = /robolectric-?([0-9]\.[0-9](\.[0-9])?)?(-all)?(-src)?\.jar/.match(f)
20        version = match[1] if match
21        version = "SNAPSHOT" unless version
22        prerelease = /\.rc/.match(f)
23        download_html += prerelease ? "<tr class=\"rc\">\n" : "<tr>\n"
24        download_html += "  <td class=\"link\"><a href=\"#{fn}\" onClick=\"javascript:pageTracker._trackPageView('#{fn}'); \">#{fn.sub(/downloads\//, '')}</a></td>\n"
25        download_html += "  <td class=\"version\">#{version}</td>\n"
26        download_html += "  <td class=\"size\">#{File.size(f) / 1024}k</td>\n"
27        download_html += "  <td class=\"date\">#{File.mtime(f).strftime("%Y/%m/%d %H:%M:%S %Z")}</td>\n"
28        download_html += "  <td class=\"sha\">#{sha1}</td>\n"
29        download_html += "</tr>\n"
30    end
31    download_html += "<!-- END_DOWNLOADS -->"
32
33    downloads_page = File.read(DOWNLOADS_FILE)
34    matcher = /<!-- START_DOWNLOADS -->.*<!-- END_DOWNLOADS -->/m
35    downloads_page = downloads_page.sub(matcher, download_html)
36    File.open(DOWNLOADS_FILE, 'w') {|f| f.write(downloads_page)}
37    puts "rewrote " + DOWNLOADS_FILE
38end
39
40fill_index_downloads