fixed some test & added Filesystem

This commit is contained in:
lukas 2020-01-17 13:45:42 +01:00
parent 151be9f92f
commit be796c16bf
4 changed files with 49 additions and 44 deletions

View file

@ -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)
(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

View file

@ -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)))))

View file

@ -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" ["."]))))))

View file

@ -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)})