#!/usr/bin/env ruby # Get latest version of tools require 'yaml' require 'uri' require 'net/http' require 'rubygems/version' # Compare versions including libjpeg ones (9b <=> 9) class Version include Comparable attr_reader :string, :parsed alias_method :to_s, :string def initialize(string) @string = string @parsed = begin Gem::Version.new(string) rescue ArgumentError nil end end def <=>(other) if parsed && other.parsed parsed <=> other.parsed else string <=> other.string end end end bare = ARGV.delete('--bare') unless [1, 2].include?(ARGV.length) abort 'expected project name and optional current version' end def version(string) Version.new(string) end def get(url) uri = URI(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == 'https') request = Net::HTTP::Get.new(uri.request_uri) http.request(request).body end name = ARGV[0].downcase config = YAML.load(DATA)[name] abort "Livecheck for #{name} was not defined" unless config url = config['url'] version_regex = Regexp.new(config['regexp']) current_version = ARGV[1] && version(ARGV[1]) data = get(url) versions = data.scan(version_regex).uniq.map{ |match| version(match[0]) } abort "No versions found for #{name} in data:\n#{data}" if versions.empty? latest_version = versions.max case when bare print latest_version when current_version if latest_version != current_version sign = latest_version > current_version ? '>' : '<' puts "#{name} #{latest_version} #{sign} #{current_version}" end else puts "#{name} #{latest_version}" end __END__ advancecomp: url: https://github.com/amadvance/advancecomp/releases.atom regexp: /amadvance/advancecomp/releases/tag/v(\d+(?:\.\d+)*) gifsicle: url: https://www.lcdf.org/gifsicle/ regexp: gifsicle-(\d+(?:\.\d+)*)\.tar\.gz jhead: url: http://www.sentex.net/~mwandel/jhead/ regexp: jhead-(\d+(?:\.\d+)*) jpegoptim: url: https://www.kokkonen.net/tjko/src/ regexp: jpegoptim-(\d+(?:\.\d+)*) jpegarchive: url: https://github.com/danielgtaylor/jpeg-archive/releases.atom regexp: JPEG-Archive (\d+(?:\.\d+)*) libjpeg: url: http://www.ijg.org/files/ regexp: jpegsrc.v(.*?).tar.gz libmozjpeg: url: https://github.com/mozilla/mozjpeg/releases.atom regexp: mozjpeg v(\d+(?:\.\d+)*) libpng: url: https://sourceforge.net/projects/libpng/rss?path=/libpng16 regexp: /libpng\d*/(\d+(?:\.\d+)*)/ libz: url: https://sourceforge.net/projects/libpng/rss?path=/zlib regexp: /zlib/(\d+(?:\.\d+)*)/ optipng: url: https://sourceforge.net/projects/optipng/rss regexp: /OptiPNG/optipng-(\d+(?:\.\d+)*)/ pngcrush: url: https://sourceforge.net/projects/pmt/rss?path=/pngcrush regexp: /pngcrush/(\d+(?:\.\d+)*)/ pngquant: url: https://github.com/pornel/pngquant/releases.atom regexp: /pornel/pngquant/releases/tag/(\d+(?:\.\d+)*)