Skip to content

Commit 58a3ddb

Browse files
authored
add caches directory accesssor to FileSystem (#161)
motivation: accessing the caches directory from the FS is a common need and apears in several places in SwiftPM, extracing it out changes: * add cachesDirectory:AbsolutePath? to FileSystem protocol * implement in LocalFileSystem using FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)
1 parent 05b9f1c commit 58a3ddb

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Sources/TSCBasic/FileSystem.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ public protocol FileSystem: class {
156156

157157
/// Get the home directory of current user
158158
var homeDirectory: AbsolutePath { get }
159+
160+
/// Get the caches directory of current user
161+
var cachesDirectory: AbsolutePath? { get }
159162

160163
/// Create the given directory.
161164
func createDirectory(_ path: AbsolutePath) throws
@@ -317,6 +320,10 @@ private class LocalFileSystem: FileSystem {
317320
var homeDirectory: AbsolutePath {
318321
return AbsolutePath(NSHomeDirectory())
319322
}
323+
324+
var cachesDirectory: AbsolutePath? {
325+
return FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first.flatMap { AbsolutePath($0.absoluteString) }
326+
}
320327

321328
func getDirectoryContents(_ path: AbsolutePath) throws -> [String] {
322329
#if canImport(Darwin)
@@ -622,6 +629,10 @@ public class InMemoryFileSystem: FileSystem {
622629
// FIXME: Maybe we should allow setting this when creating the fs.
623630
return AbsolutePath("/home/user")
624631
}
632+
633+
public var cachesDirectory: AbsolutePath? {
634+
return self.homeDirectory.appending(component: "caches")
635+
}
625636

626637
public func getDirectoryContents(_ path: AbsolutePath) throws -> [String] {
627638
guard let node = try getNode(path) else {
@@ -870,6 +881,10 @@ public class RerootedFileSystemView: FileSystem {
870881
public var homeDirectory: AbsolutePath {
871882
fatalError("homeDirectory on RerootedFileSystemView is not supported.")
872883
}
884+
885+
public var cachesDirectory: AbsolutePath? {
886+
fatalError("cachesDirectory on RerootedFileSystemView is not supported.")
887+
}
873888

874889
public func getDirectoryContents(_ path: AbsolutePath) throws -> [String] {
875890
return try underlyingFileSystem.getDirectoryContents(formUnderlyingPath(path))

Tests/TSCUtilityTests/DownloaderTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@ class FailingFileSystem: FileSystem {
468468
var homeDirectory: AbsolutePath {
469469
fatalError("unexpected call")
470470
}
471+
472+
var cachesDirectory: AbsolutePath? {
473+
fatalError("unexpected call")
474+
}
471475

472476
func changeCurrentWorkingDirectory(to path: AbsolutePath) throws {
473477
fatalError("unexpected call")

0 commit comments

Comments
 (0)