add get resources

This commit is contained in:
jem 2020-02-07 20:23:13 +01:00
parent 24b55040f0
commit f92f674680
2 changed files with 46 additions and 4 deletions

View file

@ -8,12 +8,12 @@
(ns cryogen-core.classpath-able-io.cp (ns cryogen-core.classpath-able-io.cp
(:require [clojure.java.io :as io] (:require [clojure.java.io :as io]
[clojure.string :as st]
[schema.core :as s] [schema.core :as s]
[cryogen-core.classpath-able-io.this :as this] [cryogen-core.classpath-able-io.this :as this]
[cryogen-core.classpath-able-io.fs :as fs] [cryogen-core.classpath-able-io.fs :as fs]
[cryogen-core.classpath-able-io.jar :as jar]) [cryogen-core.classpath-able-io.jar :as jar])
(:import [java.net URI] (:import [java.nio.file Paths Files]))
[java.nio.file Paths Files LinkOption]))
(s/defn path-if-exists :- this/JavaPath (s/defn path-if-exists :- this/JavaPath
[& path-elements ;:- this/VirtualPath [& path-elements ;:- this/VirtualPath
@ -28,3 +28,29 @@
(Paths/get resource-uri)))) (Paths/get resource-uri))))
(catch Exception e (catch Exception e
nil))) 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)))

View file

@ -11,8 +11,7 @@
[clojure.java.io :as io] [clojure.java.io :as io]
[schema.core :as s] [schema.core :as s]
[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]))
(deftest should-find-path-on-cp (deftest should-find-path-on-cp
(is (is
@ -21,3 +20,20 @@
(sut/path-if-exists "dummy" "dummy_from_jar")) (sut/path-if-exists "dummy" "dummy_from_jar"))
(is (is
(sut/path-if-exists "dummy_only_in_cp_fs"))) (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"])))))