diff --git a/src/elin/util/file.clj b/src/elin/util/file.clj index 750364de..3f203b67 100644 --- a/src/elin/util/file.clj +++ b/src/elin/util/file.clj @@ -1,6 +1,7 @@ (ns elin.util.file (:require [clojure.java.io :as io] + [clojure.string :as str] [elin.util.schema :as e.u.schema] [malli.core :as m])) @@ -19,3 +20,12 @@ (if (.exists file) file (recur (.getParentFile dir)))))))) + +(m/=> normalize-path [:=> [:cat string?] string?]) +(defn normalize-path [path] + (let [path (str/replace-first path #"^file:" "")] + (if (str/starts-with? path "jar:") + (-> path + (str/replace-first #"^jar:file:" "zipfile://") + (str/replace #"!/" "::")) + path))) diff --git a/test/elin/util/file_test.clj b/test/elin/util/file_test.clj index 66d48a2b..590672d4 100644 --- a/test/elin/util/file_test.clj +++ b/test/elin/util/file_test.clj @@ -22,3 +22,12 @@ (t/testing "Not found" (t/is (nil? (sut/find-file-in-parent-directories (str "non-existing" (random-uuid))))))) + + +(t/deftest normalize-path-test + (t/is (= "/foo/bar.txt" + (sut/normalize-path "/foo/bar.txt"))) + (t/is (= "/foo/bar.txt" + (sut/normalize-path "file:/foo/bar.txt"))) + (t/is (= "zipfile:///path/to/jarfile.jar::path/to/file.clj" + (sut/normalize-path "jar:file:/path/to/jarfile.jar!/path/to/file.clj"))))