forked from deployphp/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshopware.php
196 lines (174 loc) · 5.55 KB
/
shopware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace Deployer;
use MJS\TopSort\Implementations\FixedArraySort;
require_once __DIR__ . '/common.php';
add('recipes', ['shopware']);
set('repository', '[email protected]:shopware/production.git');
set('release_name', static function () {
return date('YmdHis');
});
set('shared_files', [
'.env',
]);
set('shared_dirs', [
'custom/plugins',
'config/jwt',
'files',
'var/log',
'public/media',
'public/thumbnail',
'public/sitemap',
]);
set('writable_dirs', [
'custom/plugins',
'files',
'var',
'public/media',
'public/thumbnail',
'public/sitemap',
]);
set('static_folders', []);
task('sw:update_code', static function () {
run('git clone {{repository}} {{release_or_current_path}}');
});
task('sw:system:install', static function () {
run('cd {{release_or_current_path}} && bin/console system:install');
});
task('sw:build', static function () {
run('cd {{release_or_current_path}}/bin && bash build.sh');
});
task('sw:system:setup', static function () {
run('cd {{release_or_current_path}} && bin/console system:setup');
});
task('sw:theme:compile', static function () {
run('cd {{release_or_current_path}} && bin/console theme:compile');
});
task('sw:cache:clear', static function () {
run('cd {{release_or_current_path}} && bin/console cache:clear');
});
task('sw:cache:warmup', static function () {
run('cd {{release_or_current_path}} && bin/console cache:warmup');
run('cd {{release_or_current_path}} && bin/console http:cache:warm:up');
});
task('sw:database:migrate', static function () {
run('cd {{release_or_current_path}} && bin/console database:migrate --all');
});
task('sw:plugin:refresh', function () {
run('cd {{release_or_current_path}} && bin/console plugin:refresh');
});
/**
* @return array
* @throws \MJS\TopSort\CircularDependencyException
* @throws \MJS\TopSort\ElementNotFoundException
*/
function getSortedPlugins(): array
{
cd('{{release_or_current_path}}');
$plugins = explode("\n", run('bin/console plugin:list'));
// take line over headlines and count "-" to get the size of the cells
$lengths = array_filter(array_map('strlen', explode(' ', $plugins[4])));
// ignore first seven lines (headline, title, table, ...)
$plugins = array_slice($plugins, 7, -3);
$parsedPlugins = [];
foreach ($plugins as $plugin) {
$pluginParts = [];
foreach ($lengths as $length) {
$pluginParts[] = trim(substr($plugin, 0, $length));
$plugin = substr($plugin, $length + 1);
}
$parsedPlugins[$pluginParts[0]] = $pluginParts;
}
$composer = json_decode(run('cat composer.lock'), true);
$pluginMapping = $dependencies = [];
foreach ($parsedPlugins as $plugin) {
$pluginName = $plugin[0];
// collect cpmposer plugin names
foreach ($composer['packages'] as $config) {
if (!isset($config['extra']['shopware-plugin-class'])) {
// we only collect a mapping for shopware modules name <-> composer name
continue;
}
if (str_ends_with($config['extra']['shopware-plugin-class'], $pluginName)) {
$pluginMapping[$config['name']] = $pluginName;
}
}
// collect dependencies
foreach ($composer['packages'] as $config) {
if (!isset($pluginMapping[$config['name']])) {
// if the composer.json doesn't belong to a shopware module
// or doesn't have dependencies, ignore it
continue;
}
$dependencies[$config['name']] = array_filter(array_keys($config['require'] ?? []),
static function ($composerName) use ($pluginMapping) {
// only add dependencies between shopware modules
return isset($pluginMapping[$composerName]);
});
}
}
$sorter = new FixedArraySort();
foreach ($dependencies as $name => $dep) {
$sorter->add($name, $dep);
}
return array_map(static function ($name) use ($parsedPlugins, $pluginMapping) {
return $parsedPlugins[$pluginMapping[$name]];
}, $sorter->sort());
}
task('sw:plugin:activate:all', static function () {
invoke('sw:plugin:refresh');
foreach (getSortedPlugins() as $pluginInfo) {
[
$plugin,
$label,
$version,
$upgrade,
$author,
$installed,
$active,
$upgradeable,
] = $pluginInfo;
if ($installed === 'No' || $active === 'No') {
run("cd {{release_or_current_path}} && bin/console plugin:install --activate $plugin");
}
}
});
task('sw:plugin:migrate:all', static function () {
invoke('sw:plugin:refresh');
foreach (getSortedPlugins() as $pluginInfo) {
[
$plugin,
$label,
$version,
$upgrade,
$author,
$installed,
$active,
$upgradeable,
] = $pluginInfo;
if ($installed === 'Yes' || $active === 'Yes') {
run("cd {{release_or_current_path}} && bin/console database:migrate --all $plugin || true");
}
}
});
/**
* Grouped SW deploy tasks
*/
task('sw:deploy', [
'sw:build',
'sw:plugin:activate:all',
'sw:database:migrate',
'sw:plugin:migrate:all',
'sw:theme:compile',
'sw:cache:clear',
]);
/**
* Main task
*/
desc('Deploy your project');
task('deploy', [
'deploy:prepare',
'sw:deploy',
'deploy:clear_paths',
'sw:cache:warmup',
'deploy:publish',
]);