From f92f6746808b69d165b37ed3c49ebcb7b99631ae Mon Sep 17 00:00:00 2001 From: jem Date: Fri, 7 Feb 2020 20:23:13 +0100 Subject: [PATCH] add get resources --- src/cryogen_core/classpath_able_io/cp.clj | 30 +++++++++++++++++-- .../classpath_able_io/cp_test.clj | 20 +++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/cryogen_core/classpath_able_io/cp.clj b/src/cryogen_core/classpath_able_io/cp.clj index 82a36f3..c578fe9 100644 --- a/src/cryogen_core/classpath_able_io/cp.clj +++ b/src/cryogen_core/classpath_able_io/cp.clj @@ -8,12 +8,12 @@ (ns cryogen-core.classpath-able-io.cp (:require [clojure.java.io :as io] + [clojure.string :as st] [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])) + (:import [java.nio.file Paths Files])) (s/defn path-if-exists :- this/JavaPath [& path-elements ;:- this/VirtualPath @@ -28,3 +28,29 @@ (Paths/get resource-uri)))) (catch Exception e nil))) + +(s/defn fs-prefix + [java-path + base-path + path] + (let [java-path-str (.toString java-path)] + (subs java-path-str 0 (- (count java-path-str) (+ (count base-path) (count path) 2))))) + +(s/defn get-resources ;:- [this/Resource] + "base-path is sensible for getting the right jar from classpath. So base-path + should be specific enough for the jar desired. Paths must not be empty." + [base-path :- this/VirtualPath + paths :- [this/VirtualPath]] + (flatten + (map + (fn [p] + (if-let [path-to-work-with (path-if-exists base-path p)] + (if (jar/is-from-classpath-jar? (.toUri path-to-work-with)) + (jar/get-resources base-path [p]) + (fs/get-resources + (fs-prefix path-to-work-with base-path p) + base-path + [p] + :java-classpath-filesystem)) + [])) + paths))) \ No newline at end of file diff --git a/test/cryogen_core/classpath_able_io/cp_test.clj b/test/cryogen_core/classpath_able_io/cp_test.clj index a42ecae..affb4e5 100644 --- a/test/cryogen_core/classpath_able_io/cp_test.clj +++ b/test/cryogen_core/classpath_able_io/cp_test.clj @@ -11,8 +11,7 @@ [clojure.java.io :as io] [schema.core :as s] [cryogen-core.file-test-tools :as ftt] - [cryogen-core.classpath-able-io.cp :as sut]) - (:import [java.net URI])) + [cryogen-core.classpath-able-io.cp :as sut])) (deftest should-find-path-on-cp (is @@ -21,3 +20,20 @@ (sut/path-if-exists "dummy" "dummy_from_jar")) (is (sut/path-if-exists "dummy_only_in_cp_fs"))) + + +(deftest should-get-resources-from-jar-and-fs-classpath + (is (= + [] + (sut/get-resources "" ["not-existing"]))) + (is (= + [{:virtual-path "dummy_from_jar", :source-type :java-classpath-jar, :resource-type :file}] + (map ftt/filter-object + (sut/get-resources "dummy" ["dummy_from_jar"])))) + (is (= + [{:virtual-path "test_pages/home" :source-type :java-classpath-filesystem :resource-type :dir} + {:virtual-path "test_pages/home/.gitkeep" :source-type :java-classpath-filesystem :resource-type :file} + {:virtual-path "test_posts/home" :source-type :java-classpath-filesystem :resource-type :dir} + {:virtual-path "test_posts/home/.gitkeep" :source-type :java-classpath-filesystem :resource-type :file}] + (map ftt/filter-object + (sut/get-resources "templates/md" ["test_pages/home" "test_posts/home"]))))) \ No newline at end of file