Compare commits

..

5 commits

Author SHA1 Message Date
bom
b11f3edd61 bump version to: 6.2.4-SNAPSHOT 2024-03-29 10:26:18 +01:00
bom
f33b86125a release: 6.2.3 2024-03-29 10:26:18 +01:00
bom
b2ce1fa75e Use inline-resource macro again 2024-03-29 10:25:48 +01:00
bom
1c0537f8e9 Add jar-file inlining 2024-03-29 10:21:05 +01:00
bom
50888233e0 Upgrade deps 2024-03-29 10:20:28 +01:00
7 changed files with 58 additions and 28 deletions

View file

@ -1,4 +1,4 @@
(defproject org.domaindrivenarchitecture/c4k-common-cljs "6.2.3-SNAPSHOT"
(defproject org.domaindrivenarchitecture/c4k-common-cljs "6.2.4-SNAPSHOT"
:description "Contains predicates and tools for c4k"
:url "https://domaindrivenarchitecture.org"
:license {:name "Apache License, Version 2.0"

View file

@ -1,10 +1,10 @@
(defproject org.domaindrivenarchitecture/c4k-common-clj "6.2.3-SNAPSHOT"
(defproject org.domaindrivenarchitecture/c4k-common-clj "6.2.4-SNAPSHOT"
:description "Contains predicates and tools for c4k"
:url "https://domaindrivenarchitecture.org"
:license {:name "Apache License, Version 2.0"
:url "https://www.apache.org/licenses/LICENSE-2.0.html"}
:dependencies [[org.clojure/clojure "1.11.1" :scope "provided"]
[org.clojure/tools.reader "1.4.0"]
:dependencies [[org.clojure/clojure "1.11.2"]
[org.clojure/tools.reader "1.4.1"]
[aero "1.1.6"]
[orchestra "2021.01.01-1"]
[expound "0.9.0"]
@ -26,7 +26,7 @@
:main dda.c4k-common.uberjar
:uberjar-name "c4k-common-standalone.jar"
:dependencies [[org.clojure/tools.cli "1.1.230"]
[ch.qos.logback/logback-classic "1.5.1"
[ch.qos.logback/logback-classic "1.5.3"
:exclusions [com.sun.mail/javax.mail]]
[org.slf4j/jcl-over-slf4j "2.0.12"]]}}
:release-tasks [["test"]

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,18 +1,14 @@
(ns dda.c4k-common.namespace.namespace-internal
(:require
[clojure.spec.alpha :as s]
#?(:cljs [shadow.resource :as rc])
#?(:clj [orchestra.core :refer [defn-spec]]
:cljs [orchestra.core :refer-macros [defn-spec]])
[dda.c4k-common.yaml :as yaml]))
[dda.c4k-common.yaml :as yaml]
#?(:cljs [dda.c4k-common.macros :refer-macros [inline-resources]])))
#?(:cljs
(defmethod yaml/load-resource :namespace [resource-name]
(case resource-name
"namespace/namespace.yaml" (rc/inline "namespace/namespace.yaml")
(throw (js/Error. (str "Undefined Resource: " resource-name))))))
(get (inline-resources "namespace") resource-name)))
(s/def ::namespace string?)

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"))))))