Skip to content

Commit

Permalink
Add 1-arity to xdg-*-home to get subfolder of base dir (babashka#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
eval committed Mar 19, 2023
1 parent ceb8f7f commit 5450b69
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 33 deletions.
20 changes: 14 additions & 6 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")`.
<p><sub><a href="https://github.com/babashka/fs/blob/master/src/babashka/fs.cljc#L1171-L1176">Source</a></sub></p>
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.
<p><sub><a href="https://github.com/babashka/fs/blob/master/src/babashka/fs.cljc#L1174-L1182">Source</a></sub></p>

## <a name="babashka.fs/xdg-config-home">`xdg-config-home`</a><a name="babashka.fs/xdg-config-home"></a>
``` 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")`.
<p><sub><a href="https://github.com/babashka/fs/blob/master/src/babashka/fs.cljc#L1164-L1169">Source</a></sub></p>
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.
<p><sub><a href="https://github.com/babashka/fs/blob/master/src/babashka/fs.cljc#L1164-L1172">Source</a></sub></p>

## <a name="babashka.fs/xdg-data-home">`xdg-data-home`</a><a name="babashka.fs/xdg-data-home"></a>
``` 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")`.
<p><sub><a href="https://github.com/babashka/fs/blob/master/src/babashka/fs.cljc#L1178-L1183">Source</a></sub></p>
When provided, appends `app` to the path.
<p><sub><a href="https://github.com/babashka/fs/blob/master/src/babashka/fs.cljc#L1184-L1192">Source</a></sub></p>

## <a name="babashka.fs/xdg-state-home">`xdg-state-home`</a><a name="babashka.fs/xdg-state-home"></a>
``` 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")`.
<p><sub><a href="https://github.com/babashka/fs/blob/master/src/babashka/fs.cljc#L1185-L1190">Source</a></sub></p>
When provided, appends `app` to the path.
<p><sub><a href="https://github.com/babashka/fs/blob/master/src/babashka/fs.cljc#L1194-L1202">Source</a></sub></p>

## <a name="babashka.fs/zip">`zip`</a><a name="babashka.fs/zip"></a>
``` clojure
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
36 changes: 24 additions & 12 deletions src/babashka/fs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
44 changes: 29 additions & 15 deletions test/babashka/fs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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")))))

0 comments on commit 5450b69

Please sign in to comment.