public
Created

Application File located in the lib directory of a Rails project.

  • Download Gist
app.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
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
version.rake
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.