Class: Emissary::GemHelper

Inherits:
Object show all
Defined in:
lib/emissary/gem_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ GemHelper

Returns a new instance of GemHelper.



24
25
26
27
# File 'lib/emissary/gem_helper.rb', line 24

def initialize(name)
  @name    = name
  @version = current_version
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/emissary/gem_helper.rb', line 22

def name
  @name
end

#versionObject (readonly)

Returns the value of attribute version.



22
23
24
# File 'lib/emissary/gem_helper.rb', line 22

def version
  @version
end

Instance Method Details

#current_versionObject



81
82
83
84
85
# File 'lib/emissary/gem_helper.rb', line 81

def current_version
  return Gem::Version.new(0) unless installed?
  specs = Gem.source_index.search(Gem::Dependency.new(name, normalize_version(:newest)))
  specs.map { |spec| spec.version }.sort{ |a,b| a <=> b }.first
end

#dependents(version = :current) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/emissary/gem_helper.rb', line 99

def dependents version = :current
  specs = Gem.source_index.find_name(@name, normalize_version(version))
  specs.inject([]) do |list,spec|
    list |= spec.dependent_gems
    list
  end
end

#install(version = :latest, source = :default) ⇒ Object

Raises:

  • (ArgumentError)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/emissary/gem_helper.rb', line 107

def install version = :latest, source = :default
  return @version unless installable? version
  
  source = URI.parse(source).to_s rescue :default
  
  options = {}
  options[:version], source_uri = case version
    when :latest
      ver, uri = versions(:newer_than_me).first
      [ ver, source == :default ? uri : source ]
    else
      ver, uri = versions(:newer_than_me).select { |v| v[0] == normalize_version(version) }.flatten
      [ ver, source == :default ? uri : source ]
  end
      
  raise ArgumentError, "Bad version '#{version.inspect}' - can't install specified version." unless options[:version]

  with_gem_source(source_uri) do
    installer = Gem::DependencyInstaller.new options
    installer.install name, options[:version]
    @version = Gem::Version.new(normalize_version(options[:version]).to_s.delete('<>!=~ '))
  end
end

#installable?(version = :any) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/emissary/gem_helper.rb', line 73

def installable? version = :any
  !versions(:any).empty? && !installed?(version) && normalize_version(version) > @version 
end

#installed?(version = :any) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/emissary/gem_helper.rb', line 77

def installed? version = :any
  !Gem.source_index.search(Gem::Dependency.new(name, normalize_version(version))).empty?
end

#is_a_provider?(version = :current) ⇒ Boolean Also known as: have_dependents?, has_dependents?

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/emissary/gem_helper.rb', line 62

def is_a_provider? version = :current
  return false unless (spec = Gem.source_index.find_name(@name, normalize_version(version)).first)
  !Gem::DependencyList.from_source_index(Gem.source_index).ok_to_remove?(spec.full_name)
end

#normalize_version(value) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/emissary/gem_helper.rb', line 33

def normalize_version value
  case value
    when Gem::Requirement, Gem::Version
      value
    
    when /^(<|<=|=|!=|>|>=|~>)\s*[0-9.]+$/
      Gem::Requirement.new value
      
    when /^[0-9.]+$/, String, Fixnum
      Gem::Version.new value
    
    when :current
      Gem::Version.new(self.version)
      
    when :all, :any, :installed, :available
      Gem::Requirement.default
    
    when :newer_than_me, :latest, :newest
      Gem::Requirement.new "> #{self.version}"
    
    when :older_than_me
      Gem::Requirement.new "< #{self.version}"
  end
end

#removable?(version = :current, ignore_dependents = false) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/emissary/gem_helper.rb', line 69

def removable? version = :current, ignore_dependents = false
  installed?(version) && (!!ignore_dependents || !have_dependents?(version))
end

#uninstall(version = :current, ignore_deps = false, remove_execs = false) ⇒ Object Also known as: remove

Raises:

  • (ArgumentError)


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/emissary/gem_helper.rb', line 137

def uninstall version = :current, ignore_deps = false, remove_execs = false
  options = {}
  options[:ignore] = !!ignore_deps
  options[:executables] = !!remove_execs
  
  options[:version] = case version
    when :current
      @version
    when :all
      options[:all] = true
      Gem::Requirement.default
    else
      versions(:all).select { |v| v[0] == normalize_version(version) }.flatten[0]
  end
  
  return true if not installed? version 
  raise ArgumentError, "Cannot uninstall version #{version.inspect} - is it installed? [#{options.inspect}]" unless options[:version]
  
  unless removable?(version, !!ignore_deps)
    msg = ['Refusing to uninstall gem required by other gems:']
    dependents(options[:version]).each do |gem, dep, satlist|
      msg << "    Gem '#{gem.name}-#{gem.version}' depends on '#{dep.name}' (#{dep.requirement})";
    end
    raise Exception, msg.join("\n")
  end
  
  Gem::Uninstaller.new(name, options).uninstall
  Gem.refresh 
  @version = Gem::Version.new '0'
end

#update(version = :latest, source = :default, keep_old = true) ⇒ Object



131
132
133
134
135
# File 'lib/emissary/gem_helper.rb', line 131

def update version = :latest, source = :default, keep_old = true
  return false unless updateable?
  uninstall(@version, false) unless keep_old
  install version, source 
end

#updateable?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/emissary/gem_helper.rb', line 58

def updateable?
  installed? && !versions(:newer_than_me).empty?
end

#versions(which = :all) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/emissary/gem_helper.rb', line 87

def versions which = :all
  # don't include others for latest/newest - do include otherwise
  others = [:latest, :newest ].include?(which) ? false : true
  dependency = Gem::Dependency.new(name, normalize_version(which))
  list = Gem::SpecFetcher.fetcher.find_matching(dependency, others).map do |spec, source_uri|
    _, version = spec
    [version, source_uri]
  end.sort { |a,b| b[0] <=> a[0] }
  
  which != :installed ? list : list.select { |v| installed? v[0]  } 
end