diff --git a/src/cryogen_core/classpath_able_io.clj b/src/cryogen_core/classpath_able_io.clj index 26b1498..7e161de 100644 --- a/src/cryogen_core/classpath_able_io.clj +++ b/src/cryogen_core/classpath_able_io.clj @@ -11,6 +11,7 @@ [clojure.string :as st] [schema.core :as s]) (:import [java.net URI] + [java.util.jar JarFile JarEntry] [java.nio.file FileSystems Paths Files SimpleFileVisitor LinkOption StandardCopyOption] [java.nio.file.attribute FileAttribute])) @@ -93,13 +94,17 @@ (st/join "/") (#(st/replace % #"/+" "/")))) +(s/defn + filesystem-uri + [resource-uri :- JavaUri] + (URI. (first (st/split (.toString resource-uri) #"!")))) + (s/defn init-file-system [resource-uri :- JavaUri] - (let [filesystem-uri (URI. (first (st/split (.toString resource-uri) #"!")))] - (try - (FileSystems/getFileSystem filesystem-uri) + (try + (FileSystems/getFileSystem (filesystem-uri resource-uri)) (catch Exception e - (FileSystems/newFileSystem filesystem-uri {}))))) + (FileSystems/newFileSystem (filesystem-uri resource-uri) {})))) ; contains either a jar or a file (s/defn path-from-cp ;:- JavaPath @@ -160,9 +165,24 @@ (when (some? resource) (:java-path resource)))) -(s/defn list-entries-for-dir ;:- [VirtualPath] - [resource :- Resource] - (.list (io/file (.toString (:java-path resource))))) ; TODO doesnt work in jars +(s/defn + list-entries-for-dir ;:- [VirtualPath] + [resource :- Resource] + (if (= :java-classpath-jar (:source-type resource)) + (filter + (fn [je] (and (st/starts-with? je (:virtual-path resource)) + (not (= je (str (:virtual-path resource) "/"))))) + (map #(.getName ^JarEntry %) + (enumeration-seq + (.entries + (JarFile. + (.toFile + (Paths/get + (URI. + (.getSchemeSpecificPart + (filesystem-uri (:java-uri resource))))))))))) + ; Bsp-Code: https://github.com/clojure/java.classpath/blob/c10fc96a8ff98db4eb925a13ef0f5135ad8dacc6/src/main/clojure/clojure/java/classpath.clj#L50 + (.list (.toFile (:java-path resource))))) (defn get-resources-recursive ;:- [Resource] [fs-prefix ;:- Prefix @@ -193,7 +213,6 @@ :else (recur (into (drop 1 paths) (map #(str path-to-work-with "/" %) - ; Bsp-Code: https://github.com/clojure/java.classpath/blob/c10fc96a8ff98db4eb925a13ef0f5135ad8dacc6/src/main/clojure/clojure/java/classpath.clj#L50 (list-entries-for-dir resource-to-work-with))) result)))) result))) diff --git a/test/cryogen_core/classpath_able_io_test.clj b/test/cryogen_core/classpath_able_io_test.clj index f582bfa..734c348 100644 --- a/test/cryogen_core/classpath_able_io_test.clj +++ b/test/cryogen_core/classpath_able_io_test.clj @@ -68,7 +68,7 @@ "./not-existing-so-load-from-cp" "templates/themes/bootstrap4-test" "js/subdir"))))) - (is (= ["dummy-from-jar"] + (is (= ["dummy_from_jar"] (sut/list-entries-for-dir (sut/resource-from-cp-or-fs "not-existing-filesystem-path" "" diff --git a/test/cryogen_core/classpath_able_io_test.repl b/test/cryogen_core/classpath_able_io_test.repl index 431eb14..48c8724 100644 --- a/test/cryogen_core/classpath_able_io_test.repl +++ b/test/cryogen_core/classpath_able_io_test.repl @@ -24,3 +24,20 @@ TODO: to solve reading from jars: "" "dummy")) + +(filter + #(str/starts-with? % "dummy") + (map + #(.getName ^JarEntry %) + (enumeration-seq + (.entries + (JarFile. + (.toFile + (Paths/get + (URI. + (.getSchemeSpecificPart + (filesystem-uri + (:java-uri (resource-from-cp-or-fs + "not-existing-filesystem-path" + "" + "dummy"))))))))))))