xtool.git
Publicgit clone https://git.maproj.org/xtool.gitxtool.git / Sources / XKit / Utilities / ZIPCompressor.swift
ZIPCompressor.swift65 lines · 2.4 KiBRaw
| 1 | import Foundation |
| 2 | import Dependencies |
| 3 | |
| 4 | public struct ZIPCompressor: TestDependencyKey, Sendable { |
| 5 | public var compress: @Sendable ( |
| 6 | _ directory: URL, |
| 7 | _ progress: @escaping @Sendable (_ currentProgress: Double?) -> Void |
| 8 | ) async throws -> URL |
| 9 | |
| 10 | public var decompress: @Sendable ( |
| 11 | _ file: URL, |
| 12 | _ directory: URL, |
| 13 | _ progress: @escaping @Sendable (_ currentProgress: Double?) -> Void |
| 14 | ) async throws -> Void |
| 15 | |
| 16 | public init( |
| 17 | compress: @Sendable @escaping (URL, @Sendable @escaping (Double?) -> Void) async throws -> URL, |
| 18 | decompress: @Sendable @escaping (URL, URL, @Sendable @escaping (Double?) -> Void) async throws -> Void |
| 19 | ) { |
| 20 | self.decompress = decompress |
| 21 | self.compress = compress |
| 22 | } |
| 23 | |
| 24 | public static let testValue = ZIPCompressor( |
| 25 | compress: unimplemented(), |
| 26 | decompress: unimplemented() |
| 27 | ) |
| 28 | |
| 29 | /// Decompress the zipped ipa file |
| 30 | /// |
| 31 | /// - Parameter file: The `ipa` file to decompress. |
| 32 | /// - Parameter directory: The directory into which `ipa` should be decompressed. |
| 33 | /// - Parameter progress: A closure to which the callee can provide progress updates. |
| 34 | /// - term currentProgress: The current progress, or `nil` to indicate it is indeterminate. |
| 35 | public func decompress( |
| 36 | file: URL, |
| 37 | in directory: URL, |
| 38 | progress: @escaping @Sendable (_ currentProgress: Double?) -> Void |
| 39 | ) async throws { |
| 40 | try await decompress(file, directory, progress) |
| 41 | } |
| 42 | |
| 43 | // `compress` is required because the only way to upload symlinks via afc is by |
| 44 | // putting them in a zip archive (afc_make_symlink was disabled due to security or |
| 45 | // something) |
| 46 | |
| 47 | /// Compress the app before installation. |
| 48 | /// |
| 49 | /// - Parameter directory: The `Payload` directory which is to be compressed. |
| 50 | /// - Parameter progress: A closure to which the callee can provide progress updates. |
| 51 | /// - term currentProgress: The current progress, or `nil` to indicate it is indeterminate. |
| 52 | public func compress( |
| 53 | directory: URL, |
| 54 | progress: @escaping @Sendable (_ currentProgress: Double?) -> Void |
| 55 | ) async throws -> URL { |
| 56 | try await compress(directory, progress) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | extension DependencyValues { |
| 61 | public var zipCompressor: ZIPCompressor { |
| 62 | get { self[ZIPCompressor.self] } |
| 63 | set { self[ZIPCompressor.self] = newValue } |
| 64 | } |
| 65 | } |