Skip to content

Commit 93e90d5

Browse files
committed
Port cache-dir from soc/directories-jvm
1 parent 4e1f333 commit 93e90d5

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [#46](https://github.com/clojure-emacs/orchard/pull/46): [Inspector] Show fields inherited from superclasses when rendering raw objects.
1010
* [#47](https://github.com/clojure-emacs/orchard/pull/46): [Java] Cache class-info for editable Java classes.
1111
* [#51](https://github.com/clojure-emacs/orchard/issues/51): Add basic xref functionality in `orchard.xref`.
12+
* [#64](https://github.com/clojure-emacs/orchard/issues/64): Port `cache-dir` from [soc/directories-jvm](https://github.com/soc/directories-jvm) to `orchard.directory`.
1213

1314
### Changes
1415

src/orchard/directory.clj

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
(ns orchard.directory
2+
"A port of BaseDirectories.java in soc/directories-jvm.
3+
https://github.com/soc/directories-jvm"
4+
{:author "Masashi Iizuka"
5+
:added "0.5.0"}
6+
(:require [clojure.java.io :as io]
7+
[clojure.string :as str]
8+
[orchard.misc :as u])
9+
(:import (java.io BufferedReader)))
10+
11+
(def file-separator (System/getProperty "file.separator"))
12+
13+
(defn- run-commands [expected-result-lines commands]
14+
(let [commands ^"[Ljava.lang.String;" (into-array String commands)
15+
builder (ProcessBuilder. commands)
16+
process (.start builder)]
17+
(with-open [reader ^BufferedReader (io/reader (.getInputStream process))]
18+
(try
19+
(doall (repeatedly expected-result-lines #(.readLine reader)))
20+
(finally
21+
(.destroy process))))))
22+
23+
(defn- get-win-dirs [guids]
24+
(let [commands (concat ["& {"
25+
"[Console]::OutputEncoding = [System.Text.Encoding]::UTF8"
26+
"Add-Type @\\\""
27+
"using System;"
28+
"using System.Runtime.InteropServices;"
29+
"public class Dir {"
30+
" [DllImport(\\\"shell32.dll\\\")]"
31+
" private static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath);"
32+
" public static string GetKnownFolderPath(string rfid) {"
33+
" IntPtr pszPath;"
34+
" if (SHGetKnownFolderPath(new Guid(rfid), 0, IntPtr.Zero, out pszPath) != 0) return \\\"\\\";"
35+
" string path = Marshal.PtrToStringUni(pszPath);"
36+
" Marshal.FreeCoTaskMem(pszPath);"
37+
" return path;"
38+
" }"
39+
"}"
40+
"\\\"@"]
41+
(map #(str "[Dir]::GetKnownFolderPath(\\\"" % "\\\")") guids)
42+
["}"])]
43+
(run-commands (count guids) ["powershell.exe" "-Command" (str/join "\n" commands)])))
44+
45+
(defn cache-dir
46+
"Returns the path to the user's cache directory.
47+
48+
macOS : $HOME/Library/Caches
49+
Windows: FOLDERID_LocalAppData\\cache
50+
Others : $XDG_CACHE_HOME or $HOME/.cache"
51+
{:added "0.5.0"}
52+
[]
53+
(case u/os-type
54+
::u/os-mac
55+
(str/join file-separator [(System/getProperty "user.home")
56+
"Library"
57+
"Caches"])
58+
59+
::u/os-windows
60+
(-> ["F1B32785-6FBA-4FCF-9D55-7B8E7F157091"]
61+
get-win-dirs
62+
first)
63+
64+
(let [cache-home (System/getenv "XDG_CACHE_HOME")]
65+
(if (str/blank? cache-home)
66+
(str (System/getProperty "user.home") file-separator ".cache")
67+
cache-home))))

test/orchard/directory_test.clj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(ns orchard.directory-test
2+
(:require
3+
[clojure.string :as str]
4+
[clojure.test :as test :refer [deftest is testing]]
5+
[orchard.directory :as dir]
6+
[orchard.misc :as u]))
7+
8+
(deftest cache-dir-test
9+
(testing "BSD"
10+
(with-redefs [u/os-type ::u/os-bsd]
11+
(is (str/ends-with? (dir/cache-dir) "/.cache"))))
12+
13+
(testing "Linux"
14+
(with-redefs [u/os-type ::u/os-linux]
15+
(is (str/ends-with? (dir/cache-dir) "/.cache"))))
16+
17+
(testing "Mac"
18+
(with-redefs [u/os-type ::u/os-mac]
19+
(is (str/ends-with? (dir/cache-dir) "/Library/Caches")))))
20+
21+
;; NOTE: This test case targets AppVeyor mainly
22+
(deftest cache-dir-windows-test
23+
(when (u/os-windows?)
24+
(is (re-seq #"^C:\\Users\\.+\\AppData\\Local" (dir/cache-dir)))))

0 commit comments

Comments
 (0)