refactor type -> this

This commit is contained in:
jem 2020-02-07 13:49:49 +01:00
parent 5359fdd996
commit 928950e9b2
5 changed files with 34 additions and 20 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

View file

@ -11,16 +11,24 @@
[clojure.string :as st] [clojure.string :as st]
[cryogen-core.classpath-able-io.fs :as fs] [cryogen-core.classpath-able-io.fs :as fs]
[cryogen-core.classpath-able-io.jar :as jar] [cryogen-core.classpath-able-io.jar :as jar]
[cryogen-core.classpath-able-io.type :as type] [cryogen-core.classpath-able-io.this :as this]
[schema.core :as s]) [schema.core :as s])
(:import [java.net URI] (:import [java.net URI]
[java.util.jar JarFile JarEntry] [java.util.jar JarFile JarEntry]
[java.nio.file FileSystems Paths Files LinkOption StandardCopyOption] [java.nio.file FileSystems Paths Files LinkOption StandardCopyOption]
[java.nio.file.attribute FileAttribute])) [java.nio.file.attribute FileAttribute]))
(s/defn create-fs-resource :- type/Resource (def SourceType this/SourceType)
([virtual-path :- type/VirtualPath (def ResourceType this/ResourceType)
java-path :- type/JavaPath] (def Prefix this/Prefix)
(def JavaUri this/JavaUri) ; java.net.URI
(def VirtualPath this/VirtualPath)
(def JavaPath this/JavaPath) ; java.nio.Path
(def Resource this/Resource)
(s/defn create-fs-resource :- this/Resource
([virtual-path :- this/VirtualPath
java-path :- this/JavaPath]
{:virtual-path virtual-path {:virtual-path virtual-path
:java-uri (.toUri java-path) :java-uri (.toUri java-path)
:java-path java-path :java-path java-path
@ -31,9 +39,9 @@
:else :unknown)})) :else :unknown)}))
(s/defn create-cp-resource :- type/Resource (s/defn create-cp-resource :- this/Resource
([virtual-path :- type/VirtualPath ([virtual-path :- this/VirtualPath
java-path :- type/JavaPath] java-path :- this/JavaPath]
(let [java-uri (.toUri java-path)] (let [java-uri (.toUri java-path)]
{:virtual-path virtual-path {:virtual-path virtual-path
:java-uri java-uri :java-uri java-uri
@ -63,7 +71,7 @@
; contains either a jar or a file ; contains either a jar or a file
(s/defn path-from-cp ;:- JavaPath (s/defn path-from-cp ;:- JavaPath
[resource-path :- type/VirtualPath] [resource-path :- this/VirtualPath]
(try (try
(let [resource-uri (.toURI (io/resource resource-path))] (let [resource-uri (.toURI (io/resource resource-path))]
(when (jar/is-from-classpath-jar? resource-uri) (when (jar/is-from-classpath-jar? resource-uri)
@ -74,7 +82,7 @@
nil))) nil)))
(s/defn path-from-fs ;:- JavaPath (s/defn path-from-fs ;:- JavaPath
[full-path :- type/VirtualPath] [full-path :- this/VirtualPath]
(let [path-from-fs (Paths/get (URI. (str "file://" full-path)))] ;fragile (let [path-from-fs (Paths/get (URI. (str "file://" full-path)))] ;fragile
(try (try
(when (Files/exists path-from-fs fs/no-link-option) (when (Files/exists path-from-fs fs/no-link-option)
@ -133,7 +141,7 @@
(s/defn (s/defn
list-entries-for-dir ;:- [VirtualPath] list-entries-for-dir ;:- [VirtualPath]
[resource :- type/Resource] [resource :- this/Resource]
(if (= :java-classpath-jar (:source-type resource)) (if (= :java-classpath-jar (:source-type resource))
(filter-and-remove-for-dir (filter-and-remove-for-dir
(:virtual-path resource) (:virtual-path resource)
@ -167,7 +175,7 @@
; (println (:java-path resource-to-work-with)) ; (println (:java-path resource-to-work-with))
(cond (cond
(nil? resource-to-work-with) [] (nil? resource-to-work-with) []
(type/is-file? resource-to-work-with) (this/is-file? resource-to-work-with)
(recur (drop 1 paths) result) (recur (drop 1 paths) result)
:else :else
(recur (into (drop 1 paths) (recur (into (drop 1 paths)

View file

@ -7,7 +7,7 @@
; 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 [cryogen-core.classpath-able-io.type :as type]) (:require [cryogen-core.classpath-able-io.this :as type])
(:import [java.net URI] (:import [java.net URI]
[java.nio.file Paths Files LinkOption])) [java.nio.file Paths Files LinkOption]))

View file

@ -9,7 +9,7 @@
(ns cryogen-core.classpath-able-io.jar (ns cryogen-core.classpath-able-io.jar
(:require [clojure.java.io :as io] (:require [clojure.java.io :as io]
[clojure.string :as st] [clojure.string :as st]
[cryogen-core.classpath-able-io.type :as type] [cryogen-core.classpath-able-io.this :as this]
[cryogen-core.classpath-able-io.fs :as fs]) [cryogen-core.classpath-able-io.fs :as fs])
(:import [java.net URI] (:import [java.net URI]
[java.util.jar JarFile JarEntry] [java.util.jar JarFile JarEntry]
@ -61,10 +61,10 @@
(filter #(not (empty? %)) (filter #(not (empty? %))
path-elements))))] path-elements))))]
(when (is-from-classpath-jar? resource-uri) (when (is-from-classpath-jar? resource-uri)
(init-file-system resource-uri)) (do
;; TODO: hier steckt auch eine "from-fs-cp" funktionalität drinne (init-file-system resource-uri)
(when (Files/exists (Paths/get resource-uri) fs/no-link-option) (when (Files/exists (Paths/get resource-uri) fs/no-link-option)
(Paths/get resource-uri))) (Paths/get resource-uri)))))
(catch Exception e (catch Exception e
nil))) nil)))

View file

@ -6,12 +6,11 @@
; the terms of this license. ; the terms of this license.
; 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.type (ns cryogen-core.classpath-able-io.this
(: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]))
; -------------------- Domain Definition ------------------------------
(def SourceType (s/enum :java-classpath-filesystem :java-classpath-jar :filesystem)) (def SourceType (s/enum :java-classpath-filesystem :java-classpath-jar :filesystem))
(def ResourceType (s/enum :file :dir :unknown)) (def ResourceType (s/enum :file :dir :unknown))
(def Prefix s/Str) (def Prefix s/Str)
@ -32,3 +31,10 @@
(s/defn is-dir? :- s/Bool (s/defn is-dir? :- s/Bool
[resource :- Resource] [resource :- Resource]
(= :dir (:resource-type resource))) (= :dir (:resource-type resource)))
(s/defn virtual-path-from-elements :- VirtualPath
[& path-elements ;:- VirtualPath
]
(st/join "/"
(filter #(not (empty? %))
path-elements)))