Add jar-file inlining

This commit is contained in:
bom 2024-03-29 10:21:05 +01:00
parent 50888233e0
commit 1c0537f8e9
4 changed files with 50 additions and 16 deletions

View file

@ -1,9 +1,34 @@
(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]
(let [files (.listFiles (io/file (io/resource resource-path)))
file-contents (map slurp files)
file-names (map #(str resource-path "/" (.getName %)) files)]
(zipmap file-names file-contents))
)
(let [resource-url (io/resource resource-path)
resource-protocol (.getProtocol resource-url)]
(case resource-protocol
"file" (inline-resource-file resource-url resource-path)
"jar" (inline-resource-jar resource-url))))

View file

@ -1,13 +1,21 @@
(ns dda.c4k-common.macros-test
(:require
[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
(is (= 3 (count (inline-resources "dda/c4k_common/inline_resources_test")))))
(deftest should-inline-resources
(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"))))
(is (= "2" (get (inline-resources "dda/c4k_common/inline_resources_test") (resource-path "inline_resource_2.yaml"))))
(is (= "3" (get (inline-resources "dda/c4k_common/inline_resources_test") (resource-path "inline_resource_3.yaml"))))))
(let [resource-path (fn [name] (str "dda/c4k_common/inline_resources_test/" name))
inlined-resources (inline-resources "dda/c4k_common/inline_resources_test")]
(is (= "1" (get inlined-resources (resource-path "inline_resource_1.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")))))

View file

@ -7,7 +7,8 @@
(is (= 4 (count (inline-resources "ingress")))))
(deftest should-inline-resources
(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"))))
(is (= "2" (get (inline-resources "dda/c4k_common/inline_resources_test") (resource-path "inline_resource_2.yaml"))))
(is (= "3" (get (inline-resources "dda/c4k_common/inline_resources_test") (resource-path "inline_resource_3.yaml"))))))
(let [resource-path (fn [name] (str "dda/c4k_common/inline_resources_test/" name))
inlined-resources (inline-resources "dda/c4k_common/inline_resources_test")]
(is (= "1" (get inlined-resources (resource-path "inline_resource_1.yaml"))))
(is (= "2" (get inlined-resources (resource-path "inline_resource_2.yaml"))))
(is (= "3" (get inlined-resources (resource-path "inline_resource_3.yaml"))))))