File tree Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -45,14 +45,23 @@ private extension TempFileError {
45
45
///
46
46
/// - Returns: Path to directory in which temporary file should be created.
47
47
public func determineTempDirectory( _ dir: AbsolutePath ? = nil ) throws -> AbsolutePath {
48
- // FIXME: Add other platform specific locations.
49
- let tmpDir = dir ?? AbsolutePath ( NSTemporaryDirectory ( ) )
48
+ let tmpDir = dir ?? cachedTempDirectory
50
49
guard localFileSystem. isDirectory ( tmpDir) else {
51
50
throw TempFileError . couldNotFindTmpDir
52
51
}
53
52
return tmpDir
54
53
}
55
54
55
+ /// Returns temporary directory location by searching relevant env variables.
56
+ ///
57
+ /// Evaluates once per execution.
58
+ private var cachedTempDirectory : AbsolutePath = {
59
+ let override = ProcessEnv . vars [ " TMPDIR " ] ?? ProcessEnv . vars [ " TEMP " ] ?? ProcessEnv . vars [ " TMP " ]
60
+ if let path = override. flatMap ( { try ? AbsolutePath ( validating: $0) } ) {
61
+ return path
62
+ }
63
+ return AbsolutePath ( NSTemporaryDirectory ( ) )
64
+ } ( )
56
65
57
66
/// The closure argument of the `body` closue of `withTemporaryFile`.
58
67
public struct TemporaryFile {
You can’t perform that action at this time.
0 commit comments