fs get-resources is working now
This commit is contained in:
parent
e370ed23a0
commit
4306a96d65
3 changed files with 69 additions and 40 deletions
|
@ -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
|
||||
(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))
|
||||
|
||||
; ------------------- infra ---------------------------------
|
||||
: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)))
|
||||
|
|
|
@ -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)))
|
||||
(s/defn is-dir? :- s/Bool
|
||||
[resource :- Resource]
|
||||
(= :dir (:resource-type resource)))
|
|
@ -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"])))))
|
||||
|
|
Loading…
Reference in a new issue