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

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

Open
abogoyavlensky opened this issue Aug 12, 2022 · 2 comments
Open
Assignees
Labels
analysis needed Needs more design time

Comments

@abogoyavlensky
Copy link

abogoyavlensky commented Aug 12, 2022

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
Copy link
Owner

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
Copy link
Author

abogoyavlensky commented Aug 12, 2022

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!

@seancorfield seancorfield self-assigned this Aug 13, 2022
@seancorfield seancorfield added the analysis needed Needs more design time label Aug 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analysis needed Needs more design time
Projects
None yet
Development

No branches or pull requests

2 participants