Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

neil new: Add support for deps-based templates #51

Merged
merged 25 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions neil
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
(require '[babashka.classpath :as cp]
'[babashka.cli :as cli]
'[babashka.curl :as curl]
'[babashka.deps :as deps]
'[babashka.fs :as fs]
'[borkdude.rewrite-edn :as r]
'[cheshire.core :as cheshire]
'[clojure.edn :as edn]
'[clojure.string :as str])

;; deps-new reads classpath from property
(System/setProperty "java.class.path" (cp/get-classpath))

(def spec {:lib {:desc "Fully qualified library name."}
:version {:desc "Optional. When not provided, picks newest version from Clojars or Maven Central."}
:sha {:desc "When provided, assumes lib refers to Github repo."}
Expand Down Expand Up @@ -380,6 +378,11 @@
:target-dir
:overwrite

; template deps overrides
:local/root
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary to explicitly list all the options. Babashka CLI automatically parses all command line flags. The list your using here is for converting options in order to options, using :cmds-opts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a note.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I updated the :cmds-opts to be [:template :name :target-dir]. These are the ones that made most sense to me as positional arguments.

For the special named arguments which are not directly supported by deps-new (:git/url, :git/sha, :git/tag, :local/root), we'll probably want to add those to the neil help docs in addition to the existing link to the deps-new override options.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 16 (on main) we already have a bunch of options. I'd prefer if we could align the names of options, even if they are used in different contexts. Since we already have :sha , maybe we should re-use that one? Or should we deprecate it and rename it to :git/sha (with :sha being an :alias for :git/sha)?

Copy link
Collaborator Author

@rads rads Jul 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@borkdude: Here's the solution I came up with:

  • neil new will always accept namespaced keys like :git/sha
  • neil new also supports the existing :sha and :latest-sha options
    • :sha is simply renamed to :git/sha before parsing
    • :latest-sha provides an alternative to fetching the latest :git/tag (which is the default behavior when neither :git/tag, :git/sha, nor :latest-sha is provided)
  • Any options for neil new that are not already in babashka.neil/spec should be documented specifically for neil new. If we find some neil new options to be useful elsewhere, we can promote them to babashka.neil/spec as needed.

What do you think about this?

:git/url
:git/sha

; optional overrides
:artifact/id
:description
Expand All @@ -397,11 +400,29 @@
:user
:version])

(defn- built-in-template? [template]
(contains? (set (map name (keys (ns-publics 'org.corfield.new)))) template))

(defn- github-repo-url [lib]
(str "https://github.com/" (clean-github-lib lib)))

(defn- add-template-deps [template opts]
(let [lib (edn/read-string template)
local-root (:local/root opts)]
(if local-root
(deps/add-deps {:deps {lib {:local/root local-root}}})
(let [url (or (:git/url opts) (github-repo-url lib))
sha (or (:git/sha opts) (latest-github-sha lib))]
(deps/add-deps {:deps {lib {:git/url url :git/sha sha}}})))))

(defn run-deps-new [{:keys [opts]}]
(require 'org.corfield.new)
(let [{:keys [template]
:or {template "scratch"}} opts
template-fn (requiring-resolve (symbol "org.corfield.new" template))]
(template-fn (dissoc opts :template))
:or {template "scratch"}} opts]
(when-not (built-in-template? template)
(add-template-deps template opts))
(System/setProperty "java.class.path" (cp/get-classpath))
((resolve 'org.corfield.new/create) opts)
nil))

(defn print-help [_]
Expand Down
33 changes: 27 additions & 6 deletions src/babashka/neil.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
(require '[babashka.classpath :as cp]
'[babashka.cli :as cli]
'[babashka.curl :as curl]
'[babashka.deps :as deps]
'[babashka.fs :as fs]
'[borkdude.rewrite-edn :as r]
'[cheshire.core :as cheshire]
'[clojure.edn :as edn]
'[clojure.string :as str])

;; deps-new reads classpath from property
(System/setProperty "java.class.path" (cp/get-classpath))

(def spec {:lib {:desc "Fully qualified library name."}
:version {:desc "Optional. When not provided, picks newest version from Clojars or Maven Central."}
:sha {:desc "When provided, assumes lib refers to Github repo."}
Expand Down Expand Up @@ -364,6 +362,11 @@
:target-dir
:overwrite

; template deps overrides
:local/root
:git/url
:git/sha
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the deps-new docs:

:local/root and git-based coordinates are supported, but Maven/Clojars coordinates are not.


; optional overrides
:artifact/id
:description
Expand All @@ -381,11 +384,29 @@
:user
:version])

(defn- built-in-template? [template]
(contains? (set (map name (keys (ns-publics 'org.corfield.new)))) template))

(defn- github-repo-url [lib]
(str "https://github.com/" (clean-github-lib lib)))

(defn- add-template-deps [template opts]
(let [lib (edn/read-string template)
rads marked this conversation as resolved.
Show resolved Hide resolved
local-root (:local/root opts)]
(if local-root
(deps/add-deps {:deps {lib {:local/root local-root}}})
(let [url (or (:git/url opts) (github-repo-url lib))
sha (or (:git/sha opts) (latest-github-sha lib))]
(deps/add-deps {:deps {lib {:git/url url :git/sha sha}}})))))

(defn run-deps-new [{:keys [opts]}]
(require 'org.corfield.new)
(let [{:keys [template]
:or {template "scratch"}} opts
template-fn (requiring-resolve (symbol "org.corfield.new" template))]
(template-fn (dissoc opts :template))
:or {template "scratch"}} opts]
(when-not (built-in-template? template)
(add-template-deps template opts))
(System/setProperty "java.class.path" (cp/get-classpath))
((resolve 'org.corfield.new/create) opts)
nil))

(defn print-help [_]
Expand Down
2 changes: 2 additions & 0 deletions test-resources/new/my-kit/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:paths ["src"]
:deps {}}
12 changes: 12 additions & 0 deletions test-resources/new/my-kit/src/scratch.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns scratch
"FIXME: my new io.github.rads/kit project.")

(defn exec
"Invoke me with clojure -X scratch/exec"
[opts]
(println "exec with" opts))

(defn -main
"Invoke me with clojure -M -m scratch"
[& args]
(println "-main with" args))
2 changes: 2 additions & 0 deletions test-resources/new/my-scratch/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:paths ["src"]
:deps {}}
12 changes: 12 additions & 0 deletions test-resources/new/my-scratch/src/scratch.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns scratch
"FIXME: my new org.corfield.new/scratch project.")

(defn exec
"Invoke me with clojure -X scratch/exec"
[opts]
(println "exec with" opts))

(defn -main
"Invoke me with clojure -M -m scratch"
[& args]
(println "-main with" args))
18 changes: 18 additions & 0 deletions tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,23 @@
(is (thrown-with-msg? Exception #"nonExistentLicense"
(run-license out-file "add" "nonExistentLicense"))))))

(deftest new-scratch-test
(let [target-dir (str (fs/temp-dir) "/my-scratch")]
(spit (test-file "deps.edn") "{}")
(neil (str "new scratch my-scratch :overwrite true :target-dir " target-dir))
(is (= (slurp (fs/file "test-resources/new/my-scratch/src/scratch.clj"))
(slurp (fs/file (str target-dir "/src/scratch.clj")))))
rads marked this conversation as resolved.
Show resolved Hide resolved
(is (= (slurp (fs/file "test-resources/new/my-scratch/deps.edn"))
(slurp (fs/file (str target-dir "/deps.edn")))))))

(deftest new-remote-test
(let [target-dir (str (fs/temp-dir) "/my-kit")]
(spit (test-file "deps.edn") "{}")
(neil (str "new io.github.rads/kit my-kit :overwrite true :target-dir " target-dir))
(is (= (slurp (fs/file "test-resources/new/my-kit/src/scratch.clj"))
(slurp (fs/file (str target-dir "/src/scratch.clj")))))
(is (= (slurp (fs/file "test-resources/new/my-kit/deps.edn"))
(slurp (fs/file (str target-dir "/deps.edn")))))))

(when (= *file* (System/getProperty "babashka.file"))
(t/run-tests *ns*))