Skip to content

Commit 41e1fb3

Browse files
committed
Move PackageReference to PackageModel
1 parent 5449a25 commit 41e1fb3

File tree

5 files changed

+80
-75
lines changed

5 files changed

+80
-75
lines changed

Sources/PackageGraph/RepositoryPackageContainerProvider.swift

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -98,88 +98,14 @@ enum RepositoryPackageResolutionError: Swift.Error {
9898
case unavailableRepository
9999
}
100100

101-
/// A package reference.
102-
///
103-
/// This represents a reference to a package containing its identity and location.
104-
public struct PackageReference: PackageContainerIdentifier, JSONMappable, JSONSerializable, CustomStringConvertible {
105-
106-
/// Compute identity of a package given its URL.
107-
public static func computeIdentity(packageURL: String) -> String {
108-
// Get the last path component of the URL.
109-
var lastComponent = packageURL.split(separator: "/", omittingEmptySubsequences: true).last!
110-
111-
// Strip `.git` suffix if present.
112-
if lastComponent.hasSuffix(".git") {
113-
lastComponent = lastComponent.dropLast(4)
114-
}
115-
116-
return lastComponent.lowercased()
117-
}
118-
119-
/// The identity of the package.
120-
public let identity: String
121-
122-
/// The name of the package, if available.
123-
public let name: String?
124-
101+
extension PackageReference: PackageContainerIdentifier {
125102
/// The repository of the package.
126103
///
127104
/// This should only be accessed when the reference is not local.
128105
public var repository: RepositorySpecifier {
129106
precondition(!isLocal)
130107
return RepositorySpecifier(url: path)
131108
}
132-
133-
/// The path of the package.
134-
///
135-
/// This could be a remote repository, local repository or local package.
136-
public let path: String
137-
138-
/// The package reference is a local package, i.e., it does not reference
139-
/// a git repository.
140-
public let isLocal: Bool
141-
142-
/// Create a package reference given its identity and repository.
143-
public init(identity: String, path: String, name: String? = nil, isLocal: Bool = false) {
144-
assert(identity == identity.lowercased(), "The identity is expected to be lowercased")
145-
self.name = name
146-
self.identity = identity
147-
self.path = path
148-
self.isLocal = isLocal
149-
}
150-
151-
public static func ==(lhs: PackageReference, rhs: PackageReference) -> Bool {
152-
return lhs.identity == rhs.identity
153-
}
154-
155-
public var hashValue: Int {
156-
return identity.hashValue
157-
}
158-
159-
public init(json: JSON) throws {
160-
self.name = json.get("name")
161-
self.identity = try json.get("identity")
162-
self.path = try json.get("path")
163-
self.isLocal = try json.get("isLocal")
164-
}
165-
166-
public func toJSON() -> JSON {
167-
return .init([
168-
"name": name.toJSON(),
169-
"identity": identity,
170-
"path": path,
171-
"isLocal": isLocal,
172-
])
173-
}
174-
175-
/// Create a new package reference object with the given name.
176-
public func with(newName: String) -> PackageReference {
177-
return PackageReference(identity: identity, path: path, name: newName, isLocal: isLocal)
178-
}
179-
180-
public var description: String {
181-
return identity + "[\(path)]"
182-
}
183109
}
184110

185111
public typealias RepositoryPackageConstraint = PackageContainerConstraint<PackageReference>

Sources/PackageModel/Package.swift

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,79 @@ extension Package: Hashable, Equatable {
116116
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
117117
}
118118
}
119+
120+
/// A package reference.
121+
///
122+
/// This represents a reference to a package containing its identity and location.
123+
public struct PackageReference: JSONMappable, JSONSerializable, CustomStringConvertible {
124+
125+
/// Compute identity of a package given its URL.
126+
public static func computeIdentity(packageURL: String) -> String {
127+
// Get the last path component of the URL.
128+
var lastComponent = packageURL.split(separator: "/", omittingEmptySubsequences: true).last!
129+
130+
// Strip `.git` suffix if present.
131+
if lastComponent.hasSuffix(".git") {
132+
lastComponent = lastComponent.dropLast(4)
133+
}
134+
135+
return lastComponent.lowercased()
136+
}
137+
138+
/// The identity of the package.
139+
public let identity: String
140+
141+
/// The name of the package, if available.
142+
public let name: String?
143+
144+
/// The path of the package.
145+
///
146+
/// This could be a remote repository, local repository or local package.
147+
public let path: String
148+
149+
/// The package reference is a local package, i.e., it does not reference
150+
/// a git repository.
151+
public let isLocal: Bool
152+
153+
/// Create a package reference given its identity and repository.
154+
public init(identity: String, path: String, name: String? = nil, isLocal: Bool = false) {
155+
assert(identity == identity.lowercased(), "The identity is expected to be lowercased")
156+
self.name = name
157+
self.identity = identity
158+
self.path = path
159+
self.isLocal = isLocal
160+
}
161+
162+
public static func ==(lhs: PackageReference, rhs: PackageReference) -> Bool {
163+
return lhs.identity == rhs.identity
164+
}
165+
166+
public var hashValue: Int {
167+
return identity.hashValue
168+
}
169+
170+
public init(json: JSON) throws {
171+
self.name = json.get("name")
172+
self.identity = try json.get("identity")
173+
self.path = try json.get("path")
174+
self.isLocal = try json.get("isLocal")
175+
}
176+
177+
public func toJSON() -> JSON {
178+
return .init([
179+
"name": name.toJSON(),
180+
"identity": identity,
181+
"path": path,
182+
"isLocal": isLocal,
183+
])
184+
}
185+
186+
/// Create a new package reference object with the given name.
187+
public func with(newName: String) -> PackageReference {
188+
return PackageReference(identity: identity, path: path, name: newName, isLocal: isLocal)
189+
}
190+
191+
public var description: String {
192+
return identity + "[\(path)]"
193+
}
194+
}

Sources/Workspace/PinsStore.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import Basic
1212
import Utility
1313
import SourceControl
14+
import PackageModel
1415
import PackageGraph
1516

1617
public enum PinOperationError: Swift.Error, CustomStringConvertible {

Tests/PackageGraphPerformanceTests/DependencyResolverPerfTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import XCTest
1212

1313
import Basic
14+
import PackageModel
1415
import PackageGraph
1516
import PackageLoading
1617
import SourceControl

Tests/WorkspaceTests/PinsStoreTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import XCTest
1212

1313
import Basic
1414
import Utility
15+
import PackageModel
1516
import PackageGraph
1617
import TestSupport
1718
import SourceControl

0 commit comments

Comments
 (0)