class FatGem

Constants

RAKEFILE

Attributes

directory[R]

Public Class Methods

new(directory) click to toggle source
# File bin/fatgem, line 20
def initialize(directory)
  @directory = directory
  @files = Hash.new { |h, k| h[k] = {} }
  @versions = {}
  @specs = []
end
tmp() { |new(dir)| ... } click to toggle source
# File bin/fatgem, line 12
def self.tmp
  Dir.mktmpdir do |dir|
    yield new(dir)
  end
end

Public Instance Methods

add_gem(path, ruby_version) click to toggle source
# File bin/fatgem, line 39
def add_gem(path, ruby_version)

  if @versions.has_key?(ruby_version)
    version_str = ruby_version ? "Ruby v#{ruby_version}" : "all Ruby versions"
    fail Error, ["Found two gems with same Ruby version:",
                 "- #{path} matches #{version_str}",
                 "- #{@versions[ruby_version]} also matches #{version_str}"].join("\n")
  end

  @versions[ruby_version] = path

  yaml_metadata = nil

  open_tar(open(path)) do |entry|
    case entry.full_name
    when "metadata.gz"
      yaml_metadata = Gem.gunzip(entry.read)
    when "metadata"
      yaml_metadata = entry.read
    when "data.tar.gz"
      open_tar_gz(entry) do |data|
        write_version(ruby_version, data.full_name, data.read)
      end
    end
  end

  fail Error, "#{path} does not contain a gemspec" if yaml_metadata.nil?

  write_version(ruby_version, "metadata.yml", yaml_metadata)

  spec = YAML.load(yaml_metadata)
  @versions[spec] = path
  @specs << spec
end
assert_unique(type, specs = @specs) click to toggle source
# File bin/fatgem, line 115
def assert_unique(type, specs = @specs)
  values = specs.uniq(&type)
  if values.size != 1
    errors = values.map { |s| "- #{@versions[s]} has #{type} = #{s.send(type)}" }
    if errors.empty?
      errors << "- There's no gem with a specific platform"
    end
    fail Error, ["Can only fatten gems with same '#{type}'.", *errors].join("\n")
  end
end
finalize() click to toggle source
# File bin/fatgem, line 126
def finalize
  platform_specs = @specs.select { |s| s.platform != "ruby" }

  assert_unique :name
  assert_unique :version
  assert_unique :platform, platform_specs

  main_spec = platform_specs[0]

  write("data/index", @files.to_yaml)
  write("Rakefile", RAKEFILE)

  result = nil

  Dir.chdir(@directory) do
    main_spec.files = Dir["**/*"]
    main_spec.extensions = ["Rakefile"]

    file_name = Gem::Package.build(main_spec)
    result = File.expand_path(file_name)
  end

  result
end
open_tar(io, &blk) click to toggle source
# File bin/fatgem, line 27
def open_tar(io, &blk)
  Gem::Package::TarReader.new(io) do |reader|
    reader.each(&blk)
  end
end
open_tar_gz(io, &blk) click to toggle source
# File bin/fatgem, line 33
def open_tar_gz(io, &blk)
  Zlib::GzipReader.wrap io do |gzio|
    open_tar(gzio, &blk)
  end
end
write(name, data) click to toggle source
# File bin/fatgem, line 81
def write(name, data)
  path = File.join(@directory, name)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, data)
end
write_version(version, path, content) click to toggle source
# File bin/fatgem, line 74
def write_version(version, path, content)
  hash = Digest::SHA1.hexdigest(content)
  blobpath = "data/#{hash}"
  write(blobpath, content)
  @files[version][path] = blobpath
end