Add jar-file inlining
This commit is contained in:
parent
50888233e0
commit
1c0537f8e9
4 changed files with 50 additions and 16 deletions
|
@ -1,9 +1,34 @@
|
||||||
(ns dda.c4k-common.macros
|
(ns dda.c4k-common.macros
|
||||||
(:require [clojure.java.io :as io]))
|
(:require [clojure.java.io :as io]
|
||||||
|
[clojure.string :as str])
|
||||||
|
(:import java.util.jar.JarFile))
|
||||||
|
|
||||||
|
(defn inline-resource-file [resource-url relative-resource-folder-path]
|
||||||
|
(let [files (.listFiles (io/file resource-url))
|
||||||
|
file-contents (map slurp files)
|
||||||
|
file-names (map #(str relative-resource-folder-path "/" (.getName %)) files)]
|
||||||
|
(zipmap file-names file-contents)))
|
||||||
|
|
||||||
|
(defn inline-resource-jar [resource-url]
|
||||||
|
(let [resource-url-string (.toString resource-url)
|
||||||
|
; Remove jar:file:
|
||||||
|
start-absolute (str/replace-first resource-url-string "jar:file:" "")
|
||||||
|
; Split path into jar base and search folder
|
||||||
|
jar-split (str/split start-absolute #"!/")
|
||||||
|
absolute-jar-path (first jar-split)
|
||||||
|
relative-file-path (second jar-split)
|
||||||
|
jar (JarFile. absolute-jar-path)
|
||||||
|
files (->> (enumeration-seq (.entries jar))
|
||||||
|
(filter #(str/starts-with? % relative-file-path))
|
||||||
|
(filter #(not (.isDirectory %))))
|
||||||
|
file-names (map #(.getName %) files)
|
||||||
|
file-contents (map #(slurp (.getInputStream jar %)) files)]
|
||||||
|
(zipmap file-names file-contents)))
|
||||||
|
|
||||||
(defmacro inline-resources [resource-path]
|
(defmacro inline-resources [resource-path]
|
||||||
(let [files (.listFiles (io/file (io/resource resource-path)))
|
(let [resource-url (io/resource resource-path)
|
||||||
file-contents (map slurp files)
|
resource-protocol (.getProtocol resource-url)]
|
||||||
file-names (map #(str resource-path "/" (.getName %)) files)]
|
(case resource-protocol
|
||||||
(zipmap file-names file-contents))
|
"file" (inline-resource-file resource-url resource-path)
|
||||||
)
|
"jar" (inline-resource-jar resource-url))))
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,21 @@
|
||||||
(ns dda.c4k-common.macros-test
|
(ns dda.c4k-common.macros-test
|
||||||
(:require
|
(:require
|
||||||
[clojure.test :refer [deftest is are testing run-tests]]
|
[clojure.test :refer [deftest is are testing run-tests]]
|
||||||
[dda.c4k-common.macros :refer [inline-resources]]))
|
[dda.c4k-common.macros :as cut :refer [inline-resources]]))
|
||||||
|
|
||||||
(deftest should-count-inline-resources
|
(deftest should-count-inline-resources
|
||||||
(is (= 3 (count (inline-resources "dda/c4k_common/inline_resources_test")))))
|
(is (= 3 (count (inline-resources "dda/c4k_common/inline_resources_test")))))
|
||||||
|
|
||||||
(deftest should-inline-resources
|
(deftest should-inline-resources
|
||||||
(let [resource-path (fn [name] (str "dda/c4k_common/inline_resources_test/" name))]
|
(let [resource-path (fn [name] (str "dda/c4k_common/inline_resources_test/" name))
|
||||||
(is (= "1" (get (inline-resources "dda/c4k_common/inline_resources_test") (resource-path "inline_resource_1.yaml"))))
|
inlined-resources (inline-resources "dda/c4k_common/inline_resources_test")]
|
||||||
(is (= "2" (get (inline-resources "dda/c4k_common/inline_resources_test") (resource-path "inline_resource_2.yaml"))))
|
(is (= "1" (get inlined-resources (resource-path "inline_resource_1.yaml"))))
|
||||||
(is (= "3" (get (inline-resources "dda/c4k_common/inline_resources_test") (resource-path "inline_resource_3.yaml"))))))
|
(is (= "2" (get inlined-resources (resource-path "inline_resource_2.yaml"))))
|
||||||
|
(is (= "3" (get inlined-resources (resource-path "inline_resource_3.yaml"))))))
|
||||||
|
|
||||||
|
(deftest should-inline-jar-resources
|
||||||
|
(let [jar-url (java.net.URL. "jar:file:./src/test/resources/dda/c4k_common/inline_jar_test/test.jar!/inline_resources_test/")
|
||||||
|
inlined-resources (cut/inline-resource-jar jar-url)]
|
||||||
|
(is (= "1" (get inlined-resources "inline_resources_test/inline_resource_1.yaml")))
|
||||||
|
(is (= "2" (get inlined-resources "inline_resources_test/inline_resource_2.yaml")))
|
||||||
|
(is (= "3" (get inlined-resources "inline_resources_test/inline_resource_3.yaml")))))
|
|
@ -7,7 +7,8 @@
|
||||||
(is (= 4 (count (inline-resources "ingress")))))
|
(is (= 4 (count (inline-resources "ingress")))))
|
||||||
|
|
||||||
(deftest should-inline-resources
|
(deftest should-inline-resources
|
||||||
(let [resource-path (fn [name] (str "dda/c4k_common/inline_resources_test/" name))]
|
(let [resource-path (fn [name] (str "dda/c4k_common/inline_resources_test/" name))
|
||||||
(is (= "1" (get (inline-resources "dda/c4k_common/inline_resources_test") (resource-path "inline_resource_1.yaml"))))
|
inlined-resources (inline-resources "dda/c4k_common/inline_resources_test")]
|
||||||
(is (= "2" (get (inline-resources "dda/c4k_common/inline_resources_test") (resource-path "inline_resource_2.yaml"))))
|
(is (= "1" (get inlined-resources (resource-path "inline_resource_1.yaml"))))
|
||||||
(is (= "3" (get (inline-resources "dda/c4k_common/inline_resources_test") (resource-path "inline_resource_3.yaml"))))))
|
(is (= "2" (get inlined-resources (resource-path "inline_resource_2.yaml"))))
|
||||||
|
(is (= "3" (get inlined-resources (resource-path "inline_resource_3.yaml"))))))
|
||||||
|
|
BIN
src/test/resources/dda/c4k_common/inline_jar_test/test.jar
Normal file
BIN
src/test/resources/dda/c4k_common/inline_jar_test/test.jar
Normal file
Binary file not shown.
Loading…
Reference in a new issue