50eebb
From 75821c744f0bfda185eac35b91810254bf9e2367 Mon Sep 17 00:00:00 2001
50eebb
From: Aaron Patterson <aaron.patterson@gmail.com>
50eebb
Date: Wed, 5 Feb 2014 16:42:04 -0800
50eebb
Subject: [PATCH] refactor checking `really_verbose`
50eebb
50eebb
This commit tries to reduce the number of places in RubyGems where we
50eebb
test `really_verbose`.
50eebb
---
50eebb
 lib/rubygems/commands/cleanup_command.rb |  4 ++--
50eebb
 lib/rubygems/dependency_installer.rb     |  6 +++---
50eebb
 lib/rubygems/installer.rb                |  6 +++---
50eebb
 lib/rubygems/package.rb                  |  2 +-
50eebb
 lib/rubygems/package/old.rb              |  2 +-
50eebb
 lib/rubygems/rdoc.rb                     |  2 +-
50eebb
 lib/rubygems/remote_fetcher.rb           | 23 ++++++++---------------
50eebb
 lib/rubygems/user_interaction.rb         |  8 ++++++++
50eebb
 8 files changed, 27 insertions(+), 26 deletions(-)
50eebb
50eebb
diff --git a/lib/rubygems/commands/cleanup_command.rb b/lib/rubygems/commands/cleanup_command.rb
50eebb
index c8f0082bfb..69975640fe 100644
50eebb
--- a/lib/rubygems/commands/cleanup_command.rb
50eebb
+++ b/lib/rubygems/commands/cleanup_command.rb
50eebb
@@ -66,10 +66,10 @@ def execute
50eebb
 
50eebb
     say "Clean Up Complete"
50eebb
 
50eebb
-    if Gem.configuration.really_verbose then
50eebb
+    verbose do
50eebb
       skipped = @default_gems.map { |spec| spec.full_name }
50eebb
 
50eebb
-      say "Skipped default gems: #{skipped.join ', '}"
50eebb
+      "Skipped default gems: #{skipped.join ', '}"
50eebb
     end
50eebb
   end
50eebb
 
50eebb
diff --git a/lib/rubygems/dependency_installer.rb b/lib/rubygems/dependency_installer.rb
50eebb
index 5644b5ce73..4ea3847b70 100644
50eebb
--- a/lib/rubygems/dependency_installer.rb
50eebb
+++ b/lib/rubygems/dependency_installer.rb
50eebb
@@ -165,9 +165,9 @@ def find_gems_with_sources dep, best_only=false # :nodoc:
50eebb
         # FIX if there is a problem talking to the network, we either need to always tell
50eebb
         # the user (no really_verbose) or fail hard, not silently tell them that we just
50eebb
         # couldn't find their requested gem.
50eebb
-        if Gem.configuration.really_verbose then
50eebb
-          say "Error fetching remote data:\t\t#{e.message}"
50eebb
-          say "Falling back to local-only install"
50eebb
+        verbose do
50eebb
+          "Error fetching remote data:\t\t#{e.message}\n" \
50eebb
+            "Falling back to local-only install"
50eebb
         end
50eebb
         @domain = :local
50eebb
       end
50eebb
diff --git a/lib/rubygems/installer.rb b/lib/rubygems/installer.rb
50eebb
index cbae8234a1..548f1262a8 100644
50eebb
--- a/lib/rubygems/installer.rb
50eebb
+++ b/lib/rubygems/installer.rb
50eebb
@@ -357,7 +357,7 @@ def generate_windows_script(filename, bindir)
50eebb
         file.puts windows_stub_script(bindir, filename)
50eebb
       end
50eebb
 
50eebb
-      say script_path if Gem.configuration.really_verbose
50eebb
+      verbose script_path
50eebb
     end
50eebb
   end
50eebb
 
50eebb
@@ -408,7 +408,7 @@ def generate_bin_script(filename, bindir)
50eebb
       file.print app_script_text(filename)
50eebb
     end
50eebb
 
50eebb
-    say bin_script_path if Gem.configuration.really_verbose
50eebb
+    verbose bin_script_path
50eebb
 
50eebb
     generate_windows_script filename, bindir
50eebb
   end
50eebb
@@ -694,7 +694,7 @@ def build_extension extension, dest_path # :nodoc:
50eebb
             results = builder.build(extension, gem_dir, dest_path,
50eebb
                                     results, @build_args)
50eebb
 
50eebb
-            say results.join("\n") if Gem.configuration.really_verbose
50eebb
+            verbose { results.join("\n") }
50eebb
           end
50eebb
         end
50eebb
       rescue
50eebb
diff --git a/lib/rubygems/package.rb b/lib/rubygems/package.rb
50eebb
index ae20f22b94..417b34b79f 100644
50eebb
--- a/lib/rubygems/package.rb
50eebb
+++ b/lib/rubygems/package.rb
f70500
@@ -347,7 +347,7 @@ def extract_tar_gz io, destination_dir, pattern = "*" # :nodoc:
50eebb
           out.write entry.read
50eebb
         end
50eebb
 
50eebb
-        say destination if Gem.configuration.really_verbose
50eebb
+        verbose destination
50eebb
       end
50eebb
     end
50eebb
   end
50eebb
diff --git a/lib/rubygems/package/old.rb b/lib/rubygems/package/old.rb
50eebb
index 30c30c0201..c47210d2a0 100644
50eebb
--- a/lib/rubygems/package/old.rb
50eebb
+++ b/lib/rubygems/package/old.rb
50eebb
@@ -83,7 +83,7 @@ def extract_files destination_dir
50eebb
           out.write file_data
50eebb
         end
50eebb
 
50eebb
-        say destination if Gem.configuration.really_verbose
50eebb
+        verbose destination
50eebb
       end
50eebb
     end
50eebb
   rescue Zlib::DataError
50eebb
diff --git a/lib/rubygems/rdoc.rb b/lib/rubygems/rdoc.rb
50eebb
index 633bd893a5..394e502051 100644
50eebb
--- a/lib/rubygems/rdoc.rb
50eebb
+++ b/lib/rubygems/rdoc.rb
50eebb
@@ -263,7 +263,7 @@ def legacy_rdoc *args
50eebb
       Gem::Requirement.new('>= 2.4.0') =~ self.class.rdoc_version
50eebb
 
50eebb
     r = new_rdoc
50eebb
-    say "rdoc #{args.join ' '}" if Gem.configuration.really_verbose
50eebb
+    verbose { "rdoc #{args.join ' '}" }
50eebb
 
50eebb
     Dir.chdir @spec.full_gem_path do
50eebb
       begin
50eebb
diff --git a/lib/rubygems/remote_fetcher.rb b/lib/rubygems/remote_fetcher.rb
50eebb
index 2490c69556..9df48bf6f3 100644
50eebb
--- a/lib/rubygems/remote_fetcher.rb
50eebb
+++ b/lib/rubygems/remote_fetcher.rb
50eebb
@@ -168,8 +168,7 @@ def download(spec, source_uri, install_dir = Gem.dir)
50eebb
     when 'http', 'https' then
50eebb
       unless File.exist? local_gem_path then
50eebb
         begin
50eebb
-          say "Downloading gem #{gem_file_name}" if
50eebb
-            Gem.configuration.really_verbose
50eebb
+          verbose "Downloading gem #{gem_file_name}"
50eebb
 
50eebb
           remote_gem_path = source_uri + "gems/#{gem_file_name}"
50eebb
 
50eebb
@@ -179,8 +178,7 @@ def download(spec, source_uri, install_dir = Gem.dir)
50eebb
 
50eebb
           alternate_name = "#{spec.original_name}.gem"
50eebb
 
50eebb
-          say "Failed, downloading gem #{alternate_name}" if
50eebb
-            Gem.configuration.really_verbose
50eebb
+          verbose "Failed, downloading gem #{alternate_name}"
50eebb
 
50eebb
           remote_gem_path = source_uri + "gems/#{alternate_name}"
50eebb
 
50eebb
@@ -199,8 +197,7 @@ def download(spec, source_uri, install_dir = Gem.dir)
50eebb
         local_gem_path = source_uri.to_s
50eebb
       end
50eebb
 
50eebb
-      say "Using local gem #{local_gem_path}" if
50eebb
-        Gem.configuration.really_verbose
50eebb
+      verbose "Using local gem #{local_gem_path}"
50eebb
     when nil then # TODO test for local overriding cache
50eebb
       source_path = if Gem.win_platform? && source_uri.scheme &&
50eebb
                        !source_uri.path.include?(':') then
50eebb
@@ -218,8 +215,7 @@ def download(spec, source_uri, install_dir = Gem.dir)
50eebb
         local_gem_path = source_uri.to_s
50eebb
       end
50eebb
 
50eebb
-      say "Using local gem #{local_gem_path}" if
50eebb
-        Gem.configuration.really_verbose
50eebb
+      verbose "Using local gem #{local_gem_path}"
50eebb
     else
50eebb
       raise ArgumentError, "unsupported URI scheme #{source_uri.scheme}"
50eebb
     end
50eebb
@@ -506,8 +502,7 @@ def fetch
50eebb
     begin
50eebb
       @requests[connection.object_id] += 1
50eebb
 
50eebb
-      say "#{request.method} #{uri}" if
50eebb
-        Gem.configuration.really_verbose
50eebb
+      verbose "#{request.method} #{uri}"
50eebb
 
50eebb
       file_name = File.basename(uri.path)
50eebb
       # perform download progress reporter only for gems
50eebb
@@ -536,11 +531,10 @@ def fetch
50eebb
         response = connection.request request
50eebb
       end
50eebb
 
50eebb
-      say "#{response.code} #{response.message}" if
50eebb
-        Gem.configuration.really_verbose
50eebb
+      verbose "#{response.code} #{response.message}"
50eebb
 
50eebb
     rescue Net::HTTPBadResponse
50eebb
-      say "bad response" if Gem.configuration.really_verbose
50eebb
+      verbose "bad response"
50eebb
 
50eebb
       reset connection
50eebb
 
50eebb
@@ -555,8 +549,7 @@ def fetch
50eebb
            Errno::ECONNABORTED, Errno::ECONNRESET, Errno::EPIPE
50eebb
 
50eebb
       requests = @requests[connection.object_id]
50eebb
-      say "connection reset after #{requests} requests, retrying" if
50eebb
-        Gem.configuration.really_verbose
50eebb
+      verbose "connection reset after #{requests} requests, retrying"
50eebb
 
50eebb
       raise FetchError.new('too many connection resets', uri) if retried
50eebb
 
50eebb
diff --git a/lib/rubygems/user_interaction.rb b/lib/rubygems/user_interaction.rb
50eebb
index f5e460f994..42ab6e2c24 100644
50eebb
--- a/lib/rubygems/user_interaction.rb
50eebb
+++ b/lib/rubygems/user_interaction.rb
50eebb
@@ -119,6 +119,14 @@ def say statement = ''
50eebb
   def terminate_interaction(*args)
50eebb
     ui.terminate_interaction(*args)
50eebb
   end
50eebb
+
50eebb
+  ##
50eebb
+  # Calls +say+ with +msg+ or the results of the block if really_verbose
50eebb
+  # is true.
50eebb
+
50eebb
+  def verbose msg = nil
50eebb
+    say(msg || yield) if Gem.configuration.really_verbose
50eebb
+  end
50eebb
 end
50eebb
 
50eebb
 ##