"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:
parent
534d2d54a5
commit
c673b3e134
|
@ -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"]
|
||||
|
|
|
@ -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]]
|
||||
|
|
|
@ -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")
|
||||
})
|
||||
|
||||
|
||||
|
|
|
@ -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)))
|
||||
|
||||
|
||||
(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
|
||||
"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)))))
|
||||
|
|
Loading…
Reference in a new issue