Skip to content

Instantly share code, notes, and snippets.

@Narnach
Created January 14, 2009 09:12
Show Gist options
  • Select an option

  • Save Narnach/46847 to your computer and use it in GitHub Desktop.

Select an option

Save Narnach/46847 to your computer and use it in GitHub Desktop.
Build all versions of a rubygem based on git tags for each version
#!/usr/bin/env ruby
# BuildGems
# Created by Wes Oldenbeuving, copyright 2009, licensed under MIT LICENSE.
#
# For a git project, build the gems for all gem tag releases.
# The assumptions made are the following:
# - Version tags have the format 'N.N', 'N.N.N', etc... Only digits and dots.
# - There is a 'rake gem' task that builds the gem for the checked out version
# - Gems are located in pkg/*.gem
# - You want to copy all created gems to ~/gems/ after creating them.
#
# My use case for this is to build a library of old gem versions I can install
# when working on older client applications that require old gem versions.
#
# Make sure you have no local changes, as a new branch is checked out and/or
# created for each version. These branches will be the tag name without the
# dots. So version 1.2.3.4 will be branch 1234.
# This branch is forced to reset to the tag.
class String
def clean_lines
split("\n").map{|line| line.strip}
end
end
class Hash
def sorted_each(&block)
keys.sort.each do |key|
block.call(key, self[key])
end
self
end
end
head = File.read('.git/HEAD').strip.gsub('ref: refs/heads/','')
tags = `git tag -l`.clean_lines.select {|tag| tag.match(/\d+(\.\d+)+/)}
branches = `git branch -l`.clean_lines
tag_branches = tags.inject({}) {|h,tag| h[tag] = tag.gsub(/\W/,''); h}
system("rm -rf pkg")
tag_branches.sorted_each do |tag, branch|
if branches.include?(branch)
system("git checkout -f #{branch} && git reset --hard #{tag}")
else
system("git checkout -f -b #{branch} #{tag}")
branches << branch
end
system("rake gem")
end
system "git checkout #{head}"
system "cp pkg/*.gem ~/gems/" if File.directory?(File.expand_path('~/gems/'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment