Skip to content

Commit a879961

Browse files
committed
[NFC] Move PinsStore to PackageGraph
This will allow us to access PinsStore inside the dependency resolver.
1 parent 033a6a3 commit a879961

File tree

6 files changed

+94
-82
lines changed

6 files changed

+94
-82
lines changed

Sources/PackageGraph/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
add_library(PackageGraph
10+
CheckoutState.swift
1011
DependencyResolver.swift
1112
PackageGraph.swift
1213
PackageGraphLoader.swift
1314
PackageGraphRoot.swift
15+
PinsStore.swift
1416
Pubgrub.swift
1517
RawPackageConstraints.swift
1618
RepositoryPackageContainerProvider.swift

Sources/Workspace/CheckoutState.swift renamed to Sources/PackageGraph/CheckoutState.swift

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010

1111
import Basic
12-
import PackageGraph
1312
import SourceControl
1413
import SPMUtility
1514

@@ -52,15 +51,15 @@ public struct CheckoutState: Equatable, CustomStringConvertible {
5251
return version?.description ?? branch ?? revision.identifier
5352
}
5453

55-
var isBranchOrRevisionBased: Bool {
54+
public var isBranchOrRevisionBased: Bool {
5655
return version == nil
5756
}
5857
}
5958

6059
extension CheckoutState {
6160

6261
/// Returns requirement induced by this state.
63-
func requirement() -> PackageRequirement {
62+
public func requirement() -> PackageRequirement {
6463
if let version = version {
6564
return .versionSet(.exact(version))
6665
} else if let branch = branch {
@@ -90,12 +89,3 @@ extension CheckoutState: JSONMappable, JSONSerializable {
9089
}
9190
}
9291

93-
extension ManagedDependency {
94-
public var checkoutState: CheckoutState? {
95-
if case .checkout(let checkoutState) = state {
96-
return checkoutState
97-
}
98-
return nil
99-
}
100-
}
101-

Sources/Workspace/PinsStore.swift renamed to Sources/PackageGraph/PinsStore.swift

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import Basic
1212
import SPMUtility
1313
import SourceControl
1414
import PackageModel
15-
import PackageGraph
1615

1716
public final class PinsStore {
1817
public struct Pin {
@@ -45,7 +44,7 @@ public final class PinsStore {
4544
/// The pins map.
4645
///
4746
/// Key -> Package Identity.
48-
fileprivate(set) var pinsMap: [String: Pin]
47+
public fileprivate(set) var pinsMap: [String: Pin]
4948

5049
/// The current pins.
5150
public var pins: AnySequence<Pin> {
@@ -99,25 +98,6 @@ public final class PinsStore {
9998
pinsMap[pin.packageRef.identity] = pin
10099
}
101100

102-
/// Pin a managed dependency at its checkout state.
103-
///
104-
/// This method does nothing if the dependency is in edited state.
105-
func pin(_ dependency: ManagedDependency) {
106-
107-
// Get the checkout state.
108-
let checkoutState: CheckoutState
109-
switch dependency.state {
110-
case .checkout(let state):
111-
checkoutState = state
112-
case .edited, .local:
113-
return
114-
}
115-
116-
self.pin(
117-
packageRef: dependency.packageRef,
118-
state: checkoutState)
119-
}
120-
121101
/// Unpin all of the currently pinnned dependencies.
122102
///
123103
/// This method does not automatically write to state file.
@@ -182,48 +162,3 @@ extension PinsStore.Pin: JSONMappable, JSONSerializable, Equatable {
182162
])
183163
}
184164
}
185-
186-
/// A file watcher utility for the Package.resolved file.
187-
///
188-
/// This is not intended to be used directly by clients.
189-
final class ResolvedFileWatcher {
190-
private var fswatch: FSWatch!
191-
private var existingValue: ByteString?
192-
private let valueLock: Lock = Lock()
193-
private let resolvedFile: AbsolutePath
194-
195-
public func updateValue() {
196-
valueLock.withLock {
197-
self.existingValue = try? localFileSystem.readFileContents(resolvedFile)
198-
}
199-
}
200-
201-
init(resolvedFile: AbsolutePath, onChange: @escaping () -> ()) throws {
202-
self.resolvedFile = resolvedFile
203-
204-
let block = { [weak self] (paths: [AbsolutePath]) in
205-
guard let self = self else { return }
206-
207-
// Check if resolved file is part of the received paths.
208-
let hasResolvedFile = paths.contains{ $0.appending(component: resolvedFile.basename) == resolvedFile }
209-
guard hasResolvedFile else { return }
210-
211-
self.valueLock.withLock {
212-
// Compute the contents of the resolved file and fire the onChange block
213-
// if its value is different than existing value.
214-
let newValue = try? localFileSystem.readFileContents(resolvedFile)
215-
if self.existingValue != newValue {
216-
self.existingValue = newValue
217-
onChange()
218-
}
219-
}
220-
}
221-
222-
fswatch = FSWatch(paths: [resolvedFile.parentDirectory], latency: 1, block: block)
223-
try fswatch.start()
224-
}
225-
226-
deinit {
227-
fswatch.stop()
228-
}
229-
}

Sources/Workspace/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
add_library(Workspace
10-
CheckoutState.swift
1110
Destination.swift
1211
Diagnostics.swift
1312
Export.swift
1413
InitPackage.swift
1514
ManagedDependency.swift
16-
PinsStore.swift
1715
ToolsVersionWriter.swift
1816
UserToolchain.swift
19-
Workspace.swift)
17+
Workspace.swift
18+
misc.swift)
2019
target_link_libraries(Workspace PUBLIC
2120
Basic
2221
Build

Sources/Workspace/misc.swift

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2018 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import PackageGraph
12+
13+
extension ManagedDependency {
14+
public var checkoutState: CheckoutState? {
15+
if case .checkout(let checkoutState) = state {
16+
return checkoutState
17+
}
18+
return nil
19+
}
20+
}
21+
22+
extension PinsStore {
23+
/// Pin a managed dependency at its checkout state.
24+
///
25+
/// This method does nothing if the dependency is in edited state.
26+
func pin(_ dependency: ManagedDependency) {
27+
28+
// Get the checkout state.
29+
let checkoutState: CheckoutState
30+
switch dependency.state {
31+
case .checkout(let state):
32+
checkoutState = state
33+
case .edited, .local:
34+
return
35+
}
36+
37+
self.pin(
38+
packageRef: dependency.packageRef,
39+
state: checkoutState)
40+
}
41+
}
42+
43+
/// A file watcher utility for the Package.resolved file.
44+
///
45+
/// This is not intended to be used directly by clients.
46+
final class ResolvedFileWatcher {
47+
private var fswatch: FSWatch!
48+
private var existingValue: ByteString?
49+
private let valueLock: Lock = Lock()
50+
private let resolvedFile: AbsolutePath
51+
52+
public func updateValue() {
53+
valueLock.withLock {
54+
self.existingValue = try? localFileSystem.readFileContents(resolvedFile)
55+
}
56+
}
57+
58+
init(resolvedFile: AbsolutePath, onChange: @escaping () -> ()) throws {
59+
self.resolvedFile = resolvedFile
60+
61+
let block = { [weak self] (paths: [AbsolutePath]) in
62+
guard let self = self else { return }
63+
64+
// Check if resolved file is part of the received paths.
65+
let hasResolvedFile = paths.contains{ $0.appending(component: resolvedFile.basename) == resolvedFile }
66+
guard hasResolvedFile else { return }
67+
68+
self.valueLock.withLock {
69+
// Compute the contents of the resolved file and fire the onChange block
70+
// if its value is different than existing value.
71+
let newValue = try? localFileSystem.readFileContents(resolvedFile)
72+
if self.existingValue != newValue {
73+
self.existingValue = newValue
74+
onChange()
75+
}
76+
}
77+
}
78+
79+
fswatch = FSWatch(paths: [resolvedFile.parentDirectory], latency: 1, block: block)
80+
try fswatch.start()
81+
}
82+
83+
deinit {
84+
fswatch.stop()
85+
}
86+
}

Tests/WorkspaceTests/PinsStoreTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import XCTest
1313
import Basic
1414
import SPMUtility
1515
import PackageModel
16-
import PackageGraph
16+
@testable import PackageGraph
1717
import TestSupport
1818
import SourceControl
1919
@testable import Workspace

0 commit comments

Comments
 (0)