From ffa96d79088c77b3474813640600048c08d7e820 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Sat, 29 May 2021 16:30:45 -0700 Subject: [PATCH 1/3] add `os.getCacheDir` --- changelog.md | 3 ++- lib/pure/os.nim | 45 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/changelog.md b/changelog.md index 4c30d67a772ff..db2d9a706811f 100644 --- a/changelog.md +++ b/changelog.md @@ -171,10 +171,11 @@ - `--gc:orc` is now 10% faster than previously for common workloads. If you have trouble with its changed behavior, compile with `-d:nimOldOrc`. - - `os.FileInfo` (returned by `getFileInfo`) now contains `blockSize`, 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 `**` to jsffi. diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 287fbe1255c99..cb085420da5bb 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -922,9 +922,7 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1", ## See also: ## * `getHomeDir proc <#getHomeDir>`_ ## * `getTempDir proc <#getTempDir>`_ - ## * `expandTilde proc <#expandTilde,string>`_ - ## * `getCurrentDir proc <#getCurrentDir>`_ - ## * `setCurrentDir proc <#setCurrentDir,string>`_ + ## * `getCacheDir proc <#getCacheDir>`_ runnableExamples: from std/strutils import endsWith # See `getHomeDir` for behavior regarding trailing DirSep. @@ -935,6 +933,43 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1", result = getEnv("XDG_CONFIG_HOME", getEnv("HOME") / ".config") result.normalizePathEnd(trailingSep = defined(nimLegacyHomeDir)) +proc getCacheDir*(): string = + ## Returns the cache directory of the current user for applications. + ## + ## on windows: `getEnv("LOCALAPPDATA")` + ## + ## on osx: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")` + ## + ## else: `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 + runnableExamples: + from std/strutils import endsWith + assert not getCacheDir().endsWith DirSep + 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: `getCacheDir() / app / "cache"` + ## + ## else:: `getCacheDir() / "cache"` + when defined(windows): + getCacheDir() / app / "cache" + else: + getCacheDir() / app + + when defined(windows): type DWORD = uint32 @@ -972,9 +1007,7 @@ proc getTempDir*(): string {.rtl, extern: "nos$1", ## See also: ## * `getHomeDir proc <#getHomeDir>`_ ## * `getConfigDir proc <#getConfigDir>`_ - ## * `expandTilde proc <#expandTilde,string>`_ - ## * `getCurrentDir proc <#getCurrentDir>`_ - ## * `setCurrentDir proc <#setCurrentDir,string>`_ + ## * `getCacheDir proc <#getCacheDir>`_ runnableExamples: from std/strutils import endsWith # See `getHomeDir` for behavior regarding trailing DirSep. From c26e595abad98a20d8f4f380d90b8836f1c454c0 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Sun, 30 May 2021 10:51:29 -0700 Subject: [PATCH 2/3] fixup --- lib/pure/os.nim | 3 --- tests/stdlib/tos.nim | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pure/os.nim b/lib/pure/os.nim index cb085420da5bb..800582222a08e 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -947,9 +947,6 @@ proc getCacheDir*(): string = ## * `getTempDir proc <#getTempDir>`_ ## * `getConfigDir proc <#getConfigDir>`_ # follows https://crates.io/crates/platform-dirs - runnableExamples: - from std/strutils import endsWith - assert not getCacheDir().endsWith DirSep when defined(windows): result = getEnv("LOCALAPPDATA") elif defined(osx): diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index 1fcee5a2e1ca3..4444adf638b2d 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -556,6 +556,9 @@ block getTempDir: else: doAssert getTempDir() == "/tmp" +block: # getCacheDir + doAssert getCacheDir().fileExists + block osenv: block delEnv: const dummyEnvVar = "DUMMY_ENV_VAR" # This env var wouldn't be likely to exist to begin with From 21b88dd3252b00248909ca700947879cf5c3e36b Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Sun, 30 May 2021 10:59:25 -0700 Subject: [PATCH 3/3] address comments --- lib/pure/os.nim | 14 ++++++++------ tests/stdlib/tos.nim | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 800582222a08e..c48d0d84f90b5 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -936,13 +936,15 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1", proc getCacheDir*(): string = ## Returns the cache directory of the current user for applications. ## - ## on windows: `getEnv("LOCALAPPDATA")` + ## This makes use of the following environment variables: ## - ## on osx: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")` + ## * On Windows: `getEnv("LOCALAPPDATA")` ## - ## else: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")` + ## * On macOS: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")` ## - ## See also: + ## * On other platforms: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")` + ## + ## **See also:** ## * `getHomeDir proc <#getHomeDir>`_ ## * `getTempDir proc <#getTempDir>`_ ## * `getConfigDir proc <#getConfigDir>`_ @@ -958,9 +960,9 @@ proc getCacheDir*(): string = proc getCacheDir*(app: string): string = ## Returns the cache directory for an application `app`. ## - ## on windows: `getCacheDir() / app / "cache"` + ## * On windows, this uses: `getCacheDir() / app / "cache"` ## - ## else:: `getCacheDir() / "cache"` + ## * On other platforms, this uses: `getCacheDir() / app` when defined(windows): getCacheDir() / app / "cache" else: diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index 4444adf638b2d..b9769d05c820a 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -557,7 +557,7 @@ block getTempDir: doAssert getTempDir() == "/tmp" block: # getCacheDir - doAssert getCacheDir().fileExists + doAssert getCacheDir().dirExists block osenv: block delEnv: