Skip to content

Commit 9559350

Browse files
authored
add os.getCacheDir (#18126)
* add `os.getCacheDir` * fixup * address comments
1 parent 60cbdbf commit 9559350

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,11 @@
175175
- `--gc:orc` is now 10% faster than previously for common workloads. If
176176
you have trouble with its changed behavior, compile with `-d:nimOldOrc`.
177177

178-
179178
- `os.FileInfo` (returned by `getFileInfo`) now contains `blockSize`,
180179
determining preferred I/O block size for this file object.
181180

181+
- Added `os.getCacheDir()` to return platform specific cache directory.
182+
182183
- Added a simpler to use `io.readChars` overload.
183184

184185
- Added `**` to jsffi.

lib/pure/os.nim

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -922,9 +922,7 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1",
922922
## See also:
923923
## * `getHomeDir proc <#getHomeDir>`_
924924
## * `getTempDir proc <#getTempDir>`_
925-
## * `expandTilde proc <#expandTilde,string>`_
926-
## * `getCurrentDir proc <#getCurrentDir>`_
927-
## * `setCurrentDir proc <#setCurrentDir,string>`_
925+
## * `getCacheDir proc <#getCacheDir>`_
928926
runnableExamples:
929927
from std/strutils import endsWith
930928
# See `getHomeDir` for behavior regarding trailing DirSep.
@@ -935,6 +933,42 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1",
935933
result = getEnv("XDG_CONFIG_HOME", getEnv("HOME") / ".config")
936934
result.normalizePathEnd(trailingSep = defined(nimLegacyHomeDir))
937935

936+
proc getCacheDir*(): string =
937+
## Returns the cache directory of the current user for applications.
938+
##
939+
## This makes use of the following environment variables:
940+
##
941+
## * On Windows: `getEnv("LOCALAPPDATA")`
942+
##
943+
## * On macOS: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")`
944+
##
945+
## * On other platforms: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")`
946+
##
947+
## **See also:**
948+
## * `getHomeDir proc <#getHomeDir>`_
949+
## * `getTempDir proc <#getTempDir>`_
950+
## * `getConfigDir proc <#getConfigDir>`_
951+
# follows https://crates.io/crates/platform-dirs
952+
when defined(windows):
953+
result = getEnv("LOCALAPPDATA")
954+
elif defined(osx):
955+
result = getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")
956+
else:
957+
result = getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")
958+
result.normalizePathEnd(false)
959+
960+
proc getCacheDir*(app: string): string =
961+
## Returns the cache directory for an application `app`.
962+
##
963+
## * On windows, this uses: `getCacheDir() / app / "cache"`
964+
##
965+
## * On other platforms, this uses: `getCacheDir() / app`
966+
when defined(windows):
967+
getCacheDir() / app / "cache"
968+
else:
969+
getCacheDir() / app
970+
971+
938972
when defined(windows):
939973
type DWORD = uint32
940974

@@ -972,9 +1006,7 @@ proc getTempDir*(): string {.rtl, extern: "nos$1",
9721006
## See also:
9731007
## * `getHomeDir proc <#getHomeDir>`_
9741008
## * `getConfigDir proc <#getConfigDir>`_
975-
## * `expandTilde proc <#expandTilde,string>`_
976-
## * `getCurrentDir proc <#getCurrentDir>`_
977-
## * `setCurrentDir proc <#setCurrentDir,string>`_
1009+
## * `getCacheDir proc <#getCacheDir>`_
9781010
runnableExamples:
9791011
from std/strutils import endsWith
9801012
# See `getHomeDir` for behavior regarding trailing DirSep.

tests/stdlib/tos.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,9 @@ block getTempDir:
556556
else:
557557
doAssert getTempDir() == "/tmp"
558558

559+
block: # getCacheDir
560+
doAssert getCacheDir().dirExists
561+
559562
block osenv:
560563
block delEnv:
561564
const dummyEnvVar = "DUMMY_ENV_VAR" # This env var wouldn't be likely to exist to begin with

0 commit comments

Comments
 (0)