From 9e7b8c61a99c4d4c69da36f95759e375b7a4e116 Mon Sep 17 00:00:00 2001
From: Mike McQuaid <mike@mikemcquaid.com>
Date: Mon, 25 Apr 2016 18:01:15 +0100
Subject: [PATCH] Make config command cross-platform.

Closes #168.

Signed-off-by: Mike McQuaid <mike@mikemcquaid.com>
---
 Library/Homebrew/cmd/config.rb                | 187 +-----------------
 .../Homebrew/extend/os/mac/system_config.rb   |  56 ++++++
 Library/Homebrew/extend/os/system_config.rb   |   5 +
 Library/Homebrew/system_config.rb             | 144 ++++++++++++++
 4 files changed, 207 insertions(+), 185 deletions(-)
 create mode 100644 Library/Homebrew/extend/os/mac/system_config.rb
 create mode 100644 Library/Homebrew/extend/os/system_config.rb
 create mode 100644 Library/Homebrew/system_config.rb

diff --git a/Library/Homebrew/cmd/config.rb b/Library/Homebrew/cmd/config.rb
index bf78abe389..b5f2502b03 100644
--- a/Library/Homebrew/cmd/config.rb
+++ b/Library/Homebrew/cmd/config.rb
@@ -3,193 +3,10 @@
 #:    a bug report, you will likely be asked for this information if you do not
 #:    provide it.
 
-require "hardware"
-require "software_spec"
-require "rexml/document"
-require "tap"
+require "system_config"
 
 module Homebrew
   def config
-    dump_verbose_config
-  end
-
-  def llvm
-    @llvm ||= MacOS.llvm_build_version if MacOS.has_apple_developer_tools?
-  end
-
-  def gcc_42
-    @gcc_42 ||= MacOS.gcc_42_build_version if MacOS.has_apple_developer_tools?
-  end
-
-  def gcc_40
-    @gcc_40 ||= MacOS.gcc_40_build_version if MacOS.has_apple_developer_tools?
-  end
-
-  def clang
-    @clang ||= MacOS.clang_version if MacOS.has_apple_developer_tools?
-  end
-
-  def clang_build
-    @clang_build ||= MacOS.clang_build_version if MacOS.has_apple_developer_tools?
-  end
-
-  def xcode
-    if instance_variable_defined?(:@xcode)
-      @xcode
-    elsif MacOS::Xcode.installed?
-      @xcode = MacOS::Xcode.version
-      @xcode += " => #{MacOS::Xcode.prefix}" unless MacOS::Xcode.default_prefix?
-      @xcode
-    end
-  end
-
-  def clt
-    if instance_variable_defined?(:@clt)
-      @clt
-    elsif MacOS::CLT.installed? && MacOS::Xcode.version >= "4.3"
-      @clt = MacOS::CLT.version
-    end
-  end
-
-  def head
-    Homebrew.git_head || "(none)"
-  end
-
-  def last_commit
-    Homebrew.git_last_commit || "never"
-  end
-
-  def origin
-    Homebrew.git_origin || "(none)"
-  end
-
-  def core_tap_head
-    CoreTap.instance.git_head || "(none)"
-  end
-
-  def core_tap_last_commit
-    CoreTap.instance.git_last_commit || "never"
-  end
-
-  def core_tap_origin
-    CoreTap.instance.remote || "(none)"
-  end
-
-  def describe_path(path)
-    return "N/A" if path.nil?
-    realpath = path.realpath
-    if realpath == path
-      path
-    else
-      "#{path} => #{realpath}"
-    end
-  end
-
-  def describe_x11
-    return "N/A" unless MacOS::XQuartz.installed?
-    "#{MacOS::XQuartz.version} => #{describe_path(MacOS::XQuartz.prefix)}"
-  end
-
-  def describe_perl
-    describe_path(which "perl")
-  end
-
-  def describe_python
-    python = which "python"
-    return "N/A" if python.nil?
-    python_binary = Utils.popen_read python, "-c", "import sys; sys.stdout.write(sys.executable)"
-    python_binary = Pathname.new(python_binary).realpath
-    if python == python_binary
-      python
-    else
-      "#{python} => #{python_binary}"
-    end
-  end
-
-  def describe_ruby
-    ruby = which "ruby"
-    return "N/A" if ruby.nil?
-    ruby_binary = Utils.popen_read ruby, "-rrbconfig", "-e", \
-      'include RbConfig;print"#{CONFIG["bindir"]}/#{CONFIG["ruby_install_name"]}#{CONFIG["EXEEXT"]}"'
-    ruby_binary = Pathname.new(ruby_binary).realpath
-    if ruby == ruby_binary
-      ruby
-    else
-      "#{ruby} => #{ruby_binary}"
-    end
-  end
-
-  def hardware
-    "CPU: #{Hardware.cores_as_words}-core #{Hardware::CPU.bits}-bit #{Hardware::CPU.family}"
-  end
-
-  def kernel
-    `uname -m`.chomp
-  end
-
-  def macports_or_fink
-    @ponk ||= MacOS.macports_or_fink
-    @ponk.join(", ") unless @ponk.empty?
-  end
-
-  def describe_system_ruby
-    s = ""
-    case RUBY_VERSION
-    when /^1\.[89]/, /^2\.0/
-      s << "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
-    else
-      s << RUBY_VERSION
-    end
-
-    if RUBY_PATH.to_s !~ %r{^/System/Library/Frameworks/Ruby.framework/Versions/[12]\.[089]/usr/bin/ruby}
-      s << " => #{RUBY_PATH}"
-    end
-    s
-  end
-
-  def describe_java
-    # java_home doesn't exist on all OS Xs; it might be missing on older versions.
-    return "N/A" unless File.executable? "/usr/libexec/java_home"
-
-    java_xml = Utils.popen_read("/usr/libexec/java_home", "--xml", "--failfast")
-    return "N/A" unless $?.success?
-    javas = []
-    REXML::XPath.each(REXML::Document.new(java_xml), "//key[text()='JVMVersion']/following-sibling::string") do |item|
-      javas << item.text
-    end
-    javas.uniq.join(", ")
-  end
-
-  def dump_verbose_config(f = $stdout)
-    f.puts "HOMEBREW_VERSION: #{HOMEBREW_VERSION}"
-    f.puts "ORIGIN: #{origin}"
-    f.puts "HEAD: #{head}"
-    f.puts "Last commit: #{last_commit}"
-    if CoreTap.instance.installed?
-      f.puts "Core tap ORIGIN: #{core_tap_origin}"
-      f.puts "Core tap HEAD: #{core_tap_head}"
-      f.puts "Core tap last commit: #{core_tap_last_commit}"
-    else
-      f.puts "Core tap: N/A"
-    end
-    f.puts "HOMEBREW_PREFIX: #{HOMEBREW_PREFIX}"
-    f.puts "HOMEBREW_REPOSITORY: #{HOMEBREW_REPOSITORY}"
-    f.puts "HOMEBREW_CELLAR: #{HOMEBREW_CELLAR}"
-    f.puts "HOMEBREW_BOTTLE_DOMAIN: #{BottleSpecification::DEFAULT_DOMAIN}"
-    f.puts hardware
-    f.puts "OS X: #{MacOS.full_version}-#{kernel}"
-    f.puts "Xcode: #{xcode ? xcode : "N/A"}"
-    f.puts "CLT: #{clt ? clt : "N/A"}"
-    f.puts "GCC-4.0: build #{gcc_40}" if gcc_40
-    f.puts "GCC-4.2: build #{gcc_42}" if gcc_42
-    f.puts "LLVM-GCC: build #{llvm}"  if llvm
-    f.puts "Clang: #{clang ? "#{clang} build #{clang_build}" : "N/A"}"
-    f.puts "MacPorts/Fink: #{macports_or_fink}" if macports_or_fink
-    f.puts "X11: #{describe_x11}"
-    f.puts "System Ruby: #{describe_system_ruby}"
-    f.puts "Perl: #{describe_perl}"
-    f.puts "Python: #{describe_python}"
-    f.puts "Ruby: #{describe_ruby}"
-    f.puts "Java: #{describe_java}"
+    SystemConfig.dump_verbose_config
   end
 end
diff --git a/Library/Homebrew/extend/os/mac/system_config.rb b/Library/Homebrew/extend/os/mac/system_config.rb
new file mode 100644
index 0000000000..4a06d07ef0
--- /dev/null
+++ b/Library/Homebrew/extend/os/mac/system_config.rb
@@ -0,0 +1,56 @@
+class SystemConfig
+  class << self
+    def xcode
+      if instance_variable_defined?(:@xcode)
+        @xcode
+      elsif MacOS::Xcode.installed?
+        @xcode = MacOS::Xcode.version
+        @xcode += " => #{MacOS::Xcode.prefix}" unless MacOS::Xcode.default_prefix?
+        @xcode
+      end
+    end
+
+    def clt
+      if instance_variable_defined?(:@clt)
+        @clt
+      elsif MacOS::CLT.installed? && MacOS::Xcode.version >= "4.3"
+        @clt = MacOS::CLT.version
+      end
+    end
+
+    def macports_or_fink
+      @ponk ||= MacOS.macports_or_fink
+      @ponk.join(", ") unless @ponk.empty?
+    end
+
+    def describe_xquartz
+      return "N/A" unless MacOS::XQuartz.installed?
+      "#{MacOS::XQuartz.version} => #{describe_path(MacOS::XQuartz.prefix)}"
+    end
+
+    def describe_system_ruby
+      s = ""
+      case RUBY_VERSION
+      when /^1\.[89]/, /^2\.0/
+        s << "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
+      else
+        s << RUBY_VERSION
+      end
+
+      if RUBY_PATH.to_s !~ %r{^/System/Library/Frameworks/Ruby.framework/Versions/[12]\.[089]/usr/bin/ruby}
+        s << " => #{RUBY_PATH}"
+      end
+      s
+    end
+
+    def dump_verbose_config(f = $stdout)
+      dump_generic_verbose_config(f)
+      f.puts "System Ruby: #{describe_system_ruby}"
+      f.puts "OS X: #{MacOS.full_version}-#{kernel}"
+      f.puts "Xcode: #{xcode ? xcode : "N/A"}"
+      f.puts "CLT: #{clt ? clt : "N/A"}"
+      f.puts "X11: #{describe_xquartz}"
+      f.puts "MacPorts/Fink: #{macports_or_fink}" if macports_or_fink
+    end
+  end
+end
diff --git a/Library/Homebrew/extend/os/system_config.rb b/Library/Homebrew/extend/os/system_config.rb
new file mode 100644
index 0000000000..cf7b69cb68
--- /dev/null
+++ b/Library/Homebrew/extend/os/system_config.rb
@@ -0,0 +1,5 @@
+require "system_config"
+
+if OS.mac?
+  require "extend/os/mac/system_config"
+end
diff --git a/Library/Homebrew/system_config.rb b/Library/Homebrew/system_config.rb
new file mode 100644
index 0000000000..4f56addb59
--- /dev/null
+++ b/Library/Homebrew/system_config.rb
@@ -0,0 +1,144 @@
+require "hardware"
+require "software_spec"
+require "rexml/document"
+require "tap"
+require "development_tools"
+
+class SystemConfig
+  class << self
+    def llvm
+      @llvm ||= DevelopmentTools.llvm_build_version if DevelopmentTools.installed?
+    end
+
+    def gcc_42
+      @gcc_42 ||= DevelopmentTools.gcc_42_build_version if DevelopmentTools.installed?
+    end
+
+    def gcc_40
+      @gcc_40 ||= DevelopmentTools.gcc_40_build_version if DevelopmentTools.installed?
+    end
+
+    def clang
+      @clang ||= DevelopmentTools.clang_version if DevelopmentTools.installed?
+    end
+
+    def clang_build
+      @clang_build ||= DevelopmentTools.clang_build_version if DevelopmentTools.installed?
+    end
+
+    def head
+      Homebrew.git_head || "(none)"
+    end
+
+    def last_commit
+      Homebrew.git_last_commit || "never"
+    end
+
+    def origin
+      Homebrew.git_origin || "(none)"
+    end
+
+    def core_tap_head
+      CoreTap.instance.git_head || "(none)"
+    end
+
+    def core_tap_last_commit
+      CoreTap.instance.git_last_commit || "never"
+    end
+
+    def core_tap_origin
+      CoreTap.instance.remote || "(none)"
+    end
+
+    def describe_path(path)
+      return "N/A" if path.nil?
+      realpath = path.realpath
+      if realpath == path
+        path
+      else
+        "#{path} => #{realpath}"
+      end
+    end
+
+    def describe_perl
+      describe_path(which "perl")
+    end
+
+    def describe_python
+      python = which "python"
+      return "N/A" if python.nil?
+      python_binary = Utils.popen_read python, "-c", "import sys; sys.stdout.write(sys.executable)"
+      python_binary = Pathname.new(python_binary).realpath
+      if python == python_binary
+        python
+      else
+        "#{python} => #{python_binary}"
+      end
+    end
+
+    def describe_ruby
+      ruby = which "ruby"
+      return "N/A" if ruby.nil?
+      ruby_binary = Utils.popen_read ruby, "-rrbconfig", "-e", \
+        'include RbConfig;print"#{CONFIG["bindir"]}/#{CONFIG["ruby_install_name"]}#{CONFIG["EXEEXT"]}"'
+      ruby_binary = Pathname.new(ruby_binary).realpath
+      if ruby == ruby_binary
+        ruby
+      else
+        "#{ruby} => #{ruby_binary}"
+      end
+    end
+
+    def hardware
+      return if Hardware::CPU.type == :dunno
+      "CPU: #{Hardware.cores_as_words}-core #{Hardware::CPU.bits}-bit #{Hardware::CPU.family}"
+    end
+
+    def kernel
+      `uname -m`.chomp
+    end
+
+    def describe_java
+      # java_home doesn't exist on all OS Xs; it might be missing on older versions.
+      return "N/A" unless File.executable? "/usr/libexec/java_home"
+
+      java_xml = Utils.popen_read("/usr/libexec/java_home", "--xml", "--failfast")
+      return "N/A" unless $?.success?
+      javas = []
+      REXML::XPath.each(REXML::Document.new(java_xml), "//key[text()='JVMVersion']/following-sibling::string") do |item|
+        javas << item.text
+      end
+      javas.uniq.join(", ")
+    end
+
+    def dump_verbose_config(f = $stdout)
+      f.puts "HOMEBREW_VERSION: #{HOMEBREW_VERSION}"
+      f.puts "ORIGIN: #{origin}"
+      f.puts "HEAD: #{head}"
+      f.puts "Last commit: #{last_commit}"
+      if CoreTap.instance.installed?
+        f.puts "Core tap ORIGIN: #{core_tap_origin}"
+        f.puts "Core tap HEAD: #{core_tap_head}"
+        f.puts "Core tap last commit: #{core_tap_last_commit}"
+      else
+        f.puts "Core tap: N/A"
+      end
+      f.puts "HOMEBREW_PREFIX: #{HOMEBREW_PREFIX}"
+      f.puts "HOMEBREW_REPOSITORY: #{HOMEBREW_REPOSITORY}"
+      f.puts "HOMEBREW_CELLAR: #{HOMEBREW_CELLAR}"
+      f.puts "HOMEBREW_BOTTLE_DOMAIN: #{BottleSpecification::DEFAULT_DOMAIN}"
+      f.puts hardware if hardware
+      f.puts "GCC-4.0: build #{gcc_40}" if gcc_40
+      f.puts "GCC-4.2: build #{gcc_42}" if gcc_42
+      f.puts "LLVM-GCC: build #{llvm}"  if llvm
+      f.puts "Clang: #{clang ? "#{clang} build #{clang_build}" : "N/A"}"
+      f.puts "Perl: #{describe_perl}"
+      f.puts "Python: #{describe_python}"
+      f.puts "Ruby: #{describe_ruby}"
+      f.puts "Java: #{describe_java}"
+    end
+    alias_method :dump_generic_verbose_config, :dump_verbose_config
+  end
+end
+
+require "extend/os/system_config"
-- 
GitLab