Skip to content

Commit 3ea2e7d

Browse files
committed
fixup
1 parent f284806 commit 3ea2e7d

File tree

7 files changed

+164
-172
lines changed

7 files changed

+164
-172
lines changed

Package.resolved.lock

Whitespace-only changes.

Sources/Basics/ConcurrencyHelpers.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public final class ThreadSafeKeyValueStore<Key, Value> where Key: Hashable {
4646
}
4747
}
4848

49+
@discardableResult
4950
public func removeValue(forKey key: Key) -> Value? {
5051
self.lock.withLock {
5152
self.underlying.removeValue(forKey: key)

Sources/PackageGraph/PinsStore.swift

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
This source file is part of the Swift.org open source project
3-
3+
44
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
6-
6+
77
See http://swift.org/LICENSE.txt for license information
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
@@ -16,33 +16,36 @@ import TSCBasic
1616

1717
public final class PinsStore {
1818
public typealias PinsMap = [PackageIdentity: PinsStore.Pin]
19-
19+
2020
public struct Pin: Equatable {
2121
/// The package reference of the pinned dependency.
2222
public let packageRef: PackageReference
23-
23+
2424
/// The pinned state.
2525
public let state: CheckoutState
26-
26+
2727
public init(packageRef: PackageReference, state: CheckoutState) {
2828
self.packageRef = packageRef
2929
self.state = state
3030
}
3131
}
3232

3333
private let mirrors: DependencyMirrors
34-
35-
/// The pins map.
36-
public fileprivate(set) var pinsMap: PinsMap
37-
34+
35+
/// storage
36+
private let storage: PinsStorage
37+
private let _pins: ThreadSafeKeyValueStore<PackageIdentity, PinsStore.Pin>
38+
3839
/// The current pins.
40+
41+
public var pinsMap: PinsMap {
42+
self._pins.get()
43+
}
44+
3945
public var pins: AnySequence<Pin> {
40-
return AnySequence<Pin>(pinsMap.values)
46+
return AnySequence<Pin>(self.pinsMap.values)
4147
}
4248

43-
/// storage
44-
private let storage: PinsStorage
45-
4649
/// Create a new pins store.
4750
///
4851
/// - Parameters:
@@ -51,17 +54,17 @@ public final class PinsStore {
5154
public init(pinsFile: AbsolutePath, fileSystem: FileSystem, mirrors: DependencyMirrors) throws {
5255
self.storage = .init(path: pinsFile, fileSystem: fileSystem)
5356
self.mirrors = mirrors
54-
57+
5558
do {
56-
self.pinsMap = try self.storage.load(mirrors: mirrors)
59+
self._pins = .init(try self.storage.load(mirrors: mirrors))
5760
} catch {
58-
self.pinsMap = [:]
61+
self._pins = .init()
5962
// FIXME: delete the file?
6063
// FIXME: warning instead of error?
6164
throw StringError("Package.resolved file is corrupted or malformed; fix or delete the file to continue: \(error)")
6265
}
6366
}
64-
67+
6568
/// Pin a repository at a version.
6669
///
6770
/// This method does not automatically write to state file.
@@ -75,24 +78,24 @@ public final class PinsStore {
7578
state: state
7679
))
7780
}
78-
81+
7982
/// Add a pin.
8083
///
8184
/// This will replace any previous pin with same package name.
8285
public func add(_ pin: Pin) {
83-
self.pinsMap[pin.packageRef.identity] = pin
86+
self._pins[pin.packageRef.identity] = pin
8487
}
85-
88+
8689
/// Unpin all of the currently pinned dependencies.
8790
///
8891
/// This method does not automatically write to state file.
8992
public func unpinAll() {
9093
// Reset the pins map.
91-
self.pinsMap = [:]
94+
self._pins.clear()
9295
}
93-
96+
9497
public func saveState() throws {
95-
try self.storage.save(pins: self.pinsMap, mirrors: self.mirrors, removeIfEmpty: true)
98+
try self.storage.save(pins: self._pins.get(), mirrors: self.mirrors, removeIfEmpty: true)
9699
}
97100
}
98101

@@ -108,13 +111,14 @@ fileprivate struct PinsStorage {
108111
self.path = path
109112
self.fileSystem = fileSystem
110113
}
111-
114+
112115
func load(mirrors: DependencyMirrors) throws -> PinsStore.PinsMap {
113116
if !self.fileSystem.exists(self.path) {
114117
return [:]
115118
}
116-
117-
return try self.fileSystem.withLock(on: self.path, type: .shared) {
119+
120+
// 👀 do we want a lock file here? safer but pretty noisy
121+
//return try self.fileSystem.withLock(on: self.path, type: .shared) {
118122
let version = try self.decoder.decode(path: self.path, fileSystem: self.fileSystem, as: Version.self)
119123
switch version.version {
120124
case 1:
@@ -128,15 +132,15 @@ fileprivate struct PinsStorage {
128132
default:
129133
throw InternalError("unknown RepositoryManager version: \(version)")
130134
}
131-
}
135+
//}
132136
}
133-
137+
134138
func save(pins: PinsStore.PinsMap, mirrors: DependencyMirrors, removeIfEmpty: Bool) throws {
135139
if !self.fileSystem.exists(self.path.parentDirectory) {
136140
try self.fileSystem.createDirectory(self.path.parentDirectory)
137141
}
138-
139-
try self.fileSystem.withLock(on: self.path, type: .exclusive) {
142+
// 👀 do we want a lock file here? safer but pretty noisy
143+
//try self.fileSystem.withLock(on: self.path, type: .exclusive) {
140144
// Remove the pins file if there are zero pins to save.
141145
//
142146
// This can happen if all dependencies are path-based or edited
@@ -145,61 +149,64 @@ fileprivate struct PinsStorage {
145149
try self.fileSystem.removeFileTree(self.path)
146150
return
147151
}
148-
152+
149153
let container = V1(pins: pins, mirrors: mirrors)
150154
let data = try self.encoder.encode(container)
151155
try self.fileSystem.writeFileContents(self.path, data: data)
152-
}
156+
//}
153157
}
154-
158+
155159
func reset() throws {
156160
if !self.fileSystem.exists(self.path.parentDirectory) {
157161
return
158162
}
159-
try self.fileSystem.withLock(on: self.path, type: .exclusive) {
163+
// 👀 do we want a lock file here? safer but pretty noisy
164+
//try self.fileSystem.withLock(on: self.path, type: .exclusive) {
160165
try self.fileSystem.removeFileTree(self.path)
161-
}
166+
//}
162167
}
163-
168+
164169
// version reader
165170
struct Version: Codable {
166171
let version: Int
167172
}
168-
173+
169174
// v1 storage format
170175
struct V1: Codable {
171176
let version: Int
172177
let object: Container
173-
178+
174179
init (pins: PinsStore.PinsMap, mirrors: DependencyMirrors) {
175180
self.version = 1
176181
self.object = .init(
177-
pins: pins.values.map{ Pin($0, mirrors: mirrors) }
182+
pins: pins.values
183+
.sorted(by: { $0.packageRef.identity < $1.packageRef.identity })
184+
.map{ Pin($0, mirrors: mirrors) }
178185
)
179186
}
180-
187+
181188
struct Container: Codable {
182189
var pins: [Pin]
183190
}
184-
191+
185192
struct Pin: Codable {
186193
let package: String?
187194
let repositoryURL: String
188195
let state: CheckoutInfo
189-
196+
190197
init(_ pin: PinsStore.Pin, mirrors: DependencyMirrors) {
191198
self.package = pin.packageRef.name
192199
// rdar://52529014, rdar://52529011: pin file should store the original location but remap when loading
193200
self.repositoryURL = mirrors.originalURL(for: pin.packageRef.location) ?? pin.packageRef.location
194201
self.state = .init(pin.state)
195202
}
196203
}
197-
204+
198205
struct CheckoutInfo: Codable {
199206
let revision: String
200207
let branch: String?
201208
let version: String?
202-
209+
203210
init(_ state: CheckoutState) {
204211
switch state {
205212
case .version(let version, let revision):

0 commit comments

Comments
 (0)