From 82616301ce24fc37b8cf499f1d6fd4bc5c5cfd6b Mon Sep 17 00:00:00 2001 From: Mahmood Tahir Date: Sun, 30 Jan 2022 22:55:10 -0500 Subject: [PATCH] Do not fail if xcode is already installed --- .../ParsableArguments+InstallError.swift | 30 +++++++++++++++++++ Sources/xcodes/main.swift | 24 ++------------- 2 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 Sources/xcodes/ParsableArguments+InstallError.swift diff --git a/Sources/xcodes/ParsableArguments+InstallError.swift b/Sources/xcodes/ParsableArguments+InstallError.swift new file mode 100644 index 0000000..6186a70 --- /dev/null +++ b/Sources/xcodes/ParsableArguments+InstallError.swift @@ -0,0 +1,30 @@ +import ArgumentParser +import Foundation +import LegibleError +import PromiseKit +import XcodesKit +import Rainbow + +extension ParsableArguments { + static func processDownloadOrInstall(error: Error) -> Never { + var exitCode: ExitCode = .failure + switch error { + case Process.PMKError.execution(let process, let standardOutput, let standardError): + Current.logging.log(""" + Failed executing: `\(process)` (\(process.terminationStatus)) + \([standardOutput, standardError].compactMap { $0 }.joined(separator: "\n")) + """.red) + case let error as XcodeInstaller.Error: + if case .versionAlreadyInstalled = error { + Current.logging.log(error.legibleLocalizedDescription.green) + exitCode = .success + } else { + Current.logging.log(error.legibleLocalizedDescription.red) + } + default: + Current.logging.log(error.legibleLocalizedDescription.red) + } + + Self.exit(withError: exitCode) + } +} diff --git a/Sources/xcodes/main.swift b/Sources/xcodes/main.swift index 6e67a88..5e37158 100644 --- a/Sources/xcodes/main.swift +++ b/Sources/xcodes/main.swift @@ -138,17 +138,7 @@ struct Xcodes: ParsableCommand { installer.download(installation, dataSource: globalDataSource.dataSource, downloader: downloader, destinationDirectory: destination) .catch { error in - switch error { - case Process.PMKError.execution(let process, let standardOutput, let standardError): - Current.logging.log(""" - Failed executing: `\(process)` (\(process.terminationStatus)) - \([standardOutput, standardError].compactMap { $0 }.joined(separator: "\n")) - """.red) - default: - Current.logging.log(error.legibleLocalizedDescription.red) - } - - Install.exit(withError: ExitCode.failure) + Install.processDownloadOrInstall(error: error) } RunLoop.current.run() @@ -231,17 +221,7 @@ struct Xcodes: ParsableCommand { installer.install(installation, dataSource: globalDataSource.dataSource, downloader: downloader, destination: destination) .done { Install.exit() } .catch { error in - switch error { - case Process.PMKError.execution(let process, let standardOutput, let standardError): - Current.logging.log(""" - Failed executing: `\(process)` (\(process.terminationStatus)) - \([standardOutput, standardError].compactMap { $0 }.joined(separator: "\n")) - """.red) - default: - Current.logging.log(error.legibleLocalizedDescription.red) - } - - Install.exit(withError: ExitCode.failure) + Install.processDownloadOrInstall(error: error) } RunLoop.current.run()