cargo-generate / cargo-generate

cargo, make me a project

Home Page:https://cargo-generate.github.io/cargo-generate

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Define a prompt order

SergioGasquez opened this issue · comments

Hi! Would it be possible to make the order in which the prompts appear deterministic? At the moment the order is "random" every time. I think it's possible to move them into a RHAI script, but then our several CIs that generate the template would stop working as we use the -d option to choose from the prompts.

Hi. I am trying to set up templates for myself and my team and having a prompt order will help me a lot.

@SergioGasquez could you please provide a full example where the prompt order appears to be random?

@SergioGasquez could you please provide a full example where the prompt order appears to be random?

Of course, esp-template for example:

cargo generate -a esp-rs/esp-template

It requires -a since we have a rhai post script that formats the resulting code until #884 is solved

Are you aware, that simply asking (getting) for a variable from within a RHAI script will cause cargo-generate to ask for a value, if it's not already defined that is? This actually supports the -d parameter.

Are you aware, that simply asking (getting) for a variable from within a RHAI script will cause cargo-generate to ask for a value, if it's not already defined that is? This actually supports the -d parameter.

I was not aware of that. I have just written an rhai script for a well-defined prompt order but this is way simpler. Thank you.

Are you aware, that simply asking (getting) for a variable from within a RHAI script will cause cargo-generate to ask for a value, if it's not already defined that is? This actually supports the -d parameter.

I was not aware of this feature! Can you share a small example of how this works? I've tried getting a variable on an init hook (since in pre hooks, placeholders are already solved) with no luck.

I'll see if I can get the time and create one - been very busy with life the past couple of months. Should probably have an issue of its own, and go into the examples and docs.

A fix is in the making, only the tests require a bit of refactoring.

As a temporary workaround until the fix is merged: you can actually force the order by introducing extra conditionals.
For example:

[placeholders]
question1 = { type = "bool", prompt = "Question 1?", default=false}

[conditional.'question1 || !question1'.placeholders]
question2 = { type = "bool", prompt = "Question 2?", default=false}

Not sure how far this actually scales, but it WFM.

I think the fix is now in a good enough shape that we can merge and ship it.
See this comment for further details