libgems 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) 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 +113 -0
  6. data/lib/gauntlet_libgems.rb +50 -0
  7. data/lib/libgems.rb +1246 -0
  8. data/lib/libgems/builder.rb +102 -0
  9. data/lib/libgems/command.rb +534 -0
  10. data/lib/libgems/command_manager.rb +182 -0
  11. data/lib/libgems/commands/build_command.rb +53 -0
  12. data/lib/libgems/commands/cert_command.rb +86 -0
  13. data/lib/libgems/commands/check_command.rb +80 -0
  14. data/lib/libgems/commands/cleanup_command.rb +106 -0
  15. data/lib/libgems/commands/contents_command.rb +98 -0
  16. data/lib/libgems/commands/dependency_command.rb +195 -0
  17. data/lib/libgems/commands/environment_command.rb +133 -0
  18. data/lib/libgems/commands/fetch_command.rb +67 -0
  19. data/lib/libgems/commands/generate_index_command.rb +133 -0
  20. data/lib/libgems/commands/help_command.rb +172 -0
  21. data/lib/libgems/commands/install_command.rb +178 -0
  22. data/lib/libgems/commands/list_command.rb +35 -0
  23. data/lib/libgems/commands/lock_command.rb +110 -0
  24. data/lib/libgems/commands/mirror_command.rb +111 -0
  25. data/lib/libgems/commands/outdated_command.rb +33 -0
  26. data/lib/libgems/commands/owner_command.rb +75 -0
  27. data/lib/libgems/commands/pristine_command.rb +93 -0
  28. data/lib/libgems/commands/push_command.rb +56 -0
  29. data/lib/libgems/commands/query_command.rb +280 -0
  30. data/lib/libgems/commands/rdoc_command.rb +91 -0
  31. data/lib/libgems/commands/search_command.rb +31 -0
  32. data/lib/libgems/commands/server_command.rb +86 -0
  33. data/lib/libgems/commands/sources_command.rb +157 -0
  34. data/lib/libgems/commands/specification_command.rb +125 -0
  35. data/lib/libgems/commands/stale_command.rb +27 -0
  36. data/lib/libgems/commands/uninstall_command.rb +83 -0
  37. data/lib/libgems/commands/unpack_command.rb +121 -0
  38. data/lib/libgems/commands/update_command.rb +160 -0
  39. data/lib/libgems/commands/which_command.rb +86 -0
  40. data/lib/libgems/config_file.rb +345 -0
  41. data/lib/libgems/custom_require.rb +44 -0
  42. data/lib/libgems/defaults.rb +101 -0
  43. data/lib/libgems/dependency.rb +227 -0
  44. data/lib/libgems/dependency_installer.rb +286 -0
  45. data/lib/libgems/dependency_list.rb +208 -0
  46. data/lib/libgems/doc_manager.rb +242 -0
  47. data/lib/libgems/errors.rb +35 -0
  48. data/lib/libgems/exceptions.rb +91 -0
  49. data/lib/libgems/ext.rb +18 -0
  50. data/lib/libgems/ext/builder.rb +56 -0
  51. data/lib/libgems/ext/configure_builder.rb +25 -0
  52. data/lib/libgems/ext/ext_conf_builder.rb +24 -0
  53. data/lib/libgems/ext/rake_builder.rb +39 -0
  54. data/lib/libgems/format.rb +81 -0
  55. data/lib/libgems/gem_openssl.rb +92 -0
  56. data/lib/libgems/gem_path_searcher.rb +100 -0
  57. data/lib/libgems/gem_runner.rb +79 -0
  58. data/lib/libgems/gemcutter_utilities.rb +49 -0
  59. data/lib/libgems/indexer.rb +720 -0
  60. data/lib/libgems/install_update_options.rb +125 -0
  61. data/lib/libgems/installer.rb +604 -0
  62. data/lib/libgems/local_remote_options.rb +135 -0
  63. data/lib/libgems/old_format.rb +153 -0
  64. data/lib/libgems/package.rb +97 -0
  65. data/lib/libgems/package/f_sync_dir.rb +23 -0
  66. data/lib/libgems/package/tar_header.rb +266 -0
  67. data/lib/libgems/package/tar_input.rb +222 -0
  68. data/lib/libgems/package/tar_output.rb +144 -0
  69. data/lib/libgems/package/tar_reader.rb +106 -0
  70. data/lib/libgems/package/tar_reader/entry.rb +141 -0
  71. data/lib/libgems/package/tar_writer.rb +241 -0
  72. data/lib/libgems/package_task.rb +126 -0
  73. data/lib/libgems/platform.rb +183 -0
  74. data/lib/libgems/remote_fetcher.rb +414 -0
  75. data/lib/libgems/require_paths_builder.rb +18 -0
  76. data/lib/libgems/requirement.rb +153 -0
  77. data/lib/libgems/security.rb +814 -0
  78. data/lib/libgems/server.rb +872 -0
  79. data/lib/libgems/source_index.rb +597 -0
  80. data/lib/libgems/source_info_cache.rb +395 -0
  81. data/lib/libgems/source_info_cache_entry.rb +56 -0
  82. data/lib/libgems/spec_fetcher.rb +337 -0
  83. data/lib/libgems/specification.rb +1487 -0
  84. data/lib/libgems/test_utilities.rb +147 -0
  85. data/lib/libgems/text.rb +65 -0
  86. data/lib/libgems/uninstaller.rb +278 -0
  87. data/lib/libgems/user_interaction.rb +527 -0
  88. data/lib/libgems/validator.rb +240 -0
  89. data/lib/libgems/version.rb +316 -0
  90. data/lib/libgems/version_option.rb +65 -0
  91. data/lib/rbconfig/datadir.rb +20 -0
  92. data/test/bogussources.rb +8 -0
  93. data/test/data/gem-private_key.pem +27 -0
  94. data/test/data/gem-public_cert.pem +20 -0
  95. data/test/fake_certlib/openssl.rb +7 -0
  96. data/test/foo/discover.rb +0 -0
  97. data/test/gem_installer_test_case.rb +97 -0
  98. data/test/gem_package_tar_test_case.rb +132 -0
  99. data/test/gemutilities.rb +605 -0
  100. data/test/insure_session.rb +43 -0
  101. data/test/mockgemui.rb +56 -0
  102. data/test/plugin/exception/libgems_plugin.rb +2 -0
  103. data/test/plugin/load/libgems_plugin.rb +1 -0
  104. data/test/plugin/standarderror/libgems_plugin.rb +2 -0
  105. data/test/private_key.pem +27 -0
  106. data/test/public_cert.pem +20 -0
  107. data/test/rubygems_plugin.rb +21 -0
  108. data/test/simple_gem.rb +66 -0
  109. data/test/test_config.rb +12 -0
  110. data/test/test_gem.rb +780 -0
  111. data/test/test_gem_builder.rb +27 -0
  112. data/test/test_gem_command.rb +178 -0
  113. data/test/test_gem_command_manager.rb +207 -0
  114. data/test/test_gem_commands_build_command.rb +74 -0
  115. data/test/test_gem_commands_cert_command.rb +124 -0
  116. data/test/test_gem_commands_check_command.rb +18 -0
  117. data/test/test_gem_commands_contents_command.rb +156 -0
  118. data/test/test_gem_commands_dependency_command.rb +216 -0
  119. data/test/test_gem_commands_environment_command.rb +144 -0
  120. data/test/test_gem_commands_fetch_command.rb +76 -0
  121. data/test/test_gem_commands_generate_index_command.rb +135 -0
  122. data/test/test_gem_commands_install_command.rb +315 -0
  123. data/test/test_gem_commands_list_command.rb +36 -0
  124. data/test/test_gem_commands_lock_command.rb +68 -0
  125. data/test/test_gem_commands_mirror_command.rb +60 -0
  126. data/test/test_gem_commands_outdated_command.rb +40 -0
  127. data/test/test_gem_commands_owner_command.rb +105 -0
  128. data/test/test_gem_commands_pristine_command.rb +108 -0
  129. data/test/test_gem_commands_push_command.rb +81 -0
  130. data/test/test_gem_commands_query_command.rb +426 -0
  131. data/test/test_gem_commands_server_command.rb +59 -0
  132. data/test/test_gem_commands_sources_command.rb +209 -0
  133. data/test/test_gem_commands_specification_command.rb +139 -0
  134. data/test/test_gem_commands_stale_command.rb +38 -0
  135. data/test/test_gem_commands_uninstall_command.rb +83 -0
  136. data/test/test_gem_commands_unpack_command.rb +199 -0
  137. data/test/test_gem_commands_update_command.rb +207 -0
  138. data/test/test_gem_commands_which_command.rb +66 -0
  139. data/test/test_gem_config_file.rb +287 -0
  140. data/test/test_gem_dependency.rb +149 -0
  141. data/test/test_gem_dependency_installer.rb +661 -0
  142. data/test/test_gem_dependency_list.rb +230 -0
  143. data/test/test_gem_doc_manager.rb +31 -0
  144. data/test/test_gem_ext_configure_builder.rb +84 -0
  145. data/test/test_gem_ext_ext_conf_builder.rb +173 -0
  146. data/test/test_gem_ext_rake_builder.rb +81 -0
  147. data/test/test_gem_format.rb +70 -0
  148. data/test/test_gem_gem_path_searcher.rb +78 -0
  149. data/test/test_gem_gem_runner.rb +45 -0
  150. data/test/test_gem_gemcutter_utilities.rb +103 -0
  151. data/test/test_gem_indexer.rb +673 -0
  152. data/test/test_gem_install_update_options.rb +68 -0
  153. data/test/test_gem_installer.rb +857 -0
  154. data/test/test_gem_local_remote_options.rb +97 -0
  155. data/test/test_gem_package_tar_header.rb +130 -0
  156. data/test/test_gem_package_tar_input.rb +112 -0
  157. data/test/test_gem_package_tar_output.rb +97 -0
  158. data/test/test_gem_package_tar_reader.rb +46 -0
  159. data/test/test_gem_package_tar_reader_entry.rb +109 -0
  160. data/test/test_gem_package_tar_writer.rb +144 -0
  161. data/test/test_gem_package_task.rb +59 -0
  162. data/test/test_gem_platform.rb +264 -0
  163. data/test/test_gem_remote_fetcher.rb +740 -0
  164. data/test/test_gem_requirement.rb +292 -0
  165. data/test/test_gem_server.rb +356 -0
  166. data/test/test_gem_silent_ui.rb +113 -0
  167. data/test/test_gem_source_index.rb +461 -0
  168. data/test/test_gem_spec_fetcher.rb +410 -0
  169. data/test/test_gem_specification.rb +1334 -0
  170. data/test/test_gem_stream_ui.rb +218 -0
  171. data/test/test_gem_text.rb +43 -0
  172. data/test/test_gem_uninstaller.rb +146 -0
  173. data/test/test_gem_validator.rb +63 -0
  174. data/test/test_gem_version.rb +181 -0
  175. data/test/test_gem_version_option.rb +89 -0
  176. data/test/test_kernel.rb +59 -0
  177. metadata +402 -0
@@ -0,0 +1,121 @@
1
+ require 'fileutils'
2
+ require 'libgems/command'
3
+ require 'libgems/installer'
4
+ require 'libgems/version_option'
5
+
6
+ class LibGems::Commands::UnpackCommand < LibGems::Command
7
+
8
+ include LibGems::VersionOption
9
+
10
+ def initialize
11
+ super 'unpack', 'Unpack an installed gem to the current directory',
12
+ :version => LibGems::Requirement.default,
13
+ :target => Dir.pwd
14
+
15
+ add_option('--target=DIR',
16
+ 'target directory for unpacking') do |value, options|
17
+ options[:target] = value
18
+ end
19
+
20
+ add_version_option
21
+ end
22
+
23
+ def arguments # :nodoc:
24
+ "GEMNAME name of gem to unpack"
25
+ end
26
+
27
+ def defaults_str # :nodoc:
28
+ "--version '#{LibGems::Requirement.default}'"
29
+ end
30
+
31
+ def usage # :nodoc:
32
+ "#{program_name} GEMNAME"
33
+ end
34
+
35
+ def download dependency
36
+ found = LibGems::SpecFetcher.fetcher.fetch dependency
37
+
38
+ return if found.empty?
39
+
40
+ spec, source_uri = found.first
41
+
42
+ LibGems::RemoteFetcher.fetcher.download spec, source_uri
43
+ end
44
+
45
+ #--
46
+ # TODO: allow, e.g., 'gem unpack rake-0.3.1'. Find a general solution for
47
+ # this, so that it works for uninstall as well. (And check other commands
48
+ # at the same time.)
49
+
50
+ def execute
51
+ get_all_gem_names.each do |name|
52
+ dependency = LibGems::Dependency.new name, options[:version]
53
+ path = get_path dependency
54
+
55
+ if path then
56
+ basename = File.basename path, '.gem'
57
+ target_dir = File.expand_path basename, options[:target]
58
+ FileUtils.mkdir_p target_dir
59
+ LibGems::Installer.new(path, :unpack => true).unpack target_dir
60
+ say "Unpacked gem: '#{target_dir}'"
61
+ else
62
+ alert_error "Gem '#{name}' not installed."
63
+ end
64
+ end
65
+ end
66
+
67
+ ##
68
+ #
69
+ # Find cached filename in LibGems.path. Returns nil if the file cannot be found.
70
+ #
71
+ #--
72
+ # TODO: see comments in get_path() about general service.
73
+
74
+ def find_in_cache(filename)
75
+ LibGems.path.each do |gem_dir|
76
+ this_path = File.join gem_dir, 'cache', filename
77
+ return this_path if File.exist? this_path
78
+ end
79
+
80
+ return nil
81
+ end
82
+
83
+ ##
84
+ # Return the full path to the cached gem file matching the given
85
+ # name and version requirement. Returns 'nil' if no match.
86
+ #
87
+ # Example:
88
+ #
89
+ # get_path 'rake', '> 0.4' # "/usr/lib/ruby/gems/1.8/cache/rake-0.4.2.gem"
90
+ # get_path 'rake', '< 0.1' # nil
91
+ # get_path 'rak' # nil (exact name required)
92
+ #--
93
+ # TODO: This should be refactored so that it's a general service. I don't
94
+ # think any of our existing classes are the right place though. Just maybe
95
+ # 'Cache'?
96
+ #
97
+ # TODO: It just uses LibGems.dir for now. What's an easy way to get the list of
98
+ # source directories?
99
+
100
+ def get_path dependency
101
+ return dependency.name if dependency.name =~ /\.gem$/i
102
+
103
+ specs = LibGems.source_index.search dependency
104
+
105
+ selected = specs.sort_by { |s| s.version }.last
106
+
107
+ return download(dependency) if selected.nil?
108
+
109
+ return unless dependency.name =~ /^#{selected.name}$/i
110
+
111
+ # We expect to find (basename).gem in the 'cache' directory. Furthermore,
112
+ # the name match must be exact (ignoring case).
113
+
114
+ path = find_in_cache(selected.file_name)
115
+ return download(dependency) unless path
116
+
117
+ path
118
+ end
119
+
120
+ end
121
+
@@ -0,0 +1,160 @@
1
+ require 'libgems/command'
2
+ require 'libgems/command_manager'
3
+ require 'libgems/install_update_options'
4
+ require 'libgems/local_remote_options'
5
+ require 'libgems/spec_fetcher'
6
+ require 'libgems/version_option'
7
+ require 'libgems/commands/install_command'
8
+
9
+ class LibGems::Commands::UpdateCommand < LibGems::Command
10
+
11
+ include LibGems::InstallUpdateOptions
12
+ include LibGems::LocalRemoteOptions
13
+ include LibGems::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 LibGems::Version do |value|
26
+ LibGems::Version.new value
27
+
28
+ value
29
+ end
30
+
31
+ add_local_remote_options
32
+ add_platform_option
33
+ add_prerelease_option "as update targets"
34
+ end
35
+
36
+ def arguments # :nodoc:
37
+ "GEMNAME name of gem to update"
38
+ end
39
+
40
+ def defaults_str # :nodoc:
41
+ "--rdoc --ri --no-force --no-test --install-dir #{LibGems.dir}"
42
+ end
43
+
44
+ def usage # :nodoc:
45
+ "#{program_name} GEMNAME [GEMNAME ...]"
46
+ end
47
+
48
+ def execute
49
+ @installer = LibGems::DependencyInstaller.new options
50
+ @updated = []
51
+
52
+ hig = {}
53
+
54
+ say "Updating installed gems"
55
+
56
+ hig = {} # highest installed gems
57
+
58
+ LibGems.source_index.each do |name, spec|
59
+ if hig[spec.name].nil? or hig[spec.name].version < spec.version then
60
+ hig[spec.name] = spec
61
+ end
62
+ end
63
+
64
+ gems_to_update = which_to_update hig, options[:args]
65
+
66
+ updated = update_gems gems_to_update
67
+
68
+ if updated.empty? then
69
+ say "Nothing to update"
70
+ else
71
+ say "Gems updated: #{updated.map { |spec| spec.name }.join ', '}"
72
+
73
+ if options[:generate_ri] then
74
+ updated.each do |gem|
75
+ LibGems::DocManager.new(gem, options[:rdoc_args]).generate_ri
76
+ end
77
+
78
+ LibGems::DocManager.update_ri_cache
79
+ end
80
+
81
+ if options[:generate_rdoc] then
82
+ updated.each do |gem|
83
+ LibGems::DocManager.new(gem, options[:rdoc_args]).generate_rdoc
84
+ end
85
+ end
86
+ end
87
+ end
88
+
89
+ def update_gem name, version = LibGems::Requirement.default
90
+ return if @updated.any? { |spec| spec.name == name }
91
+ success = false
92
+
93
+ say "Updating #{name}"
94
+ begin
95
+ @installer.install name, version
96
+ success = true
97
+ rescue LibGems::InstallError => e
98
+ alert_error "Error installing #{name}:\n\t#{e.message}"
99
+ success = false
100
+ end
101
+
102
+ @installer.installed_gems.each do |spec|
103
+ @updated << spec
104
+ say "Successfully installed #{spec.full_name}" if success
105
+ end
106
+ end
107
+
108
+ def update_gems gems_to_update
109
+ gems_to_update.uniq.sort.each do |name|
110
+ update_gem name
111
+ end
112
+
113
+ @updated
114
+ end
115
+
116
+ def which_to_update(highest_installed_gems, gem_names)
117
+ result = []
118
+
119
+ highest_installed_gems.each do |l_name, l_spec|
120
+ next if not gem_names.empty? and
121
+ gem_names.all? { |name| /#{name}/ !~ l_spec.name }
122
+
123
+ dependency = LibGems::Dependency.new l_spec.name, "> #{l_spec.version}"
124
+
125
+ begin
126
+ fetcher = LibGems::SpecFetcher.fetcher
127
+ spec_tuples = fetcher.find_matching dependency
128
+ rescue LibGems::RemoteFetcher::FetchError => e
129
+ raise unless fetcher.warn_legacy e do
130
+ require 'libgems/source_info_cache'
131
+
132
+ dependency.name = '' if dependency.name == //
133
+
134
+ specs = LibGems::SourceInfoCache.search_with_source dependency
135
+
136
+ spec_tuples = specs.map do |spec, source_uri|
137
+ [[spec.name, spec.version, spec.original_platform], source_uri]
138
+ end
139
+ end
140
+ end
141
+
142
+ matching_gems = spec_tuples.select do |(name, _, platform),|
143
+ name == l_name and LibGems::Platform.match platform
144
+ end
145
+
146
+ highest_remote_gem = matching_gems.sort_by do |(_, version),|
147
+ version
148
+ end.last
149
+
150
+ if highest_remote_gem and
151
+ l_spec.version < highest_remote_gem.first[1] then
152
+ result << l_name
153
+ end
154
+ end
155
+
156
+ result
157
+ end
158
+
159
+ end
160
+
@@ -0,0 +1,86 @@
1
+ require 'libgems/command'
2
+ require 'libgems/gem_path_searcher'
3
+
4
+ class LibGems::Commands::WhichCommand < LibGems::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 = LibGems::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
+ # LibGems::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
+ # LibGems 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 LibGems::sources
26
+ # +:verbose+:: See #verbose
27
+
28
+ class LibGems::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
+ LibGems.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(LibGems.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
+ LibGems.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 || LibGems.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] = LibGems.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
+