public
Description: Tool for creating Gentoo ebuilds based on gems
Clone URL: git://github.com/technicalpickles/g-gem.git
g-gem / g-gem.rb
100644 91 lines (70 sloc) 1.749 kb
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
#!/usr/bin/env ruby
 
require 'rubygems'
require 'rubygems/dependency_installer'
 
require 'ruby-debug'
require 'erubis'
 
package = ARGV.first || 'activerecord'
 
class Ebuild
  attr_accessor :spec, :source, :dependencies
  
  def initialize(spec_pair)
    @spec, @source = spec_pair
    @dependencies = []
  end
  
  def filename
    "#{p}.ebuild"
  end
  
  def p
    "#{pn}-#{pv}"
  end
  
  def pn
    spec.name.downcase
  end
  
  def pv
    spec.version.version
  end
  
  def atom_of(dependency)
    "dev-ruby/#{dependency.name}"
  end
  
  def uri
    "#{source}/gems/#{p}.gem"
  end
  
  def write
    output = eruby.evaluate(self)
    FileUtils.mkdir_p('ebuilds')
    File.open("ebuilds/#{filename}", 'w') {|f| f.write(output) }
  end
  
  protected
  
  def eruby
    unless @eruby
      input = File.read('ebuild.eruby')
      @eruby = Erubis::Eruby.new(input)
    end
    @eruby
  end
  
  def self.create_from_spec_lookup(package)
    @@inst ||= Gem::DependencyInstaller.new
    spec_pair = @@inst.find_spec_by_name_and_version(package).first
    self.new(spec_pair)
  end
end
 
 
gems_to_lookup = [package]
ebuilds = {}
until gems_to_lookup.empty?
  next_package = gems_to_lookup.shift
  
  unless ebuilds.has_key?(next_package)
    puts "Gathering info about #{next_package}..."
  
    ebuilds[next_package] = Ebuild.create_from_spec_lookup(next_package)
  else
    puts "Already know about #{next_package}"
  end
  
  ebuilds[next_package].spec.dependencies.each do |dependency|
    unless ebuilds.has_key? dependency.name
      puts "Need to lookup dependency #{dependency.name}"
      gems_to_lookup.push(dependency.name)
    end
  end
end
 
ebuilds.each_pair do |name, ebuild|
  puts "Writing out #{ebuild.filename}"
  ebuild.write
end