From 53301244284aab5ca8720768f697c0f3c51a6f3b Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 7 Feb 2020 13:50:43 +0100 Subject: [PATCH] path-if-exists on cp level --- src/cryogen_core/classpath_able_io/cp.clj | 19 ++++++++++++++++++- .../classpath_able_io/cp_test.clj | 6 ++++++ test/cryogen_core/classpath_able_io_test.clj | 4 ---- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/cryogen_core/classpath_able_io/cp.clj b/src/cryogen_core/classpath_able_io/cp.clj index ae85347..82a36f3 100644 --- a/src/cryogen_core/classpath_able_io/cp.clj +++ b/src/cryogen_core/classpath_able_io/cp.clj @@ -7,7 +7,24 @@ ; You must not remove this notice, or any other, from this software. (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] [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))) diff --git a/test/cryogen_core/classpath_able_io/cp_test.clj b/test/cryogen_core/classpath_able_io/cp_test.clj index 12033b0..2118fde 100644 --- a/test/cryogen_core/classpath_able_io/cp_test.clj +++ b/test/cryogen_core/classpath_able_io/cp_test.clj @@ -13,3 +13,9 @@ [cryogen-core.file-test-tools :as ftt] [cryogen-core.classpath-able-io.cp :as sut]) (: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"))) diff --git a/test/cryogen_core/classpath_able_io_test.clj b/test/cryogen_core/classpath_able_io_test.clj index 4711808..432a046 100644 --- a/test/cryogen_core/classpath_able_io_test.clj +++ b/test/cryogen_core/classpath_able_io_test.clj @@ -19,10 +19,6 @@ (def target "target/tmp") -(deftest test-file-from-cp - (is - (sut/path-from-cp "dummy"))) - (deftest test-resource-from-cp-or-fs (is (some? (sut/resource-from-cp-or-fs