Skip to content

Commit d861182

Browse files
committed
Extract PackageReference into separate file
1 parent d68bede commit d861182

File tree

2 files changed

+128
-115
lines changed

2 files changed

+128
-115
lines changed

Sources/PackageModel/Package.swift

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -113,118 +113,3 @@ extension Package: CustomStringConvertible {
113113
extension Package: ObjectIdentifierProtocol {
114114
}
115115

116-
/// A package reference.
117-
///
118-
/// This represents a reference to a package containing its identity and location.
119-
public struct PackageReference: JSONMappable, JSONSerializable, Codable, CustomStringConvertible, Equatable, Hashable {
120-
public typealias PackageIdentity = String
121-
122-
/// The kind of package reference.
123-
public enum Kind: String, Codable {
124-
/// A root package.
125-
case root
126-
127-
/// A non-root local package.
128-
case local
129-
130-
/// A remote package.
131-
case remote
132-
}
133-
134-
/// Compute identity of a package given its URL.
135-
public static func computeIdentity(packageURL: String) -> String {
136-
return computeDefaultName(fromURL: packageURL).lowercased()
137-
}
138-
139-
/// Compute the default name of a package given its URL.
140-
public static func computeDefaultName(fromURL url: String) -> String {
141-
#if os(Windows)
142-
let isSeparator : (Character) -> Bool = { $0 == "/" || $0 == "\\" }
143-
#else
144-
let isSeparator : (Character) -> Bool = { $0 == "/" }
145-
#endif
146-
147-
// Get the last path component of the URL.
148-
// Drop the last character in case it's a trailing slash.
149-
var endIndex = url.endIndex
150-
if let lastCharacter = url.last, isSeparator(lastCharacter) {
151-
endIndex = url.index(before: endIndex)
152-
}
153-
154-
let separatorIndex = url[..<endIndex].lastIndex(where: isSeparator)
155-
let startIndex = separatorIndex.map { url.index(after: $0) } ?? url.startIndex
156-
var lastComponent = url[startIndex..<endIndex]
157-
158-
// Strip `.git` suffix if present.
159-
if lastComponent.hasSuffix(".git") {
160-
lastComponent = lastComponent.dropLast(4)
161-
}
162-
163-
return String(lastComponent)
164-
}
165-
166-
/// The identity of the package.
167-
public let identity: PackageIdentity
168-
169-
/// The name of the package, if available.
170-
public var name: String {
171-
_name ?? Self.computeDefaultName(fromURL: path)
172-
}
173-
private let _name: String?
174-
175-
/// The path of the package.
176-
///
177-
/// This could be a remote repository, local repository or local package.
178-
public let path: String
179-
180-
/// The kind of package: root, local, or remote.
181-
public let kind: Kind
182-
183-
/// Create a package reference given its identity and repository.
184-
public init(identity: String, path: String, name: String? = nil, kind: Kind = .remote) {
185-
assert(identity == identity.lowercased(), "The identity is expected to be lowercased")
186-
self._name = name
187-
self.identity = identity
188-
self.path = path
189-
self.kind = kind
190-
}
191-
192-
public static func ==(lhs: PackageReference, rhs: PackageReference) -> Bool {
193-
return lhs.identity == rhs.identity
194-
}
195-
196-
public func hash(into hasher: inout Hasher) {
197-
hasher.combine(identity)
198-
}
199-
200-
public init(json: JSON) throws {
201-
self._name = json.get("name")
202-
self.identity = try json.get("identity")
203-
self.path = try json.get("path")
204-
205-
// Support previous version of PackageReference that contained an `isLocal` property.
206-
if let isLocal: Bool = json.get("isLocal") {
207-
kind = isLocal ? .local : .remote
208-
} else {
209-
kind = try Kind(rawValue: json.get("kind"))!
210-
}
211-
}
212-
213-
public func toJSON() -> JSON {
214-
return .init([
215-
"name": name.toJSON(),
216-
"identity": identity,
217-
"path": path,
218-
"kind": kind.rawValue,
219-
])
220-
}
221-
222-
/// Create a new package reference object with the given name.
223-
public func with(newName: String) -> PackageReference {
224-
return PackageReference(identity: identity, path: path, name: newName, kind: kind)
225-
}
226-
227-
public var description: String {
228-
return identity + (path.isEmpty ? "" : "[\(path)]")
229-
}
230-
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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

Comments
 (0)