]> git.pld-linux.org Git - packages/rpm.git/blob - rubygems.rb
- added cppcompat patch (make rpmdb.h C++ compatible)
[packages/rpm.git] / rubygems.rb
1 #!/usr/bin/env ruby
2 #--
3 # Copyright 2010 Per Ã˜yvind Karlsen <peroyvind@mandriva.org>
4 # This program is free software. It may be redistributed and/or modified under
5 # the terms of the LGPL version 2.1 (or later).
6 #
7 # FIXME: Someone with actual ruby skills should really clean up and sanitize
8 #        this! fugliness obvious...
9 #++
10
11 require 'optparse'
12 require 'rbconfig'
13
14 provides = false
15 requires = false
16
17 opts = OptionParser.new("#{$0} <--provides|--requires>")
18 opts.on("-P", "--provides", "Print provides") do |val|
19   provides = true
20 end
21 opts.on("-R", "--requires", "Print requires") do |val|
22   requires= true
23 end
24
25 rest = opts.permute(ARGV)
26
27 if rest.size != 0 or (!provides and !requires) or (provides and requires)
28   $stderr.puts "Use either --provides OR --requires"
29   $stderr.puts opts
30   exit(1)
31 end
32
33 require 'rubygems'
34 gem_dir = Gem.respond_to?(:default_dirs) ? Gem.default_dirs[:system][:gem_dir] : Gem.path.first
35 specpatt = "#{gem_dir}/specifications/.*\.gemspec$"
36 gems = []
37 ruby_versioned = false
38 abi_provide = false
39 # as ruby_version may be empty, take version from basename of archdir
40 ruby_version = RbConfig::CONFIG["ruby_version"].empty? ? File.basename(RbConfig::CONFIG["archdir"]) : RbConfig::CONFIG["ruby_version"]
41
42 for path in $stdin.readlines
43   # way fugly, but we make the assumption that if the package has
44   # this file, the package is the current ruby version, and should
45   # therefore provide ruby(abi) = version
46   if provides and path.match(RbConfig::CONFIG["archdir"] + "/rbconfig.rb")
47      abi_provide = true
48      ruby_versioned = true
49   elsif path.match(specpatt)
50     ruby_versioned = true
51     gems.push(path.chomp)
52   # this is quite ugly and lame, but the assumption made is that if any files
53   # found in any of these directories specific to this ruby version, the
54   # package is dependent on this specific version.
55   # FIXME: only supports current ruby version
56   elsif not ruby_versioned
57     if path.match(RbConfig::CONFIG["rubylibdir"])
58       ruby_versioned = true
59     elsif path.match(RbConfig::CONFIG["archdir"])
60       ruby_versioned = true
61     elsif path.match(RbConfig::CONFIG["sitelibdir"])
62       ruby_versioned = !RbConfig::CONFIG["ruby_version"].empty?
63     elsif path.match(RbConfig::CONFIG["sitearchdir"])
64       ruby_versioned = true
65     elsif path.match(RbConfig::CONFIG["vendorlibdir"])
66       ruby_versioned = !RbConfig::CONFIG["ruby_version"].empty?
67     elsif path.match(RbConfig::CONFIG["vendorarchdir"])
68       ruby_versioned = true
69     end
70   end
71 end
72
73 if requires or abi_provide
74   abidep = "ruby(abi)"
75   if ruby_versioned
76     abidep += " = %s" % ruby_version
77   end
78   print abidep + "\n"
79 end
80
81 if gems.length > 0
82   require 'rubygems'
83
84   if requires
85
86     module Gem
87       class Requirement
88         def rpm_dependency_transform(name, version)
89           pessimistic = ""
90           if version == "> 0.0.0" or version == ">= 0"
91             version = ""
92           else
93             if version[0..1] == "~>"
94               pessimistic = "rubygem(%s) < %s\n" % [name, Gem::Version.create(version[3..-1]).bump]
95               version = version.gsub(/\~>/, '=>')
96             end
97             version = version.gsub(/^/, ' ')
98           end
99           version = "rubygem(%s)%s\n%s" % [name, version, pessimistic]
100         end
101
102         def to_rpm(name)
103           result = as_list
104           return result.map { |version| rpm_dependency_transform(name, version) }
105         end
106
107       end
108     end
109   end
110
111   for gem in gems
112     data = File.read(gem)
113     spec = eval(data)
114     if provides
115       print "rubygem(%s) = %s\n" % [spec.name, spec.version]
116     end
117     if requires
118       for d in spec.dependencies
119         print d.requirement.to_rpm(d.name)[0] unless d.type != :runtime
120       end
121       for d in spec.required_rubygems_version.to_rpm("rubygems")
122         print d.gsub(/(rubygem\()|(\))/, "")
123       end
124     end
125   end
126 end
This page took 0.040066 seconds and 3 git commands to generate.