-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.clj
169 lines (131 loc) · 3.77 KB
/
paths.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
(ns pathological.paths
(:refer-clojure :exclude [name resolve])
(:require
[clojure.java.io :as io]
[pathological.utils :as u]
[pathological.file-systems :as fs]
[pathological.path-matchers :as pm]
[pathological.file-stores :as fst])
(:import
[java.nio.file FileSystem Path Files Paths]
[java.net URI]))
(defprotocol ^:private Pathable
(->path ^Path [this paths]))
(extend-type FileSystem
Pathable
(->path ^Path [^FileSystem this [path & paths]]
(.getPath this path (u/->varargs-array String paths))))
(extend-type String
Pathable
(->path ^Path [^String this paths]
(.getPath fs/*file-system* this (u/->varargs-array String paths))))
(extend-type Path
Pathable
(->path ^Path [^Path this paths]
(if (seq paths)
(.getPath (.getFileSystem this)
(str this)
(u/->varargs-array String (map str paths)))
this)))
(extend-type URI
Pathable
(->path ^Path [^URI this paths]
(let [path (Paths/get this)]
(if (seq paths)
(.getPath (.getFileSystem path)
(str path)
(u/->varargs-array String (map str paths)))
path))))
(defn- opts->open-options-array [opts]
(let [opts-map (when opts (apply hash-map opts))
open-options (if (:append opts-map) [:append] [])
open-options (u/->open-options-array open-options)]
open-options))
(extend Path
io/IOFactory
(assoc io/default-streams-impl
:make-input-stream
(fn [^Path path opts]
(io/make-input-stream
(Files/newInputStream path (opts->open-options-array opts)) opts))
:make-output-stream
(fn [^Path path opts]
(io/make-output-stream
(Files/newOutputStream path (opts->open-options-array opts)) opts))))
(defn path [& [base-path & paths]]
(->path base-path paths))
(defn subpath [^Path path from to]
(.subpath path from to))
(defn file-system [^Path path]
(.getFileSystem path))
(defn file-store [^Path path]
(fst/->file-store (Files/getFileStore path)))
(defn root [^Path path]
(.getRoot path))
(defn parent [^Path path]
(.getParent path))
(defn file-name [^Path path]
(.getFileName path))
(defn name-count [^Path path]
(.getNameCount path))
(defn name [^Path path index]
(.getName path index))
(defn names [^Path path]
(iterator-seq
(.iterator path)))
(defn normalize [^Path path]
(.normalize path))
(defmulti resolve
(fn [_ other] (type other)))
(defmethod resolve Path
[^Path base ^Path other]
(.resolve base other))
(defmethod resolve String
[^Path base ^String other]
(.resolve base other))
(defmulti resolve-sibling
(fn [_ other] (type other)))
(defmethod resolve-sibling Path
[^Path base ^Path other]
(.resolveSibling base other))
(defmethod resolve-sibling String
[^Path base ^String other]
(.resolveSibling base other))
(defn relativize [^Path base-path ^Path path]
(.relativize base-path path))
(defmulti starts-with?
(fn [_ other] (type other)))
(defmethod starts-with? Path
[^Path path ^Path other]
(.startsWith path other))
(defmethod starts-with? String
[^Path path ^String other]
(.startsWith path other))
(defmulti ends-with?
(fn [_ other] (type other)))
(defmethod ends-with? Path
[^Path path ^Path other]
(.endsWith path other))
(defmethod ends-with? String
[^Path path ^String other]
(.endsWith path other))
(defn absolute?
[^Path path]
(.isAbsolute path))
(defn matches?
[^Path path syntax-and-pattern]
(pm/matches? (pm/path-matcher syntax-and-pattern) path))
(defn ->absolute-path
[^Path path]
(.toAbsolutePath path))
(defn ->real-path
[^Path path & options]
(let [^"[Ljava.nio.file.LinkOption;"
link-options (u/->link-options-array options)]
(.toRealPath path link-options)))
(defn ->file
[^Path path]
(.toFile path))
(defn ->uri
[^Path path]
(.toUri path))