From be796c16bfb03da032c229176116120da364ca28 Mon Sep 17 00:00:00 2001 From: lukas Date: Fri, 17 Jan 2020 13:45:42 +0100 Subject: [PATCH] fixed some test & added Filesystem --- src/cryogen_core/classpath_able_io.clj | 52 ++++++++++++-------- src/cryogen_core/new_io.clj | 9 ---- test/cryogen_core/classpath_able_io_test.clj | 30 ++++++----- test/cryogen_core/file_test_tools.clj | 2 +- 4 files changed, 49 insertions(+), 44 deletions(-) diff --git a/src/cryogen_core/classpath_able_io.clj b/src/cryogen_core/classpath_able_io.clj index 5257245..9f4b025 100644 --- a/src/cryogen_core/classpath_able_io.clj +++ b/src/cryogen_core/classpath_able_io.clj @@ -75,35 +75,44 @@ [resource :- Resource] (= :file (:resource-type resource))) +(s/defn create-FileSystem + [Resourece-Uri :- Uri] + (let [path-to-filesystem (st/join (pop (st/split (.toString Resourece-Uri) #"!")))] + (try + (FileSystems/newFileSystem + (java.net.URI. path-to-filesystem) + {}) + (catch Exception e + nil)))) + ; contains either a jar or a file (s/defn path-from-cp ; :- JavaPath [resource-path :- ShortPath] (try - (let [path-from-cp (Paths/get (.toURI (io/resource resource-path)))] ; check if contains jar: - (when (Files/exists path-from-cp NoLinkOption) - path-from-cp)) + (let [Resource-Uri (.toURI (io/resource resource-path))] ; check if contains jar: + (when (st/starts-with? (.toString Resource-Uri) "jar:") + (create-FileSystem Resource-Uri)) + (when (Files/exists (Paths/get Resource-Uri) NoLinkOption) + (Paths/get Resource-Uri))) (catch Exception e nil))) (s/defn path-from-fs ; :- JavaPath - [fs-prefix :- Prefix - resource-path :- ShortPath] - (let [path-from-fs (Paths/get (java.net.URI. (str "file://" fs-prefix resource-path)))] ;fragile + [full-path :- ShortPath] + (let [path-from-fs (Paths/get (java.net.URI. (str "file://" full-path)))] ;fragile (try (when (Files/exists path-from-fs NoLinkOption) path-from-fs) (catch Exception e nil)))) -; TODO move to path-from-cp -(s/defn path-from-jar ; :- JavaPath - [resource-path :- ShortPath] - (let [uri (.toUri (io/resource "dummy"))] - (try - (when (Files/exists path-from-fs NoLinkOption) - path-from-fs) - (catch Exception e - nil)))) +(s/defn construct-full-path + [fs-prefix + base-path + resource-path] + (if (st/starts-with? fs-prefix "./") + (str (st/replace fs-prefix #"\./" (str (java.lang.System/getProperty "user.dir") "/")) "/" base-path "/" resource-path) + (str fs-prefix "/" base-path "/" resource-path))) (defn resource-from-cp-or-fs ; :- Resource [fs-prefix ; :- Prefix @@ -112,14 +121,15 @@ & {:keys [from-cp from-fs] :or {from-cp true from-fs true}}] - (let [full-path (if (empty? base-path) - resource-path - (str base-path "/" resource-path)) + (let [full-path (construct-full-path fs-prefix base-path resource-path) + cp-path (if (empty? base-path) + resource-path + (str base-path "/" resource-path)) path-from-fs (if from-fs - (path-from-fs fs-prefix full-path) + (path-from-fs full-path) nil) path-from-cp (if from-cp - (path-from-cp full-path) + (path-from-cp cp-path) nil)] (cond (some? path-from-fs) @@ -204,7 +214,7 @@ [fs-prefix ;:- Prefix base-path ;:- ShortPath source-paths ;:- [ShortPath] - target-path ;:- ShortPath #TODO is actually no path ??? + target-path ;:- ShortPath ignore-patterns ;:- s/Str ] (let [resource-paths diff --git a/src/cryogen_core/new_io.clj b/src/cryogen_core/new_io.clj index 7802de8..7c2410b 100644 --- a/src/cryogen_core/new_io.clj +++ b/src/cryogen_core/new_io.clj @@ -50,12 +50,3 @@ (doseq [resource resources] (io/make-parents (io/file (str target-path "/" (:path resource)))) (.mkdir (io/file (str target-path "/" (:path resource))))))) - -; TODO lookup if we need to close the stream -(defn extract-file-from-jar - [Uri - file-path] - (let [zippath (.getPath - (FileSystems/getFileSystem (java.net.URI. Uri)) file-path - (into-array String '()))] - (apply str (map char (Files/readAllBytes zippath))))) diff --git a/test/cryogen_core/classpath_able_io_test.clj b/test/cryogen_core/classpath_able_io_test.clj index ca23bb6..9775c5c 100644 --- a/test/cryogen_core/classpath_able_io_test.clj +++ b/test/cryogen_core/classpath_able_io_test.clj @@ -11,7 +11,9 @@ [clojure.java.io :as io] [schema.core :as s] [cryogen-core.file-test-tools :as ftt] - [cryogen-core.classpath-able-io :as sut])) + [cryogen-core.classpath-able-io :as sut]) + (:import [java.nio.file FileSystems Paths Files LinkOption StandardCopyOption] + [java.nio.file.attribute FileAttribute])) (s/set-fn-validation! true) @@ -19,6 +21,8 @@ (def target "target/tmp") +(def NoLinkOption (into-array [LinkOption/NOFOLLOW_LINKS])) + ; TODO: Fix this test! (deftest test-file-from-cp (is @@ -29,27 +33,27 @@ (deftest test-resource-from-cp-or-fs (is - (.exists - (:file + (Files/exists + (:java-path (sut/resource-from-cp-or-fs - "./test-resources/" + "./test-resources" "templates/themes/bootstrap4-test" - "js")))) + "js")) NoLinkOption)) (is - (.exists - (:file + (Files/exists + (:java-path (sut/resource-from-cp-or-fs - "./" "" ".gitkeep")))) + "./" "" ".gitkeep")) NoLinkOption)) (is (some? (sut/resource-from-cp-or-fs - "./test-resources/" + "./test-resources" "templates/themes/bootstrap4-test" "js"))) (is (some? (sut/resource-from-cp-or-fs "./not-existing-so-load-from-cp" "" ".gitkeep"))) (is (= - {:path "js/subdir" + {:short-path "js/subdir" :source-type :classpath :resource-type :dir} (ftt/filter-object @@ -63,7 +67,7 @@ [] (sut/get-resources-recursive "" "templates/themes/bootstrap4-test" ["not-existing"]))) (is (= - [{:path "js/dummy.js" + [{:short-path "js/dummy.js" :source-type :classpath :resource-type :file}] (map ftt/filter-object @@ -77,7 +81,7 @@ ["js/subdir" "js/subdir/subdummy.js" "js/subdir/test.js"] - (sort (map :path + (sort (map :short-path (sut/get-resources-recursive "" "templates/themes/bootstrap4-test" ["js/subdir"]))))) (is (= @@ -92,7 +96,7 @@ "./js/subdir" "./js/subdir/subdummy.js" "./js/subdir/test.js"] - (sort (map :path + (sort (map :short-path (sut/get-resources-recursive "" "templates/themes/bootstrap4-test" ["."])))))) diff --git a/test/cryogen_core/file_test_tools.clj b/test/cryogen_core/file_test_tools.clj index 8d0e40f..ed39b68 100644 --- a/test/cryogen_core/file_test_tools.clj +++ b/test/cryogen_core/file_test_tools.clj @@ -19,6 +19,6 @@ (defn filter-object [e] - {:path (:path e) + {:short-path (:short-path e) :source-type (:source-type e) :resource-type (:resource-type e)})