Module: Gem

Defined in:
lib/minigems.rb,
lib/minigems.rb,
lib/minigems/core.rb,
lib/minigems/core.rb,
lib/minigems/script_helper.rb

Defined Under Namespace

Modules: MiniGems Classes: Command, CommandManager, Exception, GemRunner, LoadError

Constant Summary collapse

CORE_GEM_METHODS =
Gem.methods(false)
ConfigMap =
{}
RbConfig =
Config
DIRECTORIES =
%w[cache doc gems specifications]
RubyGemsPackageVersion =
RubyGemsVersion
WIN_PATTERNS =
[/bccwin/i, /cygwin/i, /djgpp/i, /mingw/i, /mswin/i, /wince/i]
@@win_platform =
nil

Class Method Summary collapse

Class Method Details

.activate(gem, *version_requirements) ⇒ Object

Activates an installed gem matching gem. The gem must satisfy version_requirements.

Returns true if the gem is activated, false if it is already loaded, or an exception otherwise.

Gem#activate adds the library paths in gem to $LOAD_PATH. Before a Gem is activated its required Gems are activated. If the version information is omitted, the highest version Gem of the supplied name is loaded. If a Gem is not found that meets the version requirements or a required Gem is not found, a Gem::LoadError is raised.

More information on version requirements can be found in the Gem::Requirement and Gem::Version documentation.



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/minigems.rb', line 116

def self.activate(gem, *version_requirements)
  if match = find_name(gem, *version_requirements)
    activate_gem_from_path(match.first)
  elsif match = find_name(MiniGems.camel_case(gem), *version_requirements)
    activate_gem_from_path(match.first)
  else
    unless gem.respond_to?(:name) && gem.respond_to?(:version_requirements)
      gem = Gem::Dependency.new(gem, version_requirements)
    end
    report_activate_error(gem)
  end
end

.available?(name, *version_requirements) ⇒ Boolean

See if a given gem is available.

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/minigems.rb', line 96

def self.available?(name, *version_requirements)
  version_requirements = Gem::Requirement.default if version_requirements.empty?
  gem = Gem::Dependency.new(name, version_requirements)
  not find_name(gem).nil?
end

.bindir(install_dir = Gem.dir) ⇒ Object

The path where gem executables are to be installed.



92
93
94
95
96
# File 'lib/minigems/core.rb', line 92

def self.bindir(install_dir=Gem.dir)
  return File.join(install_dir, 'bin') unless
    install_dir.to_s == Gem.default_dir
  Gem.default_bindir
end

.clear_pathsObject

Reset the path value. The next time path is requested, the values will be calculated from scratch.



182
183
184
# File 'lib/minigems.rb', line 182

def self.clear_paths
  @path = nil
end

.const_missing(const) ⇒ Object

Catch references to full rubygems constants - once accessed the current Gem constants are merged.



198
199
200
201
202
203
204
205
# File 'lib/minigems.rb', line 198

def self.const_missing(const)
  load_full_rubygems!
  if Gem.const_defined?(const)
    Gem.const_get(const)
  else
    super
  end
end

.default_pathObject

Default gem load path.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/minigems.rb', line 164

def self.default_path
  @default_path ||= if defined? RUBY_FRAMEWORK_VERSION then
    File.join File.dirname(RbConfig::CONFIG["sitedir"]), 'Gems', 
      RbConfig::CONFIG["ruby_version"]
  elsif defined?(RUBY_ENGINE) && File.directory?(
    File.join(RbConfig::CONFIG["libdir"], RUBY_ENGINE, 'gems', 
      RbConfig::CONFIG["ruby_version"])
    )
      File.join RbConfig::CONFIG["libdir"], RUBY_ENGINE, 'gems', 
        RbConfig::CONFIG["ruby_version"]
  else
    File.join RbConfig::CONFIG["libdir"], 'ruby', 'gems', 
      RbConfig::CONFIG["ruby_version"]
  end
end

.dirObject

The path where gems are to be installed.



101
102
103
104
105
# File 'lib/minigems/core.rb', line 101

def self.dir
  @gem_home ||= nil
  set_home(ENV['GEM_HOME'] || default_dir) unless @gem_home
  @gem_home
end

.ensure_gem_subdirectories(gemdir) ⇒ Object

Quietly ensure the named Gem directory contains all the proper subdirectories. If we can’t create a directory due to a permission problem, then we will silently continue.



112
113
114
115
116
117
118
119
# File 'lib/minigems/core.rb', line 112

def self.ensure_gem_subdirectories(gemdir)
  require 'fileutils'

  Gem::DIRECTORIES.each do |filename|
    fn = File.join gemdir, filename
    FileUtils.mkdir_p fn rescue nil unless File.exist? fn
  end
end

.latest_gem_pathsObject

Helper method to find all current gem paths.

Find the most recent gem versions’ paths just by looking at the gem’s directory version number. This is faster than parsing gemspec files at the expense of being less complete when it comes to require paths. That’s why Gem.activate actually parses gemspecs instead of directories.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/minigems.rb', line 135

def self.latest_gem_paths
  lookup = {}
  gem_path_sets = self.path.map { |path| [path, Dir["#{path}/gems/*"]] }
  gem_path_sets.each do |root_path, gems|
    unless gems.empty?
      gems.each do |gem_path|
        if matches = File.basename(File.basename(gem_path)).match(/^(.*?)-([\d\.]+)$/)
          name, version_no = matches.captures[0,2]
          version = Gem::Version.new(version_no)
          if !lookup[name] || (lookup[name] && lookup[name][1] < version)
            lookup[name] = [gem_path, version]
          end
        end
      end
    end
  end
  lookup.collect { |name,(gem_path, version)| gem_path }.sort
end

.load_path_insert_indexObject

The index to insert activated gem paths into the $LOAD_PATH.



153
154
155
# File 'lib/minigems/core.rb', line 153

def self.load_path_insert_index
  $LOAD_PATH.index ConfigMap[:sitelibdir]
end

.loaded_gemsObject

Keep track of loaded gems, maps gem name to full_name.



85
86
87
# File 'lib/minigems.rb', line 85

def self.loaded_gems
  @loaded_gems ||= {}
end

.marshal_versionObject

The version of the Marshal format for your Ruby.



171
172
173
# File 'lib/minigems/core.rb', line 171

def self.marshal_version
  "#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}"
end

.method_missing(m, *args, &blk) ⇒ Object

Catch calls to full rubygems methods - once accessed the current Gem methods are overridden.



188
189
190
191
192
193
194
# File 'lib/minigems.rb', line 188

def self.method_missing(m, *args, &blk)
  if Gem::MiniGems::FULL_RUBYGEMS_METHODS.include?(m.to_s)
    load_full_rubygems!
    return send(m, *args, &blk)
  end
  super
end

.minigems?Boolean

Whether minigems is being used or full rubygems has taken over.

Returns:

  • (Boolean)


52
53
54
# File 'lib/minigems/core.rb', line 52

def self.minigems?
  not const_defined?(:SourceIndex)
end

.pathObject

Array of paths to search for Gems.



155
156
157
158
159
160
161
# File 'lib/minigems.rb', line 155

def self.path
  @path ||= begin
    paths = [ENV['GEM_PATH'] ? ENV['GEM_PATH'] : default_path]
    paths << APPLE_GEM_HOME if defined?(APPLE_GEM_HOME) && !ENV['GEM_PATH']
    paths
  end
end

.platformsObject

Array of platforms this RubyGems supports.



208
209
210
211
212
213
214
# File 'lib/minigems/core.rb', line 208

def self.platforms
  @platforms ||= []
  if @platforms.empty?
    @platforms = [Gem::Platform::RUBY, Gem::Platform.local]
  end
  @platforms
end

.platforms=(platforms) ⇒ Object

Set array of platforms this RubyGems supports (primarily for testing).



201
202
203
# File 'lib/minigems/core.rb', line 201

def self.platforms=(platforms)
  @platforms = platforms
end

.prefixObject

The directory prefix this RubyGems was installed at.



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/minigems/core.rb', line 219

def self.prefix
  prefix = File.dirname File.expand_path(File.join(__FILE__, '..'))

  if File.dirname(prefix) == File.expand_path(ConfigMap[:sitelibdir]) or
     File.dirname(prefix) == File.expand_path(ConfigMap[:libdir]) or
     'lib' != File.basename(prefix) then
    nil
  else
    File.dirname prefix
  end
end

.refreshObject

Refresh the current ‘cached’ gems - in this case just the list of loaded gems.



91
92
93
# File 'lib/minigems.rb', line 91

def self.refresh
  self.loaded_gems.clear
end

.rubyObject

The path to the running Ruby interpreter.



59
60
61
62
63
64
65
66
67
# File 'lib/minigems/core.rb', line 59

def self.ruby
  if @ruby.nil? then
    @ruby = File.join(ConfigMap[:bindir], ConfigMap[:ruby_install_name])
    @ruby << ConfigMap[:EXEEXT]
    # escape string in case path to ruby executable contain spaces.
    @ruby.sub!(/.*\s.*/m, '"\&"')
  end
  @ruby
end

.ruby_versionObject

A Gem::Version for the currently running ruby.



72
73
74
75
76
77
# File 'lib/minigems/core.rb', line 72

def self.ruby_version
  return @ruby_version if defined? @ruby_version
  version = RUBY_VERSION.dup
  version << ".#{RUBY_PATCHLEVEL}" if defined? RUBY_PATCHLEVEL
  @ruby_version = Gem::Version.new version
end

.suffix_patternObject

Glob pattern for require-able path suffixes.



270
271
272
# File 'lib/minigems/core.rb', line 270

def self.suffix_pattern
  @suffix_pattern ||= "{#{suffixes.join(',')}}"
end

.suffixesObject

Suffixes for require-able paths.



277
278
279
# File 'lib/minigems/core.rb', line 277

def self.suffixes
  ['', '.rb', '.rbw', '.so', '.bundle', '.dll', '.sl', '.jar']
end

.use_paths(home, paths = []) ⇒ Object

Use the home and paths values for Gem.dir and Gem.path. Used mainly by the unit tests to provide environment isolation.



285
286
287
288
289
# File 'lib/minigems/core.rb', line 285

def self.use_paths(home, paths=[])
  clear_paths
  set_home(home) if home
  set_paths(paths.join(File::PATH_SEPARATOR)) if paths
end

.user_homeObject

The home directory for the user.



294
295
296
# File 'lib/minigems/core.rb', line 294

def self.user_home
  @user_home ||= find_home
end

.win_platform?Boolean

Is this a windows platform?

Returns:

  • (Boolean)


82
83
84
85
86
87
# File 'lib/minigems/core.rb', line 82

def self.win_platform?
  if @@win_platform.nil? then
    @@win_platform = !!WIN_PATTERNS.find { |r| RUBY_PLATFORM =~ r }
  end
  @@win_platform
end