Skip to content

Commit ff188ff

Browse files
committed
docs: Document new migration and task configuration
1 parent 9c0dfaf commit ff188ff

File tree

1 file changed

+72
-38
lines changed

1 file changed

+72
-38
lines changed

docs/configuring.md

+72-38
Original file line numberDiff line numberDiff line change
@@ -1146,43 +1146,55 @@ Like [`message_before_copy`][message_after_copy] but printed before
11461146

11471147
### `migrations`
11481148

1149-
- Format: `List[dict]`
1149+
- Format: `List[str|List[str]|dict]`
11501150
- CLI flags: N/A
11511151
- Default value: `[]`
11521152

1153-
Migrations are like [tasks][tasks], but each item in the list is a `dict` with these
1154-
keys:
1153+
Migrations are like [tasks][tasks], but each item can have additional keys:
11551154

1156-
- **version**: Indicates the version that the template update has to go through to
1157-
trigger this migration. It is evaluated using [PEP 440][].
1158-
- **before** (optional): Commands to execute before performing the update. The answers
1159-
file is reloaded after running migrations in this stage, to let you migrate answer
1160-
values.
1161-
- **after** (optional): Commands to execute after performing the update.
1155+
- **command**: The migration command to run
1156+
- **version** (optional): Indicates the version that the template update has to go
1157+
through to trigger this migration. It is evaluated using [PEP 440][]. If no version
1158+
is specified the migration will run on every update.
1159+
- **when** (optional): Specifies a condition that needs to hold for the task to run.
1160+
By default, a migration will run in the after upgrade stage.
1161+
- **working_directory** (optional): Specifies the directory in which the command will
1162+
be run. Defaults to the destination directory.
1163+
1164+
If a `str` or `List[str]` is given as a migrator it will be treated as `command` with
1165+
all other items not present.
11621166

11631167
Migrations will run in the same order as declared here (so you could even run a
11641168
migration for a higher version before running a migration for a lower version if the
11651169
higher one is declared before and the update passes through both).
11661170

1167-
They will only run when _new version >= declared version > old version_. And only when
1168-
updating (not when copying for the 1st time).
1171+
When `version` is given they will only run when _new version >= declared version > old
1172+
version_. Your template will only be marked as [unsafe][unsafe] if this condition is
1173+
true. Migrations will also only run when updating (not when copying for the 1st time).
11691174

11701175
If the migrations definition contains Jinja code, it will be rendered with the same
11711176
context as the rest of the template.
11721177

1173-
Migration processes will receive these environment variables:
1174-
1175-
- `$STAGE`: Either `before` or `after`.
1176-
- `$VERSION_FROM`: [Git commit description][git describe] of the template as it was
1177-
before updating.
1178-
- `$VERSION_TO`: [Git commit description][git describe] of the template as it will be
1179-
after updating.
1180-
- `$VERSION_CURRENT`: The `version` detector as you indicated it when describing
1181-
migration tasks.
1182-
- `$VERSION_PEP440_FROM`, `$VERSION_PEP440_TO`, `$VERSION_PEP440_CURRENT`: Same as the
1183-
above, but normalized into a standard [PEP 440][] version string indicator. If your
1184-
scripts use these environment variables to perform migrations, you probably will
1185-
prefer to use these variables.
1178+
There are a number of additional variables available for templating of migrations. Those
1179+
variables are also passed to the migration process as environment variables. Migration
1180+
processes will receive these variables:
1181+
1182+
- `_stage`/`$STAGE`: Either `before` or `after`.
1183+
- `_version_from`/`$VERSION_FROM`: [Git commit description][git describe] of the
1184+
template as it was before updating.
1185+
- `_version_to`/`$VERSION_TO`: [Git commit description][git describe] of the template
1186+
as it will be after updating.
1187+
- `_version_current`/`$VERSION_CURRENT`: The `version` detector as you indicated it
1188+
when describing migration tasks (only when `version` is given).
1189+
- `_version_pep440_from`/`$VERSION_PEP440_FROM`,
1190+
`_version_pep440_to`/`$VERSION_PEP440_TO`,
1191+
`_version_pep440_current`/`$VERSION_PEP440_CURRENT`: Same as the above, but
1192+
normalized into a standard [PEP 440][] version. In Jinja templates these are
1193+
represented as
1194+
[packaging.version.Version](https://packaging.pypa.io/en/stable/version.html#packaging.version.Version)
1195+
objects and allow access to their attributes. As environment variables they are
1196+
represented as strings. If you use variables to perform migrations, you probably
1197+
will prefer to use these variables.
11861198

11871199
[git describe]: https://git-scm.com/docs/git-describe
11881200
[pep 440]: https://www.python.org/dev/peps/pep-0440/
@@ -1191,15 +1203,29 @@ Migration processes will receive these environment variables:
11911203

11921204
```yaml title="copier.yml"
11931205
_migrations:
1194-
- version: v1.0.0
1195-
before:
1196-
- rm ./old-folder
1197-
after:
1198-
# {{ _copier_conf.src_path }} points to the path where the template was
1199-
# cloned, so it can be helpful to run migration scripts stored there.
1200-
- invoke -r {{ _copier_conf.src_path }} -c migrations migrate $VERSION_CURRENT
1206+
# {{ _copier_conf.src_path }} points to the path where the template was
1207+
# cloned, so it can be helpful to run migration scripts stored there.
1208+
- invoke -r {{ _copier_conf.src_path }} -c migrations migrate $STAGE $VERSION_FROM $VERSION_TO
1209+
- version: v1.0.0
1210+
command: rm ./old-folder
1211+
when: "{{ _stage == 'before' }}"
12011212
```
12021213

1214+
In Copier versions before v9.3.0 a different configuration format had to be used. This
1215+
format is still available, but will raise a warning when used.
1216+
1217+
Each item in the list is a `dict` with the following keys:
1218+
1219+
- **version**: Indicates the version that the template update has to go through to
1220+
trigger this migration. It is evaluated using [PEP 440][].
1221+
- **before** (optional): Commands to execute before performing the update. The answers
1222+
file is reloaded after running migrations in this stage, to let you migrate answer
1223+
values.
1224+
- **after** (optional): Commands to execute after performing the update.
1225+
1226+
The migration variables mentioned above are available as environment variables, but
1227+
can't be used in jinja templates.
1228+
12031229
### `min_copier_version`
12041230

12051231
- Format: `str`
@@ -1408,14 +1434,24 @@ This allows you to keep separate the template metadata and the template code.
14081434

14091435
### `tasks`
14101436

1411-
- Format: `List[str|List[str]]`
1437+
- Format: `List[str|List[str]|dict]`
14121438
- CLI flags: N/A
14131439
- Default value: `[]`
14141440

14151441
Commands to execute after generating or updating a project from your template.
14161442

14171443
They run ordered, and with the `$STAGE=task` variable in their environment.
14181444

1445+
If a `dict` is given it can contain the following items:
1446+
1447+
- **command**: The task command to run.
1448+
- **when** (optional): Specifies a condition that needs to hold for the task to run.
1449+
- **working_directory** (optional): Specifies the directory in which the command will
1450+
be run. Defaults to the destination directory.
1451+
1452+
If a `str` or `List[str]` is given as a task it will be treated as `command` with all
1453+
other items not present.
1454+
14191455
!!! example
14201456

14211457
```yaml title="copier.yml"
@@ -1430,12 +1466,10 @@ They run ordered, and with the `$STAGE=task` variable in their environment.
14301466
# Your script can be run by the same Python environment used to run Copier
14311467
- ["{{ _copier_python }}", task.py]
14321468
# OS-specific task (supported values are "linux", "macos", "windows" and `None`)
1433-
- >-
1434-
{% if _copier_conf.os in ['linux', 'macos'] %}
1435-
rm {{ name_of_the_project }}/README.md
1436-
{% elif _copier_conf.os == 'windows' %}
1437-
Remove-Item {{ name_of_the_project }}/README.md
1438-
{% endif %}
1469+
- command: rm {{ name_of_the_project }}/README.md
1470+
when: "{{ _copier_conf.os in ['linux', 'macos'] }}"
1471+
- command: Remove-Item {{ name_of_the_project }}\\README.md
1472+
when: "{{ _copier_conf.os == 'windows' }}"
14391473
```
14401474
14411475
Note: the example assumes you use [Invoke](https://www.pyinvoke.org/) as

0 commit comments

Comments
 (0)