Create an init command

This commit is contained in:
Yann Esposito (Yogsototh) 2024-01-15 23:03:50 +01:00
parent cf35312e6d
commit 94d9d6127e
Signed by untrusted user who does not match committer: yogsototh
GPG Key ID: 7B19A4C650D59646
3 changed files with 37 additions and 10 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.local/
.cpcache/

10
deps.edn Normal file
View File

@ -0,0 +1,10 @@
{:deps {org.clojure/tools.cli {:mvn/version "1.0.214"}
ring/ring-core {:mvn/version "1.11.0"}
ring/ring-jetty-adapter {:mvn/version "1.11.0"}
babashka/fs {:mvn/version "0.5.20"}
;; logs for jetty
org.slf4j/slf4j-api {:mvn/version "2.0.9"}
ch.qos.logback/logback-classic {:mvn/version "1.4.11"}
net.logstash.logback/logstash-logback-encoder {:mvn/version "7.4"}
ch.codesmith/logger {:mvn/version "0.7.108"}
}}

35
statyk
View File

@ -10,6 +10,11 @@
ring/ring-core {:mvn/version "1.11.0"}
ring/ring-jetty-adapter {:mvn/version "1.11.0"}
babashka/fs {:mvn/version "0.5.20"}
;; logs for jetty
org.slf4j/slf4j-api {:mvn/version "2.0.9"}
ch.qos.logback/logback-classic {:mvn/version "1.4.11"}
net.logstash.logback/logstash-logback-encoder {:mvn/version "7.4"}
ch.codesmith/logger {:mvn/version "0.7.108"}
}}
'
@ -40,10 +45,11 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@"
[ring.adapter.jetty :refer [run-jetty]]))
(def config
{:source-directory "_src"
:dest-directory "_site"
:port 13375
:host "127.0.0.1"})
{:source-directory "_src"
:dest-directory "_site"
:templates-directory "_templates"
:port 13375
:host "127.0.0.1"})
(def cli-options
[])
@ -76,10 +82,10 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@"
(defn serve-dest-dir-handler
[request]
(let [path (str dest-directory (:uri request))]
(let [path (str (:dest-directory config) (:uri request))]
(if-let [content (read-file path)]
(let [mime-type (mime/ext-mime-type path)]
(cond-> (resp/ok content)
(cond-> (resp/response content)
mime-type (resp/content-type mime-type)))
{:status 404
:header {"Content-Type" "text/plain"}
@ -96,7 +102,7 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@"
(defn replace-extension
[f new-extension]
(str (fs/strip-ext f) "." new-extention))
(str (fs/strip-ext f) "." new-extension))
(defn dst-file-name-and-ext
[src-file]
@ -121,6 +127,7 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@"
)
(defn build-img
[src-file dst-file]
;; TODO jpg => webp
)
@ -136,7 +143,7 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@"
(defn generate-static-files
[]
(doseq [src-file (file-seq (:source-directory config))]
(let [{:keys [ext dst-file]} (dst-file-name src-file)]
(let [{:keys [ext dst-file]} (dst-file-name-and-ext src-file)]
(when (is-newer? src-file dst-file)
(build src-file dst-file ext)))))
@ -150,6 +157,14 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@"
"- clean delete generated files in _site"]))
(System/exit 0))
(defn init-site
[options]
(mapv (fn [d]
(println (format "Create dir: %s" d))
(fs/create-dirs d)) [(:source-directory config)
(:dest-directory config)
(:templates-directory config)]))
(defn -main [& args]
(let [{:keys [options arguments summary errors]
:as parsed}
@ -166,9 +181,9 @@ exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@"
{:port 13375
:host "127.0.0.1"}))
"gen" (generate-static-files)
"init" (init-site options)
(show-help)
(err! (format "unknown command %s" command))))
(pp/pprint parsed)))
(err! (format "unknown command %s" command))))))
(apply -main *command-line-args*)