Skip to content

Commit e85b572

Browse files
authored
Use line stripping tags in templates and reformat templates (#900)
By using the line stripping twig tags we can use normal indenting for template control structures which makes templates a bit easier to read and maintain.
1 parent 33c8484 commit e85b572

8 files changed

Lines changed: 236 additions & 236 deletions

File tree

templates/bake/config/diff.twig

Lines changed: 115 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -47,81 +47,81 @@ class {{ name }} extends AbstractMigration
4747
*/
4848
public function up(): void
4949
{
50-
{% for tableName, tableDiff in data %}
51-
{% set hasRemoveFK = tableDiff['constraints']['remove'] is not empty or tableDiff['indexes']['remove'] is
50+
{% for tableName, tableDiff in data %}
51+
{%~ set hasRemoveFK = tableDiff['constraints']['remove'] is not empty or tableDiff['indexes']['remove'] is
5252
not empty %}
53-
{% if hasRemoveFK %}
53+
{%~ if hasRemoveFK %}
5454
$this->table('{{ tableName }}')
55-
{% endif %}
56-
{% if tableDiff['constraints']['remove'] is not empty %}
57-
{% for constraintName, constraintDefinition in tableDiff['constraints']['remove'] %}
55+
{%~ endif %}
56+
{%~ if tableDiff['constraints']['remove'] is not empty %}
57+
{%~ for constraintName, constraintDefinition in tableDiff['constraints']['remove'] %}
5858
->dropForeignKey([], '{{ constraintName }}')
59-
{% endfor %}
60-
{% endif %}
61-
{% if tableDiff['indexes']['remove'] is not empty %}
62-
{% for indexName, indexDefinition in tableDiff['indexes']['remove'] %}
59+
{%~ endfor %}
60+
{%~ endif %}
61+
{%~ if tableDiff['indexes']['remove'] is not empty %}
62+
{%~ for indexName, indexDefinition in tableDiff['indexes']['remove'] %}
6363
->removeIndexByName('{{ indexName }}')
64-
{% endfor %}
65-
{% endif %}
66-
{% if hasRemoveFK %}
64+
{%~ endfor %}
65+
{%~ endif %}
66+
{%~ if hasRemoveFK %}
6767
->update();
68-
{% endif %}
69-
{% if tableDiff['columns']['remove'] is not empty or tableDiff['columns']['changed'] is not empty %}
68+
{%~ endif %}
69+
{%~ if tableDiff['columns']['remove'] is not empty or tableDiff['columns']['changed'] is not empty %}
7070
7171
{{ Migration.tableStatement(tableName, true) | raw }}
72-
{% if tableDiff['columns']['remove'] is not empty %}
73-
{% for columnName, columnDefinition in tableDiff['columns']['remove'] %}
72+
{%~ if tableDiff['columns']['remove'] is not empty %}
73+
{%~ for columnName, columnDefinition in tableDiff['columns']['remove'] %}
7474
->removeColumn('{{ columnName }}')
75-
{% endfor %}
76-
{% endif %}
77-
{% if tableDiff['columns']['changed'] is not empty %}
78-
{% for columnName, columnAttributes in tableDiff['columns']['changed'] %}
79-
{% set type = columnAttributes['type'] %}
80-
{% set columnAttributes = Migration.getColumnOption(columnAttributes) %}
81-
{% set columnAttributes = Migration.stringifyList(columnAttributes, {'indent': 4, 'remove':
75+
{%~ endfor %}
76+
{%~ endif %}
77+
{%~ if tableDiff['columns']['changed'] is not empty %}
78+
{%~ for columnName, columnAttributes in tableDiff['columns']['changed'] %}
79+
{%~ set type = columnAttributes['type'] %}
80+
{%~ set columnAttributes = Migration.getColumnOption(columnAttributes) %}
81+
{%~ set columnAttributes = Migration.stringifyList(columnAttributes, {'indent': 4, 'remove':
8282
['type']}) %}
8383
->changeColumn('{{ columnName }}', '{{ type }}', [{{ columnAttributes | raw }}])
84-
{% endfor %}
85-
{% endif -%}
86-
{% if Migration.wasTableStatementGeneratedFor(tableName) %}
84+
{%~ endfor %}
85+
{%~ endif -%}
86+
{%~ if Migration.wasTableStatementGeneratedFor(tableName) %}
8787
->update();
88-
{% endif %}
89-
{% endif %}
90-
{% endfor %}
91-
{% if tables['add'] is not empty %}
92-
{{ Migration.element('Migrations.create-tables', {'tables': tables['add'], 'autoId': autoId, 'useSchema':
88+
{%~ endif %}
89+
{%~ endif %}
90+
{% endfor %}
91+
{% if tables['add'] is not empty %}
92+
{{~ Migration.element('Migrations.create-tables', {'tables': tables['add'], 'autoId': autoId, 'useSchema':
9393
true}) | raw -}}
94-
{% endif %}
95-
{% for tableName, tableDiff in data %}
96-
{% if tableDiff['columns']['add'] is not empty or tableDiff['indexes']['add'] is not empty %}
94+
{% endif %}
95+
{% for tableName, tableDiff in data %}
96+
{%~ if tableDiff['columns']['add'] is not empty or tableDiff['indexes']['add'] is not empty %}
9797
9898
{{ Migration.tableStatement(tableName, true) | raw }}
99-
{% if tableDiff['columns']['add'] is not empty %}
100-
{{ Migration.element('Migrations.add-columns', {'columns': tableDiff['columns']['add']}) | raw -}}
101-
{% endif -%}
102-
{% if tableDiff['indexes']['add'] is not empty %}
103-
{{ Migration.element('Migrations.add-indexes', {'backend': backend, 'indexes': tableDiff['indexes']['add']}) | raw -}}
104-
{% endif -%}
105-
{% if Migration.wasTableStatementGeneratedFor(tableName) %}
99+
{%~ if tableDiff['columns']['add'] is not empty %}
100+
{{~ Migration.element('Migrations.add-columns', {'columns': tableDiff['columns']['add']}) | raw -}}
101+
{%~ endif -%}
102+
{%~ if tableDiff['indexes']['add'] is not empty %}
103+
{{~ Migration.element('Migrations.add-indexes', {'backend': backend, 'indexes': tableDiff['indexes']['add']}) | raw -}}
104+
{%~ endif -%}
105+
{%~ if Migration.wasTableStatementGeneratedFor(tableName) %}
106106
->update();
107-
{% endif -%}
108-
{% endif -%}
109-
{% endfor -%}
110-
{% for tableName, tableDiff in data -%}
111-
{% if tableDiff['constraints']['add'] is not empty -%}
107+
{%~ endif -%}
108+
{%~ endif -%}
109+
{% endfor -%}
110+
{% for tableName, tableDiff in data -%}
111+
{% if tableDiff['constraints']['add'] is not empty -%}
112112
113-
{{ Migration.element(
114-
'Migrations.add-foreign-keys',
115-
{'constraints': tableDiff['constraints']['add'], 'table': tableName, 'backend': backend}
116-
) | raw -}}
117-
{% endif -%}
118-
{% endfor -%}
119-
{% if tables['remove'] is not empty %}
120-
{% for tableName, table in tables['remove'] %}
113+
{{~ Migration.element(
114+
'Migrations.add-foreign-keys',
115+
{'constraints': tableDiff['constraints']['add'], 'table': tableName, 'backend': backend}
116+
) | raw -}}
117+
{%~ endif -%}
118+
{% endfor -%}
119+
{% if tables['remove'] is not empty %}
120+
{%~ for tableName, table in tables['remove'] %}
121121
122122
$this->table('{{ tableName }}')->drop()->save();
123-
{% endfor %}
124-
{% endif %}
123+
{%~ endfor %}
124+
{% endif %}
125125
}
126126
127127
/**
@@ -134,87 +134,87 @@ not empty %}
134134
*/
135135
public function down(): void
136136
{
137-
{% set returnedData = Migration.getReturnedData() %}
138-
{% set constraints = [] %}
139-
{% set emptyLine = false %}
140-
{% if returnedData['dropForeignKeys'] is not empty %}
141-
{% for table, columnsList in returnedData['dropForeignKeys'] %}
142-
{% set maxKey = columnsList | length - 1 %}
143-
{% if emptyLine %}
137+
{% set returnedData = Migration.getReturnedData() %}
138+
{% set constraints = [] %}
139+
{% set emptyLine = false %}
140+
{% if returnedData['dropForeignKeys'] is not empty %}
141+
{%~ for table, columnsList in returnedData['dropForeignKeys'] %}
142+
{%~ set maxKey = columnsList | length - 1 %}
143+
{%~ if emptyLine %}
144144
145-
{% else %}
146-
{% set emptyLine = true %}
147-
{% endif %}
145+
{%~ else %}
146+
{%~ set emptyLine = true %}
147+
{%~ endif %}
148148
$this->table('{{ table }}')
149-
{% for key, columns in columnsList %}
149+
{%~ for key, columns in columnsList %}
150150
->dropForeignKey(
151151
{{ columns | raw }}
152152
){{ (key == maxKey) ? '->save();' : '' }}
153-
{% endfor -%}
154-
{% endfor -%}
155-
{% endif -%}
156-
{% if tables['remove'] is not empty -%}
157-
{{ Migration.element('Migrations.create-tables', {
153+
{%~ endfor -%}
154+
{%~ endfor -%}
155+
{% endif -%}
156+
{% if tables['remove'] is not empty -%}
157+
{{~ Migration.element('Migrations.create-tables', {
158158
'tables': tables['remove'],
159159
'autoId': autoId,
160160
'useSchema': true
161161
}) | raw -}}
162-
{% endif -%}
163-
{% for tableName, tableDiff in data -%}
164-
{% do Migration.resetTableStatementGenerationFor(tableName) %}
165-
{% if tableDiff['indexes']['add'] is not empty %}
162+
{% endif -%}
163+
{% for tableName, tableDiff in data -%}
164+
{%~ do Migration.resetTableStatementGenerationFor(tableName) %}
165+
{%~ if tableDiff['indexes']['add'] is not empty %}
166166
167167
$this->table('{{ tableName }}')
168-
{% for indexName, index in tableDiff['indexes']['add'] %}
168+
{%~ for indexName, index in tableDiff['indexes']['add'] %}
169169
->removeIndexByName('{{ indexName }}')
170-
{% endfor %}
170+
{%~ endfor %}
171171
->update();
172-
{% endif %}
173-
{% if (tableDiff['columns']['remove'] is not empty or
172+
{%~ endif %}
173+
{%~ if (tableDiff['columns']['remove'] is not empty or
174174
tableDiff['columns']['changed'] is not empty or
175175
tableDiff['columns']['add'] is not empty or
176176
tableDiff['indexes']['remove'] is not empty) %}
177177
178178
{{ Migration.tableStatement(tableName, true) | raw}}
179-
{% endif %}
180-
{% if tableDiff['columns']['remove'] is not empty -%}
181-
{{ Migration.element('Migrations.add-columns', {'columns': tableDiff['columns']['remove']}) | raw -}}
182-
{% endif -%}
183-
{% if tableDiff['columns']['changed'] is not empty -%}
184-
{% set oldTableDef = dumpSchema[tableName] -%}
185-
{% for columnName, columnAttributes in tableDiff['columns']['changed'] -%}
186-
{% set columnAttributes = oldTableDef.getColumn(columnName) -%}
187-
{% set type = columnAttributes['type'] -%}
188-
{% set columnAttributes = Migration.getColumnOption(columnAttributes) -%}
189-
{% set columnAttributes = Migration.stringifyList(columnAttributes, {'indent': 4, 'remove': ['type']}) %}
179+
{%~ endif %}
180+
{%~ if tableDiff['columns']['remove'] is not empty -%}
181+
{{~ Migration.element('Migrations.add-columns', {'columns': tableDiff['columns']['remove']}) | raw -}}
182+
{%~ endif -%}
183+
{%~ if tableDiff['columns']['changed'] is not empty -%}
184+
{%~ set oldTableDef = dumpSchema[tableName] -%}
185+
{%~ for columnName, columnAttributes in tableDiff['columns']['changed'] -%}
186+
{%~ set columnAttributes = oldTableDef.getColumn(columnName) -%}
187+
{%~ set type = columnAttributes['type'] -%}
188+
{%~ set columnAttributes = Migration.getColumnOption(columnAttributes) -%}
189+
{%~ set columnAttributes = Migration.stringifyList(columnAttributes, {'indent': 4, 'remove': ['type']}) %}
190190
->changeColumn('{{ columnName }}', '{{ type }}', [{{ columnAttributes | raw }}])
191-
{% endfor -%}
192-
{% endif -%}
193-
{% if tableDiff['columns']['add'] is not empty %}
194-
{% for columnName, columnAttributes in tableDiff['columns']['add'] %}
191+
{%~ endfor -%}
192+
{%~ endif -%}
193+
{%~ if tableDiff['columns']['add'] is not empty %}
194+
{%~ for columnName, columnAttributes in tableDiff['columns']['add'] %}
195195
->removeColumn('{{ columnName }}')
196-
{% endfor -%}
197-
{% endif -%}
198-
{% if tableDiff['indexes']['remove'] is not empty -%}
199-
{{ Migration.element('Migrations.add-indexes', {'indexes': tableDiff['indexes']['remove']}) | raw -}}
200-
{% endif -%}
201-
{% if Migration.wasTableStatementGeneratedFor(tableName) %}
196+
{%~ endfor -%}
197+
{%~ endif -%}
198+
{%~ if tableDiff['indexes']['remove'] is not empty -%}
199+
{{~ Migration.element('Migrations.add-indexes', {'indexes': tableDiff['indexes']['remove']}) | raw -}}
200+
{%~ endif -%}
201+
{%~ if Migration.wasTableStatementGeneratedFor(tableName) %}
202202
->update();
203-
{% endif %}
204-
{% endfor %}
205-
{% for tableName, tableDiff in data %}
206-
{% if tableDiff['constraints']['remove'] is not empty %}
207-
{{ Migration.element(
208-
'Migrations.add-foreign-keys',
209-
{'constraints': tableDiff['constraints']['remove'], 'table': tableName}
203+
{%~ endif %}
204+
{% endfor %}
205+
{% for tableName, tableDiff in data %}
206+
{%~ if tableDiff['constraints']['remove'] is not empty %}
207+
{{~ Migration.element(
208+
'Migrations.add-foreign-keys',
209+
{'constraints': tableDiff['constraints']['remove'], 'table': tableName}
210210
) | raw -}}
211-
{% endif %}
212-
{% endfor %}
213-
{% if tables['add'] is not empty %}
211+
{%~ endif %}
212+
{% endfor %}
213+
{% if tables['add'] is not empty %}
214214
215-
{% for tableName, table in tables['add'] %}
215+
{%~ for tableName, table in tables['add'] %}
216216
$this->table('{{ tableName }}')->drop()->save();
217-
{% endfor %}
218-
{% endif %}
217+
{%~ endfor %}
218+
{% endif %}
219219
}
220220
}

templates/bake/config/skeleton.twig

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,35 @@ class {{ name }} extends AbstractMigration
5050
{
5151
{% for table in tables %}
5252
$table = $this->table('{{ table }}');
53-
{% if tableMethod != 'drop' %}
54-
{% if columnMethod == 'removeColumn' %}
55-
{% for column, config in columns['fields'] %}
53+
{%~ if tableMethod != 'drop' %}
54+
{%~ if columnMethod == 'removeColumn' %}
55+
{%~ for column, config in columns['fields'] %}
5656
$table->{{ columnMethod }}('{{ column }}');
57-
{% endfor %}
58-
{% for column, config in columns['indexes'] %}
57+
{%~ endfor %}
58+
{%~ for column, config in columns['indexes'] %}
5959
$table->{{ indexMethod }}([{{
6060
Migration.stringifyList(config['columns']) | raw
6161
}}]);
62-
{% endfor %}
63-
{% else %}
64-
{% for column, config in columns['fields'] %}
62+
{%~ endfor %}
63+
{%~ else %}
64+
{%~ for column, config in columns['fields'] %}
6565
$table->{{ columnMethod }}('{{ column }}', '{{ config['columnType'] }}', [{{
6666
Migration.stringifyList(config['options'], {'indent': 3}, wantedOptions) | raw
6767
}}]);
68-
{% endfor %}
69-
{% for column, config in columns['indexes'] %}
68+
{%~ endfor %}
69+
{%~ for column, config in columns['indexes'] %}
7070
$table->{{ indexMethod }}([{{
7171
Migration.stringifyList(config['columns'], {'indent': 3}) | raw }}
7272
], [{{
7373
Migration.stringifyList(config['options'], {'indent': 3}) | raw
7474
}}]);
75-
{% endfor %}
76-
{% if tableMethod == 'create' and columns['primaryKey'] is not empty %}
75+
{%~ endfor %}
76+
{%~ if tableMethod == 'create' and columns['primaryKey'] is not empty %}
7777
$table->addPrimaryKey([{{
7878
Migration.stringifyList(columns['primaryKey'], {'indent': 3}) | raw
7979
}}]);
80-
{% endif %}
81-
{% endif %}
80+
{%~ endif %}
81+
{%~ endif %}
8282
{% endif %}
8383
$table->{{ tableMethod }}(){% if tableMethod == 'drop' %}->save(){% endif %};
8484
{% endfor %}

templates/bake/config/snapshot.twig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,21 @@ class {{ name }} extends AbstractMigration
6464
*/
6565
public function down(): void
6666
{
67-
{% set returnedData = Migration.getReturnedData() %}
68-
{% if returnedData and returnedData['dropForeignKeys'] is not empty %}
69-
{% for table, columnsList in Migration.returnedData['dropForeignKeys'] %}
70-
{% set maxKey = columnsList|length - 1 %}
67+
{%~ set returnedData = Migration.getReturnedData() %}
68+
{%~ if returnedData and returnedData['dropForeignKeys'] is not empty %}
69+
{%~ for table, columnsList in Migration.returnedData['dropForeignKeys'] %}
70+
{%~ set maxKey = columnsList|length - 1 %}
7171
$this->table('{{ table }}')
72-
{% for key, columns in columnsList %}
72+
{%~ for key, columns in columnsList %}
7373
->dropForeignKey(
7474
{{ columns|raw }}
7575
){{ (key == maxKey) ? '->save();' : '' }}
76-
{% endfor %}
76+
{%~ endfor %}
7777
78-
{% endfor %}
79-
{% endif %}
80-
{% for table in tables %}
78+
{%~ endfor %}
79+
{% endif %}
80+
{% for table in tables %}
8181
$this->table('{{ table }}')->drop()->save();
82-
{% endfor %}
82+
{% endfor %}
8383
}
8484
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{% for columnName, columnAttributes in columns %}
2-
{% set type = columnAttributes['type'] %}
3-
{% set columnAttributes = Migration.getColumnOption(columnAttributes) %}
4-
{% set columnAttributes = Migration.stringifyList(columnAttributes, {'indent': 4, 'remove': ['type']}) %}
5-
{% if columnAttributes is not empty %}
2+
{%~ set type = columnAttributes['type'] %}
3+
{%~ set columnAttributes = Migration.getColumnOption(columnAttributes) %}
4+
{%~ set columnAttributes = Migration.stringifyList(columnAttributes, {'indent': 4, 'remove': ['type']}) %}
5+
{%~ if columnAttributes is not empty %}
66
->addColumn('{{ columnName }}', '{{ type }}', [{{ columnAttributes | raw }}])
7-
{% else %}
7+
{%~ else %}
88
->addColumn('{{ columnName }}', '{{ type }}')
9-
{% endif -%}
9+
{%~ endif -%}
1010
{% endfor -%}

0 commit comments

Comments
 (0)