|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See http://swift.org/LICENSE.txt for license information |
| 8 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import TSCBasic |
| 12 | +import TSCUtility |
| 13 | + |
| 14 | +/// A package reference. |
| 15 | +/// |
| 16 | +/// This represents a reference to a package containing its identity and location. |
| 17 | +public struct PackageReference: JSONMappable, JSONSerializable, Codable, CustomStringConvertible, Equatable, Hashable { |
| 18 | + public typealias PackageIdentity = String |
| 19 | + |
| 20 | + /// The kind of package reference. |
| 21 | + public enum Kind: String, Codable { |
| 22 | + /// A root package. |
| 23 | + case root |
| 24 | + |
| 25 | + /// A non-root local package. |
| 26 | + case local |
| 27 | + |
| 28 | + /// A remote package. |
| 29 | + case remote |
| 30 | + } |
| 31 | + |
| 32 | + /// Compute identity of a package given its URL. |
| 33 | + public static func computeIdentity(packageURL: String) -> String { |
| 34 | + return computeDefaultName(fromURL: packageURL).lowercased() |
| 35 | + } |
| 36 | + |
| 37 | + /// Compute the default name of a package given its URL. |
| 38 | + public static func computeDefaultName(fromURL url: String) -> String { |
| 39 | + #if os(Windows) |
| 40 | + let isSeparator : (Character) -> Bool = { $0 == "/" || $0 == "\\" } |
| 41 | + #else |
| 42 | + let isSeparator : (Character) -> Bool = { $0 == "/" } |
| 43 | + #endif |
| 44 | + |
| 45 | + // Get the last path component of the URL. |
| 46 | + // Drop the last character in case it's a trailing slash. |
| 47 | + var endIndex = url.endIndex |
| 48 | + if let lastCharacter = url.last, isSeparator(lastCharacter) { |
| 49 | + endIndex = url.index(before: endIndex) |
| 50 | + } |
| 51 | + |
| 52 | + let separatorIndex = url[..<endIndex].lastIndex(where: isSeparator) |
| 53 | + let startIndex = separatorIndex.map { url.index(after: $0) } ?? url.startIndex |
| 54 | + var lastComponent = url[startIndex..<endIndex] |
| 55 | + |
| 56 | + // Strip `.git` suffix if present. |
| 57 | + if lastComponent.hasSuffix(".git") { |
| 58 | + lastComponent = lastComponent.dropLast(4) |
| 59 | + } |
| 60 | + |
| 61 | + return String(lastComponent) |
| 62 | + } |
| 63 | + |
| 64 | + /// The identity of the package. |
| 65 | + public let identity: PackageIdentity |
| 66 | + |
| 67 | + /// The name of the package, if available. |
| 68 | + public var name: String { |
| 69 | + _name ?? Self.computeDefaultName(fromURL: path) |
| 70 | + } |
| 71 | + private let _name: String? |
| 72 | + |
| 73 | + /// The path of the package. |
| 74 | + /// |
| 75 | + /// This could be a remote repository, local repository or local package. |
| 76 | + public let path: String |
| 77 | + |
| 78 | + /// The kind of package: root, local, or remote. |
| 79 | + public let kind: Kind |
| 80 | + |
| 81 | + /// Create a package reference given its identity and repository. |
| 82 | + public init(identity: String, path: String, name: String? = nil, kind: Kind = .remote) { |
| 83 | + assert(identity == identity.lowercased(), "The identity is expected to be lowercased") |
| 84 | + self._name = name |
| 85 | + self.identity = identity |
| 86 | + self.path = path |
| 87 | + self.kind = kind |
| 88 | + } |
| 89 | + |
| 90 | + public static func ==(lhs: PackageReference, rhs: PackageReference) -> Bool { |
| 91 | + return lhs.identity == rhs.identity |
| 92 | + } |
| 93 | + |
| 94 | + public func hash(into hasher: inout Hasher) { |
| 95 | + hasher.combine(identity) |
| 96 | + } |
| 97 | + |
| 98 | + public init(json: JSON) throws { |
| 99 | + self._name = json.get("name") |
| 100 | + self.identity = try json.get("identity") |
| 101 | + self.path = try json.get("path") |
| 102 | + |
| 103 | + // Support previous version of PackageReference that contained an `isLocal` property. |
| 104 | + if let isLocal: Bool = json.get("isLocal") { |
| 105 | + kind = isLocal ? .local : .remote |
| 106 | + } else { |
| 107 | + kind = try Kind(rawValue: json.get("kind"))! |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public func toJSON() -> JSON { |
| 112 | + return .init([ |
| 113 | + "name": name.toJSON(), |
| 114 | + "identity": identity, |
| 115 | + "path": path, |
| 116 | + "kind": kind.rawValue, |
| 117 | + ]) |
| 118 | + } |
| 119 | + |
| 120 | + /// Create a new package reference object with the given name. |
| 121 | + public func with(newName: String) -> PackageReference { |
| 122 | + return PackageReference(identity: identity, path: path, name: newName, kind: kind) |
| 123 | + } |
| 124 | + |
| 125 | + public var description: String { |
| 126 | + return identity + (path.isEmpty ? "" : "[\(path)]") |
| 127 | + } |
| 128 | +} |
0 commit comments