Skip to content

Commit

Permalink
address #55
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Corfield <sean@corfield.org>
  • Loading branch information
seancorfield committed Jan 10, 2024
1 parent 6cb0d35 commit 2cf1f9e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changes

* v0.7.0 in progress
* Address [#55](https://github.com/seancorfield/deps-new/issues/55) by adding support for compound template names that can be resolved to git repositories and template paths within them.

* v0.6.0 64e79d1 -- 2023-12-25
* Address [#54](https://github.com/seancorfield/deps-new/issues/54) by adding `:src-dirs` option to `create` so templates can be found in directories other than on the classpath.
* Address [#52](https://github.com/seancorfield/deps-new/issues/52) by adding some notes about generated projects to the README.
Expand Down
5 changes: 4 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
:extra-deps {com.github.seancorfield/expectations {:mvn/version "2.1.182"}
io.github.cognitect-labs/test-runner
{:git/tag "v0.5.1" :git/sha "dfb30dd"}}
:exec-fn cognitect.test-runner.api/test}}}
:exec-fn cognitect.test-runner.api/test}
:new ; for local testing as a tool
{:deps {io.github.seancorfield/deps-new {:local/root "."}}
:ns-default org.corfield.new}}}
9 changes: 6 additions & 3 deletions src/org/corfield/new.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;; copyright (c) 2021-2023 sean corfield, all rights reserved
;; copyright (c) 2021-2024 sean corfield, all rights reserved

(ns org.corfield.new
"The next generation of clj-new. Uses tools.build and
Expand Down Expand Up @@ -50,9 +50,12 @@
for `:delete`, to delete it first; if `:overwrite` is `nil`
or `false`, an existing directory will not be overwritten."
[opts]
(let [{:keys [src-dirs template] :as basic-opts}
(let [{:keys [git-dir src-dirs template] :as basic-opts}
(impl/preprocess-options opts)
[dir edn-file] (impl/find-root src-dirs template)
[dir edn-file] (impl/find-root (cond->> src-dirs
git-dir
(into [git-dir]))
template)
_
(when-not dir
(throw (ex-info (str "Unable to find template.edn for " template) {})))
Expand Down
45 changes: 42 additions & 3 deletions src/org/corfield/new/impl.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
;; copyright (c) 2021-2023 sean corfield, all rights reserved
;; copyright (c) 2021-2024 sean corfield, all rights reserved

(ns ^:no-doc org.corfield.new.impl
"The implementation helpers for `org.corfield.new/create`."
(:require [clojure.java.io :as io]
[clojure.string :as str]
[clojure.tools.deps.extensions.git :as git]
[clojure.tools.build.api :as b])
[clojure.tools.build.api :as b]
[clojure.tools.gitlibs :as gl]
[org.corfield.new.impl :as impl])
(:import (java.nio.file Files)
(java.nio.file.attribute FileAttribute)
(java.text SimpleDateFormat)
Expand Down Expand Up @@ -44,6 +46,19 @@
(.getCanonicalPath file)]))
paths)))

(defn get-git-dir
"Given a template name (symbol), if it looks like a git repo, fetch it
and return the directory holding the most recent HEAD commit SHA."
[template-name tag]
(when-let [url (git/auto-git-url template-name)]
(let [sha (gl/resolve url tag)]
(gl/procure url template-name (or sha "HEAD")))))

(comment
(get-git-dir 'io.github.seancorfield/deps-new nil)
(get-git-dir 'org.corfield.new/app nil)
)

(defn ->subst-map
"Given a hash map of substitution data, return a hash map of
string substitutions, suitable for `tools.build.api/copy-dir`.
Expand Down Expand Up @@ -165,17 +180,37 @@
:scm/repo base-name
:top top}))

(comment
(for [t ['org.corfield.new/app
"io.github.seancorfield/deps-new%step%down/there"
"io.github.seancorfield/deps-new%org.corfield.new/app#v0.6.0"]]
(let [[_ repo _ root _ path _ tag]
(re-find #"^(.+?)(%(.+?))?(%(.+?))?(#(.+?))?$" (str t))]
[repo (and path root) (or path root repo) tag]))
)

(defn preprocess-options
"Given the raw options hash map, preprocess, parse, and
validate certain values, and derive defaults for others."
[{:keys [template target-dir], project-name :name, :as opts}]
(when-not (and template project-name)
(throw (ex-info "Both :template and :name are required." opts)))
(let [template (symbol template) ; allow for string or symbol
;; TODO: figure out suitable encoding for template names that supports a
;; git org/repo plus a classpath-relative path to the template dir override
;; plus, potentially later, specific tag overrides.
;; org/repo%deps/root%template-sym#tag
(let [[_ repo _ root _ path _ tag]
(re-find #"^(.+?)(%(.+?))?(%(.+?))?(#(.+?))?$" (str template))
deps-root (and path root)
template (symbol (or path root repo))
template (if (namespace template)
template
;; default ns for short template names:
(symbol "org.corfield.new" (name template)))
git-dir (get-git-dir (symbol repo) tag)
_ (when git-dir
(println "Resolving" repo "as a git dependency"
(str (when tag (str "at " tag)))))
{:keys [main] :as name-data}
(deconstruct-project-name (symbol project-name)) ; allow for string or symbol
target-dir (str (or target-dir main))
Expand All @@ -186,6 +221,10 @@
:now/date (.format (SimpleDateFormat. "yyyy-MM-dd") (Date.))
:now/year (str (+ 1900 (.getYear (Date.))))
:raw-name (str project-name)
:git-dir (when git-dir
(if deps-root
(str git-dir "/" deps-root)
git-dir))
:template (str template)
:target-dir target-dir
:user username
Expand Down

0 comments on commit 2cf1f9e

Please sign in to comment.