import URI, fix uri construction with first

This commit is contained in:
jem 2020-01-17 19:21:02 +01:00
parent 02f9c57612
commit 9a4ab2bc0f
2 changed files with 7 additions and 10 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.net Uri] (:import [java.net URI]
[java.nio.file FileSystems Paths Files SimpleFileVisitor LinkOption StandardCopyOption] [java.nio.file FileSystems Paths Files SimpleFileVisitor LinkOption StandardCopyOption]
[java.nio.file.attribute FileAttribute])) [java.nio.file.attribute FileAttribute]))
@ -78,7 +78,7 @@
(s/defn init-file-system (s/defn init-file-system
[resource-uri :- ResourceUri] [resource-uri :- ResourceUri]
(let [filesystem-uri (Uri. (pop (st/split (.toString resource-uri) #"!")))] (let [filesystem-uri (URI. (first (st/split (.toString resource-uri) #"!")))]
(try (try
(FileSystems/getFileSystem filesystem-uri) (FileSystems/getFileSystem filesystem-uri)
(catch Exception e (catch Exception e
@ -89,7 +89,7 @@
[resource-path :- ShortPath] [resource-path :- ShortPath]
(try (try
(let [resource-uri (.toURI (io/resource resource-path))] ; check if contains jar: (let [resource-uri (.toURI (io/resource resource-path))] ; check if contains jar:
(when (st/starts-with? (.toString resource-uri) "jar:") (when (= (.getScheme resource-uri) "jar")
(init-file-system resource-uri)) (init-file-system resource-uri))
(when (Files/exists (Paths/get resource-uri) NoLinkOption) (when (Files/exists (Paths/get resource-uri) NoLinkOption)
(Paths/get resource-uri))) (Paths/get resource-uri)))
@ -98,7 +98,7 @@
(s/defn path-from-fs ; :- JavaPath (s/defn path-from-fs ; :- JavaPath
[full-path :- ShortPath] [full-path :- ShortPath]
(let [path-from-fs (Paths/get (java.net.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 NoLinkOption) (when (Files/exists path-from-fs NoLinkOption)
path-from-fs) path-from-fs)
@ -205,7 +205,7 @@
[""] [""]
:from-cp false))] :from-cp false))]
(doseq [resource-path resource-paths] (doseq [resource-path resource-paths]
(Files/delete (Paths/get (java.net.URI. (str "file://" (java.lang.System/getProperty "user.dir") "/" short-path resource-path)))) (Files/delete (Paths/get (URI. (str "file://" (java.lang.System/getProperty "user.dir") "/" short-path resource-path))))
))) )))
; TODO: add ignore patterns filtering ; TODO: add ignore patterns filtering
@ -222,7 +222,7 @@
(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 (Paths/get (java.net.URI. (str "file://" (java.lang.System/getProperty "user.dir") "/" target-path "/" resource-path))) (let [target-file (Paths/get (URI. (str "file://" (java.lang.System/getProperty "user.dir") "/" target-path "/" resource-path)))
source-file (path-from-cp-or-fs source-file (path-from-cp-or-fs
fs-prefix fs-prefix
base-path base-path

View file

@ -23,13 +23,10 @@
(def NoLinkOption (into-array [LinkOption/NOFOLLOW_LINKS])) (def NoLinkOption (into-array [LinkOption/NOFOLLOW_LINKS]))
(sut/init-file-system (io/resource "dummy"))
; TODO: Fix this test! ; TODO: Fix this test!
(deftest test-file-from-cp (deftest test-file-from-cp
(is (is
(sut/path-from-cp (sut/path-from-cp "dummy")))
"dummy")))
; TODO: one dummy from jar and one dummy from cp-filesystem and one from filesystem ; TODO: one dummy from jar and one dummy from cp-filesystem and one from filesystem
; get resources and see all ; get resources and see all