Skip to content

Commit c3712ce

Browse files
committed
Exit immediately if trying to select currently selected xcode
If the currently requested xcode is already selected, there is no need to prompt the user to give sudo permissions and re-select the path. We can just avoid the step and exit early.
1 parent bfefa45 commit c3712ce

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

Sources/XcodesKit/XcodeSelect.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,34 @@ public func selectXcode(shouldPrint: Bool, pathOrVersion: String, directory: Pat
2222
}
2323
}
2424

25+
let installedXcodes = Current.files.installedXcodes(directory)
2526
if let version = Version(xcodeVersion: pathOrVersion),
26-
let installedXcode = Current.files.installedXcodes(directory).first(withVersion: version) {
27+
let installedXcode = installedXcodes.first(withVersion: version) {
28+
let selectedInstalledXcodeVersion = installedXcodes.first { output.out.hasPrefix($0.path.string) }.map { $0.version }
29+
if installedXcode.version == selectedInstalledXcodeVersion {
30+
Current.logging.log("Xcode \(version) is already selected")
31+
Current.shell.exit(0)
32+
return Promise.value(())
33+
}
34+
2735
return selectXcodeAtPath(installedXcode.path.string)
2836
.done { output in
2937
Current.logging.log("Selected \(output.out)".green)
3038
Current.shell.exit(0)
3139
}
3240
}
3341
else {
34-
return selectXcodeAtPath(pathOrVersion)
42+
// remove any whitespaces
43+
let pathToSelect = pathOrVersion.trimmingCharacters(in: .whitespacesAndNewlines)
44+
let currentPath = output.out.trimmingCharacters(in: .whitespacesAndNewlines)
45+
46+
if pathToSelect == currentPath {
47+
Current.logging.log("Xcode at path \(pathOrVersion) is already selected")
48+
Current.shell.exit(0)
49+
return Promise.value(())
50+
}
51+
52+
return selectXcodeAtPath(pathToSelect)
3553
.done { output in
3654
Current.logging.log("Selected \(output.out)".green)
3755
Current.shell.exit(0)
@@ -94,7 +112,7 @@ public func chooseFromInstalledXcodesInteractively(currentPath: String, director
94112
return Promise(error: error)
95113
}
96114

97-
return Promise.value(sortedInstalledXcodes[selectionNumber - 1])
115+
return Promise.value(sortedInstalledXcodes[selectionNumber - 1])
98116
}
99117

100118
public func selectXcodeInteractively(currentPath: String, directory: Path) -> Promise<ProcessOutput> {

0 commit comments

Comments
 (0)