slimgems 1.3.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. data/ChangeLog +5811 -0
  2. data/History.txt +887 -0
  3. data/LICENSE.txt +51 -0
  4. data/README.md +87 -0
  5. data/Rakefile +120 -0
  6. data/bin/gem +25 -0
  7. data/bin/update_slimgems +35 -0
  8. data/bootstrap/Rakefile +4 -0
  9. data/hide_lib_for_update/note.txt +5 -0
  10. data/lib/gauntlet_rubygems.rb +50 -0
  11. data/lib/rbconfig/datadir.rb +20 -0
  12. data/lib/rubygems.rb +1220 -0
  13. data/lib/rubygems/builder.rb +102 -0
  14. data/lib/rubygems/command.rb +534 -0
  15. data/lib/rubygems/command_manager.rb +182 -0
  16. data/lib/rubygems/commands/build_command.rb +53 -0
  17. data/lib/rubygems/commands/cert_command.rb +86 -0
  18. data/lib/rubygems/commands/check_command.rb +80 -0
  19. data/lib/rubygems/commands/cleanup_command.rb +106 -0
  20. data/lib/rubygems/commands/contents_command.rb +98 -0
  21. data/lib/rubygems/commands/dependency_command.rb +195 -0
  22. data/lib/rubygems/commands/environment_command.rb +132 -0
  23. data/lib/rubygems/commands/fetch_command.rb +67 -0
  24. data/lib/rubygems/commands/generate_index_command.rb +133 -0
  25. data/lib/rubygems/commands/help_command.rb +172 -0
  26. data/lib/rubygems/commands/install_command.rb +178 -0
  27. data/lib/rubygems/commands/list_command.rb +35 -0
  28. data/lib/rubygems/commands/lock_command.rb +110 -0
  29. data/lib/rubygems/commands/mirror_command.rb +111 -0
  30. data/lib/rubygems/commands/outdated_command.rb +33 -0
  31. data/lib/rubygems/commands/owner_command.rb +75 -0
  32. data/lib/rubygems/commands/pristine_command.rb +93 -0
  33. data/lib/rubygems/commands/push_command.rb +56 -0
  34. data/lib/rubygems/commands/query_command.rb +280 -0
  35. data/lib/rubygems/commands/rdoc_command.rb +91 -0
  36. data/lib/rubygems/commands/search_command.rb +31 -0
  37. data/lib/rubygems/commands/server_command.rb +86 -0
  38. data/lib/rubygems/commands/setup_command.rb +387 -0
  39. data/lib/rubygems/commands/sources_command.rb +157 -0
  40. data/lib/rubygems/commands/specification_command.rb +125 -0
  41. data/lib/rubygems/commands/stale_command.rb +27 -0
  42. data/lib/rubygems/commands/uninstall_command.rb +83 -0
  43. data/lib/rubygems/commands/unpack_command.rb +121 -0
  44. data/lib/rubygems/commands/update_command.rb +212 -0
  45. data/lib/rubygems/commands/which_command.rb +86 -0
  46. data/lib/rubygems/config_file.rb +345 -0
  47. data/lib/rubygems/custom_require.rb +44 -0
  48. data/lib/rubygems/defaults.rb +101 -0
  49. data/lib/rubygems/dependency.rb +227 -0
  50. data/lib/rubygems/dependency_installer.rb +286 -0
  51. data/lib/rubygems/dependency_list.rb +208 -0
  52. data/lib/rubygems/doc_manager.rb +242 -0
  53. data/lib/rubygems/errors.rb +35 -0
  54. data/lib/rubygems/exceptions.rb +91 -0
  55. data/lib/rubygems/ext.rb +18 -0
  56. data/lib/rubygems/ext/builder.rb +56 -0
  57. data/lib/rubygems/ext/configure_builder.rb +25 -0
  58. data/lib/rubygems/ext/ext_conf_builder.rb +24 -0
  59. data/lib/rubygems/ext/rake_builder.rb +39 -0
  60. data/lib/rubygems/format.rb +81 -0
  61. data/lib/rubygems/gem_openssl.rb +92 -0
  62. data/lib/rubygems/gem_path_searcher.rb +100 -0
  63. data/lib/rubygems/gem_runner.rb +79 -0
  64. data/lib/rubygems/gemcutter_utilities.rb +49 -0
  65. data/lib/rubygems/indexer.rb +720 -0
  66. data/lib/rubygems/install_update_options.rb +125 -0
  67. data/lib/rubygems/installer.rb +604 -0
  68. data/lib/rubygems/local_remote_options.rb +135 -0
  69. data/lib/rubygems/old_format.rb +153 -0
  70. data/lib/rubygems/package.rb +97 -0
  71. data/lib/rubygems/package/f_sync_dir.rb +23 -0
  72. data/lib/rubygems/package/tar_header.rb +266 -0
  73. data/lib/rubygems/package/tar_input.rb +222 -0
  74. data/lib/rubygems/package/tar_output.rb +144 -0
  75. data/lib/rubygems/package/tar_reader.rb +106 -0
  76. data/lib/rubygems/package/tar_reader/entry.rb +141 -0
  77. data/lib/rubygems/package/tar_writer.rb +241 -0
  78. data/lib/rubygems/package_task.rb +126 -0
  79. data/lib/rubygems/platform.rb +183 -0
  80. data/lib/rubygems/remote_fetcher.rb +414 -0
  81. data/lib/rubygems/require_paths_builder.rb +18 -0
  82. data/lib/rubygems/requirement.rb +153 -0
  83. data/lib/rubygems/security.rb +814 -0
  84. data/lib/rubygems/server.rb +872 -0
  85. data/lib/rubygems/source_index.rb +597 -0
  86. data/lib/rubygems/source_info_cache.rb +395 -0
  87. data/lib/rubygems/source_info_cache_entry.rb +56 -0
  88. data/lib/rubygems/spec_fetcher.rb +337 -0
  89. data/lib/rubygems/specification.rb +1486 -0
  90. data/lib/rubygems/test_utilities.rb +147 -0
  91. data/lib/rubygems/text.rb +65 -0
  92. data/lib/rubygems/uninstaller.rb +278 -0
  93. data/lib/rubygems/user_interaction.rb +527 -0
  94. data/lib/rubygems/validator.rb +240 -0
  95. data/lib/rubygems/version.rb +316 -0
  96. data/lib/rubygems/version_option.rb +65 -0
  97. data/lib/ubygems.rb +10 -0
  98. data/setup.rb +42 -0
  99. data/test/bogussources.rb +8 -0
  100. data/test/data/gem-private_key.pem +27 -0
  101. data/test/data/gem-public_cert.pem +20 -0
  102. data/test/fake_certlib/openssl.rb +7 -0
  103. data/test/foo/discover.rb +0 -0
  104. data/test/functional.rb +92 -0
  105. data/test/gem_installer_test_case.rb +97 -0
  106. data/test/gem_package_tar_test_case.rb +132 -0
  107. data/test/gemutilities.rb +605 -0
  108. data/test/insure_session.rb +43 -0
  109. data/test/mockgemui.rb +56 -0
  110. data/test/plugin/exception/rubygems_plugin.rb +2 -0
  111. data/test/plugin/load/rubygems_plugin.rb +1 -0
  112. data/test/plugin/standarderror/rubygems_plugin.rb +2 -0
  113. data/test/private_key.pem +27 -0
  114. data/test/public_cert.pem +20 -0
  115. data/test/rubygems_plugin.rb +21 -0
  116. data/test/simple_gem.rb +66 -0
  117. data/test/test_config.rb +12 -0
  118. data/test/test_gem.rb +766 -0
  119. data/test/test_gem_builder.rb +27 -0
  120. data/test/test_gem_command.rb +178 -0
  121. data/test/test_gem_command_manager.rb +207 -0
  122. data/test/test_gem_commands_build_command.rb +74 -0
  123. data/test/test_gem_commands_cert_command.rb +124 -0
  124. data/test/test_gem_commands_check_command.rb +18 -0
  125. data/test/test_gem_commands_contents_command.rb +156 -0
  126. data/test/test_gem_commands_dependency_command.rb +216 -0
  127. data/test/test_gem_commands_environment_command.rb +144 -0
  128. data/test/test_gem_commands_fetch_command.rb +76 -0
  129. data/test/test_gem_commands_generate_index_command.rb +135 -0
  130. data/test/test_gem_commands_install_command.rb +315 -0
  131. data/test/test_gem_commands_list_command.rb +36 -0
  132. data/test/test_gem_commands_lock_command.rb +68 -0
  133. data/test/test_gem_commands_mirror_command.rb +60 -0
  134. data/test/test_gem_commands_outdated_command.rb +40 -0
  135. data/test/test_gem_commands_owner_command.rb +105 -0
  136. data/test/test_gem_commands_pristine_command.rb +108 -0
  137. data/test/test_gem_commands_push_command.rb +81 -0
  138. data/test/test_gem_commands_query_command.rb +426 -0
  139. data/test/test_gem_commands_server_command.rb +59 -0
  140. data/test/test_gem_commands_sources_command.rb +209 -0
  141. data/test/test_gem_commands_specification_command.rb +139 -0
  142. data/test/test_gem_commands_stale_command.rb +38 -0
  143. data/test/test_gem_commands_uninstall_command.rb +83 -0
  144. data/test/test_gem_commands_unpack_command.rb +199 -0
  145. data/test/test_gem_commands_update_command.rb +353 -0
  146. data/test/test_gem_commands_which_command.rb +66 -0
  147. data/test/test_gem_config_file.rb +287 -0
  148. data/test/test_gem_dependency.rb +149 -0
  149. data/test/test_gem_dependency_installer.rb +661 -0
  150. data/test/test_gem_dependency_list.rb +230 -0
  151. data/test/test_gem_doc_manager.rb +31 -0
  152. data/test/test_gem_ext_configure_builder.rb +84 -0
  153. data/test/test_gem_ext_ext_conf_builder.rb +173 -0
  154. data/test/test_gem_ext_rake_builder.rb +81 -0
  155. data/test/test_gem_format.rb +70 -0
  156. data/test/test_gem_gem_path_searcher.rb +78 -0
  157. data/test/test_gem_gem_runner.rb +45 -0
  158. data/test/test_gem_gemcutter_utilities.rb +103 -0
  159. data/test/test_gem_indexer.rb +673 -0
  160. data/test/test_gem_install_update_options.rb +68 -0
  161. data/test/test_gem_installer.rb +857 -0
  162. data/test/test_gem_local_remote_options.rb +97 -0
  163. data/test/test_gem_package_tar_header.rb +130 -0
  164. data/test/test_gem_package_tar_input.rb +112 -0
  165. data/test/test_gem_package_tar_output.rb +97 -0
  166. data/test/test_gem_package_tar_reader.rb +46 -0
  167. data/test/test_gem_package_tar_reader_entry.rb +109 -0
  168. data/test/test_gem_package_tar_writer.rb +144 -0
  169. data/test/test_gem_package_task.rb +59 -0
  170. data/test/test_gem_platform.rb +264 -0
  171. data/test/test_gem_remote_fetcher.rb +740 -0
  172. data/test/test_gem_requirement.rb +292 -0
  173. data/test/test_gem_server.rb +356 -0
  174. data/test/test_gem_silent_ui.rb +113 -0
  175. data/test/test_gem_source_index.rb +461 -0
  176. data/test/test_gem_spec_fetcher.rb +410 -0
  177. data/test/test_gem_specification.rb +1291 -0
  178. data/test/test_gem_stream_ui.rb +218 -0
  179. data/test/test_gem_text.rb +43 -0
  180. data/test/test_gem_uninstaller.rb +146 -0
  181. data/test/test_gem_validator.rb +63 -0
  182. data/test/test_gem_version.rb +181 -0
  183. data/test/test_gem_version_option.rb +89 -0
  184. data/test/test_kernel.rb +59 -0
  185. metadata +413 -0
@@ -0,0 +1,212 @@
1
+ require 'rubygems/command'
2
+ require 'rubygems/command_manager'
3
+ require 'rubygems/install_update_options'
4
+ require 'rubygems/local_remote_options'
5
+ require 'rubygems/spec_fetcher'
6
+ require 'rubygems/version_option'
7
+ require 'rubygems/commands/install_command'
8
+
9
+ class Gem::Commands::UpdateCommand < Gem::Command
10
+
11
+ include Gem::InstallUpdateOptions
12
+ include Gem::LocalRemoteOptions
13
+ include Gem::VersionOption
14
+
15
+ def initialize
16
+ super 'update',
17
+ 'Update the named gems (or all installed gems) in the local repository',
18
+ :generate_rdoc => true,
19
+ :generate_ri => true,
20
+ :force => false,
21
+ :test => false
22
+
23
+ add_install_update_options
24
+
25
+ OptionParser.accept Gem::Version do |value|
26
+ Gem::Version.new value
27
+
28
+ value
29
+ end
30
+
31
+ add_option('--system [VERSION]', Gem::Version,
32
+ "Update the #{Gem::NAME} system software") do |value, options|
33
+ value = true unless value
34
+
35
+ options[:system] = value
36
+ end
37
+
38
+ add_local_remote_options
39
+ add_platform_option
40
+ add_prerelease_option "as update targets"
41
+ end
42
+
43
+ def arguments # :nodoc:
44
+ "GEMNAME name of gem to update"
45
+ end
46
+
47
+ def defaults_str # :nodoc:
48
+ "--rdoc --ri --no-force --no-test --install-dir #{Gem.dir}"
49
+ end
50
+
51
+ def usage # :nodoc:
52
+ "#{program_name} GEMNAME [GEMNAME ...]"
53
+ end
54
+
55
+ def execute
56
+ @installer = Gem::DependencyInstaller.new options
57
+ @updated = []
58
+
59
+ hig = {}
60
+
61
+ if options[:system] then
62
+ update_rubygems
63
+ return
64
+ else
65
+ say "Updating installed gems"
66
+
67
+ hig = {} # highest installed gems
68
+
69
+ Gem.source_index.each do |name, spec|
70
+ if hig[spec.name].nil? or hig[spec.name].version < spec.version then
71
+ hig[spec.name] = spec
72
+ end
73
+ end
74
+ end
75
+
76
+ gems_to_update = which_to_update hig, options[:args]
77
+
78
+ updated = update_gems gems_to_update
79
+
80
+ if updated.empty? then
81
+ say "Nothing to update"
82
+ else
83
+ say "Gems updated: #{updated.map { |spec| spec.name }.join ', '}"
84
+
85
+ if options[:generate_ri] then
86
+ updated.each do |gem|
87
+ Gem::DocManager.new(gem, options[:rdoc_args]).generate_ri
88
+ end
89
+
90
+ Gem::DocManager.update_ri_cache
91
+ end
92
+
93
+ if options[:generate_rdoc] then
94
+ updated.each do |gem|
95
+ Gem::DocManager.new(gem, options[:rdoc_args]).generate_rdoc
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ def update_gem name, version = Gem::Requirement.default
102
+ return if @updated.any? { |spec| spec.name == name }
103
+ success = false
104
+
105
+ say "Updating #{name}"
106
+ begin
107
+ @installer.install name, version
108
+ success = true
109
+ rescue Gem::InstallError => e
110
+ alert_error "Error installing #{name}:\n\t#{e.message}"
111
+ success = false
112
+ end
113
+
114
+ @installer.installed_gems.each do |spec|
115
+ @updated << spec
116
+ say "Successfully installed #{spec.full_name}" if success
117
+ end
118
+ end
119
+
120
+ def update_gems gems_to_update
121
+ gems_to_update.uniq.sort.each do |name|
122
+ update_gem name
123
+ end
124
+
125
+ @updated
126
+ end
127
+
128
+ ##
129
+ # Update SlimGems software to the latest version.
130
+
131
+ def update_rubygems
132
+ unless options[:args].empty? then
133
+ alert_error "Gem names are not allowed with the --system option"
134
+ terminate_interaction 1
135
+ end
136
+
137
+ options[:user_install] = false
138
+
139
+ version = options[:system]
140
+ if version == true then
141
+ version = Gem::Version.new Gem::VERSION
142
+ requirement = Gem::Requirement.new ">= #{Gem::VERSION}"
143
+ else
144
+ version = Gem::Version.new version
145
+ requirement = Gem::Requirement.new version
146
+ end
147
+
148
+ rubygems_update = Gem::Specification.new
149
+ rubygems_update.name = Gem::GEM_NAME
150
+ rubygems_update.version = version
151
+
152
+ hig = {
153
+ Gem::GEM_NAME => rubygems_update
154
+ }
155
+
156
+ gems_to_update = which_to_update hig, options[:args]
157
+
158
+ if gems_to_update.empty? then
159
+ say "#{Gem::NAME} is already up-to-date (#{Gem::VERSION})"
160
+ terminate_interaction
161
+ end
162
+
163
+ update_gem(gems_to_update.first, requirement)
164
+ spec = @updated.last
165
+ say "#{Gem::NAME} system software updated (#{spec.version})" if spec
166
+ end
167
+
168
+ def which_to_update(highest_installed_gems, gem_names)
169
+ result = []
170
+
171
+ highest_installed_gems.each do |l_name, l_spec|
172
+ next if not gem_names.empty? and
173
+ gem_names.all? { |name| /#{name}/ !~ l_spec.name }
174
+
175
+ dependency = Gem::Dependency.new l_spec.name, "> #{l_spec.version}"
176
+
177
+ begin
178
+ fetcher = Gem::SpecFetcher.fetcher
179
+ spec_tuples = fetcher.find_matching dependency
180
+ rescue Gem::RemoteFetcher::FetchError => e
181
+ raise unless fetcher.warn_legacy e do
182
+ require 'rubygems/source_info_cache'
183
+
184
+ dependency.name = '' if dependency.name == //
185
+
186
+ specs = Gem::SourceInfoCache.search_with_source dependency
187
+
188
+ spec_tuples = specs.map do |spec, source_uri|
189
+ [[spec.name, spec.version, spec.original_platform], source_uri]
190
+ end
191
+ end
192
+ end
193
+
194
+ matching_gems = spec_tuples.select do |(name, _, platform),|
195
+ name == l_name and Gem::Platform.match platform
196
+ end
197
+
198
+ highest_remote_gem = matching_gems.sort_by do |(_, version),|
199
+ version
200
+ end.last
201
+
202
+ if highest_remote_gem and
203
+ l_spec.version < highest_remote_gem.first[1] then
204
+ result << l_name
205
+ end
206
+ end
207
+
208
+ result
209
+ end
210
+
211
+ end
212
+
@@ -0,0 +1,86 @@
1
+ require 'rubygems/command'
2
+ require 'rubygems/gem_path_searcher'
3
+
4
+ class Gem::Commands::WhichCommand < Gem::Command
5
+
6
+ EXT = %w[.rb .rbw .so .dll .bundle] # HACK
7
+
8
+ def initialize
9
+ super 'which', 'Find the location of a library file you can require',
10
+ :search_gems_first => false, :show_all => false
11
+
12
+ add_option '-a', '--[no-]all', 'show all matching files' do |show_all, options|
13
+ options[:show_all] = show_all
14
+ end
15
+
16
+ add_option '-g', '--[no-]gems-first',
17
+ 'search gems before non-gems' do |gems_first, options|
18
+ options[:search_gems_first] = gems_first
19
+ end
20
+ end
21
+
22
+ def arguments # :nodoc:
23
+ "FILE name of file to find"
24
+ end
25
+
26
+ def defaults_str # :nodoc:
27
+ "--no-gems-first --no-all"
28
+ end
29
+
30
+ def execute
31
+ searcher = Gem::GemPathSearcher.new
32
+
33
+ found = false
34
+
35
+ options[:args].each do |arg|
36
+ arg = arg.sub(/#{Regexp.union(*EXT)}$/, '')
37
+ dirs = $LOAD_PATH
38
+ spec = searcher.find arg
39
+
40
+ if spec then
41
+ if options[:search_gems_first] then
42
+ dirs = gem_paths(spec) + $LOAD_PATH
43
+ else
44
+ dirs = $LOAD_PATH + gem_paths(spec)
45
+ end
46
+ end
47
+
48
+ paths = find_paths arg, dirs
49
+
50
+ if paths.empty? then
51
+ alert_error "Can't find ruby library file or shared library #{arg}"
52
+ else
53
+ say paths
54
+ found = true
55
+ end
56
+ end
57
+
58
+ terminate_interaction 1 unless found
59
+ end
60
+
61
+ def find_paths(package_name, dirs)
62
+ result = []
63
+
64
+ dirs.each do |dir|
65
+ EXT.each do |ext|
66
+ full_path = File.join dir, "#{package_name}#{ext}"
67
+ if File.exist? full_path then
68
+ result << full_path
69
+ return result unless options[:show_all]
70
+ end
71
+ end
72
+ end
73
+
74
+ result
75
+ end
76
+
77
+ def gem_paths(spec)
78
+ spec.require_paths.collect { |d| File.join spec.full_gem_path, d }
79
+ end
80
+
81
+ def usage # :nodoc:
82
+ "#{program_name} FILE [FILE ...]"
83
+ end
84
+
85
+ end
86
+
@@ -0,0 +1,345 @@
1
+ #--
2
+ # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
3
+ # All rights reserved.
4
+ # See LICENSE.txt for permissions.
5
+ #++
6
+
7
+ ##
8
+ # Gem::ConfigFile SlimGems options and gem command options from ~/.gemrc.
9
+ #
10
+ # ~/.gemrc is a YAML file that uses strings to match gem command arguments and
11
+ # symbols to match SlimGems options.
12
+ #
13
+ # Gem command arguments use a String key that matches the command name and
14
+ # allow you to specify default arguments:
15
+ #
16
+ # install: --no-rdoc --no-ri
17
+ # update: --no-rdoc --no-ri
18
+ #
19
+ # You can use <tt>gem:</tt> to set default arguments for all commands.
20
+ #
21
+ # SlimGems options use symbol keys. Valid options are:
22
+ #
23
+ # +:backtrace+:: See #backtrace
24
+ # +:benchmark+:: See #benchmark
25
+ # +:sources+:: Sets Gem::sources
26
+ # +:verbose+:: See #verbose
27
+
28
+ class Gem::ConfigFile
29
+
30
+ DEFAULT_BACKTRACE = false
31
+ DEFAULT_BENCHMARK = false
32
+ DEFAULT_BULK_THRESHOLD = 1000
33
+ DEFAULT_VERBOSITY = true
34
+ DEFAULT_UPDATE_SOURCES = true
35
+
36
+ ##
37
+ # For Ruby packagers to set configuration defaults. Set in
38
+ # rubygems/defaults/operating_system.rb
39
+
40
+ OPERATING_SYSTEM_DEFAULTS = {}
41
+
42
+ ##
43
+ # For Ruby implementers to set configuration defaults. Set in
44
+ # rubygems/defaults/#{RUBY_ENGINE}.rb
45
+
46
+ PLATFORM_DEFAULTS = {}
47
+
48
+ system_config_path =
49
+ begin
50
+ require 'Win32API'
51
+
52
+ CSIDL_COMMON_APPDATA = 0x0023
53
+ path = 0.chr * 260
54
+ if RUBY_VERSION > '1.9' then
55
+ SHGetFolderPath = Win32API.new 'shell32', 'SHGetFolderPath', 'PLPLP',
56
+ 'L', :stdcall
57
+ SHGetFolderPath.call nil, CSIDL_COMMON_APPDATA, nil, 1, path
58
+ else
59
+ SHGetFolderPath = Win32API.new 'shell32', 'SHGetFolderPath', 'LLLLP',
60
+ 'L'
61
+ SHGetFolderPath.call 0, CSIDL_COMMON_APPDATA, 0, 1, path
62
+ end
63
+
64
+ path.strip
65
+ rescue LoadError
66
+ '/etc'
67
+ end
68
+
69
+ SYSTEM_WIDE_CONFIG_FILE = File.join system_config_path, 'gemrc'
70
+
71
+ ##
72
+ # List of arguments supplied to the config file object.
73
+
74
+ attr_reader :args
75
+
76
+ ##
77
+ # Where to look for gems (deprecated)
78
+
79
+ attr_accessor :path
80
+
81
+ ##
82
+ # Where to install gems (deprecated)
83
+
84
+ attr_accessor :home
85
+
86
+ ##
87
+ # True if we print backtraces on errors.
88
+
89
+ attr_writer :backtrace
90
+
91
+ ##
92
+ # True if we are benchmarking this run.
93
+
94
+ attr_accessor :benchmark
95
+
96
+ ##
97
+ # Bulk threshold value. If the number of missing gems are above this
98
+ # threshold value, then a bulk download technique is used. (deprecated)
99
+
100
+ attr_accessor :bulk_threshold
101
+
102
+ ##
103
+ # Verbose level of output:
104
+ # * false -- No output
105
+ # * true -- Normal output
106
+ # * :loud -- Extra output
107
+
108
+ attr_accessor :verbose
109
+
110
+ ##
111
+ # True if we want to update the SourceInfoCache every time, false otherwise
112
+
113
+ attr_accessor :update_sources
114
+
115
+ ##
116
+ # API key for SlimGems.org
117
+
118
+ attr_reader :rubygems_api_key
119
+
120
+ ##
121
+ # Create the config file object. +args+ is the list of arguments
122
+ # from the command line.
123
+ #
124
+ # The following command line options are handled early here rather
125
+ # than later at the time most command options are processed.
126
+ #
127
+ # <tt>--config-file</tt>, <tt>--config-file==NAME</tt>::
128
+ # Obviously these need to be handled by the ConfigFile object to ensure we
129
+ # get the right config file.
130
+ #
131
+ # <tt>--backtrace</tt>::
132
+ # Backtrace needs to be turned on early so that errors before normal
133
+ # option parsing can be properly handled.
134
+ #
135
+ # <tt>--debug</tt>::
136
+ # Enable Ruby level debug messages. Handled early for the same reason as
137
+ # --backtrace.
138
+
139
+ def initialize(arg_list)
140
+ @config_file_name = nil
141
+ need_config_file_name = false
142
+
143
+ arg_list = arg_list.map do |arg|
144
+ if need_config_file_name then
145
+ @config_file_name = arg
146
+ need_config_file_name = false
147
+ nil
148
+ elsif arg =~ /^--config-file=(.*)/ then
149
+ @config_file_name = $1
150
+ nil
151
+ elsif arg =~ /^--config-file$/ then
152
+ need_config_file_name = true
153
+ nil
154
+ else
155
+ arg
156
+ end
157
+ end.compact
158
+
159
+ @backtrace = DEFAULT_BACKTRACE
160
+ @benchmark = DEFAULT_BENCHMARK
161
+ @bulk_threshold = DEFAULT_BULK_THRESHOLD
162
+ @verbose = DEFAULT_VERBOSITY
163
+ @update_sources = DEFAULT_UPDATE_SOURCES
164
+
165
+ operating_system_config = Marshal.load Marshal.dump(OPERATING_SYSTEM_DEFAULTS)
166
+ platform_config = Marshal.load Marshal.dump(PLATFORM_DEFAULTS)
167
+ system_config = load_file SYSTEM_WIDE_CONFIG_FILE
168
+ user_config = load_file config_file_name.dup.untaint
169
+
170
+ @hash = operating_system_config.merge platform_config
171
+ @hash = @hash.merge system_config
172
+ @hash = @hash.merge user_config
173
+
174
+ # HACK these override command-line args, which is bad
175
+ @backtrace = @hash[:backtrace] if @hash.key? :backtrace
176
+ @benchmark = @hash[:benchmark] if @hash.key? :benchmark
177
+ @bulk_threshold = @hash[:bulk_threshold] if @hash.key? :bulk_threshold
178
+ @home = @hash[:gemhome] if @hash.key? :gemhome
179
+ @path = @hash[:gempath] if @hash.key? :gempath
180
+ @update_sources = @hash[:update_sources] if @hash.key? :update_sources
181
+ @verbose = @hash[:verbose] if @hash.key? :verbose
182
+
183
+ load_rubygems_api_key
184
+
185
+ Gem.sources = @hash[:sources] if @hash.key? :sources
186
+ handle_arguments arg_list
187
+ end
188
+
189
+ ##
190
+ # Location of SlimGems.org credentials
191
+
192
+ def credentials_path
193
+ File.join(Gem.user_home, '.gem', 'credentials')
194
+ end
195
+
196
+ def load_rubygems_api_key
197
+ api_key_hash = File.exists?(credentials_path) ? load_file(credentials_path) : @hash
198
+
199
+ @rubygems_api_key = api_key_hash[:rubygems_api_key] if api_key_hash.key? :rubygems_api_key
200
+ end
201
+
202
+ def rubygems_api_key=(api_key)
203
+ config = load_file(credentials_path).merge(:rubygems_api_key => api_key)
204
+
205
+ dirname = File.dirname(credentials_path)
206
+ Dir.mkdir(dirname) unless File.exists?(dirname)
207
+
208
+ Gem.load_yaml
209
+
210
+ File.open(credentials_path, 'w') do |f|
211
+ f.write config.to_yaml
212
+ end
213
+
214
+ @rubygems_api_key = api_key
215
+ end
216
+
217
+ def load_file(filename)
218
+ require "yaml"
219
+ return {} unless filename and File.exists?(filename)
220
+ begin
221
+ YAML.load(File.read(filename))
222
+ rescue ArgumentError
223
+ warn "Failed to load #{config_file_name}"
224
+ rescue Errno::EACCES
225
+ warn "Failed to load #{config_file_name} due to permissions problem."
226
+ end or {}
227
+ end
228
+
229
+ # True if the backtrace option has been specified, or debug is on.
230
+ def backtrace
231
+ @backtrace or $DEBUG
232
+ end
233
+
234
+ # The name of the configuration file.
235
+ def config_file_name
236
+ @config_file_name || Gem.config_file
237
+ end
238
+
239
+ # Delegates to @hash
240
+ def each(&block)
241
+ hash = @hash.dup
242
+ hash.delete :update_sources
243
+ hash.delete :verbose
244
+ hash.delete :benchmark
245
+ hash.delete :backtrace
246
+ hash.delete :bulk_threshold
247
+
248
+ yield :update_sources, @update_sources
249
+ yield :verbose, @verbose
250
+ yield :benchmark, @benchmark
251
+ yield :backtrace, @backtrace
252
+ yield :bulk_threshold, @bulk_threshold
253
+
254
+ yield 'config_file_name', @config_file_name if @config_file_name
255
+
256
+ hash.each(&block)
257
+ end
258
+
259
+ # Handle the command arguments.
260
+ def handle_arguments(arg_list)
261
+ @args = []
262
+
263
+ arg_list.each do |arg|
264
+ case arg
265
+ when /^--(backtrace|traceback)$/ then
266
+ @backtrace = true
267
+ when /^--bench(mark)?$/ then
268
+ @benchmark = true
269
+ when /^--debug$/ then
270
+ $DEBUG = true
271
+ else
272
+ @args << arg
273
+ end
274
+ end
275
+ end
276
+
277
+ # Really verbose mode gives you extra output.
278
+ def really_verbose
279
+ case verbose
280
+ when true, false, nil then false
281
+ else true
282
+ end
283
+ end
284
+
285
+ # to_yaml only overwrites things you can't override on the command line.
286
+ def to_yaml # :nodoc:
287
+ yaml_hash = {}
288
+ yaml_hash[:backtrace] = @hash.key?(:backtrace) ? @hash[:backtrace] :
289
+ DEFAULT_BACKTRACE
290
+ yaml_hash[:benchmark] = @hash.key?(:benchmark) ? @hash[:benchmark] :
291
+ DEFAULT_BENCHMARK
292
+ yaml_hash[:bulk_threshold] = @hash.key?(:bulk_threshold) ?
293
+ @hash[:bulk_threshold] : DEFAULT_BULK_THRESHOLD
294
+ yaml_hash[:sources] = Gem.sources
295
+ yaml_hash[:update_sources] = @hash.key?(:update_sources) ?
296
+ @hash[:update_sources] : DEFAULT_UPDATE_SOURCES
297
+ yaml_hash[:verbose] = @hash.key?(:verbose) ? @hash[:verbose] :
298
+ DEFAULT_VERBOSITY
299
+
300
+ keys = yaml_hash.keys.map { |key| key.to_s }
301
+ keys << 'debug'
302
+ re = Regexp.union(*keys)
303
+
304
+ @hash.each do |key, value|
305
+ key = key.to_s
306
+ next if key =~ re
307
+ yaml_hash[key.to_s] = value
308
+ end
309
+
310
+ yaml_hash.to_yaml
311
+ end
312
+
313
+ # Writes out this config file, replacing its source.
314
+ def write
315
+ open config_file_name, 'w' do |io|
316
+ io.write to_yaml
317
+ end
318
+ end
319
+
320
+ # Return the configuration information for +key+.
321
+ def [](key)
322
+ @hash[key.to_s]
323
+ end
324
+
325
+ # Set configuration option +key+ to +value+.
326
+ def []=(key, value)
327
+ @hash[key.to_s] = value
328
+ end
329
+
330
+ def ==(other) # :nodoc:
331
+ self.class === other and
332
+ @backtrace == other.backtrace and
333
+ @benchmark == other.benchmark and
334
+ @bulk_threshold == other.bulk_threshold and
335
+ @verbose == other.verbose and
336
+ @update_sources == other.update_sources and
337
+ @hash == other.hash
338
+ end
339
+
340
+ protected
341
+
342
+ attr_reader :hash
343
+
344
+ end
345
+