Skip to content

Commit 8b8b2d3

Browse files
Merge pull request #3637 from SwiftPackageIndex/issue-3469-dependency-transition-22
Issue 3469 dependency transition 22
2 parents e6419d3 + ddba24c commit 8b8b2d3

24 files changed

+432
-421
lines changed

Sources/App/Commands/Analyze.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ extension Analyze {
8787
.forEach { pair in
8888
guard let (path, mod) = pair else { return }
8989
@Dependency(\.date.now) var now
90+
@Dependency(\.fileManager) var fileManager
9091
let cutoff = now.addingTimeInterval(-Constants.gitCheckoutMaxAge)
9192
if mod < cutoff {
92-
try Current.fileManager.removeItem(atPath: path)
93+
try fileManager.removeItem(atPath: path)
9394
AppMetrics.analyzeTrimCheckoutsCount?.inc()
9495
}
9596
}
@@ -143,7 +144,7 @@ extension Analyze {
143144
@Dependency(\.fileManager) var fileManager
144145
let checkoutDir = fileManager.checkoutsDirectory()
145146
Current.logger().info("Checkout directory: \(checkoutDir)")
146-
if !Current.fileManager.fileExists(atPath: checkoutDir) {
147+
if !fileManager.fileExists(atPath: checkoutDir) {
147148
try await createCheckoutsDirectory(client: client, path: checkoutDir)
148149
}
149150

@@ -236,9 +237,10 @@ extension Analyze {
236237
path: String) async throws {
237238
Current.logger().info("Creating checkouts directory at path: \(path)")
238239
do {
239-
try Current.fileManager.createDirectory(atPath: path,
240-
withIntermediateDirectories: false,
241-
attributes: nil)
240+
@Dependency(\.fileManager) var fileManager
241+
try fileManager.createDirectory(atPath: path,
242+
withIntermediateDirectories: false,
243+
attributes: nil)
242244
} catch {
243245
let error = AppError.genericError(nil, "Failed to create checkouts directory: \(error.localizedDescription)")
244246
Current.logger().report(error: error)
@@ -266,11 +268,12 @@ extension Analyze {
266268
/// - url: url to fetch from
267269
/// - Throws: Shell errors
268270
static func fetch(cacheDir: String, branch: String, url: String) async throws {
271+
@Dependency(\.fileManager) var fileManager
269272
Current.logger().info("pulling \(url) in \(cacheDir)")
270273
// clean up stray lock files that might have remained from aborted commands
271274
for fileName in ["HEAD.lock", "index.lock"] {
272275
let filePath = cacheDir + "/.git/\(fileName)"
273-
if Current.fileManager.fileExists(atPath: filePath) {
276+
if fileManager.fileExists(atPath: filePath) {
274277
Current.logger().info("Removing stale \(fileName) at path: \(filePath)")
275278
try await Current.shell.run(command: .removeFile(from: filePath))
276279
}
@@ -295,7 +298,7 @@ extension Analyze {
295298
}
296299

297300
do {
298-
guard Current.fileManager.fileExists(atPath: cacheDir) else {
301+
guard fileManager.fileExists(atPath: cacheDir) else {
299302
try await clone(cacheDir: cacheDir, url: package.model.url)
300303
return
301304
}
@@ -529,7 +532,8 @@ extension Analyze {
529532
/// - Throws: Shell errors or AppError.invalidRevision if there is no Package.swift file
530533
/// - Returns: `Manifest` data
531534
static func dumpPackage(at path: String) async throws -> Manifest {
532-
guard Current.fileManager.fileExists(atPath: path + "/Package.swift") else {
535+
@Dependency(\.fileManager) var fileManager
536+
guard fileManager.fileExists(atPath: path + "/Package.swift") else {
533537
// It's important to check for Package.swift - otherwise `dump-package` will go
534538
// up the tree through parent directories to find one
535539
throw AppError.invalidRevision(nil, "no Package.swift")

Sources/App/Commands/ReAnalyzeVersions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ enum ReAnalyzeVersions {
185185
try await dependencies.yield {
186186
@Dependency(\.fileManager) var fileManager
187187
guard let cacheDir = fileManager.cacheDirectoryPath(for: pkg.model) else { return }
188-
if !Current.fileManager.fileExists(atPath: cacheDir) || refreshCheckouts {
188+
if !fileManager.fileExists(atPath: cacheDir) || refreshCheckouts {
189189
try await Analyze.refreshCheckout(package: pkg)
190190
}
191191

Sources/App/Core/AppEnvironment.swift

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import FoundationNetworking
2323

2424

2525
struct AppEnvironment: Sendable {
26-
var fileManager: FileManager
2726
var git: Git
2827
var logger: @Sendable () -> Logger
2928
var setLogger: @Sendable (Logger) -> Void
@@ -35,7 +34,6 @@ extension AppEnvironment {
3534
nonisolated(unsafe) static var logger: Logger!
3635

3736
static let live = AppEnvironment(
38-
fileManager: .live,
3937
git: .live,
4038
logger: { logger },
4139
setLogger: { logger in Self.logger = logger },
@@ -44,29 +42,6 @@ extension AppEnvironment {
4442
}
4543

4644

47-
struct FileManager: Sendable {
48-
var createDirectory: @Sendable (String, Bool, [FileAttributeKey : Any]?) throws -> Void
49-
var fileExists: @Sendable (String) -> Bool
50-
var removeItem: @Sendable (_ path: String) throws -> Void
51-
var workingDirectory: @Sendable () -> String
52-
53-
// pass-through methods to preserve argument labels
54-
func createDirectory(atPath path: String,
55-
withIntermediateDirectories createIntermediates: Bool,
56-
attributes: [FileAttributeKey : Any]?) throws {
57-
try createDirectory(path, createIntermediates, attributes)
58-
}
59-
func fileExists(atPath path: String) -> Bool { fileExists(path) }
60-
func removeItem(atPath path: String) throws { try removeItem(path) }
61-
62-
static let live: Self = .init(
63-
createDirectory: { try Foundation.FileManager.default.createDirectory(atPath: $0, withIntermediateDirectories: $1, attributes: $2) },
64-
fileExists: { Foundation.FileManager.default.fileExists(atPath: $0) },
65-
removeItem: { try Foundation.FileManager.default.removeItem(atPath: $0) },
66-
workingDirectory: { DirectoryConfiguration.detect().workingDirectory }
67-
)
68-
}
69-
7045

7146
struct Git: Sendable {
7247
var commitCount: @Sendable (String) async throws -> Int

Sources/App/Core/Dependencies/FileManagerClient.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ struct FileManagerClient {
2626
var checkoutsDirectory: @Sendable () -> String = { reportIssue("checkoutsDirectory"); return "SPI-checkouts" }
2727
var contents: @Sendable (_ atPath: String) -> Data?
2828
var contentsOfDirectory: @Sendable (_ atPath: String) throws -> [String]
29+
var createDirectory: @Sendable (_ atPath: String, _ withIntermediateDirectories: Bool, _ attributes: [FileAttributeKey : Any]?) throws -> Void
30+
var fileExists: @Sendable (_ atPath: String) -> Bool = { reportIssue("fileExists"); return Foundation.FileManager.default.fileExists(atPath: $0) }
31+
var removeItem: @Sendable (_ atPath: String) throws -> Void
32+
var workingDirectory: @Sendable () -> String = { reportIssue("workingDirectory"); return "" }
2933
}
3034

3135

@@ -43,7 +47,11 @@ extension FileManagerClient: DependencyKey {
4347
attributesOfItem: { try Foundation.FileManager.default.attributesOfItem(atPath: $0) },
4448
checkoutsDirectory: { Environment.get("CHECKOUTS_DIR") ?? DirectoryConfiguration.detect().workingDirectory + "SPI-checkouts" },
4549
contents: { Foundation.FileManager.default.contents(atPath: $0) },
46-
contentsOfDirectory: { try Foundation.FileManager.default.contentsOfDirectory(atPath: $0) }
50+
contentsOfDirectory: { try Foundation.FileManager.default.contentsOfDirectory(atPath: $0) },
51+
createDirectory: { try Foundation.FileManager.default.createDirectory(atPath: $0, withIntermediateDirectories: $1, attributes: $2) },
52+
fileExists: { Foundation.FileManager.default.fileExists(atPath: $0) },
53+
removeItem: { try Foundation.FileManager.default.removeItem(atPath: $0) },
54+
workingDirectory: { DirectoryConfiguration.detect().workingDirectory }
4755
)
4856
}
4957
}
@@ -54,6 +62,7 @@ extension FileManagerClient: TestDependencyKey {
5462
var mock = Self()
5563
// Override the `unimplemented` default because it is a very common dependency.
5664
mock.checkoutsDirectory = { "SPI-checkouts" }
65+
mock.workingDirectory = { DirectoryConfiguration.detect().workingDirectory }
5766
return mock
5867
}
5968
}

Sources/App/Core/Emoji.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// limitations under the License.
1414

1515
import Foundation
16+
17+
import Dependencies
1618
import Vapor
1719

1820

@@ -33,8 +35,8 @@ struct EmojiStorage {
3335
var regularExpression: NSRegularExpression?
3436

3537
init() {
36-
let pathToEmojiFile = Current.fileManager.workingDirectory()
37-
.appending("Resources/emoji.json")
38+
@Dependency(\.fileManager) var fileManager
39+
let pathToEmojiFile = fileManager.workingDirectory().appending("Resources/emoji.json")
3840

3941
lookup = [:]
4042
regularExpression = nil

Sources/App/Core/Extensions/Badge.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import Vapor
1615
import Foundation
1716

17+
import Dependencies
18+
import Vapor
19+
1820

1921
struct Badge: Content, Equatable {
2022
var schemaVersion: Int
@@ -68,8 +70,8 @@ enum BadgeType: String, Codable {
6870
extension Badge {
6971

7072
static private func loadSVGLogo() -> String? {
71-
let pathToFile = Current.fileManager.workingDirectory()
72-
.appending("Public/images/logo-tiny.svg")
73+
@Dependency(\.fileManager) var fileManager
74+
let pathToFile = fileManager.workingDirectory().appending("Public/images/logo-tiny.svg")
7375

7476
return try? String(contentsOfFile: pathToFile, encoding: .utf8)
7577
}

Sources/App/Core/PackageCollection+signing.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ extension SignedCollection {
5959
return true
6060
}
6161

62-
static let certsDir = URL(fileURLWithPath: Current.fileManager.workingDirectory())
63-
.appendingPathComponent("Resources")
64-
.appendingPathComponent("Certs")
62+
static var certsDir: URL {
63+
@Dependency(\.fileManager) var fileManager
64+
return URL(fileURLWithPath: fileManager.workingDirectory())
65+
.appendingPathComponent("Resources")
66+
.appendingPathComponent("Certs")
67+
}
6568

6669
static let signer = PackageCollectionSigning(
6770
trustedRootCertsDir: certsDir,

Sources/App/Core/PackageContributors.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// limitations under the License.
1414

1515
import Foundation
16+
17+
import Dependencies
1618
import ShellOut
1719
import Vapor
1820

@@ -65,8 +67,8 @@ enum PackageContributors {
6567

6668
/// Gets the git history in a string log
6769
private static func runShortlog(gitCacheDirectoryPath: String, packageID: UUID?) async throws -> String {
68-
69-
if Current.fileManager.fileExists(atPath: gitCacheDirectoryPath) == false {
70+
@Dependency(\.fileManager) var fileManager
71+
if fileManager.fileExists(atPath: gitCacheDirectoryPath) == false {
7072
throw AppError.cacheDirectoryDoesNotExist(packageID, gitCacheDirectoryPath)
7173
}
7274

Sources/App/Views/Blog/BlogActions+Model.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ enum BlogActions {
2525
struct Model {
2626

2727
static var blogIndexYmlPath: String {
28-
Current.fileManager.workingDirectory().appending("Resources/Blog/posts.yml")
28+
@Dependency(\.fileManager) var fileManager
29+
return fileManager.workingDirectory().appending("Resources/Blog/posts.yml")
2930
}
3031

3132
struct PostSummary: Equatable {
@@ -80,7 +81,7 @@ extension BlogActions.Model.PostSummary {
8081

8182
var postMarkdown: String {
8283
@Dependency(\.fileManager) var fileManager
83-
let markdownPath = Current.fileManager.workingDirectory()
84+
let markdownPath = fileManager.workingDirectory()
8485
.appending("Resources/Blog/Posts/")
8586
.appending(slug)
8687
.appending(".md")

Sources/App/Views/MarkdownPage.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
// limitations under the License.
1414

1515
import Foundation
16-
import Plot
16+
17+
import Dependencies
1718
import Ink
19+
import Plot
1820

1921

2022
class MarkdownPage: PublicPage {
@@ -28,7 +30,8 @@ class MarkdownPage: PublicPage {
2830
let html: String?
2931

3032
init(path: String, _ markdownFilename: String) {
31-
let pathToMarkdownFile = Current.fileManager.workingDirectory()
33+
@Dependency(\.fileManager) var fileManager
34+
let pathToMarkdownFile = fileManager.workingDirectory()
3235
.appending("Resources/Markdown/")
3336
.appending(markdownFilename)
3437

0 commit comments

Comments
 (0)