Implement dependency wrapping & version spec

This commit is contained in:
2024-11-10 03:30:55 +11:00
parent 941dfae317
commit 5e4cf1bbc9
8 changed files with 248 additions and 11 deletions

View File

@ -0,0 +1,22 @@
// SPDX-License-Identifier: Apache-2.0
import Foundation
struct ApkIndexDependency: ApkIndexRequirementRef {
let name: String
let versionSpec: ApkVersionSpecification
init(extract: String) throws(ApkRequirement.ParseError) {
(self.name, self.versionSpec) = try ApkRequirement.extract(blob: extract)
}
}
extension ApkIndexDependency: CustomStringConvertible {
var description: String {
switch self.versionSpec {
case .any: self.name
case .conflict: "!\(self.name)"
case .constraint(let op, let version): "\(self.name)\(op)\(version)"
}
}
}

View File

@ -0,0 +1,10 @@
// SPDX-License-Identifier: Apache-2.0
struct ApkIndexInstallIf: ApkIndexRequirementRef {
let name: String
let versionSpec: ApkVersionSpecification
init(extract: String) throws(ApkRequirement.ParseError) {
(self.name, self.versionSpec) = try ApkRequirement.extract(blob: extract)
}
}

View File

@ -2,7 +2,7 @@
import Foundation
struct ApkIndexPackage {
struct ApkIndexPackage: Hashable {
let indexChecksum: String //TODO: Decode cus why not
let name: String
let version: String
@ -17,9 +17,9 @@ struct ApkIndexPackage {
let buildTime: Date?
let commit: String?
let providerPriority: UInt16?
let dependencies: [String] //TODO: stuff
let provides: [String] //TODO: stuff
let installIf: [String] //TODO: stuff
let dependencies: [ApkIndexDependency]
let provides: [ApkIndexProvides]
let installIf: [ApkIndexInstallIf]
var downloadFilename: String { "\(self.name)-\(version).apk" }
@ -39,9 +39,9 @@ extension ApkIndexPackage {
var packageSize: UInt64? = nil
var installedSize: UInt64? = nil
var dependencies = [String]()
var provides = [String]()
var installIf = [String]()
var dependencies = [ApkIndexDependency]()
var provides = [ApkIndexProvides]()
var installIf = [ApkIndexInstallIf]()
// Optional fields
var architecture: String? = nil
@ -67,7 +67,8 @@ extension ApkIndexPackage {
case "A":
architecture = record.value
case "D":
dependencies = record.value.components(separatedBy: " ")
do { dependencies = try ApkIndexDependency.extract(record.value) }
catch { throw .badValue(key: record.key) }
case "C":
indexChecksum = record.value // base64-encoded SHA1 hash prefixed with "Q1"
case "S":
@ -81,9 +82,11 @@ extension ApkIndexPackage {
}
installedSize = value
case "p":
provides = record.value.components(separatedBy: " ")
do { provides = try ApkIndexProvides.extract(record.value) }
catch { throw .badValue(key: record.key) }
case "i":
installIf = record.value.components(separatedBy: " ")
do { installIf = try ApkIndexInstallIf.extract(record.value) }
catch { throw .badValue(key: record.key) }
case "o":
origin = record.value
case "m":

View File

@ -0,0 +1,9 @@
// SPDX-License-Identifier: Apache-2.0
struct ApkIndexProvides: ApkIndexRequirementRef {
let name: String
init(extract: String) throws(ApkRequirement.ParseError) {
(self.name, _) = try ApkRequirement.extract(blob: extract)
}
}

View File

@ -0,0 +1,30 @@
// SPDX-License-Identifier: Apache-2.0
protocol ApkIndexRequirementRef: Equatable, Hashable {
var name: String { get }
var invert: Bool { get }
init(extract: String) throws(ApkRequirement.ParseError)
func satisfied(by other: ApkIndexPackage) -> Bool
}
extension ApkIndexRequirementRef {
var invert: Bool { false }
func satisfied(by _: ApkIndexPackage) -> Bool { true }
static func == (lhs: Self, rhs: Self) -> Bool {
return !(lhs.name != rhs.name && !lhs.invert)
}
func hash(into hasher: inout Hasher) {
self.name.hash(into: &hasher)
}
static func extract<T: ApkIndexRequirementRef>(_ blob: String) throws(ApkRequirement.ParseError) -> [T] {
return try blob.components(separatedBy: " ")
.map { token throws(ApkRequirement.ParseError) in
try .init(extract: token)
}
}
}

View File

@ -47,7 +47,7 @@ public struct ApkIndexUpdater {
}
for package in index.packages {
print(package)
print("\(package.name):", package.dependencies)
}
}