Using GGem#ebuild_name to standardize gem names
[g-gem.git] / depatom.rb
blobc321800243ca2985fa58b7fda02b27c6f79794e0
1 require 'rubygems'
3 class DependencyAtom
5   PREFIX_EXT_RE = /[!~]/
6   PREFIX_RE = /(>=|<=|<|>|=)/
7   CATEGORY_RE = /\w+-\w+/
8   POSTFIX_RE = /\*/
9   VERSION_RE = /(\d+(\.\d+)*)+/
10   VERSION_EXT_RE = /[a-z]?(_(alpha|beta|pre|rc|p)\d+)?/
11   PACKAGE_NAME_RE = /[a-zA-Z0-9_-]+/
13   attr_reader :package_name, :version, :version_ext,
14               :prefix, :prefix_ext, :postfix, :origin,
15               :gem_name
17   # if convert is true, use GGem.ebuild_name() to convert the name
18   def initialize(str, convert = true)
19     @gem_name = str
20     str = GGem.ebuild_name(str) if convert == true
21     @origin = str
22     @prefix_ext, str = $&, $' if /^#{PREFIX_EXT_RE}/ === str
23     @prefix, str = $&, $' if /^#{PREFIX_RE}/ === str
24     @category, str = $1, $' if /^(#{CATEGORY_RE})\// === str
25     @postfix, str = $&, $` if /#{POSTFIX_RE}$/ === str
26     @version, @version_ext, str = $1, $3, $` if /-#{VERSION_RE}(#{VERSION_EXT_RE})/ === str
27     raise "Invalid atom: #{@origin}" unless /^#{PACKAGE_NAME_RE}$/ === str
28     @package_name = $&
29   end
30   private :initialize
32   alias_method :name, :package_name
33   attr_accessor :category
35   def category
36     @category or Portage.category
37   end
39   ##
40   # FIXME: support other gentoo atom prefixes
41   ##
42   def gem_requirement
43     return Gem::Version::Requirement.default unless version
44     full_prefix = "#{prefix_ext}#{prefix}"
45     gem_prefix = if /^(~>|>|>=|=|!=|<=|<)$/ === full_prefix
46                    full_prefix
47                  elsif not prefix
48                    "="
49                  end
50     Gem::Version::Requirement.create(["#{gem_prefix} #{version}"])
51   end
53   def gem_dependency
54     Gem::Dependency.new(package_name, [gem_requirement.to_s])
55   end
57   def to_s
58     "#{prefix_ext}#{(prefix or "=") if version}#{category+"/" if category}#{package_name}"+
59       "#{"-"+version+version_ext if version}#{postfix}"
60   end
62   class << self
63     def from_s(str)
64       if str =~ /(.*\/)?(\S+)\.gem$/ # From gem file
65         atom = from_s($2)
66         atom.instance_eval { @origin = str }
67         atom
68       else
69         new(str)
70       end
71     end
73     alias_method :parse, :from_s
75     def parse_list(list)
76       list.map { |str| parse(str) }
77     end
79     def from_gem_dependency(dep, category)
80       # fixme:
81       if dep.is_a? Array
82         dep = Gem::Dependency.new(dep[0], dep[1])
83       end
84       prefix, version = *dep.version_requirements.instance_eval { @requirements.first }
85       new("#{prefix}#{category}/#{dep.name}-#{version}")
86     end
88     def from_required_ruby_version(spec)
89       spec.required_ruby_version.instance_eval do
90         if @requirements != nil 
91           prefix, version = @requirements.first
92           "#{prefix}dev-lang/ruby-#{version}"
93         else
94           "dev-lang/ruby"
95         end
96       end
97     end
98   end
99 end  # class DependencyAtom