remove unused code

This commit is contained in:
jem 2020-02-14 18:37:38 +01:00
parent 0d84c309e3
commit 2239a211ea
4 changed files with 47 additions and 159 deletions

View file

@ -7,16 +7,12 @@
; 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 (ns cryogen-core.classpath-able-io
(:require [clojure.java.io :as io] (:require [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.cp :as cp] [cryogen-core.classpath-able-io.cp :as cp]
[cryogen-core.classpath-able-io.this :as this] [cryogen-core.classpath-able-io.this :as this]
[schema.core :as s]) [schema.core :as s])
(:import [java.net URI] (:import [java.nio.file Files StandardCopyOption]
[java.util.jar JarFile JarEntry]
[java.nio.file FileSystems Paths Files LinkOption StandardCopyOption]
[java.nio.file.attribute FileAttribute])) [java.nio.file.attribute FileAttribute]))
(def SourceType this/SourceType) (def SourceType this/SourceType)
@ -27,40 +23,10 @@
(def JavaPath this/JavaPath) ; java.nio.Path (def JavaPath this/JavaPath) ; java.nio.Path
(def Resource this/Resource) (def Resource this/Resource)
(defn create-fs-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 fs/no-link-option) :dir
(Files/isRegularFile java-path fs/no-link-option) :file
:else :unknown)}))
(s/defn create-cp-resource :- this/Resource
([virtual-path :- this/VirtualPath
java-path :- this/JavaPath]
(let [java-uri (.toUri java-path)]
{:virtual-path virtual-path
:java-uri java-uri
:java-path java-path
:source-type (cond (jar/is-from-classpath-jar? java-uri)
:java-classpath-jar
:else :java-classpath-filesystem)
:resource-type (cond
(Files/isDirectory java-path fs/no-link-option) :dir
(Files/isRegularFile java-path fs/no-link-option) :file
:else :unknown)})))
(defn filter-for-ignore-patterns (defn filter-for-ignore-patterns
[ignore-patterns source-list] [ignore-patterns source-list]
(filter #(not (re-matches ignore-patterns %)) source-list)) (filter #(not (re-matches ignore-patterns %)) source-list))
; ------------------- infra ---------------------------------
; TODO replace this fn ? ; TODO replace this fn ?
(defn path (defn path
"Creates path from given parts, ignore empty elements" "Creates path from given parts, ignore empty elements"
@ -70,27 +36,6 @@
(st/join "/") (st/join "/")
(#(st/replace % #"/+" "/")))) (#(st/replace % #"/+" "/"))))
; contains either a jar or a file
(s/defn path-from-cp ;:- JavaPath
[resource-path :- this/VirtualPath]
(try
(let [resource-uri (.toURI (io/resource resource-path))]
(when (jar/is-from-classpath-jar? resource-uri)
(jar/init-file-system resource-uri))
(when (Files/exists (Paths/get resource-uri) fs/no-link-option)
(Paths/get resource-uri)))
(catch Exception e
nil)))
(s/defn path-from-fs ;:- JavaPath
[full-path :- this/VirtualPath]
(let [path-from-fs (Paths/get (URI. (str "file://" full-path)))] ;fragile
(try
(when (Files/exists path-from-fs fs/no-link-option)
path-from-fs)
(catch Exception e
nil))))
(defn resource-from-cp-or-fs ;:- Resource (defn resource-from-cp-or-fs ;:- Resource
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix
base-path ;:- VirtualPath base-path ;:- VirtualPath
@ -98,21 +43,15 @@
& {:keys [from-cp from-fs] & {:keys [from-cp from-fs]
:or {from-cp true :or {from-cp true
from-fs true}}] from-fs true}}]
(let [full-path (.toString (fs/absolut-path fs-prefix base-path resource-path)) (let [path-from-fs (when from-fs
cp-path (if (empty? base-path) (fs/path-if-exists fs-prefix base-path resource-path))
resource-path path-from-cp (when from-cp
(str base-path "/" resource-path)) (cp/path-if-exists))]
path-from-fs (if from-fs
(path-from-fs full-path)
nil)
path-from-cp (if from-cp
(path-from-cp cp-path)
nil)]
(cond (cond
(some? path-from-fs) (some? path-from-fs)
(create-fs-resource resource-path path-from-fs) (fs/create-resource resource-path path-from-fs)
(some? path-from-cp) (some? path-from-cp)
(create-cp-resource resource-path path-from-cp) (cp/create-resource resource-path path-from-cp)
:else nil))) :else nil)))
(defn path-from-cp-or-fs ; :- JavaPath (defn path-from-cp-or-fs ; :- JavaPath
@ -129,64 +68,6 @@
(when (some? resource) (when (some? resource)
(:java-path resource)))) (:java-path resource))))
(defn filter-and-remove-for-dir
[path-to-filter-for
elements-list]
(let [norm-path-to-filter-for (str path-to-filter-for "/")]
(map
#(subs % (count norm-path-to-filter-for))
(filter
(fn [element] (and (st/starts-with? element norm-path-to-filter-for)
(not (= element norm-path-to-filter-for))))
elements-list))))
(s/defn
list-entries-for-dir ;:- [VirtualPath]
[resource :- this/Resource]
(if (= :java-classpath-jar (:source-type resource))
(filter-and-remove-for-dir
(:virtual-path resource)
(map #(.getName ^JarEntry %)
(enumeration-seq
(.entries
(jar/jar-file-for-resource resource)))))
(.list (.toFile (:java-path resource)))))
(defn get-resources-recursive-old ;:- [Resource]
[fs-prefix ;:- Prefix
base-path ;:- VirtualPath
paths ;:- [VirtualPath]
& {:keys [from-cp from-fs]
:or {from-cp true
from-fs true}}]
(loop [paths paths
result []]
(if (not (empty? paths))
(do
(let [path-to-work-with (first paths)
resource-to-work-with (resource-from-cp-or-fs
fs-prefix
base-path
path-to-work-with
:from-cp from-cp
:from-fs from-fs)
result (into result
[resource-to-work-with])]
; (println path-to-work-with)
; (println (:java-path resource-to-work-with))
(cond
(nil? resource-to-work-with) []
(this/is-file? resource-to-work-with)
(recur (drop 1 paths) result)
:else
(recur (into (drop 1 paths)
(map #(str path-to-work-with "/" %)
(list-entries-for-dir resource-to-work-with)))
result))))
result)))
; TODO: rename? Allow base-path to be ""? ; TODO: rename? Allow base-path to be ""?
; base-path must not be "" ; base-path must not be ""
(defn get-resources-recursive ;:- [Resource] (defn get-resources-recursive ;:- [Resource]

View file

@ -36,6 +36,16 @@
(let [java-path-str (.toString java-path)] (let [java-path-str (.toString java-path)]
(subs java-path-str 0 (- (count java-path-str) (+ (count base-path) (count path) 2))))) (subs java-path-str 0 (- (count java-path-str) (+ (count base-path) (count path) 2)))))
(defn create-resource
([virtual-path
java-path]
(let [is-jar-resource (jar/is-from-classpath-jar? (.toUri java-path))]
(when (some? java-path)
(if is-jar-resource
(jar/create-resource virtual-path java-path)
(fs/create-resource virtual-path java-path :java-classpath-filesystem))
))))
(s/defn get-resources ;:- [this/Resource] (s/defn get-resources ;:- [this/Resource]
"base-path is sensible for getting the right jar from classpath. So base-path "base-path is sensible for getting the right jar from classpath. So base-path
should be specific enough for the jar desired. Paths must not be empty." should be specific enough for the jar desired. Paths must not be empty."

View file

@ -20,6 +20,35 @@
] ]
(= (.getScheme uri) "jar")) (= (.getScheme uri) "jar"))
(defn
filesystem-uri
[resource-uri ;:- JavaUri
]
(URI. (first (st/split (.toString resource-uri) #"!"))))
(defn init-file-system
[resource-uri ;:- JavaUri
]
(try
(FileSystems/getFileSystem (filesystem-uri resource-uri))
(catch Exception e
(FileSystems/newFileSystem (filesystem-uri resource-uri) {}))))
(defn path-if-exists ;:- JavaPath
[& path-elements ;:- VirtualPath
]
(try
(let [resource-uri
(.toURI (io/resource
(apply this/virtual-path-from-elements path-elements)))]
(when (is-from-classpath-jar? resource-uri)
(do
(init-file-system resource-uri)
(when (Files/exists (Paths/get resource-uri) fs/no-link-option)
(Paths/get resource-uri)))))
(catch Exception e
nil)))
(defn create-resource (defn create-resource
([virtual-path ;:- VirtualPath ([virtual-path ;:- VirtualPath
java-path ;:- JavaPath java-path ;:- JavaPath
@ -36,36 +65,6 @@
(Files/isRegularFile java-path fs/no-link-option) :file (Files/isRegularFile java-path fs/no-link-option) :file
:else :unknown)})))) :else :unknown)}))))
(defn
filesystem-uri
[resource-uri ;:- JavaUri
]
(URI. (first (st/split (.toString resource-uri) #"!"))))
(defn init-file-system
[resource-uri ;:- JavaUri
]
(try
(FileSystems/getFileSystem (filesystem-uri resource-uri))
(catch Exception e
(FileSystems/newFileSystem (filesystem-uri resource-uri) {}))))
; (path-if-exists "") => ".../test"
(defn path-if-exists ;:- JavaPath
[& path-elements ;:- VirtualPath
]
(try
(let [resource-uri
(.toURI (io/resource
(apply this/virtual-path-from-elements path-elements)))]
(when (is-from-classpath-jar? resource-uri)
(do
(init-file-system resource-uri)
(when (Files/exists (Paths/get resource-uri) fs/no-link-option)
(Paths/get resource-uri)))))
(catch Exception e
nil)))
(defn filter-and-remove-for-dir (defn filter-and-remove-for-dir
[base-path-to-filter-for [base-path-to-filter-for
elements-list] elements-list]

View file

@ -15,8 +15,6 @@
(s/set-fn-validation! true) (s/set-fn-validation! true)
(def theme "bootstrap4-test")
(deftest test-get-resources-recursive (deftest test-get-resources-recursive
(is (= (is (=
[] []