Cross-platform tasks? Pehaps via bundled Python scripts? #649
-
I am evaluating Copier as a means of installing resources for another open source project. One of our use cases is to inject entries into a yaml file if the file exists, or create a new file otherwise. Tasks seem to be a great way to have this more powerful customized install logic, but by nature of being tied to terminal syntax, it seems these would necessarily be OS-specific. How are folks working around this today, for instance, if a template needs to be supported on Windows, Mac, and Linux? My first thought was to bundle a python script that could perform these actions using platform-agnostic core python libraries. Questions:
Curious what strategies have worked for other folks. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can definitely add a task that runs a Python script. This Python script can then delete itself indeed: _tasks:
- "{{ _copier_python }} postgen.py" # postgen.py, at the root of your template
def update_yaml_file():
...
def delete_self():
os.remove(__file__)
def main():
update_yaml_file()
delete_self()
if __name__ == "__main__":
main() Note that |
Beta Was this translation helpful? Give feedback.
You can definitely add a task that runs a Python script. This Python script can then delete itself indeed:
Note that
{{ _copier_python }}
is the Python interpreter used by Copier, so if Copier was installed in an isolated venv (through pipx for example), then you will only have access to the packages installed in this venv. If you are absolutely sure that there will always be apython
orpython3
(or/usr/local/bin/python
or Anaconda, etc…