-
Notifications
You must be signed in to change notification settings - Fork 10
/
file.clj
191 lines (154 loc) · 5.96 KB
/
file.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
(ns leiningen.polylith.file
(:require [clojure.string :as str]
[clojure.java.io :as io]
[zprint.core :as zp])
(:import [java.io File PushbackReader]
[java.nio.file Files LinkOption]
[java.nio.file.attribute FileAttribute PosixFilePermission]))
(defn execute-fn [f message path]
(try
(f)
(catch Exception e
(println (str "Warning. " message " '" path "': " (.getMessage e))))))
(defn rename! [root-path from to]
(.renameTo (File. (str root-path "/" from))
(File. (str root-path "/" to))))
(defn delete-file!
([path]
(delete-file! path true))
([path silently]
(execute-fn #(io/delete-file path silently)
"Could not delete file" path)))
(defn delete-dir [path]
(doseq [f (reverse (file-seq (clojure.java.io/file path)))]
(if (or (Files/isSymbolicLink (.toPath f)) (.exists f))
(delete-file! f true))))
(defn paths [dir-path]
"Returns all directories and files in a directory recursively"
(drop-last (reverse (file-seq (io/file dir-path)))))
(defn filter-invalid-paths [paths]
(filter #(not (or (and (= File (type %)) (.isDirectory %))
(str/starts-with? (str %) ".")
(str/includes? (str %) "/.")
(str/includes? (str %) "/target/")))
paths))
(defn valid-paths [ws-path]
(filter-invalid-paths (paths ws-path)))
(defn files [dir-path]
"Returns all files in a directory recursively"
(filter #(.isFile %) (paths dir-path)))
(defn relative-paths [path]
(let [length (inc (count path))]
(map #(str (subs % length))
(map str (paths path)))))
(defn changed? [file point-in-time]
(> (.lastModified file) point-in-time))
(defn ->time-and-name [file length]
(vector (.lastModified file)
(subs (str file) length)))
(defn ->name [file length]
(subs (str file) length))
(defn changed-relative-paths [include-time? path paths point-in-time]
(let [length (inc (count path))]
(map #(if include-time? (->time-and-name % length) (->name % length))
(filter #(changed? % point-in-time) paths))))
(defn latest-modified [paths]
(reduce max (map #(.lastModified %) paths)))
(defn file-exists [path]
(.exists (io/as-file path)))
(defn create-dir [^String path]
(.mkdir (File. path)))
(defn str->path [path]
(.toPath (clojure.java.io/file path)))
(defn create-symlink [path target]
(when-not (file-exists path)
(execute-fn
#(Files/createSymbolicLink (str->path path)
(str->path target)
(make-array FileAttribute 0))
"Could not create symbolic link" path)))
(defn create-file [path rows]
(delete-file! path true)
(doseq [row rows]
(execute-fn
#(spit path (str row "\n") :append true)
"Could not create file" path)))
(defn replace-file! [path content]
(delete-file! path)
(create-file path content))
(defn read-file [path]
(with-open [rdr (-> path
(io/reader)
(PushbackReader.))]
(doall
(take-while #(not= ::done %)
(repeatedly #(try (read rdr)
(catch Exception _ ::done)))))))
(defn path->filename [path]
(last (str/split path #"/")))
(defn file->real-path [file-path]
(let [path (str (.toRealPath (.toPath file-path) (into-array LinkOption [])))]
(if (str/starts-with? path "/private")
(subs path 8)
path)))
(defn- keep? [path]
(not (str/starts-with? (path->filename path) ".")))
(defn- component-path [dir path]
(let [parts (str/split (subs path (count dir)) #"/")]
[(second parts) path]))
(defn paths-in-dir [dir]
(let [f (clojure.java.io/file dir)
fs (file-seq f)
paths (map str (filter #(.isFile %) fs))
file-paths (filter keep? paths)]
(map #(component-path dir %) file-paths)))
(defn path->dir-name [file-path]
(let [dir (last (str/split (str file-path) #"/"))]
(str/replace dir #"_" "-")))
(defn directory? [file]
(or (Files/isSymbolicLink (.toPath file))
(-> file file->real-path str clojure.java.io/file .isDirectory)
(.isDirectory file)))
(defn directories [path]
(let [files (.listFiles (clojure.java.io/file path))]
(filter directory? files)))
(defn directory-names [dir]
(filterv #(not (= "target" %))
(map path->dir-name (directories dir))))
(defn current-path []
(let [path (.getAbsolutePath (File. "."))]
(subs path 0 (- (count path) 2))))
(defn create-temp-dir! [folder-name]
(let [temp-file (execute-fn #(File/createTempFile folder-name "")
"Could not create directory in temp directory" folder-name)
_ (.delete temp-file)
_ (.mkdirs temp-file)]
(.getPath temp-file)))
(defn make-executable! [file-path]
(let [path (.toPath (File. ^String file-path))
rights (hash-set PosixFilePermission/OWNER_READ
PosixFilePermission/OWNER_WRITE
PosixFilePermission/OWNER_EXECUTE)]
(execute-fn #(Files/setPosixFilePermissions path rights)
"Could not make file executable" path)))
(defn copy-resource-file! [source target-path]
(delete-file! target-path true)
(let [resource-file (io/input-stream (io/resource source))
target-file (io/file target-path)]
(execute-fn #(io/copy resource-file target-file)
"Could not copy resource file" target-path)))
(defn write-to-file [path filename content]
(spit path (zp/zprint-file-str (str content "\n")
filename
{:width 60
:map {:comma? false}
:vector {:respect-nl? true
:wrap-coll? false}
:style :community})))
(defn list-files [path]
(mapv str (.listFiles (File. path))))
(defn contains-file [path file-name]
(not
(empty?
(filterv #(str/ends-with? % (str "/" file-name))
(list-files path)))))