Last active
March 30, 2022 20:16
-
-
Save johanmynhardt/09adb321247c651eb46a940323eca398 to your computer and use it in GitHub Desktop.
Create a new bb.edn or deps.edn skeleton project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bb | |
(ns new-clj | |
"Utility to create new deps or bb projects." | |
(:require [clojure.tools.cli :as cli] | |
[clojure.pprint :refer [pprint]] | |
[clojure.java.io :as io] | |
[clojure.string :as str])) | |
(def cli-opts-spec | |
[["-t" "--target TARGET" | |
"Project Type: (deps|bb)" | |
:default :bb | |
:parse-fn #'keyword | |
:validate [#(contains? #{:deps :bb} %)]] | |
["-h" "--help" | |
"Print help"]]) | |
(defn rand-string | |
"Return string of `size` containing only `a-Z`" | |
[size] | |
(->> (range 65 123) | |
(remove #(contains? (into #{} (range 91 (inc 96))) %)) | |
(map char) | |
(shuffle) | |
(take size) | |
(str/join ""))) | |
(defn new-project | |
[{:keys [target] :as options} arguments] | |
(let [user-dir (System/getenv "PWD") | |
project-dir | |
(cond (first arguments) | |
(io/file user-dir (first arguments)) | |
:else | |
(io/file (System/getProperty "java.io.tmpdir") | |
(format "%s-project-%s" (name target) (rand-string 10))))] | |
(pprint {:user-dir user-dir | |
:project-dir (str project-dir) | |
:arguments arguments}) | |
(cond (.exists project-dir) | |
(println "Project already exists at" (str project-dir)) | |
:else | |
(do | |
(println "Creating directory: " (str project-dir)) | |
(io/make-parents (io/file project-dir "src" ".")) | |
(spit (io/file project-dir (format "%s.edn" (name target))) "{:paths [\"src\"]}") | |
(spit (io/file project-dir "src" "user.clj") "(ns user)"))))) | |
(defn -main [args] | |
(let [{:keys [errors options arguments] :as parsed-options} | |
(cli/parse-opts args cli-opts-spec)] | |
(cond errors | |
(pprint (:summary parsed-options)) | |
(:help options) | |
(println (:summary parsed-options)) | |
(contains? #{:bb :deps} (:target options)) | |
(new-project options arguments) | |
:else | |
(throw (ex-info "No handler for target" (select-keys options [:target])))))) | |
(-main *command-line-args*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment