Skip to content

Commit b0dd2ab

Browse files
liquidzbbatsov
authored andcommitted
Port cache-dir from soc/directories-jvm
1 parent 55e0e72 commit b0dd2ab

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-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.os`.
1213

1314
### Changes
1415

src/orchard/os.clj

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

test/orchard/os_test.clj

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

0 commit comments

Comments
 (0)