human_version_to_cfbundle_version.rb from redshed at Krugle
Show human_version_to_cfbundle_version.rb syntax highlighted
#! /usr/bin/ruby
#
# human_version_to_cfbundle_version.rb 1.0d1 (or 01000.00.01 ;-)
# Based on <http://www.dribin.org/dave/blog/archives/2006/08/02/versioning_os_x_apps/>
# Copyright (c) 2006 Jonathan 'Wolf' Rentzsch.
# Some rights reserved: <http://creativecommons.org/licenses/by/2.0/>
#
# A tool that generates CFVersionNumber+Launch Services-happy version numbers given a human version number.
# For example, 1.2.3b4 => becomes 01020.32.04.
# All information is encoded -- no information is lost. It's thus reversible, which this tool also does.
class VersionNumber
DEVELOPMENT = 'd'
ALPHA = 'a'
BETA = 'b'
FINAL_CANDIDATE = 'fc'
FINAL = ''
CFBUNDLEVERSION_MAP = {DEVELOPMENT=>0,ALPHA=>1,BETA=>2,FINAL_CANDIDATE=>3,FINAL=>9}
def initialize(version_string)
# First try to match CFBundleVersion's format ('01031.08.04').
m = version_string.strip.match(/^(\d\d)(\d\d)(\d\.\d)([01239])\.(\d\d)$/)
if m.nil?
# Nope, try human-style ('1.3.10b4').
# Man, where's RCR 237 (Named capture) when I need it?
m = version_string.gsub(/\s/,'').match(/^(\d{1,2})(\.(\d{1,2}))?(\.(\d{1,2}))?((d|a|b|fc)(\d{1,2}))?$/)
raise ArgumentError, "can't parse '#{version_string}'" if m.nil?
@major = $1.to_i
@minor = $3.to_i
@bug = $5.to_i
if $7.nil?
@stage = FINAL
@stageNum = 0
else
@stage = $7 # Kept as a string.
@stageNum = $8.to_i
end
@original_version_string_was_human = true
else
@major = $1.to_i
@minor = $2.to_i
@bug = $3.delete('.').to_i
@stage = CFBUNDLEVERSION_MAP.invert[$4.to_i]
@stageNum = $5.to_i
@original_version_string_was_human = false
end
end
def human_version
result = "#{@major}.#{@minor}"
result += ".#{@bug}" if @bug != 0
result += "#{@stage}"+"#{@stageNum}" if @stage != FINAL
result
end
alias to_s human_version
def cfbundle_version
result = sprintf('%02d%02d%02d%s.%02d',
@major.to_i,
@minor.to_i,
@bug.to_i,
CFBUNDLEVERSION_MAP[@stage],
@stageNum.to_i)
result.insert(5,'.') # Weird placement for the first decimal: it lands in the middle of the 2-digit bug number.
end
def other_version
@original_version_string_was_human ? cfbundle_version : human_version
end
end
if ARGV[0]
puts VersionNumber.new(ARGV[0]).other_version
else
puts 'usage: ruby human_version_to_cfbundle_version.rb 1.3.10b4'
end
See more files for this project here