Rescue graph building test into subcommand

This commit is contained in:
2024-11-23 21:02:52 +11:00
parent 28eb8cb588
commit 08c76c799c
5 changed files with 18 additions and 18 deletions

View File

@ -16,6 +16,7 @@ struct DarwinApkCLI: AsyncParsableCommand {
DpkUpdateCommand.self,
DpkUpgradeCommand.self,
DpkSearchCommand.self,
DpkInfoCommand.self
DpkInfoCommand.self,
DpkGraphCommand.self
])
}

View File

@ -0,0 +1,32 @@
/*
* darwin-apk © 2024 Gay Pizza Specifications
* SPDX-License-Identifier: Apache-2.0
*/
import Foundation
import ArgumentParser
import darwin_apk
struct DpkGraphCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(commandName: "graph")
func run() async throws(ExitCode) {
let graph: ApkPackageGraph
do {
let localRepositories = try await ApkRepositoriesConfig()
graph = ApkPackageGraph(index: try await ApkIndexReader.resolve(localRepositories.repositories, fetch: .lazy))
graph.buildGraphNode()
try graph.pkgIndex.description.write(to: URL(filePath: "packages.txt"), atomically: false, encoding: .utf8)
} catch {
fatalError(error.localizedDescription)
}
if var out = TextFileWriter(URL(filePath: "shallowIsolates.txt")) {
for node in graph.shallowIsolates { print(node, to: &out) }
}
if var out = TextFileWriter(URL(filePath: "deepIsolates.txt")) {
for node in graph.deepIsolates { print(node, to: &out) }
}
}
}

View File

@ -0,0 +1,29 @@
/*
* darwin-apk © 2024 Gay Pizza Specifications
* SPDX-License-Identifier: Apache-2.0
*/
import Foundation
import Darwin
struct TextFileWriter: TextOutputStream {
private var _hnd: FileHandle
init?(_ to: URL) {
let file = open(to.path(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, 0o644)
guard file >= 0 else {
return nil
}
self._hnd = FileHandle(fileDescriptor: file, closeOnDealloc: true)
}
mutating func write(_ string: String) {
if let data = string.data(using: .utf8) {
try? self._hnd.write(contentsOf: data)
}
}
mutating func close() throws {
try self._hnd.close()
}
}