Skip to content

Commit 522d8c7

Browse files
authored
Additional refactoring of ManagedDependencies (#3750)
* Convert ManagedDependency from class to structure * Make ManagedDependency.State an indirect enumeration * Remove unnecessary ManagedDependency computed property packageIdentity * Consolidate dependency path determination into dedicated APIs
1 parent 3874cdc commit 522d8c7

File tree

4 files changed

+20
-27
lines changed

4 files changed

+20
-27
lines changed

Sources/Workspace/ManagedDependency.swift

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ extension Workspace {
1919
///
2020
/// Each dependency will have a checkout containing the sources at a
2121
/// particular revision, and may have an associated version.
22-
final public class ManagedDependency {
22+
public struct ManagedDependency: Equatable {
2323
/// Represents the state of the managed dependency.
24-
public enum State: Equatable {
24+
public indirect enum State: Equatable {
2525
/// The dependency is a managed checkout.
2626
case checkout(CheckoutState)
2727

@@ -34,19 +34,6 @@ extension Workspace {
3434

3535
// The dependency is a local package.
3636
case local
37-
38-
public static func == (lhs: Workspace.ManagedDependency.State, rhs: Workspace.ManagedDependency.State) -> Bool {
39-
switch (lhs, rhs) {
40-
case (.local, .local):
41-
return true
42-
case (.checkout(let lState), .checkout(let rState)):
43-
return lState == rState
44-
case (.edited(let lBasedOn, let lUnmanagedPath), .edited(let rBasedOn, let rUnmanagedPath)):
45-
return lBasedOn?.packageRef == rBasedOn?.packageRef && lUnmanagedPath == rUnmanagedPath
46-
default:
47-
return false
48-
}
49-
}
5037
}
5138

5239
/// The package reference.
@@ -70,10 +57,6 @@ extension Workspace {
7057
/// The checked out path of the dependency on disk, relative to the workspace checkouts path.
7158
public let subpath: RelativePath
7259

73-
public var packageIdentity: PackageIdentity {
74-
self.packageRef.identity
75-
}
76-
7760
internal init(
7861
packageRef: PackageReference,
7962
state: State,

Sources/Workspace/Workspace.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ extension Workspace {
11581158

11591159
// Remove the existing checkout.
11601160
do {
1161-
let oldCheckoutPath = self.location.repositoriesCheckoutsDirectory.appending(dependency.subpath)
1161+
let oldCheckoutPath = self.location.repositoriesCheckoutSubdirectory(for: dependency)
11621162
try fileSystem.chmod(.userWritable, path: oldCheckoutPath, options: [.recursive, .onlyFiles])
11631163
try fileSystem.removeFileTree(oldCheckoutPath)
11641164
}
@@ -1196,7 +1196,7 @@ extension Workspace {
11961196
}
11971197

11981198
// Form the edit working repo path.
1199-
let path = self.location.editsDirectory.appending(dependency.subpath)
1199+
let path = self.location.editsSubdirectory(for: dependency)
12001200
// Check for uncommited and unpushed changes if force removal is off.
12011201
if !forceRemove {
12021202
let workingCopy = try repositoryManager.openWorkingCopy(at: path)
@@ -1314,7 +1314,7 @@ extension Workspace {
13141314
/// Returns all manifests contained in DependencyManifests.
13151315
public func allDependencyManifests() -> OrderedDictionary<PackageIdentity, Manifest> {
13161316
return self.dependencies.reduce(into: OrderedDictionary<PackageIdentity, Manifest>()) { partial, item in
1317-
partial[item.dependency.packageIdentity] = item.manifest
1317+
partial[item.dependency.packageRef.identity] = item.manifest
13181318
}
13191319
}
13201320

@@ -1350,7 +1350,7 @@ extension Workspace {
13501350
func computePackageURLs() -> (required: Set<PackageReference>, missing: Set<PackageReference>) {
13511351
let manifestsMap: [PackageIdentity: Manifest] = Dictionary(uniqueKeysWithValues:
13521352
self.root.packages.map { ($0.key, $0.value.manifest) } +
1353-
self.dependencies.map { ($0.dependency.packageIdentity, $0.manifest) }
1353+
self.dependencies.map { ($0.dependency.packageRef.identity, $0.manifest) }
13541354
)
13551355

13561356
var inputIdentities: Set<PackageReference> = []
@@ -1486,9 +1486,9 @@ extension Workspace {
14861486
public func path(to dependency: Workspace.ManagedDependency) -> AbsolutePath {
14871487
switch dependency.state {
14881488
case .checkout:
1489-
return self.location.repositoriesCheckoutsDirectory.appending(dependency.subpath)
1489+
return self.location.repositoriesCheckoutSubdirectory(for: dependency)
14901490
case .edited(_, let path):
1491-
return path ?? self.location.editsDirectory.appending(dependency.subpath)
1491+
return path ?? self.location.editsSubdirectory(for: dependency)
14921492
case .local:
14931493
return AbsolutePath(dependency.packageRef.location)
14941494
}
@@ -3046,7 +3046,7 @@ extension Workspace {
30463046
/// Removes the clone and checkout of the provided specifier.
30473047
fileprivate func removeRepository(dependency: ManagedDependency) throws {
30483048
// Remove the checkout.
3049-
let dependencyPath = self.location.repositoriesCheckoutsDirectory.appending(dependency.subpath)
3049+
let dependencyPath = self.location.repositoriesCheckoutSubdirectory(for: dependency)
30503050
let workingCopy = try self.repositoryManager.openWorkingCopy(at: dependencyPath)
30513051
guard !workingCopy.hasUncommittedChanges() else {
30523052
throw WorkspaceDiagnostics.UncommitedChanges(repositoryPath: dependencyPath)

Sources/Workspace/WorkspaceConfiguration.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,21 @@ extension Workspace {
4242
self.workingDirectory.appending(component: "repositories")
4343
}
4444

45+
/// Returns the path to the repository checkout directory for a package.
46+
public func editsSubdirectory(for dependency: ManagedDependency) -> AbsolutePath {
47+
self.editsDirectory.appending(dependency.subpath)
48+
}
49+
4550
/// Path to the repository checkouts.
4651
public var repositoriesCheckoutsDirectory: AbsolutePath {
4752
self.workingDirectory.appending(component: "checkouts")
4853
}
4954

55+
/// Returns the path to the repository checkout directory for a package.
56+
public func repositoriesCheckoutSubdirectory(for dependency: ManagedDependency) -> AbsolutePath {
57+
self.repositoriesCheckoutsDirectory.appending(dependency.subpath)
58+
}
59+
5060
/// Path to the downloaded binary artifacts.
5161
public var artifactsDirectory: AbsolutePath {
5262
self.workingDirectory.appending(component: "artifacts")

Sources/Workspace/WorkspaceState.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ fileprivate struct WorkspaceStateStorage {
374374
}
375375

376376
extension Workspace.ManagedDependency {
377-
fileprivate convenience init(_ dependency: WorkspaceStateStorage.V4.Dependency) throws {
377+
fileprivate init(_ dependency: WorkspaceStateStorage.V4.Dependency) throws {
378378
try self.init(
379379
packageRef: .init(dependency.packageRef),
380380
state: dependency.state.underlying,

0 commit comments

Comments
 (0)