Skip to content

workspace-state.json will be really saved until changes happened #5700

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

Closed
Closed
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
11 changes: 11 additions & 0 deletions Sources/Workspace/ManagedArtifact.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ extension Workspace.ManagedArtifact: CustomStringConvertible {
}
}

extension Workspace.ManagedArtifact: Equatable {

}


extension Workspace.ManagedArtifact.Source: CustomStringConvertible {
public var description: String {
switch self {
Expand Down Expand Up @@ -172,3 +177,9 @@ extension Workspace.ManagedArtifacts: CustomStringConvertible {
"<ManagedArtifacts: \(Array(self.artifacts))>"
}
}

extension Workspace.ManagedArtifacts: Equatable {
public static func == (lhs: Workspace.ManagedArtifacts, rhs: Workspace.ManagedArtifacts) -> Bool {
lhs.artifactMap == rhs.artifactMap
}
}
6 changes: 6 additions & 0 deletions Sources/Workspace/ManagedDependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,9 @@ extension Workspace.ManagedDependencies: CustomStringConvertible {
"<ManagedDependencies: \(Array(self.dependencies.values))>"
}
}

extension Workspace.ManagedDependencies: Equatable {
public static func == (lhs: Workspace.ManagedDependencies, rhs: Workspace.ManagedDependencies) -> Bool {
lhs.dependencies == rhs.dependencies
}
}
26 changes: 21 additions & 5 deletions Sources/Workspace/Workspace+State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class WorkspaceState {
public let storagePath: AbsolutePath

/// storage
private let storage: WorkspaceStateStorage
private var storage: WorkspaceStateStorage

init(
fileSystem: FileSystem,
Expand Down Expand Up @@ -90,13 +90,15 @@ fileprivate struct WorkspaceStateStorage {
private let fileSystem: FileSystem
private let encoder = JSONEncoder.makeWithDefaults()
private let decoder = JSONDecoder.makeWithDefaults()
private var managedDependencies: Workspace.ManagedDependencies?
private var managedArtifacts: Workspace.ManagedArtifacts?

init(path: AbsolutePath, fileSystem: FileSystem) {
self.path = path
self.fileSystem = fileSystem
}

func load() throws -> (dependencies: Workspace.ManagedDependencies, artifacts: Workspace.ManagedArtifacts){
mutating func load() throws -> (dependencies: Workspace.ManagedDependencies, artifacts: Workspace.ManagedArtifacts){
if !self.fileSystem.exists(self.path) {
return (dependencies: .init(), artifacts: .init())
}
Expand All @@ -108,12 +110,20 @@ fileprivate struct WorkspaceStateStorage {
let v4 = try self.decoder.decode(path: self.path, fileSystem: self.fileSystem, as: V4.self)
let dependencies = try v4.object.dependencies.map{ try Workspace.ManagedDependency($0) }
let artifacts = try v4.object.artifacts.map{ try Workspace.ManagedArtifact($0) }
return try (dependencies: .init(dependencies), artifacts: .init(artifacts))
let managedDependencies = try Workspace.ManagedDependencies(dependencies)
let managedArtifacts = try Workspace.ManagedArtifacts(artifacts)
self.managedDependencies = managedDependencies
self.managedArtifacts = managedArtifacts
return (dependencies: managedDependencies, artifacts: managedArtifacts)
case 5:
let v5 = try self.decoder.decode(path: self.path, fileSystem: self.fileSystem, as: V5.self)
let dependencies = try v5.object.dependencies.map{ try Workspace.ManagedDependency($0) }
let artifacts = try v5.object.artifacts.map{ try Workspace.ManagedArtifact($0) }
return try (dependencies: .init(dependencies), artifacts: .init(artifacts))
let managedDependencies = try Workspace.ManagedDependencies(dependencies)
let managedArtifacts = try Workspace.ManagedArtifacts(artifacts)
self.managedDependencies = managedDependencies
self.managedArtifacts = managedArtifacts
return (dependencies: managedDependencies, artifacts: managedArtifacts)
default:
throw StringError("unknown 'WorkspaceStateStorage' version '\(version.version)' at '\(self.path)'")
}
Expand All @@ -125,6 +135,10 @@ fileprivate struct WorkspaceStateStorage {
try self.fileSystem.createDirectory(self.path.parentDirectory)
}

if self.managedDependencies == dependencies && self.managedArtifacts == artifacts {
return
}

try self.fileSystem.withLock(on: self.path, type: .exclusive) {
let storage = V5(dependencies: dependencies, artifacts: artifacts)

Expand All @@ -133,7 +147,9 @@ fileprivate struct WorkspaceStateStorage {
}
}

func reset() throws {
mutating func reset() throws {
self.managedDependencies = nil
self.managedArtifacts = nil
if !self.fileSystem.exists(self.path.parentDirectory) {
return
}
Expand Down