From 4306a96d65dc4214acccb85b41bdc44b9db70a28 Mon Sep 17 00:00:00 2001 From: jem Date: Tue, 4 Feb 2020 18:43:21 +0100 Subject: [PATCH] fs get-resources is working now --- src/cryogen_core/classpath_able_io/fs.clj | 70 +++++++++++++------ src/cryogen_core/classpath_able_io/type.clj | 25 ++----- .../classpath_able_io/fs_test.clj | 14 ++++ 3 files changed, 69 insertions(+), 40 deletions(-) diff --git a/src/cryogen_core/classpath_able_io/fs.clj b/src/cryogen_core/classpath_able_io/fs.clj index 2112641..156c563 100644 --- a/src/cryogen_core/classpath_able_io/fs.clj +++ b/src/cryogen_core/classpath_able_io/fs.clj @@ -7,13 +7,9 @@ ; You must not remove this notice, or any other, from this software. (ns cryogen-core.classpath-able-io.fs - (:require [clojure.java.io :as io] - [clojure.string :as st] - [schema.core :as s]) + (:require [cryogen-core.classpath-able-io.type :as type]) (:import [java.net URI] - [java.util.jar JarFile JarEntry] - [java.nio.file FileSystems Paths Files LinkOption StandardCopyOption] - [java.nio.file.attribute FileAttribute])) + [java.nio.file Paths Files LinkOption])) ; ----------------------- Domain functions ------------------------ (def no-link-option (into-array [LinkOption/NOFOLLOW_LINKS])) @@ -32,25 +28,55 @@ (Paths/get (user-dir) (into-array String path-elements))))) (defn path-if-exists - [full-path] - (let [path-from-fs (Paths/get (URI. (str "file://" (user-dir) "/" full-path)))] + [& path-elements] + (let [path-from-fs (Paths/get (URI. (str "file://" (apply absolut-path path-elements))))] (when (Files/exists path-from-fs follow-link-option) path-from-fs))) -(defn source-type - [] - :filesystem) - -(defn resource-type - [java-path] - (cond - (Files/isDirectory java-path follow-link-option) :dir - (Files/isRegularFile java-path follow-link-option) :file - :else :unknown)) - -; ------------------- infra --------------------------------- +(defn create-resource + ([virtual-path + java-path] + {:virtual-path virtual-path + :java-uri (.toUri java-path) + :java-path java-path + :source-type :filesystem + :resource-type (cond + (Files/isDirectory java-path follow-link-option) :dir + (Files/isRegularFile java-path follow-link-option) :file + :else :unknown)}) + ([fs-prefix + base-path + virtual-path] + (create-resource virtual-path (path-if-exists fs-prefix base-path virtual-path)))) (defn list-entries-for-dir - [java-path] - (.list (.toFile java-path))) + [resource] + (.list (.toFile (:java-path resource)))) + + (defn get-resources ;:- [Resource] + [fs-prefix ;:- Prefix + base-path ;:- VirtualPath + paths ;:- [VirtualPath] + ] + (loop [paths paths + result []] + (if (not (empty? paths)) + (let [path-to-work-with (first paths) + resource-to-work-with (create-resource + fs-prefix + base-path + path-to-work-with) + result (into result + [resource-to-work-with])] + (cond + (nil? resource-to-work-with) [] + (type/is-file? resource-to-work-with) + (recur (drop 1 paths) result) + (type/is-dir? resource-to-work-with) + (recur (into (drop 1 paths) + (map #(str path-to-work-with "/" %) + (list-entries-for-dir resource-to-work-with))) + result) + :else [])) + result))) diff --git a/src/cryogen_core/classpath_able_io/type.clj b/src/cryogen_core/classpath_able_io/type.clj index b4a65ce..602711a 100644 --- a/src/cryogen_core/classpath_able_io/type.clj +++ b/src/cryogen_core/classpath_able_io/type.clj @@ -9,12 +9,7 @@ (ns cryogen-core.classpath-able-io.type (:require [clojure.java.io :as io] [clojure.string :as st] - [schema.core :as s] - [cryogen-core.classpath-able-io.fs :as fs]) - (:import [java.net URI] - [java.util.jar JarFile JarEntry] - [java.nio.file FileSystems Paths Files LinkOption StandardCopyOption] - [java.nio.file.attribute FileAttribute])) + [schema.core :as s])) ; -------------------- Domain Definition ------------------------------ (def SourceType (s/enum :java-classpath-filesystem :java-classpath-jar :filesystem)) @@ -30,16 +25,10 @@ :java-uri JavaUri :java-path JavaPath}) -(defn create-resource - ([virtual-path - java-path] - {:virtual-path virtual-path - :java-uri (.toUri java-path) - :java-path java-path - :source-type (fs/source-type) - :resource-type (fs/resource-type java-path)})) +(s/defn is-file? :- s/Bool + [resource :- Resource] + (= :file (:resource-type resource))) -(defn - list-entries-for-dir - [resource] - (fs/list-entries-for-dir (:java-path resource))) \ No newline at end of file +(s/defn is-dir? :- s/Bool + [resource :- Resource] + (= :dir (:resource-type resource))) \ No newline at end of file diff --git a/test/cryogen_core/classpath_able_io/fs_test.clj b/test/cryogen_core/classpath_able_io/fs_test.clj index 8cbe313..9701aa4 100644 --- a/test/cryogen_core/classpath_able_io/fs_test.clj +++ b/test/cryogen_core/classpath_able_io/fs_test.clj @@ -29,3 +29,17 @@ (is (= nil (sut/path-if-exists (str fs-root "/not-existing"))))) + +(deftest test-list-entries-for-dir + (is + (= ["dummy_from_fs"] + (seq + (sut/list-entries-for-dir + (sut/create-resource "dummy" (sut/path-if-exists (str fs-root "/dummy")))))))) + +(deftest test-get-resources + (is + (= [{:virtual-path "dummy" :source-type :filesystem :resource-type :dir} + {:virtual-path "dummy/dummy_from_fs" :source-type :filesystem :resource-type :file}] + (map ftt/filter-object + (sut/get-resources fs-root "" ["dummy"])))))