diff --git a/src/nextjournal/garden_cli/template.clj b/src/nextjournal/garden_cli/template.clj index 41d1652..087a5bd 100644 --- a/src/nextjournal/garden_cli/template.clj +++ b/src/nextjournal/garden_cli/template.clj @@ -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 @@ -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)))) diff --git a/src/nextjournal/garden_cli/unzip.clj b/src/nextjournal/garden_cli/unzip.clj new file mode 100644 index 0000000..b358e22 --- /dev/null +++ b/src/nextjournal/garden_cli/unzip.clj @@ -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)))))))) diff --git a/standalone-build.sh b/standalone-build.sh index 3fe63dc..4fdc555 100755 --- a/standalone-build.sh +++ b/standalone-build.sh @@ -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"