# frozen_string_literal: true#--# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.# All rights reserved.# See LICENSE.txt for permissions.#++require'uri'require_relative'../rubygems'### Mixin methods for local and remote Gem::Command options.moduleGem::LocalRemoteOptions### Allows Gem::OptionParser to handle HTTP URIs.defaccept_uri_httpGem::OptionParser.acceptURI::HTTPdo|value|beginuri=URI.parsevaluerescueURI::InvalidURIErrorraiseGem::OptionParser::InvalidArgument,valueendvalid_uri_schemes=["http","https","file","s3"]unlessvalid_uri_schemes.include?(uri.scheme)msg="Invalid uri scheme for #{value}\nPreface URLs with one of #{valid_uri_schemes.map{|s|"#{s}://"}}"raiseArgumentError,msgendvalueendend### Add local/remote options to the command line parser.defadd_local_remote_optionsadd_option(:"Local/Remote",'-l','--local','Restrict operations to the LOCAL domain')do|value,options|options[:domain]=:localendadd_option(:"Local/Remote",'-r','--remote','Restrict operations to the REMOTE domain')do|value,options|options[:domain]=:remoteendadd_option(:"Local/Remote",'-b','--both','Allow LOCAL and REMOTE operations')do|value,options|options[:domain]=:bothendadd_bulk_threshold_optionadd_clear_sources_optionadd_source_optionadd_proxy_optionadd_update_sources_optionend### Add the --bulk-threshold optiondefadd_bulk_threshold_optionadd_option(:"Local/Remote",'-B','--bulk-threshold COUNT',"Threshold for switching to bulk","synchronization (default #{Gem.configuration.bulk_threshold})")do|value,options|Gem.configuration.bulk_threshold=value.to_iendend### Add the --clear-sources optiondefadd_clear_sources_optionadd_option(:"Local/Remote",'--clear-sources','Clear the gem sources')do|value,options|Gem.sources=niloptions[:sources_cleared]=trueendend### Add the --http-proxy optiondefadd_proxy_optionaccept_uri_httpadd_option(:"Local/Remote",'-p','--[no-]http-proxy [URL]',URI::HTTP,'Use HTTP proxy for remote operations')do|value,options|options[:http_proxy]=(value==false)?:no_proxy:valueGem.configuration[:http_proxy]=options[:http_proxy]endend### Add the --source optiondefadd_source_optionaccept_uri_httpadd_option(:"Local/Remote",'-s','--source URL',URI::HTTP,'Append URL to list of remote gem sources')do|source,options|source<<'/'ifsource!~/\/\z/ifoptions.delete:sources_clearedGem.sources=[source]elseGem.sources<<sourceunlessGem.sources.include?(source)endendend### Add the --update-sources optiondefadd_update_sources_optionadd_option(:Deprecated,'-u','--[no-]update-sources','Update local source cache')do|value,options|Gem.configuration.update_sources=valueendend### Is fetching of local and remote information enabled?defboth?options[:domain]==:bothend### Is local fetching enabled?deflocal?options[:domain]==:local||options[:domain]==:bothend### Is remote fetching enabled?defremote?options[:domain]==:remote||options[:domain]==:bothendend