Files
darwin-apk/Sources/dpk-cli/Subcommands/DpkGraphCommand.swift

73 lines
2.7 KiB
Swift
Raw Normal View History

2024-11-10 17:51:53 +11:00
/*
2025-07-10 21:51:30 +10:00
* darwin-apk © 2024, 2025 Gay Pizza Specifications
2024-11-10 17:51:53 +11:00
* 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) {
do {
let localRepositories = try await ApkRepositoriesConfig()
2024-11-11 21:06:37 +11:00
2025-07-10 21:51:30 +10:00
var timerStart = DispatchTime.now()
2025-07-11 20:40:23 +10:00
let pkgIndex = try await ApkIndexReader.resolve(localRepositories.repositories, fetch: .lazy)
2025-07-10 21:51:30 +10:00
print("Index build took \(timerStart.distance(to: .now()).seconds) seconds")
2025-07-11 20:40:23 +10:00
try pkgIndex.description.write(to: URL(filePath: "packages.txt"), atomically: false, encoding: .utf8)
2024-11-11 21:06:37 +11:00
2025-07-10 21:51:30 +10:00
timerStart = DispatchTime.now()
2025-07-11 20:40:23 +10:00
let providerCache = ApkIndexProviderCache(index: pkgIndex)
2025-07-11 20:50:12 +10:00
var graph = ApkPackageGraph()
2025-07-11 20:40:23 +10:00
graph.buildGraphNode(index: pkgIndex, providers: providerCache)
2025-07-10 21:51:30 +10:00
print("Graph build took \(timerStart.distance(to: .now()).seconds) seconds")
2025-07-11 20:40:23 +10:00
try graph.shallowIsolates.map { pkgIndex.at(node: $0).nameDescription }.joined(separator: "\n")
2025-07-10 21:51:30 +10:00
.write(to: URL(filePath: "shallowIsolates.txt"), atomically: false, encoding: .utf8)
2025-07-11 20:40:23 +10:00
try graph.deepIsolates.map { pkgIndex.at(node: $0).nameDescription }.joined(separator: "\n")
2025-07-10 21:51:30 +10:00
.write(to: URL(filePath: "deepIsolates.txt"), atomically: false, encoding: .utf8)
2025-07-10 22:39:56 +10:00
timerStart = DispatchTime.now()
#if false
2024-11-11 23:08:01 +11:00
let sorted = try graph.parallelOrderSort()
2025-07-10 22:39:56 +10:00
print("Parallel sort took \(timerStart.distance(to: .now()).seconds) seconds")
2025-07-10 21:51:30 +10:00
if var out = TextFileWriter(URL(filePath: "sorted.txt")) {
for (i, set) in sorted.enumerated() {
2025-07-11 20:40:23 +10:00
print("\(i):", to: &out)
2025-07-10 21:51:30 +10:00
for item in set {
2025-07-11 20:40:23 +10:00
let pkg = pkgIndex.at(node: item)
print(" \(pkg.nameDescription)", to: &out)
2025-07-10 21:51:30 +10:00
}
2025-07-11 20:40:23 +10:00
print(to: &out)
2025-07-10 21:51:30 +10:00
}
}
2025-07-10 22:39:56 +10:00
#else
let sorted = try graph.orderSort()
print("Order sort took \(timerStart.distance(to: .now()).seconds) seconds")
2025-07-11 20:40:23 +10:00
try sorted.map { node in pkgIndex.at(node: node).nameDescription }.joined(separator: "\n")
2025-07-10 22:39:56 +10:00
.write(to: URL(filePath: "sorted.txt"), atomically: false, encoding: .utf8)
#endif
2024-11-11 23:08:01 +11:00
} catch {
fatalError(error.localizedDescription)
}
2025-07-10 21:51:30 +10:00
}
}
fileprivate extension DispatchTimeInterval {
var seconds: Double {
switch self {
case .seconds(let value): Double(value)
case .milliseconds(let value): Double(value) / 1_000
case .microseconds(let value): Double(value) / 1_000_000
case .nanoseconds(let value): Double(value) / 1_000_000_000
case .never: .infinity
2025-07-11 20:40:23 +10:00
@unknown default: fatalError("Unsupported")
2025-07-10 21:51:30 +10:00
}
}
}