Skip to content

Instantly share code, notes, and snippets.

@fstrube
Created June 1, 2012 22:17
Show Gist options
  • Save fstrube/2855458 to your computer and use it in GitHub Desktop.
Save fstrube/2855458 to your computer and use it in GitHub Desktop.
Rake tasks for a Magento extension using modman
require 'rake'
desc 'Displays current version'
task :version do
puts "Current version is #{Version.new.to_s}"
end
namespace :version do
desc "Bump version number"
task :bump do
version = Version.new
puts "Bumped version #{version.to_s} --> #{ENV['VER'] ? version.bump(ENV['VER']) : version.bump}"
end
desc "Set version number"
task :set do
version = Version.new
puts "Changed version #{version.to_s} --> #{version.set(ENV['VER'])}"
end
end
# Version object
class Version
DEFAULT_CONFIG_FILE = "code/etc/config.xml"
def initialize(config_file_path=DEFAULT_CONFIG_FILE)
@config_file_path = config_file_path
@config = File.open('code/etc/config.xml', 'rb').read
@parts = Hash.new
@parts[:major], @parts[:minor], @parts[:patch] = @config.match(/version>(.*)</)[1].split('.').collect(&:to_i)
end
def bump(ver= :patch)
ver = ver.to_sym
@parts[ver] = @parts[ver].to_i + 1
if ver == :minor
@parts[:patch] = 0
elsif ver == :major
@parts[:minor] = @parts[:patch] = 0
end
save
end
def set(new_version)
@parts[:major], @parts[:minor], @parts[:patch] = new_version.split('.').collect(&:to_i)
save
end
def to_s
"#{major}.#{minor}.#{patch}"
end
def method_missing(method, *args)
if [:major,:minor,:patch].include?(method)
@parts[method]
else
super
end
end
def save
begin
File.open(@config_file_path, 'w') do |f|
f.write(@config.sub /<version>.*<.version>/, "<version>#{to_s}</version>")
end
rescue
File.open(@config_file_path, 'w') {|f| f.write(@config)}
raise
end
to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment