"It's a good sort of brake but it doesn't work yet"

Trying to list resource names from jar file...
This commit is contained in:
Simon Brooke 2021-12-10 11:45:24 +00:00
parent 534d2d54a5
commit c673b3e134
No known key found for this signature in database
GPG key ID: A7A4F18D1D4DF987
4 changed files with 51 additions and 16 deletions

View file

@ -1,6 +1,8 @@
(defproject mw-ui "0.1.6-SNAPSHOT" (defproject mw-ui "0.1.6-SNAPSHOT"
:description "Web-based user interface for MicroWorld" :description "Web-based user interface for MicroWorld"
:dependencies [[org.clojure/clojure "1.10.3"] :dependencies [[org.clojure/clojure "1.10.3"]
[clj-time "0.15.2"] ;; this is a hack. Something in libnoir requires
;; JodaTime, but doesn't request it. clj-time does.
[mw-engine "0.1.6-SNAPSHOT"] [mw-engine "0.1.6-SNAPSHOT"]
[mw-parser "0.1.6-SNAPSHOT"] [mw-parser "0.1.6-SNAPSHOT"]
[lib-noir "0.9.9"] [lib-noir "0.9.9"]

View file

@ -1,7 +1,7 @@
(ns ^{:doc "Routes which serve the main pages of the application." (ns ^{:doc "Routes which serve the main pages of the application."
:author "Simon Brooke"} :author "Simon Brooke"}
mw-ui.routes.home mw-ui.routes.home
(:require [clojure.java/io :refer [file]] (:require [clojure.java.io :refer [file]]
[clojure.walk :refer [keywordize-keys]] [clojure.walk :refer [keywordize-keys]]
[compojure.core :refer [defroutes GET POST]] [compojure.core :refer [defroutes GET POST]]
[hiccup.core :refer [html]] [hiccup.core :refer [html]]

View file

@ -35,9 +35,9 @@
(defn- send-params [] (defn- send-params []
{:title "Choose your world" {:title "Choose your world"
:heightmaps (util/list-resources "/img/heightmaps" #"([0-9a-z-_]+).png") :heightmaps (util/list-resources "/img/heightmaps" #"/?([0-9a-z-_]+).png")
:pause (or (session/get :pause) 5) :pause (or (session/get :pause) 5)
:rulesets (util/list-resources "/rulesets" #"([0-9a-z-_]+).txt") :rulesets (util/list-resources "/rulesets" #"/?([0-9a-z-_]+).txt")
}) })

View file

@ -1,10 +1,12 @@
(ns ^{:doc "Utility functions used by other namespaces in this package." (ns ^{:doc "Utility functions used by other namespaces in this package."
:author "Simon Brooke"} :author "Simon Brooke"}
mw-ui.util mw-ui.util
(:require [clojure.java.io :refer [file]] (:require [clojure.java.io :refer [file]]
[clojure.string :refer [starts-with?]]
[markdown.core :as md]
[noir.io :as io] [noir.io :as io]
[noir.session :as session] [noir.session :as session]
[markdown.core :as md])) [taoensso.timbre :as timbre]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
@ -29,25 +31,56 @@
;;;; ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def running-from-filesystem (atom true))
(def compile-time-resources
"The resources which were visible at compile time. If we are running from
a JAR file, it is highly likely that these are all the resources available
at run time."
(let [n (count (io/resource-path))]
(remove nil?
(map #(let [s (str %)]
(when (> (count s) n)
(subs s 56)))
(file-seq (file (io/resource-path)))))))
(defn md->html (defn md->html
"reads a markdown file from public/md and returns an HTML string" "reads a markdown file from public/md and returns an HTML string"
[filename] [filename]
(->> (->>
(io/slurp-resource filename) (io/slurp-resource filename)
(md/md-to-html-string))) (md/md-to-html-string)))
(defn cache-seq-match
"Do the same processing that list-resources does on names fetched from
the file system, except on the resource list cached at compile time."
[path pattern]
(let [n (count path)]
(remove nil?
(map #(when (> (count %) n)
(let [name (subs % n)]
(first (rest (re-matches pattern name)))))
(filter #(starts-with? % path)
compile-time-resources)))))
;; TODO: The reason we can't list files in a jar file, and what to do about it,
;; is here. Too tired to fix this tonight.
;; https://stackoverflow.com/questions/46488466/clojure-list-subfolders-in-resources-in-uberjar
(defn list-resources (defn list-resources
"List resource files matching `pattern` in `directory`." "List resource files matching `pattern` in `directory`."
[directory pattern] [directory pattern]
(let (let
[path (str (io/resource-path) directory)] [path (str (io/resource-path) directory)]
(session/put! :list-resources-path path) (session/put! :list-resources-path path)
(sort (try
(remove nil? (sort
(map #(first (rest (re-matches pattern (.getName %)))) (remove nil?
(file-seq (file path))))))) (if @running-from-filesystem
(map #(first (rest (re-matches pattern (.getName %))))
(file-seq (file path)))
(cache-seq-match directory pattern))))
(catch Exception any
(timbre/log (str "Not running from filesystem?"
(.getName (.getClass any))))
(reset! running-from-filesystem false)
(cache-seq-match directory pattern)))))