Skip to content

Instantly share code, notes, and snippets.

@revans
Created January 10, 2010 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revans/273714 to your computer and use it in GitHub Desktop.
Save revans/273714 to your computer and use it in GitHub Desktop.
module Project
class Version
##
# The location of the version number file
# VERSION_FILE = File.join(Rails.root, 'config/build.version')
VERSION_FILE = File.join(File.dirname(__FILE__), 'build.version')
@@releases = %w[major minor point].freeze
##
# Generate build version file
#
def self.generate_build_file
File.open(VERSION_FILE, 'w') { |f| f.write '0.0.1' }
print "Created your build file (build.version) located at #{VERSION_FILE.to_s}"
end
##
# Current Version
#
# Pulls the current build version
#
def self.current
print File.read(VERSION_FILE).chomp
end
##
# Verion it
#
# @param [String, Symbol]
#
def self.version_it(direction, release)
version = File.read(VERSION_FILE).chomp
int_ver = version.split('.').join().to_i
new_version = case release
when :major then update_build_version(int_ver, direction, 100)
when :minor then update_build_version(int_ver, direction, 10)
when :point then update_build_version(int_ver, direction, 1)
else
raise ArgumentError, "You can only increase the version number by major, minor, or point."
end
File.open(VERSION_FILE, 'w') { |f| f.write new_version }
print "Previous Version: #{version} - New Version: #{new_version}" and return
end
##
# Method Missing
#
# @param [String, Symbol]
#
# Project::Version.up(:point)
# Project::Version.down(:point)
# Project::Version.up(:minor)
# Project::Version.down(:major)
#
#
def self.method_missing(direction, release)
if @@releases.include?(release.to_s.downcase)
version_it(direction, release)
else
super
end
end
private
##
# Update build version
#
# @param [Integer, Symbol, Symbol]
# @return [String]
#
def self.update_build_version(version, direction, points)
new_version = case direction
when :up then ("%03d" % (version + points)).split(//).join('.')
when :down then ("%03d" % (version - points unless version == 0)).split(//).join('.')
else
"%03d" % version
end
return new_version
end
end
end
# Project::Version.generate_build_file
# Project::Version.current
# Project::Version.up(:point)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment