first async refactor of fetch/index

This commit is contained in:
2024-11-17 02:31:44 +11:00
parent badc6dd39e
commit 8c7f0f23d5
13 changed files with 112 additions and 32 deletions

View File

@ -1,38 +0,0 @@
/*
* darwin-apk © 2024 Gay Pizza Specifications
* SPDX-License-Identifier: Apache-2.0
*/
import Foundation
import ArgumentParser
import darwin_apk
struct RepositoriesConfig {
let repositories: [ApkIndexRepository]
init() async throws(ExitCode) {
do {
self.repositories = try await Self.readConfig(name: "repositories").flatMap { repo in
Self.readConfig(name: "arch").map { arch in
ApkIndexRepository(name: repo, arch: arch)
}
}.reduce(into: []) { $0.append($1) }
} catch {
print("Failed to read repository configurations, \(error.localizedDescription)")
throw .failure
}
}
var localRepositories: [URL] {
self.repositories.map { repo in
URL(filePath: repo.localName, directoryHint: .notDirectory)
}
}
private static func readConfig(name: String)
-> AsyncFilterSequence<AsyncMapSequence<AsyncLineSequence<URL.AsyncBytes>, String>> {
return URL(filePath: name, directoryHint: .notDirectory).lines
.map { $0.trimmingCharacters(in: .whitespaces) }
.filter { !$0.isEmpty && $0.first != "#" } // Ignore empty & commented lines
}
}

View File

@ -41,10 +41,10 @@ struct DpkSearchCommand: AsyncParsableCommand {
let match: any PatternMatcher
match = try matcher.init(patterns: patterns, ignoreCase: !self.caseSensitive)
let localRepositories = try await RepositoriesConfig().localRepositories
let localRepositories = try await ApkRepositoriesConfig()
let index: ApkIndex
do {
index = ApkIndex.merge(try localRepositories.map(ApkIndex.init))
index = try await ApkIndex.resolve(localRepositories, fetch: .local)
} catch {
print("Failed to build package index: \(error.localizedDescription)")
throw .failure

View File

@ -13,11 +13,13 @@ struct DpkUpdateCommand: AsyncParsableCommand {
abstract: "Update the system package repositories.",
aliases: [ "u" ])
@Flag(help: "Index on-disk cache")
var lazyDownload: Bool = false
func run() async throws {
let repositories = try await ApkRepositoriesConfig().repositories
print("Updating package repositories")
let repositories = try await RepositoriesConfig().repositories
var updater = ApkIndexUpdater()
updater.repositories.append(contentsOf: repositories)
updater.update()
let index = try await ApkIndex.resolve(repositories, fetch: self.lazyDownload ? .lazy : .update)
print("Indexed \(index.packages.count) package(s)")
}
}