add os.getCacheDir (#18126)
* add `os.getCacheDir` * fixup * address comments
This commit is contained in:
parent
60cbdbf37a
commit
9559350e34
3 changed files with 43 additions and 7 deletions
|
|
@ -175,10 +175,11 @@
|
||||||
- `--gc:orc` is now 10% faster than previously for common workloads. If
|
- `--gc:orc` is now 10% faster than previously for common workloads. If
|
||||||
you have trouble with its changed behavior, compile with `-d:nimOldOrc`.
|
you have trouble with its changed behavior, compile with `-d:nimOldOrc`.
|
||||||
|
|
||||||
|
|
||||||
- `os.FileInfo` (returned by `getFileInfo`) now contains `blockSize`,
|
- `os.FileInfo` (returned by `getFileInfo`) now contains `blockSize`,
|
||||||
determining preferred I/O block size for this file object.
|
determining preferred I/O block size for this file object.
|
||||||
|
|
||||||
|
- Added `os.getCacheDir()` to return platform specific cache directory.
|
||||||
|
|
||||||
- Added a simpler to use `io.readChars` overload.
|
- Added a simpler to use `io.readChars` overload.
|
||||||
|
|
||||||
- Added `**` to jsffi.
|
- Added `**` to jsffi.
|
||||||
|
|
|
||||||
|
|
@ -922,9 +922,7 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1",
|
||||||
## See also:
|
## See also:
|
||||||
## * `getHomeDir proc <#getHomeDir>`_
|
## * `getHomeDir proc <#getHomeDir>`_
|
||||||
## * `getTempDir proc <#getTempDir>`_
|
## * `getTempDir proc <#getTempDir>`_
|
||||||
## * `expandTilde proc <#expandTilde,string>`_
|
## * `getCacheDir proc <#getCacheDir>`_
|
||||||
## * `getCurrentDir proc <#getCurrentDir>`_
|
|
||||||
## * `setCurrentDir proc <#setCurrentDir,string>`_
|
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
from std/strutils import endsWith
|
from std/strutils import endsWith
|
||||||
# See `getHomeDir` for behavior regarding trailing DirSep.
|
# See `getHomeDir` for behavior regarding trailing DirSep.
|
||||||
|
|
@ -935,6 +933,42 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1",
|
||||||
result = getEnv("XDG_CONFIG_HOME", getEnv("HOME") / ".config")
|
result = getEnv("XDG_CONFIG_HOME", getEnv("HOME") / ".config")
|
||||||
result.normalizePathEnd(trailingSep = defined(nimLegacyHomeDir))
|
result.normalizePathEnd(trailingSep = defined(nimLegacyHomeDir))
|
||||||
|
|
||||||
|
proc getCacheDir*(): string =
|
||||||
|
## Returns the cache directory of the current user for applications.
|
||||||
|
##
|
||||||
|
## This makes use of the following environment variables:
|
||||||
|
##
|
||||||
|
## * On Windows: `getEnv("LOCALAPPDATA")`
|
||||||
|
##
|
||||||
|
## * On macOS: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")`
|
||||||
|
##
|
||||||
|
## * On other platforms: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")`
|
||||||
|
##
|
||||||
|
## **See also:**
|
||||||
|
## * `getHomeDir proc <#getHomeDir>`_
|
||||||
|
## * `getTempDir proc <#getTempDir>`_
|
||||||
|
## * `getConfigDir proc <#getConfigDir>`_
|
||||||
|
# follows https://crates.io/crates/platform-dirs
|
||||||
|
when defined(windows):
|
||||||
|
result = getEnv("LOCALAPPDATA")
|
||||||
|
elif defined(osx):
|
||||||
|
result = getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")
|
||||||
|
else:
|
||||||
|
result = getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")
|
||||||
|
result.normalizePathEnd(false)
|
||||||
|
|
||||||
|
proc getCacheDir*(app: string): string =
|
||||||
|
## Returns the cache directory for an application `app`.
|
||||||
|
##
|
||||||
|
## * On windows, this uses: `getCacheDir() / app / "cache"`
|
||||||
|
##
|
||||||
|
## * On other platforms, this uses: `getCacheDir() / app`
|
||||||
|
when defined(windows):
|
||||||
|
getCacheDir() / app / "cache"
|
||||||
|
else:
|
||||||
|
getCacheDir() / app
|
||||||
|
|
||||||
|
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
type DWORD = uint32
|
type DWORD = uint32
|
||||||
|
|
||||||
|
|
@ -972,9 +1006,7 @@ proc getTempDir*(): string {.rtl, extern: "nos$1",
|
||||||
## See also:
|
## See also:
|
||||||
## * `getHomeDir proc <#getHomeDir>`_
|
## * `getHomeDir proc <#getHomeDir>`_
|
||||||
## * `getConfigDir proc <#getConfigDir>`_
|
## * `getConfigDir proc <#getConfigDir>`_
|
||||||
## * `expandTilde proc <#expandTilde,string>`_
|
## * `getCacheDir proc <#getCacheDir>`_
|
||||||
## * `getCurrentDir proc <#getCurrentDir>`_
|
|
||||||
## * `setCurrentDir proc <#setCurrentDir,string>`_
|
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
from std/strutils import endsWith
|
from std/strutils import endsWith
|
||||||
# See `getHomeDir` for behavior regarding trailing DirSep.
|
# See `getHomeDir` for behavior regarding trailing DirSep.
|
||||||
|
|
|
||||||
|
|
@ -556,6 +556,9 @@ block getTempDir:
|
||||||
else:
|
else:
|
||||||
doAssert getTempDir() == "/tmp"
|
doAssert getTempDir() == "/tmp"
|
||||||
|
|
||||||
|
block: # getCacheDir
|
||||||
|
doAssert getCacheDir().dirExists
|
||||||
|
|
||||||
block osenv:
|
block osenv:
|
||||||
block delEnv:
|
block delEnv:
|
||||||
const dummyEnvVar = "DUMMY_ENV_VAR" # This env var wouldn't be likely to exist to begin with
|
const dummyEnvVar = "DUMMY_ENV_VAR" # This env var wouldn't be likely to exist to begin with
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue