Class: Gemini

Inherits:
Object
  • Object
show all
Defined in:
lib/webget_gemini.rb

Constant Summary collapse

TRUST_POLICY_NICKNAME =
{'no'=>'NoSecurity','low'=>'LowSecurity','medium'=>'MediumSecurity','high'=>'HighSecurity'}

Class Method Summary collapse

Class Method Details

.find(ops) ⇒ Object

Return an array of all gemspecs that match the name and version requirements

Options:

  • name e.g. “foo” [required]

  • version: version requirements as defined in Gem.source_index.find_name



49
50
51
52
# File 'lib/webget_gemini.rb', line 49

def self.find(ops)
  name,version = [:name,:version].map{|k| ops[k]||ops[k.to_s]}
  return Gem.source_index.find_name(name,version||[])
end

.install(x = nil) ⇒ Object

Install a gem, if needed, by running a system command.

This dispatches on the first parameter:

  • nil: install_from_rails_root_cofig_gems_xml

  • File: install_from_xml_file

  • String: install_from_xml_text

  • REXML::Document: install_from_xml_doc

  • Array: install_from_array

  • Hash: install_from_options

Return status, stdout, stderr



67
68
69
70
71
72
73
74
# File 'lib/webget_gemini.rb', line 67

def self.install(x=nil)
  if x.class==NilClass         then return self.install_from_rails_root_config_gems_xml end
  if x.class==File             then return self.install_from_file(x) end
  if x.class==String           then return self.install_from_xml_text(x) end
  if x.class==REXML::Document  then return self.install_from_xml_doc(x) end
  if x.class==Array            then return self.install_from_array(x) end
  return self.install_from_options(x)
end

.install_command(ops) ⇒ Object

Create the system command to do a gem install

Options

  • name e.g. “foo” [required]

  • version e.g. “>=1.2.3”

  • trust: no|low|medium|high (aka NoSecurity|LowSecurity|MediumSecurity|HighSecurity)

  • append (aka installing): anything to append to the gem install command e.g. “ –no-ri –no-rdoc –no-user-install”

Return the command as a string



125
126
127
128
129
130
131
132
133
# File 'lib/webget_gemini.rb', line 125

def self.install_command(ops)
  name,version,trust,source,append,installing = [:name,:version,:trust,:source,:append,:installing].map{|k| ops[k]||ops[k.to_s]}
  append||=installing
  return "sudo gem install #{name}" +
    (version ? " --version \"#{version}\"" : '' ) +
    (trust ? " --trust-policy #{TRUST_POLICY_NICKNAME[trust]||trust}" : '') +
    (source ? [*source].map{|x| " --source \"#{x}\""}.join : '') +
    (append ? (' '+append) : '')
end

.install_from_array(array) ⇒ Object



92
93
94
# File 'lib/webget_gemini.rb', line 92

def self.install_from_array(array)
  return x.map{|options| self.install(options)}
end

.install_from_options(options) ⇒ Object

Install a gem, if needed, by running a system command.

Options

  • name e.g. “foo” [required]

  • version e.g. “>=1.2.3”

  • trust: no|low|medium|high (same as NoSecurity|LowSecurity|MediumSecurity|HighSecurity)

  • append: anything to append to the gem command e.g. “ –no-ri –no-rdoc –no-user-install”



108
109
110
111
112
# File 'lib/webget_gemini.rb', line 108

def self.install_from_options(options)
  return installed?(options) \
  ? [nil,nil,nil] \
  : commander(self.install_command(options))
end

.install_from_rails_root_config_gems_xmlObject



76
77
78
# File 'lib/webget_gemini.rb', line 76

def self.install_from_rails_root_config_gems_xml
  return self.install_from_xml_file(File.new(RAILS_ROOT+'/config/gems.xml'))
end

.install_from_xml_docObject



88
89
90
# File 'lib/webget_gemini.rb', line 88

def self.install_from_xml_doc
  return self.install_from_array(x.elements.each('gems/gem'){}.map{|e| e.attributes})
end

.install_from_xml_file(file) ⇒ Object



80
81
82
# File 'lib/webget_gemini.rb', line 80

def self.install_from_xml_file(file)
  return self.install_from_xml_text(File.read(x))
end

.install_from_xml_textObject



84
85
86
# File 'lib/webget_gemini.rb', line 84

def self.install_from_xml_text
  return self.install_from_xml_doc(REXML::Document.new(x))
end

.installed?(ops) ⇒ Boolean

Return true iff a given name is installed

Options

  • name e.g. “foo” [required]

  • version string e.g. “>=1.2.3”

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/webget_gemini.rb', line 36

def self.installed?(ops)
  name,version = [:name,:version].map{|k| ops[k]||ops[k.to_s]}
  dep = Gem::Dependency.new name, (version||Gem::Requirement.default)
  !Gem.source_index.search(dep).empty?
end

.sudo_gem_listObject

Return a lookup hash of all the gems installed on the system, by running the system command ‘sudo gem list’.

  • hash key is a gem name.

  • hash value is an array of the gem’s installed version numbers.



143
144
145
# File 'lib/webget_gemini.rb', line 143

def self.sudo_gem_list
  @sudo_gem_list=`sudo gem list`.split(/\n/).map{|line| line.split(/[ \,\(\)]+/)}.inject({}){|h,v| h[v[0]]=v[1,v.length]; h}
end

.sudo_gem_list_installed?(ops) ⇒ Boolean

Return true iff the sudo_gem_list hash shows a given gem is installed.

Returns:

  • (Boolean)


150
151
152
153
# File 'lib/webget_gemini.rb', line 150

def self.sudo_gem_list_installed?(ops)
  name,version = [:name,:version].map{|k| ops[k]||ops[k.to_s]}
  self.sudo_gem_list[name] and (!version or self.sudo_gem_list[name].include?(version))
end