Skip to content

Handle helper installation errors #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions Xcodes/Backend/AppState+Install.swift
Original file line number Diff line number Diff line change
Expand Up @@ -303,32 +303,29 @@ extension AppState {
}

func enableDeveloperMode() -> AnyPublisher<Void, Error> {
if helperInstallState == .notInstalled {
installHelper()
}

return Current.helper.devToolsSecurityEnable()
installHelperIfNecessary()
.flatMap {
Current.helper.devToolsSecurityEnable()
}
.flatMap {
Current.helper.addStaffToDevelopersGroup()
}
.eraseToAnyPublisher()
}

func approveLicense(for xcode: InstalledXcode) -> AnyPublisher<Void, Error> {
if helperInstallState == .notInstalled {
installHelper()
}

return Current.helper.acceptXcodeLicense(xcode.path.string)
installHelperIfNecessary()
.flatMap {
Current.helper.acceptXcodeLicense(xcode.path.string)
}
.eraseToAnyPublisher()
}

func installComponents(for xcode: InstalledXcode) -> AnyPublisher<Void, Swift.Error> {
if helperInstallState == .notInstalled {
installHelper()
}

return Current.helper.runFirstLaunch(xcode.path.string)
installHelperIfNecessary()
.flatMap {
Current.helper.runFirstLaunch(xcode.path.string)
}
.flatMap {
Current.shell.getUserCacheDir().map { $0.out }
.combineLatest(
Expand Down
35 changes: 27 additions & 8 deletions Xcodes/Backend/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,29 @@ class AppState: ObservableObject {

// MARK: - Helper

func installHelper() {
Current.helper.install()
checkIfHelperIsInstalled()
func installHelperIfNecessary() {
installHelperIfNecessary()
.sink(
receiveCompletion: { [unowned self] completion in
if case let .failure(error) = completion {
self.error = error
}
},
receiveValue: {}
)
.store(in: &cancellables)
}

func installHelperIfNecessary() -> AnyPublisher<Void, Error> {
Result {
if helperInstallState == .notInstalled {
try Current.helper.install()
checkIfHelperIsInstalled()
}
}
.publisher
.subscribe(on: DispatchQueue.main)
.eraseToAnyPublisher()
}

private func checkIfHelperIsInstalled() {
Expand Down Expand Up @@ -320,16 +340,15 @@ class AppState: ObservableObject {
}

func select(id: Xcode.ID) {
if helperInstallState == .notInstalled {
installHelper()
}

guard
let installedXcode = Current.files.installedXcodes(Path.root/"Applications").first(where: { $0.version == id }),
selectPublisher == nil
else { return }

selectPublisher = HelperClient().switchXcodePath(installedXcode.path.string)
selectPublisher = installHelperIfNecessary()
.flatMap {
Current.helper.switchXcodePath(installedXcode.path.string)
}
.flatMap { [unowned self] _ in
self.updateSelectedXcodePath()
}
Expand Down
2 changes: 1 addition & 1 deletion Xcodes/Backend/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public struct Defaults {

private let helperClient = HelperClient()
public struct Helper {
var install: () -> Void = helperClient.install
var install: () throws -> Void = helperClient.install
var checkIfLatestHelperIsInstalled: () -> AnyPublisher<Bool, Never> = helperClient.checkIfLatestHelperIsInstalled
var getVersion: () -> AnyPublisher<String, Error> = helperClient.getVersion
var switchXcodePath: (_ absolutePath: String) -> AnyPublisher<Void, Error> = helperClient.switchXcodePath
Expand Down
10 changes: 8 additions & 2 deletions Xcodes/Backend/HelperClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ final class HelperClient {
// MARK: - Install
// From https://github.com/securing/SimpleXPCApp/

func install() {
func install() throws {
Logger.helperClient.info(#function)

var authItem = kSMRightBlessPrivilegedHelper.withCString { name in
Expand All @@ -329,13 +329,19 @@ final class HelperClient {
Logger.helperClient.info("\(#function): Finished installation")
} catch {
Logger.helperClient.error("\(#function): \(error.localizedDescription)")

throw error
}
}

private func executeAuthorizationFunction(_ authorizationFunction: () -> (OSStatus) ) throws {
let osStatus = authorizationFunction()
guard osStatus == errAuthorizationSuccess else {
throw HelperClientError.message(String(describing: SecCopyErrorMessageString(osStatus, nil)))
if let message = SecCopyErrorMessageString(osStatus, nil) {
throw HelperClientError.message(String(message as NSString))
} else {
throw HelperClientError.message("Unknown error")
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Xcodes/Frontend/Preferences/AdvancedPreferencePane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct AdvancedPreferencePane: View {
HStack {
Text("Helper is not installed")
Button("Install helper") {
appState.installHelper()
appState.installHelperIfNecessary()
}
}
}
Expand Down