From 53d7c4332d538bb6757b9a1350f244a3e227b150 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 3 Aug 2022 17:06:53 +0200 Subject: [PATCH] :tada: Add prefetch builtin templates script --- backend/scripts/build | 5 ++-- backend/scripts/prefetch-templates.clj | 40 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100755 backend/scripts/prefetch-templates.clj diff --git a/backend/scripts/build b/backend/scripts/build index 1537b7f91..98174a5c1 100755 --- a/backend/scripts/build +++ b/backend/scripts/build @@ -17,5 +17,6 @@ cp scripts/manage.template.sh target/dist/manage.sh; chmod +x target/dist/run.sh; chmod +x target/dist/manage.sh; - - +# Prefetch +bb ./scripts/prefetch-templates.clj resources/app/onboarding.edn builtin-templates/ +cp -r builtin-templates target/dist/ diff --git a/backend/scripts/prefetch-templates.clj b/backend/scripts/prefetch-templates.clj new file mode 100755 index 000000000..68936b158 --- /dev/null +++ b/backend/scripts/prefetch-templates.clj @@ -0,0 +1,40 @@ +#!/usr/bin/env bb + +(require '[babashka.curl :as curl] + '[babashka.fs :as fs]) + +(defn download-if-needed! + [dest data] + (doseq [{:keys [id file-uri] :as item} data] + (let [file (fs/file dest id) + rsp (curl/get file-uri {:as :stream})] + + (when (not= 200 (:status rsp)) + (println (format "unable to download %s (uri: %s)" id file-uri)) + (System/exit -1)) + + (when-not (fs/exists? (str file)) + (println (format "=> downloading %s" id)) + (with-open [output (io/output-stream file)] + (io/copy (:body rsp) output)))))) + +(defn read-defs-file + [path] + (with-open [content (io/reader path)] + (edn/read-string (slurp content)))) + +(let [[path dest] *command-line-args*] + (when (or (nil? path) + (nil? dest)) + (println "invalid arguments") + (System/exit -1)) + + (when-not (fs/exists? path) + (println (format "file %s does not exists" path)) + (System/exit -1)) + + (when-not (fs/exists? dest) + (fs/create-dirs dest)) + + (let [data (read-defs-file path)] + (download-if-needed! dest data)))