Skip to content

Commit

Permalink
Merge pull request #16388 from Bo98/fast-xcode-version
Browse files Browse the repository at this point in the history
os/mac/xcode: add fast path for Xcode version detection
  • Loading branch information
Bo98 committed Dec 23, 2023
2 parents 8537849 + 04ad24e commit 3798cac
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions Library/Homebrew/os/mac/xcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,32 @@ def self.detect_version
# if return is used in the middle, which we do many times in here.
return if !MacOS::Xcode.installed? && !MacOS::CLT.installed?

%W[
#{prefix}/usr/bin/xcodebuild
#{which("xcodebuild")}
].uniq.each do |xcodebuild_path|
next unless File.executable? xcodebuild_path

xcodebuild_output = Utils.popen_read(xcodebuild_path, "-version")
next unless $CHILD_STATUS.success?

xcode_version = xcodebuild_output[/Xcode (\d+(\.\d+)*)/, 1]
return xcode_version if xcode_version

# Xcode 2.x's xcodebuild has a different version string
case xcodebuild_output[/DevToolsCore-(\d+\.\d)/, 1]
when "798.0" then return "2.5"
when "515.0" then return "2.0"
if MacOS::Xcode.installed?
# Fast path that will probably almost always work unless `xcode-select -p` is misconfigured
version_plist = T.must(prefix).parent/"version.plist"
if version_plist.file?
data = Plist.parse_xml(version_plist, marshal: false)
version = data["CFBundleShortVersionString"] if data
return version if version
end

%W[
#{prefix}/usr/bin/xcodebuild
#{which("xcodebuild")}
].uniq.each do |xcodebuild_path|
next unless File.executable? xcodebuild_path

xcodebuild_output = Utils.popen_read(xcodebuild_path, "-version")
next unless $CHILD_STATUS.success?

xcode_version = xcodebuild_output[/Xcode (\d+(\.\d+)*)/, 1]
return xcode_version if xcode_version

# Xcode 2.x's xcodebuild has a different version string
case xcodebuild_output[/DevToolsCore-(\d+\.\d)/, 1]
when "798.0" then return "2.5"
when "515.0" then return "2.0"
end
end
end

Expand Down

0 comments on commit 3798cac

Please sign in to comment.