From c673b3e134e08512443184716d2fbda6d4518c70 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Fri, 10 Dec 2021 11:45:24 +0000 Subject: [PATCH] "It's a good sort of brake but it doesn't work yet" Trying to list resource names from jar file... --- project.clj | 2 ++ src/mw_ui/routes/home.clj | 2 +- src/mw_ui/routes/params.clj | 4 +-- src/mw_ui/util.clj | 59 +++++++++++++++++++++++++++++-------- 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/project.clj b/project.clj index 582373c..3a05862 100644 --- a/project.clj +++ b/project.clj @@ -1,6 +1,8 @@ (defproject mw-ui "0.1.6-SNAPSHOT" :description "Web-based user interface for MicroWorld" :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-parser "0.1.6-SNAPSHOT"] [lib-noir "0.9.9"] diff --git a/src/mw_ui/routes/home.clj b/src/mw_ui/routes/home.clj index 0e82dc1..fe6b86d 100644 --- a/src/mw_ui/routes/home.clj +++ b/src/mw_ui/routes/home.clj @@ -1,7 +1,7 @@ (ns ^{:doc "Routes which serve the main pages of the application." :author "Simon Brooke"} mw-ui.routes.home - (:require [clojure.java/io :refer [file]] + (:require [clojure.java.io :refer [file]] [clojure.walk :refer [keywordize-keys]] [compojure.core :refer [defroutes GET POST]] [hiccup.core :refer [html]] diff --git a/src/mw_ui/routes/params.clj b/src/mw_ui/routes/params.clj index 2d6d781..993d45e 100644 --- a/src/mw_ui/routes/params.clj +++ b/src/mw_ui/routes/params.clj @@ -35,9 +35,9 @@ (defn- send-params [] {: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) - :rulesets (util/list-resources "/rulesets" #"([0-9a-z-_]+).txt") + :rulesets (util/list-resources "/rulesets" #"/?([0-9a-z-_]+).txt") }) diff --git a/src/mw_ui/util.clj b/src/mw_ui/util.clj index c5ff74d..5f975b6 100644 --- a/src/mw_ui/util.clj +++ b/src/mw_ui/util.clj @@ -1,10 +1,12 @@ (ns ^{:doc "Utility functions used by other namespaces in this package." :author "Simon Brooke"} - mw-ui.util + mw-ui.util (:require [clojure.java.io :refer [file]] + [clojure.string :refer [starts-with?]] + [markdown.core :as md] [noir.io :as io] [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 "reads a markdown file from public/md and returns an HTML string" [filename] (->> - (io/slurp-resource filename) - (md/md-to-html-string))) + (io/slurp-resource filename) + (md/md-to-html-string))) -;; 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 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))))) + + +(defn list-resources "List resource files matching `pattern` in `directory`." [directory pattern] (let - [path (str (io/resource-path) directory)] + [path (str (io/resource-path) directory)] (session/put! :list-resources-path path) - (sort - (remove nil? - (map #(first (rest (re-matches pattern (.getName %)))) - (file-seq (file path))))))) + (try + (sort + (remove nil? + (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)))))