From 5450b69f85898d6770ec382181d8b73701091a3c Mon Sep 17 00:00:00 2001 From: Gert Goet Date: Sun, 19 Mar 2023 15:57:37 +0100 Subject: [PATCH] Add 1-arity to xdg-*-home to get subfolder of base dir (#91) Closes #91 --- API.md | 20 ++++++++++++------ CHANGELOG.md | 4 ++++ src/babashka/fs.cljc | 36 +++++++++++++++++++++----------- test/babashka/fs_test.clj | 44 ++++++++++++++++++++++++++------------- 4 files changed, 71 insertions(+), 33 deletions(-) diff --git a/API.md b/API.md index b0416ba..0e76cfc 100644 --- a/API.md +++ b/API.md @@ -985,45 +985,53 @@ Writes `lines`, a seqable of strings to [`path`](#babashka.fs/path) via `java.ni ``` clojure (xdg-cache-home) +(xdg-cache-home app) ``` Path representing the base directory relative to which user-specific non-essential data files should be stored as described in the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html). - Returns path based on the value of env-var `XDG_CACHE_HOME` (if set), else `(fs/path (fs/home) ".cache")`. -

Source

+ Returns path based on the value of env-var `XDG_CACHE_HOME` (if set), else `(fs/path (fs/home) ".cache")`. + When provided, appends `app` to the path. +

Source

## `xdg-config-home` ``` clojure (xdg-config-home) +(xdg-config-home app) ``` Path representing the base directory relative to which user-specific configuration files should be stored as described in the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html). - Returns path based on the value of env-var `XDG_CONFIG_HOME` (if set), else `(fs/path (fs/home) ".config")`. -

Source

+ Returns path based on the value of env-var `XDG_CONFIG_HOME` (if set), else `(fs/path (fs/home) ".config")`. + When provided, appends `app` to the path. +

Source

## `xdg-data-home` ``` clojure (xdg-data-home) +(xdg-data-home app) ``` Path representing the base directory relative to which user-specific data files should be stored as described in the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html). Returns path based on the value of env-var `XDG_DATA_HOME` (if set), else `(fs/path (fs/home) ".local" "share")`. -

Source

+ When provided, appends `app` to the path. +

Source

## `xdg-state-home` ``` clojure (xdg-state-home) +(xdg-state-home app) ``` Path representing the base directory relative to which user-specific state files should be stored as described in the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html). Returns path based on the value of env-var `XDG_STATE_HOME` (if set), else `(fs/path (fs/home) ".local" "state")`. -

Source

+ When provided, appends `app` to the path. +

Source

## `zip` ``` clojure diff --git a/CHANGELOG.md b/CHANGELOG.md index 528eebe..7985010 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ For a list of breaking changes, check [here](#breaking-changes). Babashka [fs](https://github.com/babashka/fs): file system utility library for Clojure +## Unreleased + +- [#91](https://github.com/babashka/fs/issues/91): add 1-arity to `xdg-*-home` to get subfolder of base directory ([@eval](https://github.com/eval)) + ## v0.3.17 (2023-02-28) - [#67](https://github.com/babashka/fs/issues/67): add `:root` and `:path-fn` options to `fs/zip` diff --git a/src/babashka/fs.cljc b/src/babashka/fs.cljc index cc1d1ff..ecdbf92 100644 --- a/src/babashka/fs.cljc +++ b/src/babashka/fs.cljc @@ -1164,27 +1164,39 @@ (defn xdg-config-home "Path representing the base directory relative to which user-specific configuration files should be stored as described in the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html). - Returns path based on the value of env-var `XDG_CONFIG_HOME` (if set), else `(fs/path (fs/home) \".config\")`. " - [] - (xdg-home-for :config)) + Returns path based on the value of env-var `XDG_CONFIG_HOME` (if set), else `(fs/path (fs/home) \".config\")`. + When provided, appends `app` to the path." + ([] (xdg-config-home nil)) + ([app] + (cond-> (xdg-home-for :config) + (seq app) (path app)))) (defn xdg-cache-home "Path representing the base directory relative to which user-specific non-essential data files should be stored as described in the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html). - Returns path based on the value of env-var `XDG_CACHE_HOME` (if set), else `(fs/path (fs/home) \".cache\")`. " - [] - (xdg-home-for :cache)) + Returns path based on the value of env-var `XDG_CACHE_HOME` (if set), else `(fs/path (fs/home) \".cache\")`. + When provided, appends `app` to the path." + ([] (xdg-cache-home nil)) + ([app] + (cond-> (xdg-home-for :cache) + (seq app) (path app)))) (defn xdg-data-home "Path representing the base directory relative to which user-specific data files should be stored as described in the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html). - Returns path based on the value of env-var `XDG_DATA_HOME` (if set), else `(fs/path (fs/home) \".local\" \"share\")`." - [] - (xdg-home-for :data)) + Returns path based on the value of env-var `XDG_DATA_HOME` (if set), else `(fs/path (fs/home) \".local\" \"share\")`. + When provided, appends `app` to the path." + ([] (xdg-data-home nil)) + ([app] + (cond-> (xdg-home-for :data) + (seq app) (path app)))) (defn xdg-state-home "Path representing the base directory relative to which user-specific state files should be stored as described in the [XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html). - Returns path based on the value of env-var `XDG_STATE_HOME` (if set), else `(fs/path (fs/home) \".local\" \"state\")`." - [] - (xdg-home-for :state)) + Returns path based on the value of env-var `XDG_STATE_HOME` (if set), else `(fs/path (fs/home) \".local\" \"state\")`. + When provided, appends `app` to the path." + ([] (xdg-state-home nil)) + ([app] + (cond-> (xdg-home-for :state) + (seq app) (path app)))) diff --git a/test/babashka/fs_test.clj b/test/babashka/fs_test.clj index eca89b5..8201f1e 100644 --- a/test/babashka/fs_test.clj +++ b/test/babashka/fs_test.clj @@ -622,24 +622,38 @@ (let [custom-path (if windows? "C:\\some\\path" "/some/path")] (with-redefs [fs/get-env {"XDG_CONFIG_HOME" custom-path}] (is (= (fs/path custom-path) - (fs/xdg-config-home)))))) + (fs/xdg-config-home))) + (is (= (fs/path custom-path "clj-kondo") + (fs/xdg-config-home "clj-kondo")))))) (testing "yields default-path when env-var contains no absolute path" (with-redefs [fs/get-env {"XDG_CONFIG_HOME" ""}] (is (= default-path (fs/xdg-config-home))))))) (deftest xdg-config-home-test - (is (= (fs/path (fs/home) ".config") - (fs/xdg-config-home)))) - -(deftest xdg-cache-path-test - (is (= (fs/path (fs/home) ".cache") - (fs/xdg-cache-home)))) - -(deftest xdg-data-path-test - (is (= (fs/path (fs/home) ".local" "share") - (fs/xdg-data-home)))) - -(deftest xdg-state-path-test - (is (= (fs/path (fs/home) ".local" "state") - (fs/xdg-state-home)))) + (let [default-home (fs/path (fs/home) ".config")] + (is (= default-home + (fs/xdg-config-home))) + (is (= (fs/path default-home "clj-kondo") + (fs/xdg-config-home "clj-kondo"))))) + +(deftest xdg-cache-home-test + (let [default-home (fs/path (fs/home) ".cache")] + (is (= default-home + (fs/xdg-cache-home))) + (is (= (fs/path default-home "clj-kondo") + (fs/xdg-cache-home "clj-kondo"))))) + +(deftest xdg-data-home-test + (let [default-home (fs/path (fs/home) ".local" "share")] + (is (= default-home + (fs/xdg-data-home))) + (is (= (fs/path default-home "clj-kondo") + (fs/xdg-data-home "clj-kondo"))))) + +(deftest xdg-state-home-test + (let [default-home (fs/path (fs/home) ".local" "state")] + (is (= default-home + (fs/xdg-state-home))) + (is (= (fs/path default-home "clj-kondo") + (fs/xdg-state-home "clj-kondo")))))