Skip to content

Instantly share code, notes, and snippets.

@revans
Created May 25, 2011 02:31
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/990208 to your computer and use it in GitHub Desktop.
Save revans/990208 to your computer and use it in GitHub Desktop.
Application File located in the lib directory of a Rails project.
require 'colored'
module App
class Project
class << self
def name
'Project Name'
end
def domain
'project_domain.com'
end
def support_email
'support@project_domain.com'
end
def version
Version.current
end
end
end
class VersionError < StandardError; end
class Version
class << self
##
# The location of the version number file
VERSION_CONFIG = File.expand_path(File.join(File.dirname(__FILE__), "..", "config"))
VERSION_FILE = File.expand_path(File.join(VERSION_CONFIG, "build.version"))
@@releases = %w[major minor point].freeze
# Generate a build.version file to the config/ directory of a rails application.
def generate_build_file
# create the config directory if it doesn't already exist
FileUtils.mkdir_p(VERSION_CONFIG) unless File.exists?(VERSION_CONFIG)
# create the build.version file and add the first version, 0.0.1
File.open(VERSION_FILE, 'w') { |f| f.write '0.0.1' }
print "Created a build version file (build.version) located at #{VERSION_FILE.to_s}"
end
# Return the current version number from the build version file.
def current
read_build_file
end
# Read the Build File
def read_build_file
(File.read(VERSION_FILE).chomp rescue 0)
end
# Verison up or down for major, minor, and point releases
def versioning(direction, release)
version = read_build_file
int_ver = version.split('.').join().to_i
# case to determine versioning release
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
# save the version number to the build version file
File.open(VERSION_FILE, 'w') { |f| f.write new_version }
# give the user a message that it has completed and show the previous/current version.
puts "Previous Version: #{version} - New Version: #{new_version}".green
end
# Method Missing for capturing the Version.up and Version.down
def method_missing(direction, release)
raise VersionError.new("You can only version up or down.") unless ['up', 'down'].include?(direction)
# if major/minor/point is requested
if @@releases.include?(release.to_s.downcase)
versioning(direction, release)
else
super
end
end
private
# Does the actual updating of the version numbers
def update_build_version(version, direction, points)
# what direction is being requested
new_version = case direction
# add version points to the current version
when :up then ("%03d" % (version + points)).split(//).join('.')
# minus version points from the current version
when :down then ("%03d" % (version - points unless version == 0)).split(//).join('.')
else
# the current version
"%03d" % version
end
new_version
end
end
end
end
# Easy rake tasks associated with the application versioning system.
# located in lib/tasks/version.rake
#
namespace :project do
desc "Get the project's version number"
task :version => :environment do
puts "#{App::Project.name}'s version: #{App::Project.version}."
end
task :name => :environment do
puts "Project name: #{App::Project.name}"
end
task :domain => :environment do
puts "Project's domain name: #{App::Project.domain}"
end
task :support_email => :environment do
puts "Support Email: #{App::Project.support_email}"
end
end
namespace :version do
desc 'Point Release'
task :point_release => :environment do
App::Version.version_it(:up, :point)
end
desc 'Minor Release'
task :minor_release => :environment do
App::Version.version_it(:up, :minor)
end
desc 'Major Release'
task :major_release => :environment do
App::Version.version_it(:up, :major)
end
# Downgrade version
namespace :down do
desc 'Downgrade Point Release'
task :point_release => :environment do
App::Version.version_it(:down, :point)
end
desc 'Downgrade Minor Release'
task :minor_release => :environment do
App::Version.version_it(:down, :minor)
end
desc 'Downgrade Major Release'
task :major_release => :environment do
App::Version.version_it(:down, :major)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment