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

Shared variables #716

Closed
punkstarman opened this issue Jul 13, 2022 · 6 comments
Closed

Shared variables #716

punkstarman opened this issue Jul 13, 2022 · 6 comments

Comments

@punkstarman
Copy link

punkstarman commented Jul 13, 2022

In order to factor out common calculations, especially amongst file and directory names, it would be useful to be able to define variables in copier.yml whose values are calculated based on answers or other variables.

For example, when creating a template for a Java project, based on the name of the project, I would like to calculate the name of the root package, and also the path of the root package.

project_name:
  type: str
  default: hello-world

root_package:
  type: str
  default: "org.example.{{ project_name | title | regex_replace('-', '.') }}"

root_package_dir:
  type: str
  default: "{{ root_package | regex_replace('.', '/') }}"

The root package variable would be referred to by multiple files.

The root package directory must be calculated this way since you cannot have a filename with a slash (/).

Describe the solution you'd like
I suggest adding support for defining variables in copier.yml in a similar way as defining questions. The two are distinguished in some way, perhaps by a prefix such as a double underscore (__) or by a key such as hidden: true. I suggest that it be possible to mix defining variables and questions such that a question definition can refer to prior variables.

Describe alternatives you've considered
This feature is called Private Variables in Cookicutter.

An obvious workaround is to define questions with default values. This is unsatisfactory because this forces users to read the trivial question and decide to hit the enter key, which adds unnecessary overhead and

Additional context
The given example refers to Java, but templates for other programming languages, including Python, could have similar issues.

@pawamoy
Copy link
Contributor

pawamoy commented Jul 13, 2022

See previous conversation on the subject in #629 (as well as #700), and #692 about arbitrarily deep filesystem tree generation (following discussion in #683).

@punkstarman
Copy link
Author

I find it a bit heavy-handed to write a context hook in order to add a variable. It doesn't allow default answers to depend on unprompted variables.

@yajo
Copy link
Member

yajo commented Jul 16, 2022

For the time being, you already know the workarounds we have. However, you inspired me to write #718. Let's close this one. Thanks!

@yajo yajo closed this as not planned Won't fix, can't repro, duplicate, stale Jul 16, 2022
@FlorianLudwig
Copy link

@punkstarman / @yajo

you can have unprompted variables with when: false:

example_variable:
  default: "{{ project_slug|lower|replace(' ', '')|replace('_', '')|replace('-', '') }}
  when: false

Works fine for me.

@pawamoy
Copy link
Contributor

pawamoy commented Sep 30, 2022

@FlorianLudwig when: false can lead to surprising behavior, see #678 (comment) (just for your information, not trying to prevent you from using when: false if that works for you 🙂 )

@punkstarman
Copy link
Author

@FlorianLudwig, that is my current workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants