Skip to content

Commit

Permalink
package template in zip for uberjar (#249)
Browse files Browse the repository at this point in the history
fs/copy-tree does not work if the source is a resource from a jar file
so we create a zip file with the template and put that in the jar

fs/unzip does not work if the source is a resource from a jar file
so we use our own unzip
  • Loading branch information
Sohalt committed Mar 7, 2024
1 parent 38292df commit 6737566
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/nextjournal/garden_cli/template.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[babashka.fs :as fs]
[cheshire.core :as cheshire]
[clojure.string :as str]
[clojure.java.io :as io]))
[clojure.java.io :as io]
[nextjournal.garden-cli.unzip :as unzip]))

;; adapted from https://github.com/babashka/neil

Expand Down Expand Up @@ -64,9 +65,14 @@
(spit path)))

(defn template [target-dir]
(let [perms "rwxr-xr-x"]
(fs/copy-tree (fs/path (io/resource "project-template")) target-dir {:replace-existing true
:posix-file-permissions perms})
(fs/walk-file-tree target-dir {:pre-visit-dir (fn [dir _] (fs/set-posix-file-permissions dir perms) :continue)
:visit-file (fn [file _] (fs/set-posix-file-permissions file perms) :continue)})
(let [perms "rwxr-xr-x"
template (io/resource "project-template")]
(if (= "file" (.getProtocol template))
;;running from git dep
(do (fs/copy-tree (fs/path template) target-dir {:replace-existing true
:posix-file-permissions perms})
(fs/walk-file-tree target-dir {:pre-visit-dir (fn [dir _] (fs/set-posix-file-permissions dir perms) :continue)
:visit-file (fn [file _] (fs/set-posix-file-permissions file perms) :continue)}))
;;running from jar
(unzip/unzip (io/resource "project-template.zip") target-dir))
(substitute-file (str (fs/path target-dir "deps.edn")) (data-fn))))
54 changes: 54 additions & 0 deletions src/nextjournal/garden_cli/unzip.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(ns nextjournal.garden-cli.unzip
(:require [clojure.java.io :as io]
[babashka.fs :refer [create-dirs parent]])
(:import [java.net URI]
[java.nio.file CopyOption
Files
StandardCopyOption
LinkOption Path]
[java.util.zip ZipInputStream]))

(defn- as-path
^Path [path]
(if (instance? Path path) path
(if (instance? URI path)
(java.nio.file.Paths/get ^URI path)
(.toPath (io/file path)))))

(defn- ->copy-opts ^"[Ljava.nio.file.CopyOption;"
[replace-existing copy-attributes atomic-move nofollow-links]
(into-array CopyOption
(cond-> []
replace-existing (conj StandardCopyOption/REPLACE_EXISTING)
copy-attributes (conj StandardCopyOption/COPY_ATTRIBUTES)
atomic-move (conj StandardCopyOption/ATOMIC_MOVE)
nofollow-links (conj LinkOption/NOFOLLOW_LINKS))))

;; adapted from babashka.fs to work with resources as input streams
(defn unzip
"Unzips `zip-file` to `dest` directory (default `\".\"`).
Options:
* `:replace-existing` - `true` / `false`: overwrite existing files"
([zip-file] (unzip zip-file "."))
([zip-file dest] (unzip zip-file dest nil))
([zip-file dest {:keys [replace-existing]}]
(let [output-path (as-path dest)
_ (create-dirs dest)
cp-opts (->copy-opts replace-existing nil nil nil)]
(with-open
[fis (io/input-stream zip-file)
zis (ZipInputStream. fis)]
(loop []
(let [entry (.getNextEntry zis)]
(when entry
(let [entry-name (.getName entry)
new-path (.resolve output-path entry-name)]
(if (.isDirectory entry)
(create-dirs new-path)
(do
(create-dirs (parent new-path))
(Files/copy ^java.io.InputStream zis
new-path
cp-opts))))
(recur))))))))
4 changes: 4 additions & 0 deletions standalone-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ targetdir="$(readlink -f "$1")"
mkdir -p "$targetdir"
cd "$clidir"
bb_version="$(bb -o "(:min-bb-version (clojure.edn/read-string (slurp \"bb.edn\")))")"
cd resources/project-template
zip -r project-template.zip .
mv project-template.zip ..
cd ../..
bb uberjar cli.jar -m nextjournal.garden-cli
tmpdir="$(mktemp -d)"
mv cli.jar "$tmpdir"
Expand Down

0 comments on commit 6737566

Please # to comment.