[Rubygems-developers] How to rebuild a gem

Han Holl han.holl at tietoenator.com
Mon Jan 26 08:17:46 EST 2009

On Friday 23 January 2009, Luis Lavena wrote:

> Oh yeha, my fault:
> gem spec pg --version '' --remote >
> specifications/pg-
Thanks Luis, we're getting closer and closer.
Still, the output from the aboce command starts with:
--- !ruby/object:Gem::Specification
name: pg
version: !ruby/object:Gem::Version
platform: ruby

It looks like YAML.
A .gemspec file is like:
Gem::Specification.new do |s|
  s.name = %q{commandline}
  s.version = "0.7.10"

  s.specification_version = 1 if s.respond_to? :specification_version=

The missing link would appear to be a function that can dump a 
Gem::Specification object in .gemspec form.
I found them, and can report that the following works fine:
#!/usr/bin/env ruby

require 'yaml'
require 'rubygems'
require 'rubygems/specification'
require 'tempfile'
require 'fileutils'

gem = ARGV[0]
raise 'give name (gem-version)' if gem.nil?
out = "#{Gem::dir}/specifications/#{gem}.gemspec"
raise "#{out} already exists" if test(?f, out)
pkt, version = gem.split('-', 2)

IO.popen("gem spec #{pkt} --version '#{version}' --remote") do |io|
  Tempfile.open('gem') do |temp|
    temp.puts Gem::Specification.from_yaml(io).to_ruby
    FileUtils.mv(temp.path, out)

Thanks a lot for your help.


Han Holl

More information about the Rubygems-developers mailing list