seancorfield / deps-new

Create new projects for the Clojure CLI / deps.edn
Eclipse Public License 1.0
346 stars 19 forks source link

[Feature request] Add ability to use conditions in templates #35

Open abogoyavlensky opened 2 years ago

abogoyavlensky commented 2 years ago

In my opinion, an ability to make templates a slightly dynamic will help visualize possible changes in templates based on template variables. I'm thinking about something similar to Selmers' {{ if some-var = "value" }}...{{ endif }}. I don't propose any other tags but if probably would be helpful.

An example for the case when conditions could be useful. Let's suppose we have basic deps.edn file template and we want to add to it different features that have its own dependencies. So for instance, we would like to give our users optional ability to add database config to project created from a template. Now basic deps.edn could look like this:

{:deps {org.clojure/clojure {:mvn/version "1.11.1"}{{extra-deps}}}
 ...}

And then, if we pass arg, for example, :db true to generating command, we add deps internally and get result:

 {:deps {org.clojure/clojure {:mvn/version "1.11.1"}
         com.github.seancorfield/next.jdbc {:mvn/version "1.2.780"}
         org.postgresql/postgresql {:mvn/version "42.4.0"}}
  ...}

But if we had had built-in if condition in template, we could have written the same template a slightly more explicit:

 {:deps {org.clojure/clojure {:mvn/version "1.11.1"}
         {{ if db }}
         com.github.seancorfield/next.jdbc {:mvn/version "1.2.780"}
         org.postgresql/postgresql {:mvn/version "42.4.0"}}
         {{ endif }}
  ...}
seancorfield commented 2 years ago

This is already possible with https://github.com/seancorfield/deps-new/blob/develop/doc/templates.md#programmatic-transformation since the :transform-fn can rewrite how files are selected -- and could rewrite the files themselves (to, say, a temporary folder, using Selmer to perform transformations) and return an updated template.edn data structure pointing at the transformed files.

Some helper functions might make this easier, perhaps, and a fully-documented example would also help, but the existing hooks suffice for this.

I'll leave this ticket open as a placeholder for me to create an example template repo on GH showing how this can be done.

abogoyavlensky commented 2 years ago

First of all, thank you for the fast and detailed reply!

to, say, a temporary folder, using Selmer to perform transformations

Yes, I get it, and I do it almost like this. And it's totally ok as a solution, but the same time it feels a little bit inconvenient comparing with built-in abilities 🤔.

Some helper functions might make this easier, perhaps

an example template repo on GH showing how this can be done

Yes, it would be great to have some helpers and, probably, guides. Thanks!