Skip to content

Commit 2599009

Browse files
vemvbbatsov
authored andcommitted
Make jar/file processing more homegeneous
1 parent cb4df08 commit 2599009

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/orchard/misc.clj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
(:refer-clojure :exclude [update-keys update-vals])
44
(:require
55
[clojure.java.io :as io]
6-
[clojure.string :as str]))
6+
[clojure.string :as str]
7+
[orchard.util.io :as util.io]))
78

89
(defn os-windows? []
910
(.startsWith (System/getProperty "os.name") "Windows"))
@@ -31,7 +32,7 @@
3132
"Whether the argument is a directory or an url that points to a directory"
3233
[f]
3334
(if (url? f)
34-
(and (= (.getProtocol ^java.net.URL f) "file")
35+
(and (util.io/direct-url-to-file? f)
3536
(.isDirectory (io/as-file f)))
3637
(.isDirectory (io/as-file f))))
3738

@@ -40,7 +41,7 @@
4041
file extensions"
4142
[f & exts]
4243
(when-let [file (if (url? f)
43-
(when (= (.getProtocol ^java.net.URL f) "file")
44+
(when (util.io/direct-url-to-file? f)
4445
(io/as-file f))
4546
(io/as-file f))]
4647
(and

src/orchard/util/io.clj

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,34 @@
3131

3232
(defn url-protocol
3333
"Get the URL protocol as a string, e.g. http, file, jar."
34+
^String
3435
[^java.net.URL url]
3536
(.getProtocol url))
3637

38+
(def jar? #{"jar"})
39+
40+
(def file? #{"file"})
41+
3742
(defn url-to-file-within-archive?
38-
"Does this URL point to a file inside a jar (or zip) archive.
39-
i.e. does it use the jar: protocol."
43+
"Does this URL point to a file inside a jar (or zip) archive?
44+
i.e., does it use the jar: protocol."
45+
[^java.net.URL url]
46+
(jar? (url-protocol url)))
47+
48+
(defn direct-url-to-file?
49+
"Does this URL point to a file directly?
50+
i.e., does it use the file: protocol."
4051
[^java.net.URL url]
41-
(= "jar" (url-protocol url)))
52+
(file? (url-protocol url)))
4253

4354
(defn resource-jarfile
4455
"Given a jar:file:...!/... URL, return the location of the jar file on the
4556
filesystem. Returns nil on any other URL."
4657
^File [^URL jar-resource]
47-
(assert (= "jar" (url-protocol jar-resource)))
58+
(assert (jar? (url-protocol jar-resource)))
4859
(let [^JarURLConnection conn (.openConnection jar-resource)
4960
inner-url (.getJarFileURL conn)]
50-
(when (= "file" (url-protocol inner-url))
61+
(when (file? (url-protocol inner-url))
5162
(io/as-file inner-url))))
5263

5364
(defn resource-artifact
@@ -59,12 +70,14 @@
5970
Throws when the URL is not a `file:` or `jar:` URL."
6071
^File [^java.net.URL resource]
6172
(let [protocol (url-protocol resource)]
62-
(case protocol
63-
"file"
73+
(cond
74+
(file? protocol)
6475
(io/as-file resource)
65-
"jar"
76+
77+
(jar? protocol)
6678
(resource-jarfile resource)
67-
#_else
79+
80+
:else
6881
(throw (ex-info (str "URLs with a " protocol
6982
" protocol can't be situated on the filesystem.")
7083
{:resource resource})))))

0 commit comments

Comments
 (0)