path-if-exists on cp level

This commit is contained in:
jem 2020-02-07 13:50:43 +01:00
parent c6149801c4
commit 5330124428
3 changed files with 24 additions and 5 deletions

View file

@ -7,7 +7,24 @@
; You must not remove this notice, or any other, from this software. ; You must not remove this notice, or any other, from this software.
(ns cryogen-core.classpath-able-io.cp (ns cryogen-core.classpath-able-io.cp
(:require [cryogen-core.classpath-able-io.type :as type]) (:require [clojure.java.io :as io]
[schema.core :as s]
[cryogen-core.classpath-able-io.this :as this]
[cryogen-core.classpath-able-io.fs :as fs]
[cryogen-core.classpath-able-io.jar :as jar])
(:import [java.net URI] (:import [java.net URI]
[java.nio.file Paths Files LinkOption])) [java.nio.file Paths Files LinkOption]))
(s/defn path-if-exists :- this/JavaPath
[& path-elements ;:- this/VirtualPath
]
(try
(let [resource-uri
(.toURI (io/resource
(apply this/virtual-path-from-elements path-elements)))]
(if (jar/is-from-classpath-jar? resource-uri)
(apply jar/path-if-exists path-elements)
(when (Files/exists (Paths/get resource-uri) fs/no-link-option)
(Paths/get resource-uri))))
(catch Exception e
nil)))

View file

@ -13,3 +13,9 @@
[cryogen-core.file-test-tools :as ftt] [cryogen-core.file-test-tools :as ftt]
[cryogen-core.classpath-able-io.cp :as sut]) [cryogen-core.classpath-able-io.cp :as sut])
(:import [java.net URI])) (:import [java.net URI]))
(deftest should-find-path-on-cp
(is
(sut/path-if-exists "dummy"))
(is
(sut/path-if-exists "dummy_only_in_cp_fs")))

View file

@ -19,10 +19,6 @@
(def target "target/tmp") (def target "target/tmp")
(deftest test-file-from-cp
(is
(sut/path-from-cp "dummy")))
(deftest test-resource-from-cp-or-fs (deftest test-resource-from-cp-or-fs
(is (is
(some? (sut/resource-from-cp-or-fs (some? (sut/resource-from-cp-or-fs