Skip to content

Commit 8e5ee07

Browse files
committed
added -cache-path option
1 parent 1956dd2 commit 8e5ee07

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

Sources/Commands/Options.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public class ToolOptions {
2424
/// The custom build directory, if provided.
2525
public var buildPath: AbsolutePath?
2626

27+
/// The custom cache directory, if provided.
28+
public var cachePath: AbsolutePath?
29+
2730
/// The custom working directory that the tool should operate in (deprecated).
2831
public var chdir: AbsolutePath?
2932

Sources/Commands/SwiftTool.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ public class SwiftTool<Options: ToolOptions> {
249249
/// Path to the build directory.
250250
let buildPath: AbsolutePath
251251

252+
/// Path to the repository cache.
253+
let cachePath: AbsolutePath
254+
252255
/// Reference to the argument parser.
253256
let parser: ArgumentParser
254257

@@ -330,9 +333,15 @@ public class SwiftTool<Options: ToolOptions> {
330333
binder.bind(
331334
option: parser.add(
332335
option: "--build-path", kind: PathArgument.self,
333-
usage: "Specify build/cache directory [default: ./.build]"),
336+
usage: "Specify build directory [default: ./.build]"),
334337
to: { $0.buildPath = $1.path })
335338

339+
binder.bind(
340+
option: parser.add(
341+
option: "--cache-path", kind: PathArgument.self,
342+
usage: "Specify the cache directory [default: \(Workspace.globalCachePath.prettyPath(cwd: localFileSystem.homeDirectory))]"),
343+
to: { $0.cachePath = $1.path })
344+
336345
binder.bind(
337346
option: parser.add(
338347
option: "--chdir", shortName: "-C", kind: PathArgument.self),
@@ -537,12 +546,14 @@ public class SwiftTool<Options: ToolOptions> {
537546

538547
// Create local variables to use while finding build path to avoid capture self before init error.
539548
let customBuildPath = options.buildPath
549+
let customCachePath = options.cachePath
540550
let packageRoot = findPackageRoot()
541551

542552
self.packageRoot = packageRoot
543553
self.buildPath = getEnvBuildPath(workingDir: cwd) ??
544554
customBuildPath ??
545555
(packageRoot ?? cwd).appending(component: ".build")
556+
self.cachePath = getEnvCachePath(workingDir: cwd) ?? customCachePath ?? Workspace.globalCachePath
546557
}
547558

548559
class func postprocessArgParserResult(result: ArgumentParser.Result, diagnostics: DiagnosticsEngine) throws {
@@ -619,6 +630,7 @@ public class SwiftTool<Options: ToolOptions> {
619630
let provider = GitRepositoryProvider(processSet: processSet)
620631
let workspace = Workspace(
621632
dataPath: buildPath,
633+
cachePath: cachePath,
622634
editablesPath: try editablesPath(),
623635
pinsFile: try resolvedFilePath(),
624636
manifestLoader: try getManifestLoader(),
@@ -920,6 +932,14 @@ private func getEnvBuildPath(workingDir: AbsolutePath) -> AbsolutePath? {
920932
return AbsolutePath(env, relativeTo: workingDir)
921933
}
922934

935+
/// Returns the cache path from the environment, if present.
936+
private func getEnvCachePath(workingDir: AbsolutePath) -> AbsolutePath? {
937+
// Don't rely on build path from env for SwiftPM's own tests.
938+
guard ProcessEnv.vars["SWIFTPM_TESTS_MODULECACHE"] == nil else { return nil }
939+
guard let env = ProcessEnv.vars["SWIFTPM_CACHE_DIR"] else { return nil }
940+
return AbsolutePath(env)
941+
}
942+
923943
/// Returns the sandbox profile to be used when parsing manifest on macOS.
924944
private func sandboxProfile(allowedDirectories: [AbsolutePath]) -> String {
925945
let stream = BufferedOutputByteStream()

Sources/Workspace/Workspace.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@ public class Workspace {
370370

371371
fileprivate let additionalFileRules: [FileRuleDescription]
372372

373+
/// The default location of the git repository cache
374+
public static let globalCachePath: AbsolutePath = {
375+
guard let cacheURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
376+
return localFileSystem.homeDirectory.appending(components: ".swiftpm", "repositories")
377+
}
378+
return AbsolutePath(cacheURL.path).appending(components: "org.swift.swiftpm", "repositories")
379+
}()
380+
373381
/// Create a new package workspace.
374382
///
375383
/// This will automatically load the persisted state for the package, if
@@ -386,6 +394,7 @@ public class Workspace {
386394
/// - Throws: If the state was present, but could not be loaded.
387395
public init(
388396
dataPath: AbsolutePath,
397+
cachePath: AbsolutePath? = nil,
389398
editablesPath: AbsolutePath,
390399
pinsFile: AbsolutePath,
391400
manifestLoader: ManifestLoaderProtocol,
@@ -421,16 +430,8 @@ public class Workspace {
421430
self.resolvedFile = pinsFile
422431
self.additionalFileRules = additionalFileRules
423432

424-
/// The default location of the git repository cache
425-
let repositoriesPath: AbsolutePath = {
426-
guard let cacheURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
427-
return localFileSystem.homeDirectory.appending(components: ".swiftpm", "repositories")
428-
}
429-
return AbsolutePath(cacheURL.path).appending(components: "org.swift.swiftpm", "repositories")
430-
}()
431-
432433
let repositoryManager = repositoryManager ?? RepositoryManager(
433-
path: repositoriesPath,
434+
path: cachePath ?? Workspace.globalCachePath,
434435
provider: repositoryProvider,
435436
delegate: delegate.map(WorkspaceRepositoryManagerDelegate.init(workspaceDelegate:)),
436437
fileSystem: fileSystem)
@@ -633,6 +634,7 @@ extension Workspace {
633634

634635
repositoryManager.reset()
635636
try? fileSystem.removeFileTree(dataPath)
637+
// Reset global cache
636638
try? fileSystem.removeFileTree(repositoryManager.path)
637639
}
638640

0 commit comments

Comments
 (0)