Created
April 12, 2012 04:27
-
-
Save rockpapergoat/2364582 to your computer and use it in GitHub Desktop.
using version compare to determine whether to install
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| def version_compare(version_a, version_b) | |
| vre = /[-.]|\d+|[^-.\d]+/ | |
| ax = version_a.scan(vre) | |
| bx = version_b.scan(vre) | |
| while (ax.length>0 && bx.length>0) | |
| a = ax.shift | |
| b = bx.shift | |
| if( a == b ) then next | |
| elsif (a == '-' && b == '-') then next | |
| elsif (a == '-') then return -1 | |
| elsif (b == '-') then return 1 | |
| elsif (a == '.' && b == '.') then next | |
| elsif (a == '.' ) then return -1 | |
| elsif (b == '.' ) then return 1 | |
| elsif (a =~ /^\d+$/ && b =~ /^\d+$/) then | |
| if( a =~ /^0/ or b =~ /^0/ ) then | |
| return a.to_s.upcase <=> b.to_s.upcase | |
| end | |
| return a.to_i <=> b.to_i | |
| else | |
| return a.upcase <=> b.upcase | |
| end | |
| end | |
| version_a <=> version_b; | |
| end | |
| def get_version(app) | |
| if File.exists?("#{app}/Contents/Info.plist") | |
| vers = `/usr/bin/defaults read "#{app}"/Contents/Info CFBundleShortVersionString`.chomp | |
| else | |
| vers = 0 | |
| end | |
| end | |
| app = "/Applications/TextMate.app" | |
| vnew = "1.5.11" | |
| vold = get_version(app) | |
| case version_compare(vold,vnew) | |
| when 0 | |
| puts "versions are equal (#{vold}). installing anyway." | |
| exit(0) | |
| when -1 | |
| puts "version #{vold} on disk is older, installing #{vnew}." | |
| exit(0) | |
| when 1 | |
| puts "version #{vold} on disk is newer. exiting" | |
| exit(1) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment