From eeb8a075aeb83e6d2bece2d09f01394f147a4b00 Mon Sep 17 00:00:00 2001
From: Aoi Okawa <49633549+aomathwift@users.noreply.github.com>
Date: Wed, 16 Jun 2021 02:08:38 +0900
Subject: [PATCH] improve version fetching command performance by reading
 Xcode's version.plist instead of using the `xcodebuild` command (#427)

* Use the version.plist instead of the xcodebuild command to detect Xcode version in fetch_version

* Update spec
---
 lib/xcode/install.rb   | 5 ++---
 spec/installed_spec.rb | 4 ++--
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/lib/xcode/install.rb b/lib/xcode/install.rb
index 6b40a303..0eefb978 100644
--- a/lib/xcode/install.rb
+++ b/lib/xcode/install.rb
@@ -710,11 +710,10 @@ def install_components
       `touch #{cache_dir}com.apple.dt.Xcode.InstallCheckCache_#{osx_build_version}_#{tools_version}`
     end
 
-    # This method might take a few ms, this could be improved by implementing https://github.com/KrauseFx/xcode-install/issues/273
     def fetch_version
-      output = `DEVELOPER_DIR='' "#{@path}/Contents/Developer/usr/bin/xcodebuild" -version`
+      output = `/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "#{@path}/Contents/version.plist"`
       return '0.0' if output.nil? || output.empty? # ¯\_(ツ)_/¯
-      output.split("\n").first.split(' ')[1]
+      output.sub("\n", '')
     end
 
     def verify_integrity
diff --git a/spec/installed_spec.rb b/spec/installed_spec.rb
index 1c02afd3..3b6d4427 100644
--- a/spec/installed_spec.rb
+++ b/spec/installed_spec.rb
@@ -5,13 +5,13 @@ module XcodeInstall
 
   describe InstalledXcode do
     it 'finds the current Xcode version with whitespace chars' do
-      InstalledXcode.any_instance.expects(:`).with("DEVELOPER_DIR='' \"#{xcode_path}/Contents/Developer/usr/bin/xcodebuild\" -version").returns("Xcode 6.3.1\nBuild version 6D1002")
+      InstalledXcode.any_instance.expects(:`).with("/usr/libexec/PlistBuddy -c \"Print :CFBundleShortVersionString\" \"#{xcode_path}/Contents/version.plist\"").returns('6.3.1')
       installed = InstalledXcode.new(xcode_path)
       installed.version.should == '6.3.1'
     end
 
     it 'is robust against broken Xcode installations' do
-      InstalledXcode.any_instance.expects(:`).with("DEVELOPER_DIR='' \"#{xcode_path}/Contents/Developer/usr/bin/xcodebuild\" -version").returns(nil)
+      InstalledXcode.any_instance.expects(:`).with("/usr/libexec/PlistBuddy -c \"Print :CFBundleShortVersionString\" \"#{xcode_path}/Contents/version.plist\"").returns(nil)
       installed = InstalledXcode.new(xcode_path)
       installed.version.should == '0.0'
     end