-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathXcodeSelect.swift
More file actions
158 lines (141 loc) · 6.14 KB
/
XcodeSelect.swift
File metadata and controls
158 lines (141 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import Foundation
import PromiseKit
import Path
import Version
import Rainbow
public func selectXcode(shouldPrint: Bool, pathOrVersion: String, directory: Path, fallbackToInteractive: Bool = true) -> Promise<Void> {
firstly { () -> Promise<ProcessOutput> in
Current.shell.xcodeSelectPrintPath()
}
.then { output -> Promise<Void> in
if shouldPrint {
if output.out.isEmpty == false {
Current.logging.log(output.out)
Current.shell.exit(0)
return Promise.value(())
}
else {
Current.logging.log("No selected Xcode")
Current.shell.exit(0)
return Promise.value(())
}
}
let versionToSelect = pathOrVersion.isEmpty ? Version.fromXcodeVersionFile() : Version(xcodeVersion: pathOrVersion)
let installedXcodes = Current.files.installedXcodes(directory)
if let version = versionToSelect,
let installedXcode = installedXcodes.first(withVersion: version) {
let selectedInstalledXcodeVersion = installedXcodes.first { output.out.hasPrefix($0.path.string) }.map { $0.version }
if installedXcode.version == selectedInstalledXcodeVersion {
Current.logging.log("Xcode \(version) is already selected".green)
Current.shell.exit(0)
return Promise.value(())
}
return selectXcodeAtPath(installedXcode.path.string)
.done { output in
Current.logging.log("Selected \(output.out)".green)
Current.shell.exit(0)
}
}
else {
let pathToSelect = pathOrVersion.trimmingCharacters(in: .whitespacesAndNewlines)
let currentPath = output.out.trimmingCharacters(in: .whitespacesAndNewlines)
if pathToSelect == currentPath {
Current.logging.log("Xcode at path \(pathOrVersion) is already selected".green)
Current.shell.exit(0)
return Promise.value(())
}
let selectPromise = selectXcodeAtPath(pathToSelect)
.done { output in
Current.logging.log("Selected \(output.out)".green)
Current.shell.exit(0)
}
if fallbackToInteractive {
return selectPromise
.recover { _ in
selectXcodeInteractively(currentPath: output.out, directory: directory)
.done { output in
Current.logging.log("Selected \(output.out)".green)
Current.shell.exit(0)
}
}
} else {
return selectPromise
}
}
}
}
public func selectXcodeInteractively(currentPath: String, directory: Path, shouldRetry: Bool) -> Promise<ProcessOutput> {
if shouldRetry {
func selectWithRetry(currentPath: String) -> Promise<ProcessOutput> {
return firstly {
selectXcodeInteractively(currentPath: currentPath, directory: directory)
}
.recover { error throws -> Promise<ProcessOutput> in
guard case XcodeSelectError.invalidIndex = error else { throw error }
Current.logging.log("\(error.legibleLocalizedDescription)\n".red)
return selectWithRetry(currentPath: currentPath)
}
}
return selectWithRetry(currentPath: currentPath)
}
else {
return firstly {
selectXcodeInteractively(currentPath: currentPath, directory: directory)
}
}
}
public func chooseFromInstalledXcodesInteractively(currentPath: String, directory: Path) -> Promise<InstalledXcode> {
let sortedInstalledXcodes = Current.files.installedXcodes(directory).sorted { $0.version < $1.version }
Current.logging.log("Available Xcode versions:")
sortedInstalledXcodes
.enumerated()
.forEach { index, installedXcode in
var output = "\(index + 1)) \(installedXcode.version.appleDescriptionWithBuildIdentifier)"
if currentPath.hasPrefix(installedXcode.path.string) {
output += " (\("Selected".green))"
}
Current.logging.log(output)
}
let possibleSelectionNumberString = Current.shell.readLine(prompt: "Enter the number of the Xcode to select: ")
guard
let selectionNumberString = possibleSelectionNumberString,
let selectionNumber = Int(selectionNumberString),
sortedInstalledXcodes.indices.contains(selectionNumber - 1)
else {
let error = XcodeSelectError.invalidIndex(min: 1, max: sortedInstalledXcodes.count, given: possibleSelectionNumberString)
return Promise(error: error)
}
return Promise.value(sortedInstalledXcodes[selectionNumber - 1])
}
public func selectXcodeInteractively(currentPath: String, directory: Path) -> Promise<ProcessOutput> {
return chooseFromInstalledXcodesInteractively(currentPath: currentPath, directory: directory)
.map(\.path.string)
.then(selectXcodeAtPath)
}
public func selectXcodeAtPath(_ pathString: String) -> Promise<ProcessOutput> {
firstly {
guard Current.files.fileExists(atPath: pathString) else {
throw XcodeSelectError.invalidPath(pathString)
}
Current.logging.log("xcodes requires superuser privileges to select an Xcode")
return Current.shell.authenticateSudoer().asVoid()
}
.then {
Current.shell.xcodeSelectSwitch(path: pathString)
}
.then { _ in
Current.shell.xcodeSelectPrintPath()
}
}
public enum XcodeSelectError: LocalizedError {
case invalidPath(String)
case invalidIndex(min: Int, max: Int, given: String?)
public var errorDescription: String? {
switch self {
case .invalidPath(let pathString):
return "Not a valid Xcode path: \(pathString)"
case .invalidIndex(let min, let max, let given):
return "Not a valid number. Expecting a whole number between \(min)-\(max), but given \(given ?? "nothing")."
}
}
}