|
| 1 | +// Copyright Dave Verwer, Sven A. Schmidt, and other contributors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import Foundation |
| 16 | + |
| 17 | +public struct ResolvedDependency: Codable, Equatable { |
| 18 | + public var packageName: String |
| 19 | + public var repositoryURL: String |
| 20 | + |
| 21 | + public init(packageName: String, repositoryURL: String) { |
| 22 | + self.packageName = packageName |
| 23 | + self.repositoryURL = repositoryURL |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +public protocol FileManager { |
| 29 | + func contents(atPath: String) -> Data? |
| 30 | + func fileExists(atPath: String) -> Bool |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +extension Foundation.FileManager: FileManager { } |
| 35 | + |
| 36 | + |
| 37 | +public func getResolvedDependencies(_ fileManager: FileManager = Foundation.FileManager.default, at path: String) -> [ResolvedDependency]? { |
| 38 | + struct PackageResolved: Decodable { |
| 39 | + // object: |
| 40 | + // pins: |
| 41 | + // - package: String |
| 42 | + // repositoryURL: URL |
| 43 | + // - ... |
| 44 | + // version: 1 |
| 45 | + struct V1: Decodable { |
| 46 | + var object: Object |
| 47 | + |
| 48 | + struct Object: Decodable { |
| 49 | + var pins: [Pin] |
| 50 | + |
| 51 | + struct Pin: Decodable { |
| 52 | + var package: String |
| 53 | + var repositoryURL: URL |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // object: |
| 59 | + // pins: |
| 60 | + // - identity: String |
| 61 | + // location: URL |
| 62 | + // - ... |
| 63 | + // version: 1 |
| 64 | + struct V2: Decodable { |
| 65 | + var pins: [Pin] |
| 66 | + |
| 67 | + struct Pin: Decodable { |
| 68 | + var identity: String |
| 69 | + var location: URL |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + let filePath = URL(fileURLWithPath: path) |
| 75 | + .appendingPathComponent("Package.resolved").path |
| 76 | + |
| 77 | + guard fileManager.fileExists(atPath: filePath), |
| 78 | + let json = fileManager.contents(atPath: filePath) else { return nil } |
| 79 | + |
| 80 | + if let packageResolvedV2 = try? JSONDecoder() |
| 81 | + .decode(PackageResolved.V2.self, from: json) { |
| 82 | + return packageResolvedV2.pins.map { |
| 83 | + ResolvedDependency( |
| 84 | + packageName: $0.identity, |
| 85 | + repositoryURL: $0.location.absoluteString |
| 86 | + ) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if let packageResolvedV1 = try? JSONDecoder() |
| 91 | + .decode(PackageResolved.V1.self, from: json) { |
| 92 | + return packageResolvedV1.object.pins.map { |
| 93 | + ResolvedDependency( |
| 94 | + packageName: $0.package, |
| 95 | + repositoryURL: $0.repositoryURL.absoluteString |
| 96 | + ) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
0 commit comments