From 0204170811b678504d50d88b1cdacebf4384fd83 Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 6 Dec 2019 14:51:06 +0100 Subject: [PATCH] "Mob Session DONE [ci-skip]" --- src/cryogen_core/classpath_able_io.clj | 37 ++++++++++++-------- test/cryogen_core/classpath_able_io_test.clj | 7 ++-- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/cryogen_core/classpath_able_io.clj b/src/cryogen_core/classpath_able_io.clj index 3da2276..bd65b1a 100644 --- a/src/cryogen_core/classpath_able_io.clj +++ b/src/cryogen_core/classpath_able_io.clj @@ -3,22 +3,31 @@ [clojure.string :as s])) ; TODO: loading from cpasspath results in nil even if file exists +(defn file-from-cp + [resource-path] + (let [file-from-cp (io/file (io/resource resource-path))] + (try + (when (.exists file-from-cp) + file-from-cp) + (catch Exception e + nil)))) + +(defn file-from-fs + [fs-prefix resource-path] + (let [file-from-fs (io/file (str fs-prefix resource-path))] + (try + (when (.exists file-from-fs) + file-from-fs) + (catch Exception e + nil)))) + (defn file-from-cp-or-filesystem [fs-prefix resource-path] - (let [file-from-cp (io/file (io/resource resource-path)) - file-from-fs (io/file (str fs-prefix resource-path))] - (println file-from-cp) - (println file-from-fs) - (try - (when (.exists file-from-fs) - file-from-fs) - (catch Exception e - (try (when (.exists file-from-cp) - file-from-cp) - (catch Exception e - (throw (IllegalArgumentException. - (str "resource " resource-path " neither found on classpath nor filesystem"))) - )))))) + (let [from-fs (file-from-fs fs-prefix resource-path)] + (if (some? from-fs) + from-fs + (file-from-cp resource-path)))) + (defn copy-dir [source-path target-path ignored-files] diff --git a/test/cryogen_core/classpath_able_io_test.clj b/test/cryogen_core/classpath_able_io_test.clj index 3e51390..2b41ad2 100644 --- a/test/cryogen_core/classpath_able_io_test.clj +++ b/test/cryogen_core/classpath_able_io_test.clj @@ -15,10 +15,11 @@ (and (verify-file-exists path) (.isDirectory (io/file path)))) +(deftest test-file-from-cp + (is + (sut/file-from-cp ".gitkeep"))) + (deftest test-file-from-cp-or-filesystem - (is (thrown? IllegalArgumentException - (sut/file-from-cp-or-filesystem - "./" "Not_Existing"))) (is (.exists (sut/file-from-cp-or-filesystem "./test-resources/" "templates/themes/bootstrap4-test/js")))