Class: Gem::PristineCommand

Inherits:
Command
  • Object
show all
Includes:
CommandAids, VersionOption
Defined in:
lib/rubygems/gem_commands.rb

Instance Attribute Summary

Attributes inherited from Command

#command, #defaults, #options, #program_name, #summary

Instance Method Summary collapse

Methods included from CommandAids

#begins?, #get_all_gem_names, #get_one_gem_name, #get_one_optional_argument

Methods included from VersionOption

#add_version_option

Methods inherited from Command

add_common_option, #add_option, add_specific_extra_args, common_options, extra_args, extra_args=, #handles?, #invoke, #merge_options, #remove_option, #show_help, specific_extra_args, specific_extra_args_hash, #when_invoked

Methods included from DefaultUserInteraction

#ui, ui, #ui=, ui=, #use_ui, use_ui

Constructor Details

#initializePristineCommand

Returns a new instance of PristineCommand.



1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
# File 'lib/rubygems/gem_commands.rb', line 1097

def initialize
  super('pristine',
    'Restores gem directories to pristine condition from files located in the gem cache',
    {
      :version => "> 0.0.0"
    })
  add_option('--all',
    'Restore all installed gems to pristine', 'condition'
    ) do |value, options|
    options[:all] = value
  end
  add_version_option('restore to', 'pristine condition')
end

Instance Method Details

#argumentsObject



1119
1120
1121
# File 'lib/rubygems/gem_commands.rb', line 1119

def arguments
  "GEMNAME          The gem to restore to pristine condition (unless --all)"
end

#defaults_strObject



1111
1112
1113
# File 'lib/rubygems/gem_commands.rb', line 1111

def defaults_str
  "--all"
end

#executeObject



1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
# File 'lib/rubygems/gem_commands.rb', line 1123

def execute
  say "Restoring gem(s) to pristine condition..."
  if options[:all]
    all_gems = true
    specs = Gem::SourceIndex.from_installed_gems.collect do |name, spec|
      spec
    end
  else
    all_gems = false
    gem_name = get_one_gem_name
    specs = Gem::SourceIndex.from_installed_gems.search(gem_name, options[:version])
  end

  if specs.empty?
    fail "Failed to find gem #{gem_name} #{options[:version]} to restore to pristine condition"
  end
  install_dir = Gem.dir # TODO use installer option
  raise Gem::FilePermissionError.new(install_dir) unless File.writable?(install_dir)

  gems_were_pristine = true

  specs.each do |spec|
    installer = Gem::Installer.new nil, :wrappers => true # HACK ugly TODO use installer option

    gem_file = File.join(install_dir, "cache", "#{spec.full_name}.gem")
    security_policy = nil # TODO use installer option
    format = Gem::Format.from_file_by_path(gem_file, security_policy)
    target_directory = File.join(install_dir, "gems", format.spec.full_name).untaint
    pristine_files = format.file_entries.collect {|data| data[0]["path"]}
    file_map = {}
    format.file_entries.each {|entry, file_data| file_map[entry["path"]] = file_data}
    require 'fileutils'

    Dir.chdir target_directory do
      deployed_files = Dir.glob(File.join("**", "*")) +
                       Dir.glob(File.join("**", ".*"))
      to_redeploy = (pristine_files - deployed_files).collect {|path| path.untaint}
      if to_redeploy.length > 0
        gems_were_pristine = false
        say "Restoring #{to_redeploy.length} file#{to_redeploy.length == 1 ? "" : "s"} to #{spec.full_name}..."
        to_redeploy.each do |path|
          say "  #{path}"
          FileUtils.mkdir_p File.dirname(path)
          File.open(path, "wb") do |out|
            out.write file_map[path]
          end
        end
      end
    end

    installer.generate_bin spec, install_dir
  end

  say "Rebuilt all bin stubs"

  if gems_were_pristine
    if all_gems
      say "All installed gem files are already in pristine condition"
    else
      say "#{specs[0].full_name} is already in pristine condition"
    end
  else
    if all_gems
      say "All installed gem files restored to pristine condition"
    else
      say "#{specs[0].full_name} restored to pristine condition"
    end
  end
end

#usageObject



1115
1116
1117
# File 'lib/rubygems/gem_commands.rb', line 1115

def usage
  "#{program_name} [args]"
end