diff options
author | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-03-31 22:40:06 +0000 |
---|---|---|
committer | drbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-03-31 22:40:06 +0000 |
commit | 8cc45aae947d453acca029e13eb64f3f5f0bf942 (patch) | |
tree | f9485a20c99defe1aae3f32555a41d23c2298ad8 /lib/rubygems/commands | |
parent | dc8359969ec71ece10357ba9396430db7f029e45 (diff) | |
download | ruby-8cc45aae947d453acca029e13eb64f3f5f0bf942.tar.gz |
Import RubyGems 1.1.0
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15873 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rubygems/commands')
-rw-r--r-- | lib/rubygems/commands/cleanup_command.rb | 136 | ||||
-rw-r--r-- | lib/rubygems/commands/environment_command.rb | 20 | ||||
-rw-r--r-- | lib/rubygems/commands/fetch_command.rb | 16 | ||||
-rw-r--r-- | lib/rubygems/commands/install_command.rb | 12 | ||||
-rw-r--r-- | lib/rubygems/commands/list_command.rb | 6 | ||||
-rw-r--r-- | lib/rubygems/commands/mirror_command.rb | 2 | ||||
-rw-r--r-- | lib/rubygems/commands/query_command.rb | 57 | ||||
-rw-r--r-- | lib/rubygems/commands/sources_command.rb | 25 | ||||
-rw-r--r-- | lib/rubygems/commands/specification_command.rb | 12 | ||||
-rw-r--r-- | lib/rubygems/commands/uninstall_command.rb | 13 | ||||
-rw-r--r-- | lib/rubygems/commands/unpack_command.rb | 18 | ||||
-rw-r--r-- | lib/rubygems/commands/update_command.rb | 43 |
12 files changed, 224 insertions, 136 deletions
diff --git a/lib/rubygems/commands/cleanup_command.rb b/lib/rubygems/commands/cleanup_command.rb index f6deac9829..40dcb9db34 100644 --- a/lib/rubygems/commands/cleanup_command.rb +++ b/lib/rubygems/commands/cleanup_command.rb @@ -2,92 +2,90 @@ require 'rubygems/command' require 'rubygems/source_index' require 'rubygems/dependency_list' -module Gem - module Commands - class CleanupCommand < Command - def initialize - super( - 'cleanup', +class Gem::Commands::CleanupCommand < Gem::Command + + def initialize + super 'cleanup', 'Clean up old versions of installed gems in the local repository', - { - :force => false, - :test => false, - :install_dir => Gem.dir - }) - add_option('-d', '--dryrun', "") do |value, options| - options[:dryrun] = true - end - end + :force => false, :test => false, :install_dir => Gem.dir - def arguments # :nodoc: - "GEMNAME name of gem to cleanup" - end + add_option('-d', '--dryrun', "") do |value, options| + options[:dryrun] = true + end + end - def defaults_str # :nodoc: - "--no-dryrun" - end + def arguments # :nodoc: + "GEMNAME name of gem to cleanup" + end + + def defaults_str # :nodoc: + "--no-dryrun" + end + + def usage # :nodoc: + "#{program_name} [GEMNAME ...]" + end + + def execute + say "Cleaning up installed gems..." + primary_gems = {} - def usage # :nodoc: - "#{program_name} [GEMNAME ...]" + Gem.source_index.each do |name, spec| + if primary_gems[spec.name].nil? or + primary_gems[spec.name].version < spec.version then + primary_gems[spec.name] = spec end + end - def execute - say "Cleaning up installed gems..." - srcindex = Gem::SourceIndex.from_installed_gems - primary_gems = {} + gems_to_cleanup = [] - srcindex.each do |name, spec| - if primary_gems[spec.name].nil? or primary_gems[spec.name].version < spec.version - primary_gems[spec.name] = spec - end + unless options[:args].empty? then + options[:args].each do |gem_name| + specs = Gem.cache.search(/^#{gem_name}$/i) + specs.each do |spec| + gems_to_cleanup << spec end + end + else + Gem.source_index.each do |name, spec| + gems_to_cleanup << spec + end + end - gems_to_cleanup = [] - - unless options[:args].empty? then - options[:args].each do |gem_name| - specs = Gem.cache.search(/^#{gem_name}$/i) - specs.each do |spec| - gems_to_cleanup << spec - end - end - else - srcindex.each do |name, spec| - gems_to_cleanup << spec - end - end + gems_to_cleanup = gems_to_cleanup.select { |spec| + primary_gems[spec.name].version != spec.version + } - gems_to_cleanup = gems_to_cleanup.select { |spec| - primary_gems[spec.name].version != spec.version - } + uninstall_command = Gem::CommandManager.instance['uninstall'] + deplist = Gem::DependencyList.new + gems_to_cleanup.uniq.each do |spec| deplist.add spec end - uninstall_command = Gem::CommandManager.instance['uninstall'] - deplist = DependencyList.new - gems_to_cleanup.uniq.each do |spec| deplist.add(spec) end + deps = deplist.strongly_connected_components.flatten.reverse - deplist.dependency_order.each do |spec| - if options[:dryrun] then - say "Dry Run Mode: Would uninstall #{spec.full_name}" - else - say "Attempting uninstall on #{spec.full_name}" + deps.each do |spec| + if options[:dryrun] then + say "Dry Run Mode: Would uninstall #{spec.full_name}" + else + say "Attempting to uninstall #{spec.full_name}" - options[:args] = [spec.name] - options[:version] = "= #{spec.version}" - options[:executables] = true + options[:args] = [spec.name] + options[:version] = "= #{spec.version}" + options[:executables] = false - uninstall_command.merge_options(options) + uninstaller = Gem::Uninstaller.new spec.name, options - begin - uninstall_command.execute - rescue Gem::DependencyRemovalException => ex - say "Unable to uninstall #{spec.full_name} ... continuing with remaining gems" - end - end + begin + uninstaller.uninstall + rescue Gem::DependencyRemovalException, + Gem::GemNotInHomeException => e + say "Unable to uninstall #{spec.full_name}:" + say "\t#{e.class}: #{e.message}" end - - say "Clean Up Complete" end end - + + say "Clean Up Complete" end + end + diff --git a/lib/rubygems/commands/environment_command.rb b/lib/rubygems/commands/environment_command.rb index ab85361753..56b373cfbe 100644 --- a/lib/rubygems/commands/environment_command.rb +++ b/lib/rubygems/commands/environment_command.rb @@ -25,19 +25,18 @@ class Gem::Commands::EnvironmentCommand < Gem::Command def execute out = '' arg = options[:args][0] - if begins?("packageversion", arg) then + case arg + when /^packageversion/ then out << Gem::RubyGemsPackageVersion - elsif begins?("version", arg) then + when /^version/ then out << Gem::RubyGemsVersion - elsif begins?("gemdir", arg) then + when /^gemdir/, /^gemhome/, /^home/, /^GEM_HOME/ then out << Gem.dir - elsif begins?("gempath", arg) then - out << Gem.path.join("\n") - elsif begins?("remotesources", arg) then + when /^gempath/, /^path/, /^GEM_PATH/ then + out << Gem.path.join(File::PATH_SEPARATOR) + when /^remotesources/ then out << Gem.sources.join("\n") - elsif arg then - fail Gem::CommandLineError, "Unknown enviroment option [#{arg}]" - else + when nil then out = "RubyGems Environment:\n" out << " - RUBYGEMS VERSION: #{Gem::RubyGemsVersion} (#{Gem::RubyGemsPackageVersion})\n" @@ -75,6 +74,9 @@ class Gem::Commands::EnvironmentCommand < Gem::Command Gem.sources.each do |s| out << " - #{s}\n" end + + else + fail Gem::CommandLineError, "Unknown enviroment option [#{arg}]" end say out true diff --git a/lib/rubygems/commands/fetch_command.rb b/lib/rubygems/commands/fetch_command.rb index 7db365eba0..ccedc45401 100644 --- a/lib/rubygems/commands/fetch_command.rb +++ b/lib/rubygems/commands/fetch_command.rb @@ -44,17 +44,15 @@ class Gem::Commands::FetchCommand < Gem::Command spec, source_uri = specs_and_sources.last - gem_file = "#{spec.full_name}.gem" - - gem_path = File.join source_uri, 'gems', gem_file - - gem = Gem::RemoteFetcher.fetcher.fetch_path gem_path - - File.open gem_file, 'wb' do |fp| - fp.write gem + if spec.nil? then + alert_error "Could not find #{gem_name} in any repository" + next end - say "Downloaded #{gem_file}" + path = Gem::RemoteFetcher.fetcher.download spec, source_uri + FileUtils.mv path, "#{spec.full_name}.gem" + + say "Downloaded #{spec.full_name}" end end diff --git a/lib/rubygems/commands/install_command.rb b/lib/rubygems/commands/install_command.rb index aa9f480c2a..ce0bc6ba04 100644 --- a/lib/rubygems/commands/install_command.rb +++ b/lib/rubygems/commands/install_command.rb @@ -62,13 +62,15 @@ class Gem::Commands::InstallCommand < Gem::Command :install_dir => options[:install_dir], :security_policy => options[:security_policy], :wrappers => options[:wrappers], + :bin_dir => options[:bin_dir] } + exit_code = 0 + get_all_gem_names.each do |gem_name| begin - inst = Gem::DependencyInstaller.new gem_name, options[:version], - install_options - inst.install + inst = Gem::DependencyInstaller.new install_options + inst.install gem_name, options[:version] inst.installed_gems.each do |spec| say "Successfully installed #{spec.full_name}" @@ -77,8 +79,10 @@ class Gem::Commands::InstallCommand < Gem::Command installed_gems.push(*inst.installed_gems) rescue Gem::InstallError => e alert_error "Error installing #{gem_name}:\n\t#{e.message}" + exit_code |= 1 rescue Gem::GemNotFoundException => e alert_error e.message + exit_code |= 2 # rescue => e # # TODO: Fix this handle to allow the error to propagate to # # the top level handler. Examine the other errors as @@ -121,6 +125,8 @@ class Gem::Commands::InstallCommand < Gem::Command end end end + + raise Gem::SystemExitException, exit_code end end diff --git a/lib/rubygems/commands/list_command.rb b/lib/rubygems/commands/list_command.rb index e179ff57ee..f8b377fcde 100644 --- a/lib/rubygems/commands/list_command.rb +++ b/lib/rubygems/commands/list_command.rb @@ -6,10 +6,8 @@ module Gem class ListCommand < QueryCommand def initialize - super( - 'list', - 'Display all gems whose name starts with STRING' - ) + super 'list', 'Display gems whose name starts with STRING' + remove_option('--name-matches') end diff --git a/lib/rubygems/commands/mirror_command.rb b/lib/rubygems/commands/mirror_command.rb index fc4f086ad3..959b8eaec3 100644 --- a/lib/rubygems/commands/mirror_command.rb +++ b/lib/rubygems/commands/mirror_command.rb @@ -2,7 +2,7 @@ require 'yaml' require 'zlib' require 'rubygems/command' -require 'rubygems/gem_open_uri' +require 'open-uri' class Gem::Commands::MirrorCommand < Gem::Command diff --git a/lib/rubygems/commands/query_command.rb b/lib/rubygems/commands/query_command.rb index 4f957625ee..fdc5a6a4ea 100644 --- a/lib/rubygems/commands/query_command.rb +++ b/lib/rubygems/commands/query_command.rb @@ -1,15 +1,25 @@ require 'rubygems/command' require 'rubygems/local_remote_options' require 'rubygems/source_info_cache' +require 'rubygems/version_option' class Gem::Commands::QueryCommand < Gem::Command include Gem::LocalRemoteOptions + include Gem::VersionOption def initialize(name = 'query', summary = 'Query gem information in local or remote repositories') super name, summary, - :name => /.*/, :domain => :local, :details => false, :versions => true + :name => //, :domain => :local, :details => false, :versions => true, + :installed => false, :version => Gem::Requirement.default + + add_option('-i', '--[no-]installed', + 'Check for installed gem') do |value, options| + options[:installed] = value + end + + add_version_option add_option('-n', '--name-matches REGEXP', 'Name of gem(s) to query on matches the', @@ -28,33 +38,70 @@ class Gem::Commands::QueryCommand < Gem::Command options[:details] = false unless value end + add_option('-a', '--all', + 'Display all gem versions') do |value, options| + options[:all] = value + end + add_local_remote_options end def defaults_str # :nodoc: - "--local --name-matches '.*' --no-details --versions" + "--local --name-matches // --no-details --versions --no-installed" end def execute + exit_code = 0 + name = options[:name] + if options[:installed] then + if name.source.empty? then + alert_error "You must specify a gem name" + exit_code |= 4 + elsif installed? name.source, options[:version] then + say "true" + else + say "false" + exit_code |= 1 + end + + raise Gem::SystemExitException, exit_code + end + if local? then say say "*** LOCAL GEMS ***" say - output_query_results Gem.cache.search(name) + + output_query_results Gem.source_index.search(name) end if remote? then say say "*** REMOTE GEMS ***" say - output_query_results Gem::SourceInfoCache.search(name) + + begin + Gem::SourceInfoCache.cache.refresh options[:all] + rescue Gem::RemoteFetcher::FetchError + # no network + end + + output_query_results Gem::SourceInfoCache.search(name, false, true) end end private + ## + # Check if gem +name+ version +version+ is installed. + + def installed?(name, version = Gem::Requirement.default) + dep = Gem::Dependency.new name, version + !Gem.source_index.search(dep).empty? + end + def output_query_results(gemspecs) output = [] gem_list_with_version = {} @@ -98,7 +145,7 @@ class Gem::Commands::QueryCommand < Gem::Command ## # Used for wrapping and indenting text - # + def format_text(text, wrap, indent=0) result = [] work = text.dup diff --git a/lib/rubygems/commands/sources_command.rb b/lib/rubygems/commands/sources_command.rb index a0977f90dc..6d9d5b5b90 100644 --- a/lib/rubygems/commands/sources_command.rb +++ b/lib/rubygems/commands/sources_command.rb @@ -39,8 +39,11 @@ class Gem::Commands::SourcesCommand < Gem::Command options[:list] = !(options[:add] || options[:remove] || options[:clear_all] || options[:update]) if options[:clear_all] then - remove_cache_file("user", Gem::SourceInfoCache.user_cache_file) - remove_cache_file("system", Gem::SourceInfoCache.system_cache_file) + sic = Gem::SourceInfoCache + remove_cache_file 'user', sic.user_cache_file + remove_cache_file 'latest user', sic.latest_user_cache_file + remove_cache_file 'system', sic.system_cache_file + remove_cache_file 'latest system', sic.latest_system_cache_file end if options[:add] then @@ -48,7 +51,7 @@ class Gem::Commands::SourcesCommand < Gem::Command sice = Gem::SourceInfoCacheEntry.new nil, nil begin - sice.refresh source_uri + sice.refresh source_uri, true Gem::SourceInfoCache.cache_data[source_uri] = sice Gem::SourceInfoCache.cache.update @@ -66,7 +69,7 @@ class Gem::Commands::SourcesCommand < Gem::Command end if options[:update] then - Gem::SourceInfoCache.cache.refresh + Gem::SourceInfoCache.cache.refresh true Gem::SourceInfoCache.cache.flush say "source cache successfully updated" @@ -78,6 +81,11 @@ class Gem::Commands::SourcesCommand < Gem::Command unless Gem.sources.include? source_uri then say "source #{source_uri} not present in cache" else + begin # HACK figure out how to get the cache w/o update + Gem::SourceInfoCache.cache + rescue Gem::RemoteFetcher::FetchError + end + Gem::SourceInfoCache.cache_data.delete source_uri Gem::SourceInfoCache.cache.update Gem::SourceInfoCache.cache.flush @@ -100,11 +108,12 @@ class Gem::Commands::SourcesCommand < Gem::Command private - def remove_cache_file(desc, fn) - FileUtils.rm_rf fn rescue nil - if ! File.exist?(fn) + def remove_cache_file(desc, path) + FileUtils.rm_rf path + + if not File.exist?(path) then say "*** Removed #{desc} source cache ***" - elsif ! File.writable?(fn) + elsif not File.writable?(path) then say "*** Unable to remove #{desc} source cache (write protected) ***" else say "*** Unable to remove #{desc} source cache ***" diff --git a/lib/rubygems/commands/specification_command.rb b/lib/rubygems/commands/specification_command.rb index 1ab2ad9260..7c8598e53b 100644 --- a/lib/rubygems/commands/specification_command.rb +++ b/lib/rubygems/commands/specification_command.rb @@ -3,6 +3,7 @@ require 'rubygems/command' require 'rubygems/local_remote_options' require 'rubygems/version_option' require 'rubygems/source_info_cache' +require 'rubygems/format' class Gem::Commands::SpecificationCommand < Gem::Command @@ -41,13 +42,16 @@ class Gem::Commands::SpecificationCommand < Gem::Command gem = get_one_gem_name if local? then - source_index = Gem::SourceIndex.from_installed_gems - specs.push(*source_index.search(/\A#{gem}\z/, options[:version])) + if File.exist? gem then + specs << Gem::Format.from_file_by_path(gem).spec rescue nil + end + + if specs.empty? then + specs.push(*Gem.source_index.search(/\A#{gem}\z/, options[:version])) + end end if remote? then - alert_warning "Remote information is not complete\n\n" - Gem::SourceInfoCache.cache_data.each do |_,sice| specs.push(*sice.source_index.search(gem, options[:version])) end diff --git a/lib/rubygems/commands/uninstall_command.rb b/lib/rubygems/commands/uninstall_command.rb index 2d9c46ee52..3d6e2383bc 100644 --- a/lib/rubygems/commands/uninstall_command.rb +++ b/lib/rubygems/commands/uninstall_command.rb @@ -35,6 +35,11 @@ module Gem options[:install_dir] = File.expand_path(value) end + add_option('-n', '--bindir DIR', + 'Directory to remove binaries from') do |value, options| + options[:bin_dir] = File.expand_path(value) + end + add_version_option add_platform_option end @@ -54,7 +59,13 @@ module Gem def execute get_all_gem_names.each do |gem_name| - Gem::Uninstaller.new(gem_name, options).uninstall + begin + Gem::Uninstaller.new(gem_name, options).uninstall + rescue Gem::GemNotInHomeException => e + spec = e.spec + alert("In order to remove #{spec.name}, please execute:\n" \ + "\tgem uninstall #{spec.name} --install-dir=#{spec.installation_path}") + end end end end diff --git a/lib/rubygems/commands/unpack_command.rb b/lib/rubygems/commands/unpack_command.rb index 23ebabc21a..d187f8a9ea 100644 --- a/lib/rubygems/commands/unpack_command.rb +++ b/lib/rubygems/commands/unpack_command.rb @@ -38,6 +38,7 @@ class Gem::Commands::UnpackCommand < Gem::Command def execute gemname = get_one_gem_name path = get_path(gemname, options[:version]) + if path then basename = File.basename(path).sub(/\.gem$/, '') target_dir = File.expand_path File.join(options[:target], basename) @@ -66,16 +67,27 @@ class Gem::Commands::UnpackCommand < Gem::Command # source directories? def get_path(gemname, version_req) return gemname if gemname =~ /\.gem$/i - specs = Gem::SourceIndex.from_installed_gems.search(/\A#{gemname}\z/, version_req) + + specs = Gem::source_index.search(/\A#{gemname}\z/, version_req) + selected = specs.sort_by { |s| s.version }.last + return nil if selected.nil? + # We expect to find (basename).gem in the 'cache' directory. # Furthermore, the name match must be exact (ignoring case). if gemname =~ /^#{selected.name}$/i filename = selected.full_name + '.gem' - return File.join(Gem.dir, 'cache', filename) + path = nil + + Gem.path.find do |gem_dir| + path = File.join gem_dir, 'cache', filename + File.exist? path + end + + path else - return nil + nil end end diff --git a/lib/rubygems/commands/update_command.rb b/lib/rubygems/commands/update_command.rb index 88d48d705e..b8de911e20 100644 --- a/lib/rubygems/commands/update_command.rb +++ b/lib/rubygems/commands/update_command.rb @@ -1,8 +1,10 @@ require 'rubygems/command' +require 'rubygems/command_manager' require 'rubygems/install_update_options' require 'rubygems/local_remote_options' require 'rubygems/source_info_cache' require 'rubygems/version_option' +require 'rubygems/commands/install_command' class Gem::Commands::UpdateCommand < Gem::Command @@ -45,7 +47,7 @@ class Gem::Commands::UpdateCommand < Gem::Command def execute if options[:system] then - say "Updating RubyGems..." + say "Updating RubyGems" unless options[:args].empty? then fail "No gem names are allowed with the --system option" @@ -53,10 +55,10 @@ class Gem::Commands::UpdateCommand < Gem::Command options[:args] = ["rubygems-update"] else - say "Updating installed gems..." + say "Updating installed gems" end - hig = highest_installed_gems = {} + hig = {} Gem::SourceIndex.from_installed_gems.each do |name, spec| if hig[spec.name].nil? or hig[spec.name].version < spec.version then @@ -64,25 +66,28 @@ class Gem::Commands::UpdateCommand < Gem::Command end end - remote_gemspecs = Gem::SourceInfoCache.search(//) + pattern = if options[:args].empty? then + // + else + Regexp.union(*options[:args]) + end - gems_to_update = if options[:args].empty? then - which_to_update(highest_installed_gems, remote_gemspecs) - else - options[:args] - end + remote_gemspecs = Gem::SourceInfoCache.search pattern - options[:domain] = :remote # install from remote source + gems_to_update = which_to_update hig, remote_gemspecs - # HACK use the real API - install_command = Gem::CommandManager.instance['install'] + updated = [] + # HACK use the real API gems_to_update.uniq.sort.each do |name| - say "Attempting remote update of #{name}" - options[:args] = [name] - options[:ignore_dependencies] = true # HACK skip seen gems instead - install_command.merge_options(options) - install_command.execute + next if updated.any? { |spec| spec.name == name } + say "Updating #{name}" + installer = Gem::DependencyInstaller.new options + installer.install name + installer.installed_gems.each do |spec| + updated << spec + say "Successfully installed #{spec.full_name}" + end end if gems_to_update.include? "rubygems-update" then @@ -97,12 +102,10 @@ class Gem::Commands::UpdateCommand < Gem::Command say "RubyGems system software updated" if installed else - updated = gems_to_update.uniq.sort.collect { |g| g.to_s } - if updated.empty? then say "Nothing to update" else - say "Gems updated: #{updated.join ', '}" + say "Gems updated: #{updated.map { |spec| spec.name }.join ', '}" end end end |