-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.clj
51 lines (39 loc) · 917 Bytes
/
path.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(ns tools.path
(:require
[clojure.java.io :as io]
[clojure.string :as str]
[tools.system :as system])
(:import
(java.io
File)))
(def file-separator
File/separator)
(defn user-dir
[]
(system/get-property "user.dir"))
(defn user-home
[]
(system/get-property "user.home"))
(defn symbol->path
[sym]
(some-> sym
(str/replace "." file-separator)))
(defn ensure-dir
^File [dir]
(let [dir' (io/file dir)]
(if (.exists dir')
dir'
(if (.mkdirs dir')
dir'
(throw (ex-info (str "Can't create directory " dir) {:dir dir}))))))
(defn ensure-file
([file]
(ensure-file file ""))
([file contents & opts]
(let [file (io/file file)
parent (.getParent file)]
(if (.exists (io/file parent))
(apply spit file contents opts)
(do
(ensure-dir parent)
(apply spit file contents opts))))))