Skip to content

Additional refactoring of ManagedDependencies #3750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions Sources/Workspace/ManagedDependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ extension Workspace {
///
/// Each dependency will have a checkout containing the sources at a
/// particular revision, and may have an associated version.
final public class ManagedDependency {
public struct ManagedDependency: Equatable {
/// Represents the state of the managed dependency.
public enum State: Equatable {
public indirect enum State: Equatable {
/// The dependency is a managed checkout.
case checkout(CheckoutState)

Expand All @@ -34,19 +34,6 @@ extension Workspace {

// The dependency is a local package.
case local

public static func == (lhs: Workspace.ManagedDependency.State, rhs: Workspace.ManagedDependency.State) -> Bool {
switch (lhs, rhs) {
case (.local, .local):
return true
case (.checkout(let lState), .checkout(let rState)):
return lState == rState
case (.edited(let lBasedOn, let lUnmanagedPath), .edited(let rBasedOn, let rUnmanagedPath)):
return lBasedOn?.packageRef == rBasedOn?.packageRef && lUnmanagedPath == rUnmanagedPath
default:
return false
}
}
}

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

public var packageIdentity: PackageIdentity {
self.packageRef.identity
}

internal init(
packageRef: PackageReference,
state: State,
Expand Down
14 changes: 7 additions & 7 deletions Sources/Workspace/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ extension Workspace {

// Remove the existing checkout.
do {
let oldCheckoutPath = self.location.repositoriesCheckoutsDirectory.appending(dependency.subpath)
let oldCheckoutPath = self.location.repositoriesCheckoutSubdirectory(for: dependency)
try fileSystem.chmod(.userWritable, path: oldCheckoutPath, options: [.recursive, .onlyFiles])
try fileSystem.removeFileTree(oldCheckoutPath)
}
Expand Down Expand Up @@ -1136,7 +1136,7 @@ extension Workspace {
}

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

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

var inputIdentities: Set<PackageReference> = []
Expand Down Expand Up @@ -1426,9 +1426,9 @@ extension Workspace {
public func path(to dependency: Workspace.ManagedDependency) -> AbsolutePath {
switch dependency.state {
case .checkout:
return self.location.repositoriesCheckoutsDirectory.appending(dependency.subpath)
return self.location.repositoriesCheckoutSubdirectory(for: dependency)
case .edited(_, let path):
return path ?? self.location.editsDirectory.appending(dependency.subpath)
return path ?? self.location.editsSubdirectory(for: dependency)
case .local:
return AbsolutePath(dependency.packageRef.location)
}
Expand Down Expand Up @@ -2986,7 +2986,7 @@ extension Workspace {
/// Removes the clone and checkout of the provided specifier.
fileprivate func removeRepository(dependency: ManagedDependency) throws {
// Remove the checkout.
let dependencyPath = self.location.repositoriesCheckoutsDirectory.appending(dependency.subpath)
let dependencyPath = self.location.repositoriesCheckoutSubdirectory(for: dependency)
let workingCopy = try self.repositoryManager.openWorkingCopy(at: dependencyPath)
guard !workingCopy.hasUncommittedChanges() else {
throw WorkspaceDiagnostics.UncommitedChanges(repositoryPath: dependencyPath)
Expand Down
10 changes: 10 additions & 0 deletions Sources/Workspace/WorkspaceConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ extension Workspace {
self.workingDirectory.appending(component: "repositories")
}

/// Returns the path to the repository checkout directory for a package.
public func editsSubdirectory(for dependency: ManagedDependency) -> AbsolutePath {
self.editsDirectory.appending(dependency.subpath)
}

/// Path to the repository checkouts.
public var repositoriesCheckoutsDirectory: AbsolutePath {
self.workingDirectory.appending(component: "checkouts")
}

/// Returns the path to the repository checkout directory for a package.
public func repositoriesCheckoutSubdirectory(for dependency: ManagedDependency) -> AbsolutePath {
self.repositoriesCheckoutsDirectory.appending(dependency.subpath)
}

/// Path to the downloaded binary artifacts.
public var artifactsDirectory: AbsolutePath {
self.workingDirectory.appending(component: "artifacts")
Expand Down
2 changes: 1 addition & 1 deletion Sources/Workspace/WorkspaceState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ fileprivate struct WorkspaceStateStorage {
}

extension Workspace.ManagedDependency {
fileprivate convenience init(_ dependency: WorkspaceStateStorage.V4.Dependency) throws {
fileprivate init(_ dependency: WorkspaceStateStorage.V4.Dependency) throws {
try self.init(
packageRef: .init(dependency.packageRef),
state: dependency.state.underlying,
Expand Down