fixed some bugs & replaced io with nio

This commit is contained in:
lukas 2020-01-15 15:53:52 +01:00
parent 2e98717b43
commit d3227b3f55
2 changed files with 25 additions and 17 deletions

View file

@ -10,7 +10,8 @@
(: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])
(:import [java.nio.file FileSystems Paths Files LinkOption])) (:import [java.nio.file FileSystems Paths Files LinkOption StandardCopyOption]
[java.nio.file.attribute FileAttribute]))
(def SourceType (s/enum :classpath :filesystem)) (def SourceType (s/enum :classpath :filesystem))
@ -60,7 +61,7 @@
java-path :- JavaPath java-path :- JavaPath
source-type :- SourceType] source-type :- SourceType]
{:short-path short-path {:short-path short-path
:uri (.toURI java-path) :uri (java.net.URI. (str "file://" (.toString java-path)))
:java-path java-path :java-path java-path
:source-type source-type :source-type source-type
:resource-type (cond :resource-type (cond
@ -76,7 +77,7 @@
[resource-path :- ShortPath] [resource-path :- ShortPath]
(try (try
(let [path-from-cp (Paths/get (java.net.URI. (.toString (io/resource resource-path))))] (let [path-from-cp (Paths/get (java.net.URI. (.toString (io/resource resource-path))))]
(when (.exists path-from-cp) (when (Files/exists path-from-cp (into-array [LinkOption/NOFOLLOW_LINKS]))
path-from-cp)) path-from-cp))
(catch Exception e (catch Exception e
nil))) nil)))
@ -84,9 +85,9 @@
(s/defn path-from-fs ; :- JavaPath (s/defn path-from-fs ; :- JavaPath
[fs-prefix :- Prefix [fs-prefix :- Prefix
resource-path :- ShortPath] resource-path :- ShortPath]
(let [path-from-fs (Paths/get (java.net.URI. (str fs-prefix resource-path)))] ;with this, you need to give the absolute path (let [path-from-fs (Paths/get (java.net.URI. (str "file://" fs-prefix resource-path)))] ;with this, you need to give the absolute path
(try (try
(when (.exists path-from-fs) (when (Files/exists path-from-fs (into-array [LinkOption/NOFOLLOW_LINKS]))
path-from-fs) path-from-fs)
(catch Exception e (catch Exception e
nil)))) nil))))
@ -155,7 +156,7 @@
:else :else
(recur (into (drop 1 paths) (recur (into (drop 1 paths)
(map #(str path-to-work-with "/" %) (map #(str path-to-work-with "/" %)
(.list (:java-path resource-to-work-with)))) (.list (io/file (.toString (:java-path resource-to-work-with)))))) ; TODO better function
result)))) result))))
result))) result)))
@ -177,16 +178,20 @@
[short-path :- s/Str] [short-path :- s/Str]
(let [resource-paths (let [resource-paths
(reverse (get-resource-paths-recursive (reverse (get-resource-paths-recursive
"" short-path [""] :from-cp false))] (str (java.lang.System/getProperty "user.dir") "/")
short-path
[""]
:from-cp false))]
(doseq [resource-path resource-paths] (doseq [resource-path resource-paths]
(io/delete-file (str short-path resource-path))))) (Files/delete (Paths/get (java.net.URI. (str "file://" (java.lang.System/getProperty "user.dir") "/" short-path resource-path))))
)))
; TODO: add ignore patterns filtering ; TODO: add ignore patterns filtering
(defn copy-resources! (defn copy-resources!
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix
base-path ;:- ShortPath base-path ;:- ShortPath
source-paths ;:- [ShortPath] source-paths ;:- [ShortPath]
target-path ;:- ShortPath target-path ;:- ShortPath #TODO is actually no path ???
ignore-patterns ;:- s/Str ignore-patterns ;:- s/Str
] ]
(let [resource-paths (let [resource-paths
@ -195,14 +200,16 @@
(throw (IllegalArgumentException. (str "resource " resource-paths ", " (throw (IllegalArgumentException. (str "resource " resource-paths ", "
source-paths " not found"))) source-paths " not found")))
(doseq [resource-path resource-paths] (doseq [resource-path resource-paths]
(let [target-file (io/file target-path resource-path) (let [target-file (Paths/get (java.net.URI. (str "file://" (java.lang.System/getProperty "user.dir") "/" target-path "/" resource-path)))
source-file (io/file (path-from-cp-or-fs source-file (path-from-cp-or-fs
fs-prefix fs-prefix
base-path base-path
resource-path))] resource-path)]
(io/make-parents target-file) (when (Files/isDirectory source-file (into-array [LinkOption/NOFOLLOW_LINKS]))
(when (.isFile source-file) (Files/createDirectories target-file (into-array FileAttribute [])))
(io/copy source-file target-file))))))) (when (Files/isRegularFile source-file (into-array [LinkOption/NOFOLLOW_LINKS]))
(Files/copy source-file target-file (into-array StandardCopyOption [StandardCopyOption/COPY_ATTRIBUTES StandardCopyOption/REPLACE_EXISTING]))
))))))
(defn distinct-resources-by-path (defn distinct-resources-by-path
[resources] [resources]