implement jar-path-if-exits
This commit is contained in:
parent
505ab143a3
commit
b27deffd7d
2 changed files with 53 additions and 2 deletions
|
@ -7,11 +7,55 @@
|
|||
; You must not remove this notice, or any other, from this software.
|
||||
|
||||
(ns cryogen-core.classpath-able-io.jar
|
||||
(:require [cryogen-core.classpath-able-io.type :as type])
|
||||
(:require [clojure.java.io :as io]
|
||||
[clojure.string :as st]
|
||||
[cryogen-core.classpath-able-io.type :as type]
|
||||
[cryogen-core.classpath-able-io.fs :as fs])
|
||||
(:import [java.net URI]
|
||||
[java.nio.file Paths Files LinkOption]))
|
||||
[java.nio.file Paths Files FileSystems]))
|
||||
|
||||
(defn is-from-classpath-jar?
|
||||
[uri ;:- JavaUri
|
||||
]
|
||||
(= (.getScheme uri) "jar"))
|
||||
|
||||
(defn create-resource
|
||||
([virtual-path ;:- VirtualPath
|
||||
java-path ;:- JavaPath
|
||||
]
|
||||
(let [java-uri (.toUri java-path)]
|
||||
{:virtual-path virtual-path
|
||||
:java-uri java-uri
|
||||
:java-path java-path
|
||||
:source-type :java-classpath-jar
|
||||
:resource-type (cond
|
||||
(Files/isDirectory java-path fs/no-link-option) :dir
|
||||
(Files/isRegularFile java-path fs/no-link-option) :file
|
||||
: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) {}))))
|
||||
|
||||
(defn path-if-exists ;:- JavaPath
|
||||
[resource-path ;:- VirtualPath
|
||||
]
|
||||
(try
|
||||
(let [resource-uri (.toURI (io/resource resource-path))]
|
||||
(when (is-from-classpath-jar? resource-uri)
|
||||
(init-file-system resource-uri))
|
||||
;; TODO: hier steckt auch eine "from-fs-cp" funktionalität drinne
|
||||
(when (Files/exists (Paths/get resource-uri) fs/no-link-option)
|
||||
(Paths/get resource-uri)))
|
||||
(catch Exception e
|
||||
nil)))
|
|
@ -18,3 +18,10 @@
|
|||
(is
|
||||
(sut/is-from-classpath-jar? (.toURI (io/resource "dummy"))))
|
||||
)
|
||||
|
||||
(deftest test-path-if-exists
|
||||
(is
|
||||
(sut/path-if-exists "dummy/dummy_from_jar")))
|
||||
(is
|
||||
(= nil
|
||||
(sut/path-if-exists "not-existing")))
|
||||
|
|
Loading…
Reference in a new issue