@@ -1146,43 +1146,55 @@ Like [`message_before_copy`][message_after_copy] but printed before
1146
1146
1147
1147
# ## `migrations`
1148
1148
1149
- - Format : ` List[dict]`
1149
+ - Format : ` List[str|List[str]| dict]`
1150
1150
- CLI flags : N/A
1151
1151
- Default value : ` []`
1152
1152
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 :
1155
1154
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.
1162
1166
1163
1167
Migrations will run in the same order as declared here (so you could even run a
1164
1168
migration for a higher version before running a migration for a lower version if the
1165
1169
higher one is declared before and the update passes through both).
1166
1170
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).
1169
1174
1170
1175
If the migrations definition contains Jinja code, it will be rendered with the same
1171
1176
context as the rest of the template.
1172
1177
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.
1186
1198
1187
1199
[git describe] : https://git-scm.com/docs/git-describe
1188
1200
[pep 440] : https://www.python.org/dev/peps/pep-0440/
@@ -1191,15 +1203,29 @@ Migration processes will receive these environment variables:
1191
1203
1192
1204
` ` ` yaml title="copier.yml"
1193
1205
_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' }}"
1201
1212
` ` `
1202
1213
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
+
1203
1229
# ## `min_copier_version`
1204
1230
1205
1231
- Format : ` str`
@@ -1408,14 +1434,24 @@ This allows you to keep separate the template metadata and the template code.
1408
1434
1409
1435
# ## `tasks`
1410
1436
1411
- - Format : ` List[str|List[str]]`
1437
+ - Format : ` List[str|List[str]|dict ]`
1412
1438
- CLI flags : N/A
1413
1439
- Default value : ` []`
1414
1440
1415
1441
Commands to execute after generating or updating a project from your template.
1416
1442
1417
1443
They run ordered, and with the `$STAGE=task` variable in their environment.
1418
1444
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
+
1419
1455
!!! example
1420
1456
1421
1457
` ` ` yaml title="copier.yml"
@@ -1430,12 +1466,10 @@ They run ordered, and with the `$STAGE=task` variable in their environment.
1430
1466
# Your script can be run by the same Python environment used to run Copier
1431
1467
- ["{{ _copier_python }}", task.py]
1432
1468
# 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' }}"
1439
1473
` ` `
1440
1474
1441
1475
Note: the example assumes you use [Invoke](https://www.pyinvoke.org/) as
0 commit comments