Class | Gem::ConfigFile |
In: |
lib/rubygems/config_file.rb
|
Parent: | Object |
Gem::ConfigFile RubyGems options and gem command options from ~/.gemrc.
~/.gemrc is a YAML file that uses strings to match gem command arguments and symbols to match RubyGems options.
Gem command arguments use a String key that matches the command name and allow you to specify default arguments:
install: --no-rdoc --no-ri update: --no-rdoc --no-ri
You can use gem: to set default arguments for all commands.
RubyGems options use symbol keys. Valid options are:
+:backtrace+: | See backtrace |
+:benchmark+: | See benchmark |
+:sources+: | Sets Gem::sources |
+:verbose+: | See verbose |
DEFAULT_BACKTRACE | = | false | ||
DEFAULT_BENCHMARK | = | false | ||
DEFAULT_BULK_THRESHOLD | = | 1000 | ||
DEFAULT_VERBOSITY | = | true | ||
DEFAULT_UPDATE_SOURCES | = | true | ||
OPERATING_SYSTEM_DEFAULTS | = | {} | For Ruby packagers to set configuration defaults. Set in rubygems/defaults/operating_system.rb | |
PLATFORM_DEFAULTS | = | {} | For Ruby implementers to set configuration defaults. Set in rubygems/defaults/#{RUBY_ENGINE}.rb | |
CSIDL_COMMON_APPDATA | = | 0x0023 | ||
SHGetFolderPath | = | Win32API.new 'shell32', 'SHGetFolderPath', 'PLPLP', 'L', :stdcall | ||
SHGetFolderPath | = | Win32API.new 'shell32', 'SHGetFolderPath', 'LLLLP', 'L' | ||
SYSTEM_WIDE_CONFIG_FILE | = | File.join system_config_path, 'gemrc' |
api_keys | [R] | Hash of RubyGems.org and alternate API keys |
args | [R] | List of arguments supplied to the config file object. |
backtrace | [W] | True if we print backtraces on errors. |
benchmark | [RW] | True if we are benchmarking this run. |
bulk_threshold | [RW] | Bulk threshold value. If the number of missing gems are above this threshold value, then a bulk download technique is used. (deprecated) |
hash | [R] | |
home | [RW] | Where to install gems (deprecated) |
path | [RW] | Where to look for gems (deprecated) |
rubygems_api_key | [R] | API key for RubyGems.org |
ssl_ca_cert | [R] | Path name of directory or file of openssl CA certificate, used for remote https connection |
ssl_verify_mode | [R] | openssl verify mode value, used for remote https connection |
update_sources | [RW] | True if we want to update the SourceInfoCache every time, false otherwise |
verbose | [RW] |
Verbose level of output:
|
Create the config file object. args is the list of arguments from the command line.
The following command line options are handled early here rather than later at the time most command options are processed.
—config-file, —config-file==NAME: | Obviously these need to be handled by the ConfigFile object to ensure we get the right config file. |
—backtrace: | Backtrace needs to be turned on early so that errors before normal option parsing can be properly handled. |
—debug: | Enable Ruby level debug messages. Handled early for the same reason as —backtrace. |
# File lib/rubygems/config_file.rb, line 160 160: def initialize(arg_list) 161: @config_file_name = nil 162: need_config_file_name = false 163: 164: arg_list = arg_list.map do |arg| 165: if need_config_file_name then 166: @config_file_name = arg 167: need_config_file_name = false 168: nil 169: elsif arg =~ /^--config-file=(.*)/ then 170: @config_file_name = $1 171: nil 172: elsif arg =~ /^--config-file$/ then 173: need_config_file_name = true 174: nil 175: else 176: arg 177: end 178: end.compact 179: 180: @backtrace = DEFAULT_BACKTRACE 181: @benchmark = DEFAULT_BENCHMARK 182: @bulk_threshold = DEFAULT_BULK_THRESHOLD 183: @verbose = DEFAULT_VERBOSITY 184: @update_sources = DEFAULT_UPDATE_SOURCES 185: 186: operating_system_config = Marshal.load Marshal.dump(OPERATING_SYSTEM_DEFAULTS) 187: platform_config = Marshal.load Marshal.dump(PLATFORM_DEFAULTS) 188: system_config = load_file SYSTEM_WIDE_CONFIG_FILE 189: user_config = load_file config_file_name.dup.untaint 190: 191: @hash = operating_system_config.merge platform_config 192: @hash = @hash.merge system_config 193: @hash = @hash.merge user_config 194: 195: # HACK these override command-line args, which is bad 196: @backtrace = @hash[:backtrace] if @hash.key? :backtrace 197: @benchmark = @hash[:benchmark] if @hash.key? :benchmark 198: @bulk_threshold = @hash[:bulk_threshold] if @hash.key? :bulk_threshold 199: @home = @hash[:gemhome] if @hash.key? :gemhome 200: @path = @hash[:gempath] if @hash.key? :gempath 201: @update_sources = @hash[:update_sources] if @hash.key? :update_sources 202: @verbose = @hash[:verbose] if @hash.key? :verbose 203: @ssl_verify_mode = @hash[:ssl_verify_mode] if @hash.key? :ssl_verify_mode 204: @ssl_ca_cert = @hash[:ssl_ca_cert] if @hash.key? :ssl_ca_cert 205: 206: load_api_keys 207: 208: Gem.sources = @hash[:sources] if @hash.key? :sources 209: handle_arguments arg_list 210: end
Return the configuration information for key.
# File lib/rubygems/config_file.rb, line 351 351: def [](key) 352: @hash[key.to_s] 353: end
Set configuration option key to value.
# File lib/rubygems/config_file.rb, line 356 356: def []=(key, value) 357: @hash[key.to_s] = value 358: end
The name of the configuration file.
# File lib/rubygems/config_file.rb, line 265 265: def config_file_name 266: @config_file_name || Gem.config_file 267: end
Location of RubyGems.org credentials
# File lib/rubygems/config_file.rb, line 215 215: def credentials_path 216: File.join Gem.user_home, '.gem', 'credentials' 217: end
Delegates to @hash
# File lib/rubygems/config_file.rb, line 270 270: def each(&block) 271: hash = @hash.dup 272: hash.delete :update_sources 273: hash.delete :verbose 274: hash.delete :benchmark 275: hash.delete :backtrace 276: hash.delete :bulk_threshold 277: 278: yield :update_sources, @update_sources 279: yield :verbose, @verbose 280: yield :benchmark, @benchmark 281: yield :backtrace, @backtrace 282: yield :bulk_threshold, @bulk_threshold 283: 284: yield 'config_file_name', @config_file_name if @config_file_name 285: 286: hash.each(&block) 287: end
Handle the command arguments.
# File lib/rubygems/config_file.rb, line 290 290: def handle_arguments(arg_list) 291: @args = [] 292: 293: arg_list.each do |arg| 294: case arg 295: when /^--(backtrace|traceback)$/ then 296: @backtrace = true 297: when /^--bench(mark)?$/ then 298: @benchmark = true 299: when /^--debug$/ then 300: $DEBUG = true 301: else 302: @args << arg 303: end 304: end 305: end
# File lib/rubygems/config_file.rb, line 219 219: def load_api_keys 220: @api_keys = if File.exist? credentials_path then 221: load_file(credentials_path) 222: else 223: @hash 224: end 225: if @api_keys.key? :rubygems_api_key then 226: @rubygems_api_key = @api_keys[:rubygems_api_key] 227: @api_keys[:rubygems] = @api_keys.delete :rubygems_api_key unless @api_keys.key? :rubygems 228: end 229: end
# File lib/rubygems/config_file.rb, line 246 246: def load_file(filename) 247: Gem.load_yaml 248: 249: return {} unless filename and File.exist? filename 250: begin 251: YAML.load(File.read(filename)) 252: rescue ArgumentError 253: warn "Failed to load #{config_file_name}" 254: rescue Errno::EACCES 255: warn "Failed to load #{config_file_name} due to permissions problem." 256: end or {} 257: end
Really verbose mode gives you extra output.
# File lib/rubygems/config_file.rb, line 308 308: def really_verbose 309: case verbose 310: when true, false, nil then false 311: else true 312: end 313: end
# File lib/rubygems/config_file.rb, line 231 231: def rubygems_api_key=(api_key) 232: config = load_file(credentials_path).merge(:rubygems_api_key => api_key) 233: 234: dirname = File.dirname credentials_path 235: Dir.mkdir(dirname) unless File.exist? dirname 236: 237: Gem.load_yaml 238: 239: File.open(credentials_path, 'w') do |f| 240: f.write config.to_yaml 241: end 242: 243: @rubygems_api_key = api_key 244: end