-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ First steps in rewriting of package loading func
- Loading branch information
Showing
12 changed files
with
186 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// Package Loading Error.swift | ||
// Cork | ||
// | ||
// Created by David Bureš on 10.11.2024. | ||
// | ||
|
||
import Foundation | ||
|
||
enum PackageLoadingError: LocalizedError | ||
{ | ||
/// When attempting to get the list of raw URLs from the folder containing the packages, the function for loading packages returned nil, therefore, an error occured | ||
case couldNotReadContentsOfParentFolder(failureReason: String) | ||
case failedWhileLoadingPackages(failureReason: String?) | ||
case failedWhileLoadingCertainPackage(String, URL, failureReason: String) | ||
case packageDoesNotHaveAnyVersionsInstalled(String) | ||
case packageIsNotAFolder(String, URL) | ||
|
||
var errorDescription: String? | ||
{ | ||
switch self | ||
{ | ||
case .couldNotReadContentsOfParentFolder(let failureReason): | ||
return String(localized: "error.package-loading.could-not-read-contents-of-parent-folder.\(failureReason)") | ||
case .failedWhileLoadingPackages(let failureReason): | ||
if let failureReason | ||
{ | ||
return String(localized: "error.package-loading.could-not-load-packages.\(failureReason)") | ||
} | ||
else | ||
{ | ||
return String(localized: "error.package-loading.could-not-load-packages") | ||
} | ||
case .failedWhileLoadingCertainPackage(let string, let uRL, let failureReason): | ||
return String(localized: "error.package-loading.could-not-load-\(string)-at-\(uRL.absoluteString)-because-\(failureReason)", comment: "Couldn't load package (package name) at (package URL) because (failure reason)") | ||
case .packageDoesNotHaveAnyVersionsInstalled(let string): | ||
return String(localized: "error.package-loading.\(string)-does-not-have-any-versions-installed") | ||
case .packageIsNotAFolder(let string, _): | ||
return String(localized: "error.package-loading.\(string)-not-a-folder", comment: "Package folder in this context means a folder that encloses package versions. Every package has its own folder, and this error occurs when the provided URL does not point to a folder that encloses package versions") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
Cork/Logic/Package Loading/Load Up Installed Packages.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// Load Up Installed Packages.swift | ||
// Cork | ||
// | ||
// Created by David Bureš on 10.11.2024. | ||
// | ||
|
||
import CorkShared | ||
import Foundation | ||
|
||
extension BrewDataStorage | ||
{ | ||
/// Parent function for loading installed packages from disk | ||
/// Abstracts away the function ``loadInstalledPackagesFromFolder(packageTypeToLoad:)``, transforming errors thrown by ``loadInstalledPackagesFromFolder(packageTypeToLoad:)`` into displayable errors | ||
/// - Parameters: | ||
/// - packageTypeToLoad: Which ``PackageType`` to load | ||
/// - appState: ``AppState`` used to display loading errors | ||
/// - Returns: A set of loaded ``BrewPackage``s for the specified ``PackageType`` | ||
func loadInstalledPackages( | ||
packageTypeToLoad: PackageType, appState: AppState | ||
) async -> Set<BrewPackage>? | ||
{ | ||
/// Start tracking when loading started | ||
let timeLoadingStarted: Date = .now | ||
AppConstants.shared.logger.debug( | ||
"Started \(packageTypeToLoad.rawValue, privacy: .public) loading task at \(timeLoadingStarted, privacy: .public)" | ||
) | ||
|
||
/// Calculate how long loading took | ||
defer | ||
{ | ||
AppConstants.shared.logger.debug("Finished \(packageTypeToLoad.rawValue, privacy: .public). Took \(timeLoadingStarted.timeIntervalSince(.now), privacy: .public)") | ||
} | ||
|
||
do | ||
{ | ||
return try await self.loadInstalledPackagesFromFolder( | ||
packageTypeToLoad: packageTypeToLoad) | ||
} | ||
catch let packageLoadingError | ||
{ | ||
switch packageLoadingError | ||
{ | ||
case .couldNotReadContentsOfParentFolder(let loadingError): | ||
appState.showAlert(errorToShow: .couldNotGetContentsOfPackageFolder(loadingError)) | ||
case .failedWhileLoadingPackages: | ||
appState.showAlert( | ||
errorToShow: .couldNotLoadAnyPackages(packageLoadingError)) | ||
case .failedWhileLoadingCertainPackage( | ||
let offendingPackage, let offendingPackageURL, let failureReason | ||
): | ||
appState.showAlert( | ||
errorToShow: .couldNotLoadCertainPackage( | ||
offendingPackage, offendingPackageURL, | ||
failureReason: failureReason | ||
)) | ||
case .packageDoesNotHaveAnyVersionsInstalled(let offendingPackage): | ||
appState.showAlert( | ||
errorToShow: .installedPackageHasNoVersions( | ||
corruptedPackageName: offendingPackage)) | ||
case .packageIsNotAFolder(let offendingFile, let offendingFileURL): | ||
appState.showAlert( | ||
errorToShow: .installedPackageIsNotAFolder( | ||
itemName: offendingFile, itemURL: offendingFileURL | ||
)) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
} | ||
|
||
private extension BrewDataStorage | ||
{ | ||
/// Load packages from disk, and convert them into ``BrewPackage``s | ||
func loadInstalledPackagesFromFolder( | ||
packageTypeToLoad: PackageType | ||
) async throws(PackageLoadingError) -> Set<BrewPackage>? | ||
{ | ||
do | ||
{ | ||
let urlsInParentFolder: [URL] = try getContentsOfFolder(targetFolder: packageTypeToLoad.parentFolder, options: [.skipsHiddenFiles]) | ||
|
||
AppConstants.shared.logger.debug("Loaded contents of folder: \(urlsInParentFolder)") | ||
|
||
return nil | ||
} | ||
catch let parentFolderReadingError | ||
{ | ||
AppConstants.shared.logger.error("Couldn't get contents of folder \(packageTypeToLoad.parentFolder, privacy: .public)") | ||
|
||
throw .couldNotReadContentsOfParentFolder(failureReason: parentFolderReadingError.localizedDescription) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters