2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
4 # See LICENSE.txt for permissions.
10 # Store the gem command options specified in the configuration file. The
11 # config file object acts much like a hash.
15 DEFAULT_BACKTRACE = false
16 DEFAULT_BENCHMARK = false
17 DEFAULT_BULK_THRESHOLD = 1000
18 DEFAULT_VERBOSITY = true
19 DEFAULT_UPDATE_SOURCES = true
25 CSIDL_COMMON_APPDATA = 0x0023
27 SHGetFolderPath = Win32API.new 'shell32', 'SHGetFolderPath', 'LLLLP', 'L'
28 SHGetFolderPath.call 0, CSIDL_COMMON_APPDATA, 0, 1, path
35 SYSTEM_WIDE_CONFIG_FILE = File.join system_config_path, 'gemrc'
37 # List of arguments supplied to the config file object.
40 # True if we print backtraces on errors.
41 attr_writer :backtrace
43 # True if we are benchmarking this run.
44 attr_accessor :benchmark
46 # Bulk threshold value. If the number of missing gems are above
47 # this threshold value, then a bulk download technique is used.
48 attr_accessor :bulk_threshold
50 # Verbose level of output:
51 # * false -- No output
52 # * true -- Normal output
53 # * :loud -- Extra output
54 attr_accessor :verbose
56 # True if we want to update the SourceInfoCache every time, false otherwise
57 attr_accessor :update_sources
59 # Create the config file object. +args+ is the list of arguments
60 # from the command line.
62 # The following command line options are handled early here rather
63 # than later at the time most command options are processed.
65 # * --config-file and --config-file==NAME -- Obviously these need
66 # to be handled by the ConfigFile object to ensure we get the
69 # * --backtrace -- Backtrace needs to be turned on early so that
70 # errors before normal option parsing can be properly handled.
72 # * --debug -- Enable Ruby level debug messages. Handled early
73 # for the same reason as --backtrace.
75 def initialize(arg_list)
76 @config_file_name = nil
77 need_config_file_name = false
79 arg_list = arg_list.map do |arg|
80 if need_config_file_name then
81 @config_file_name = arg
83 elsif arg =~ /^--config-file=(.*)/ then
84 @config_file_name = $1
86 elsif arg =~ /^--config-file$/ then
87 need_config_file_name = true
94 @backtrace = DEFAULT_BACKTRACE
95 @benchmark = DEFAULT_BENCHMARK
96 @bulk_threshold = DEFAULT_BULK_THRESHOLD
97 @verbose = DEFAULT_VERBOSITY
98 @update_sources = DEFAULT_UPDATE_SOURCES
100 @hash = load_file(SYSTEM_WIDE_CONFIG_FILE)
101 @hash.merge!(load_file(config_file_name.dup.untaint))
103 # HACK these override command-line args, which is bad
104 @backtrace = @hash[:backtrace] if @hash.key? :backtrace
105 @benchmark = @hash[:benchmark] if @hash.key? :benchmark
106 @bulk_threshold = @hash[:bulk_threshold] if @hash.key? :bulk_threshold
107 Gem.sources.replace @hash[:sources] if @hash.key? :sources
108 @verbose = @hash[:verbose] if @hash.key? :verbose
109 @update_sources = @hash[:update_sources] if @hash.key? :update_sources
111 handle_arguments arg_list
114 def load_file(filename)
116 YAML.load(File.read(filename)) if filename and File.exist?(filename)
118 warn "Failed to load #{config_file_name}"
120 warn "Failed to load #{config_file_name} due to permissions problem."
124 # True if the backtrace option has been specified, or debug is on.
129 # The name of the configuration file.
131 @config_file_name || Gem.config_file
137 hash.delete :update_sources
139 hash.delete :benchmark
140 hash.delete :backtrace
141 hash.delete :bulk_threshold
143 yield :update_sources, @update_sources
144 yield :verbose, @verbose
145 yield :benchmark, @benchmark
146 yield :backtrace, @backtrace
147 yield :bulk_threshold, @bulk_threshold
149 yield 'config_file_name', @config_file_name if @config_file_name
154 # Handle the command arguments.
155 def handle_arguments(arg_list)
158 arg_list.each do |arg|
160 when /^--(backtrace|traceback)$/ then
162 when /^--bench(mark)?$/ then
164 when /^--debug$/ then
172 # Really verbose mode gives you extra output.
175 when true, false, nil then false
180 # to_yaml only overwrites things you can't override on the command line.
181 def to_yaml # :nodoc:
183 yaml_hash[:backtrace] = @hash.key?(:backtrace) ? @hash[:backtrace] :
185 yaml_hash[:benchmark] = @hash.key?(:benchmark) ? @hash[:benchmark] :
187 yaml_hash[:bulk_threshold] = @hash.key?(:bulk_threshold) ?
188 @hash[:bulk_threshold] : DEFAULT_BULK_THRESHOLD
189 yaml_hash[:sources] = Gem.sources
190 yaml_hash[:update_sources] = @hash.key?(:update_sources) ?
191 @hash[:update_sources] : DEFAULT_UPDATE_SOURCES
192 yaml_hash[:verbose] = @hash.key?(:verbose) ? @hash[:verbose] :
195 keys = yaml_hash.keys.map { |key| key.to_s }
197 re = Regexp.union(*keys)
199 @hash.each do |key, value|
202 yaml_hash[key.to_s] = value
208 # Writes out this config file, replacing its source.
210 File.open config_file_name, 'w' do |fp|
211 fp.write self.to_yaml
215 # Return the configuration information for +key+.
220 # Set configuration option +key+ to +value+.
222 @hash[key.to_s] = value
225 def ==(other) # :nodoc:
226 self.class === other and
227 @backtrace == other.backtrace and
228 @benchmark == other.benchmark and
229 @bulk_threshold == other.bulk_threshold and
230 @verbose == other.verbose and
231 @update_sources == other.update_sources and