now using paths instead of files

This commit is contained in:
lukas 2020-01-14 17:53:27 +01:00
parent da9bfedfd8
commit 2e98717b43
2 changed files with 60 additions and 58 deletions

View file

@ -10,7 +10,7 @@
(: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])) (:import [java.nio.file FileSystems Paths Files LinkOption]))
(def SourceType (s/enum :classpath :filesystem)) (def SourceType (s/enum :classpath :filesystem))
@ -20,14 +20,14 @@
(def Uri s/Any) ; java.net.URI (def Uri s/Any) ; java.net.URI
(def Path s/Str) (def ShortPath s/Str)
(def File s/Any) ; java.io.File (def JavaPath s/Any) ; java.nio.Path
(def Resource (def Resource
{:path Path {:short-path ShortPath
:uri Uri :uri Uri
:file File :java-path JavaPath
:source-type SourceType :source-type SourceType
:resource-type ResourceType}) :resource-type ResourceType})
@ -46,78 +46,78 @@
(filter #(not (re-matches ignore-patterns %)) source-list)) (filter #(not (re-matches ignore-patterns %)) source-list))
(s/defn create-resource :- Resource (s/defn create-resource :- Resource
([path :- Path ([short-path :- ShortPath
uri :- Uri uri :- Uri
file :- File java-path :- JavaPath
source-type :- SourceType source-type :- SourceType
resource-type :- ResourceType] resource-type :- ResourceType]
{:path path {:short-path short-path
:uri uri :uri uri
:file file :java-path java-path
:source-type source-type :source-type source-type
:resource-type resource-type}) :resource-type resource-type})
([path :- Path ([short-path :- ShortPath
file :- File java-path :- JavaPath
source-type :- SourceType] source-type :- SourceType]
{:path path {:short-path short-path
:uri (.toURI file) :uri (.toURI java-path)
:file file :java-path java-path
:source-type source-type :source-type source-type
:resource-type (cond :resource-type (cond
(.isDirectory file) :dir (Files/isDirectory java-path (into-array [LinkOption/NOFOLLOW_LINKS])) :dir
(.isFile file) :file (Files/isRegularFile java-path (into-array [LinkOption/NOFOLLOW_LINKS])) :java-path
:else :unknown)})) :else :unknown)}))
(s/defn is-file? :- s/Bool (s/defn is-file? :- s/Bool
[resource :- Resource] [resource :- Resource]
(= :file (:resource-type resource))) (= :java-path (:resource-type resource)))
(s/defn file-from-cp ; :- File (s/defn path-from-cp ; :- JavaPath
[resource-path :- Path] [resource-path :- ShortPath]
(try (try
(let [file-from-cp (io/file (io/resource resource-path))] (let [path-from-cp (Paths/get (java.net.URI. (.toString (io/resource resource-path))))]
(when (.exists file-from-cp) (when (.exists path-from-cp)
file-from-cp)) path-from-cp))
(catch Exception e (catch Exception e
nil))) nil)))
(s/defn file-from-fs ; :- File (s/defn path-from-fs ; :- JavaPath
[fs-prefix :- Prefix [fs-prefix :- Prefix
resource-path :- Path] resource-path :- ShortPath]
(let [file-from-fs (io/file (str fs-prefix resource-path))] (let [path-from-fs (Paths/get (java.net.URI. (str fs-prefix resource-path)))] ;with this, you need to give the absolute path
(try (try
(when (.exists file-from-fs) (when (.exists path-from-fs)
file-from-fs) path-from-fs)
(catch Exception e (catch Exception e
nil)))) nil))))
(defn resource-from-cp-or-fs ; :- Resource (defn resource-from-cp-or-fs ; :- Resource
[fs-prefix ; :- Prefix [fs-prefix ; :- Prefix
base-path ; :- Path base-path ; :- ShortPath
resource-path ; :- Path resource-path ; :- ShortPath
& {: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 (if (empty? base-path) (let [full-path (if (empty? base-path)
resource-path resource-path
(str base-path "/" resource-path)) (str base-path "/" resource-path))
file-from-fs (if from-fs path-from-fs (if from-fs
(file-from-fs fs-prefix full-path) (path-from-fs fs-prefix full-path)
nil) nil)
file-from-cp (if from-cp path-from-cp (if from-cp
(file-from-cp full-path) (path-from-cp full-path)
nil)] nil)]
(cond (cond
(some? file-from-fs) (some? path-from-fs)
(create-resource resource-path file-from-fs :filesystem) (create-resource resource-path path-from-fs :filesystem)
(some? file-from-cp) (some? path-from-cp)
(create-resource resource-path file-from-cp :classpath) (create-resource resource-path path-from-cp :classpath)
:else nil))) :else nil)))
(defn file-from-cp-or-fs ; :- File (defn path-from-cp-or-fs ; :- JavaPath
[fs-prefix ; :- Prefix [fs-prefix ; :- Prefix
base-path ; :- Path base-path ; :- ShortPath
resource-path; :- Path resource-path; :- ShortPath
& {:keys [from-cp from-fs] & {:keys [from-cp from-fs]
:or {from-cp true :or {from-cp true
from-fs true}}] from-fs true}}]
@ -126,12 +126,12 @@
:from-cp from-cp :from-cp from-cp
:from-fs from-fs)] :from-fs from-fs)]
(when (some? resource) (when (some? resource)
(:file resource)))) (:java-path resource))))
(defn get-resources-recursive ;:- [Resource] (defn get-resources-recursive ;:- [Resource]
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix
base-path ;:- Path base-path ;:- ShortPath
paths ;:- [Path] paths ;:- [ShortPath]
& {:keys [from-cp from-fs] & {:keys [from-cp from-fs]
:or {from-cp true :or {from-cp true
from-fs true}}] from-fs true}}]
@ -155,18 +155,18 @@
: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 (:file resource-to-work-with)))) (.list (:java-path resource-to-work-with))))
result)))) result))))
result))) result)))
(defn get-resource-paths-recursive ;:- [Path] (defn get-resource-paths-recursive ;:- [ShortPath]
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix
base-path ;:- Path base-path ;:- ShortPath
paths ;:- [Path] paths ;:- [ShortPath]
& {:keys [from-cp from-fs] & {:keys [from-cp from-fs]
:or {from-cp true :or {from-cp true
from-fs true}}] from-fs true}}]
(map #(:path %) (map #(:short-path %)
(get-resources-recursive (get-resources-recursive
fs-prefix base-path paths fs-prefix base-path paths
:from-cp from-cp :from-cp from-cp
@ -174,19 +174,19 @@
; TODO: Add files to keep ; TODO: Add files to keep
(s/defn delete-resource-recursive! (s/defn delete-resource-recursive!
[path :- s/Str] [short-path :- s/Str]
(let [resource-paths (let [resource-paths
(reverse (get-resource-paths-recursive (reverse (get-resource-paths-recursive
"" path [""] :from-cp false))] "" short-path [""] :from-cp false))]
(doseq [resource-path resource-paths] (doseq [resource-path resource-paths]
(io/delete-file (str path resource-path))))) (io/delete-file (str 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 ;:- Path base-path ;:- ShortPath
source-paths ;:- [Path] source-paths ;:- [ShortPath]
target-path ;:- Path target-path ;:- ShortPath
ignore-patterns ;:- s/Str ignore-patterns ;:- s/Str
] ]
(let [resource-paths (let [resource-paths
@ -196,7 +196,7 @@
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 (io/file target-path resource-path)
source-file (io/file (file-from-cp-or-fs source-file (io/file (path-from-cp-or-fs
fs-prefix fs-prefix
base-path base-path
resource-path))] resource-path))]
@ -206,11 +206,11 @@
(defn distinct-resources-by-path (defn distinct-resources-by-path
[resources] [resources]
(loop [paths (set (map :path resources)) (loop [paths (set (map :short-path resources))
resources resources resources resources
acc []] acc []]
(cond (empty? resources) acc (cond (empty? resources) acc
(contains? paths (:path (first resources))) (recur (disj paths (:path (first resources))) (contains? paths (:short-path (first resources))) (recur (disj paths (:short-path (first resources)))
(rest resources) (rest resources)
(conj acc (first resources))) (conj acc (first resources)))
:else (recur paths (rest resources) acc)))) :else (recur paths (rest resources) acc))))

View file

@ -22,8 +22,10 @@
; TODO: Fix this test! ; TODO: Fix this test!
(deftest test-file-from-cp (deftest test-file-from-cp
(is (is
(sut/file-from-cp (sut/path-from-cp
"dummy"))) "dummy")))
; TODO: one dummy from jar and one dummy from cp-filesystem and one from filesystem
; get resources and see all
(deftest test-resource-from-cp-or-fs (deftest test-resource-from-cp-or-fs
(is (is