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.
|
; You must not remove this notice, or any other, from this software.
|
||||||
|
|
||||||
(ns cryogen-core.classpath-able-io.fs
|
(ns cryogen-core.classpath-able-io.fs
|
||||||
(:require [clojure.java.io :as io]
|
(:require [cryogen-core.classpath-able-io.type :as type])
|
||||||
[clojure.string :as st]
|
|
||||||
[schema.core :as s])
|
|
||||||
(:import [java.net URI]
|
(:import [java.net URI]
|
||||||
[java.util.jar JarFile JarEntry]
|
[java.nio.file Paths Files LinkOption]))
|
||||||
[java.nio.file FileSystems Paths Files LinkOption StandardCopyOption]
|
|
||||||
[java.nio.file.attribute FileAttribute]))
|
|
||||||
|
|
||||||
; ----------------------- Domain functions ------------------------
|
; ----------------------- Domain functions ------------------------
|
||||||
(def no-link-option (into-array [LinkOption/NOFOLLOW_LINKS]))
|
(def no-link-option (into-array [LinkOption/NOFOLLOW_LINKS]))
|
||||||
|
@ -32,25 +28,55 @@
|
||||||
(Paths/get (user-dir) (into-array String path-elements)))))
|
(Paths/get (user-dir) (into-array String path-elements)))))
|
||||||
|
|
||||||
(defn path-if-exists
|
(defn path-if-exists
|
||||||
[full-path]
|
[& path-elements]
|
||||||
(let [path-from-fs (Paths/get (URI. (str "file://" (user-dir) "/" full-path)))]
|
(let [path-from-fs (Paths/get (URI. (str "file://" (apply absolut-path path-elements))))]
|
||||||
(when (Files/exists path-from-fs follow-link-option)
|
(when (Files/exists path-from-fs follow-link-option)
|
||||||
path-from-fs)))
|
path-from-fs)))
|
||||||
|
|
||||||
(defn source-type
|
(defn create-resource
|
||||||
[]
|
([virtual-path
|
||||||
:filesystem)
|
java-path]
|
||||||
|
{:virtual-path virtual-path
|
||||||
(defn resource-type
|
:java-uri (.toUri java-path)
|
||||||
[java-path]
|
:java-path java-path
|
||||||
(cond
|
:source-type :filesystem
|
||||||
|
:resource-type (cond
|
||||||
(Files/isDirectory java-path follow-link-option) :dir
|
(Files/isDirectory java-path follow-link-option) :dir
|
||||||
(Files/isRegularFile java-path follow-link-option) :file
|
(Files/isRegularFile java-path follow-link-option) :file
|
||||||
:else :unknown))
|
:else :unknown)})
|
||||||
|
([fs-prefix
|
||||||
; ------------------- infra ---------------------------------
|
base-path
|
||||||
|
virtual-path]
|
||||||
|
(create-resource virtual-path (path-if-exists fs-prefix base-path virtual-path))))
|
||||||
|
|
||||||
(defn
|
(defn
|
||||||
list-entries-for-dir
|
list-entries-for-dir
|
||||||
[java-path]
|
[resource]
|
||||||
(.list (.toFile java-path)))
|
(.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
|
(ns cryogen-core.classpath-able-io.type
|
||||||
(:require [clojure.java.io :as io]
|
(:require [clojure.java.io :as io]
|
||||||
[clojure.string :as st]
|
[clojure.string :as st]
|
||||||
[schema.core :as s]
|
[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]))
|
|
||||||
|
|
||||||
; -------------------- Domain Definition ------------------------------
|
; -------------------- Domain Definition ------------------------------
|
||||||
(def SourceType (s/enum :java-classpath-filesystem :java-classpath-jar :filesystem))
|
(def SourceType (s/enum :java-classpath-filesystem :java-classpath-jar :filesystem))
|
||||||
|
@ -30,16 +25,10 @@
|
||||||
:java-uri JavaUri
|
:java-uri JavaUri
|
||||||
:java-path JavaPath})
|
:java-path JavaPath})
|
||||||
|
|
||||||
(defn create-resource
|
(s/defn is-file? :- s/Bool
|
||||||
([virtual-path
|
[resource :- Resource]
|
||||||
java-path]
|
(= :file (:resource-type resource)))
|
||||||
{: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)}))
|
|
||||||
|
|
||||||
(defn
|
(s/defn is-dir? :- s/Bool
|
||||||
list-entries-for-dir
|
[resource :- Resource]
|
||||||
[resource]
|
(= :dir (:resource-type resource)))
|
||||||
(fs/list-entries-for-dir (:java-path resource)))
|
|
|
@ -29,3 +29,17 @@
|
||||||
(is
|
(is
|
||||||
(= nil
|
(= nil
|
||||||
(sut/path-if-exists (str fs-root "/not-existing")))))
|
(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