/
ComposerScripts.php
214 lines (171 loc) · 8.01 KB
/
ComposerScripts.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace DotsUnited;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Json\JsonFile;
use Composer\Json\JsonManipulator;
use Composer\Package\Locker;
use Composer\Script\Event;
class ComposerScripts
{
public static function postCreateProject(Event $event)
{
$args = $event->getArguments();
$io = $event->getIO();
$projectIdentifier = isset($args['directory']) ? $args['directory'] : 'my-project';
$projectIdentifier = $io->ask('Enter the project identifier [<comment>' . $projectIdentifier . '</comment>]: ', $projectIdentifier);
$projectName = ucwords(str_replace('-', ' ', $projectIdentifier));
$projectName = $io->ask('Enter the project name [<comment>' . $projectName . '</comment>]: ', $projectName);
// --- Add Deployment
$deployment = $io->ask('Add a deployment type (<comment>ssh/ftp/NONE</comment>): ', 'none');
self::setupDeployment($deployment, $projectName, $projectIdentifier);
// --- Add Wordpress
// TEMP DISABLED
// $withWordpress = $io->askConfirmation('Should wordpress be added to the project? (<comment>y/N</comment>) ', false);
// self::setupWordpress($withWordpress, $io);
// --- Replace in files & dirs
self::replace(__DIR__ . '/.gitignore', $projectName, $projectIdentifier);
self::replace(__DIR__ . '/package.json', $projectName, $projectIdentifier);
self::replace(__DIR__ . '/package-lock.json', $projectName, $projectIdentifier);
self::replace(__DIR__ . '/README.md.template', $projectName, $projectIdentifier);
self::replace(__DIR__ . '/tailwind.config.js', $projectName, $projectIdentifier);
self::replace(__DIR__ . '/webpack.config.js', $projectName, $projectIdentifier);
self::replace(__DIR__ . '/webpack-blocks.config.js', $projectName, $projectIdentifier);
self::replace(__DIR__ . '/public/wp-config.dist.php', $projectName, $projectIdentifier);
self::replaceDir(__DIR__ . '/assets', $projectName, $projectIdentifier);
self::replaceDir(__DIR__ . '/public/wp-content/mu-plugins/wordpress-boilerplate', $projectName, $projectIdentifier);
self::replaceDir(__DIR__ . '/public/wp-content/themes', $projectName, $projectIdentifier);
self::replaceDir(__DIR__ . '/public/favicons', $projectName, $projectIdentifier);
// --- Copy/rename files & dirs
rename(__DIR__ . '/assets/gutenberg/plugins/wordpress-boilerplate', __DIR__ . '/assets/gutenberg/plugins/' . $projectIdentifier);
rename(__DIR__ . '/public/wp-content/mu-plugins/wordpress-boilerplate/wordpress-boilerplate.php', __DIR__ . '/public/wp-content/mu-plugins/wordpress-boilerplate/' . $projectIdentifier . '.php');
rename(__DIR__ . '/public/wp-content/mu-plugins/wordpress-boilerplate', __DIR__ . '/public/wp-content/mu-plugins/' . $projectIdentifier);
rename(__DIR__ . '/public/wp-content/themes/wordpress-boilerplate', __DIR__ . '/public/wp-content/themes/' . $projectIdentifier);
rename(__DIR__ . '/public/wp-config.dist.php', __DIR__ . '/public/wp-config.php');
// --- Cleanup
unlink(__DIR__ . '/LICENSE');
unlink(__DIR__ . '/README.md');
rename(__DIR__ . '/README.md.template', __DIR__ . '/README.md');
self::removeDir(__DIR__ . '/.docker');
self::removeDir(__DIR__ . '/.github');
// ---
$manipulator = new JsonManipulator(
file_get_contents(__DIR__ . '/composer.json')
);
$manipulator->removeProperty('name');
$manipulator->removeProperty('description');
$manipulator->removeProperty('keywords');
$manipulator->removeProperty('homepage');
$manipulator->removeProperty('license');
$manipulator->removeSubNode('require-dev', 'composer/composer');
$manipulator->removeProperty('autoload');
$manipulator->removeSubNode('scripts', 'post-create-project-cmd');
file_put_contents(
__DIR__ . '/composer.json',
$manipulator->getContents()
);
self::updateComposerLock(
__DIR__ . '/composer.json',
$event->getComposer(),
$io
);
// ---
unlink(__DIR__ . '/ComposerScripts.php');
}
private static function replaceDir($dir, $projectName, $projectIdentifier)
{
foreach (glob(rtrim($dir, '\\/') . '/*') as $file) {
if (is_dir($file)) {
self::replaceDir($file, $projectName, $projectIdentifier);
} else {
self::replace($file, $projectName, $projectIdentifier);
}
}
}
private static function removeDir($dir)
{
$files = glob($dir . '/*');
foreach ($files as $file) {
is_dir($file) ? self::removeDir($file) : unlink($file);
}
rmdir($dir);
}
private static function replace($file, $projectName, $projectIdentifier)
{
$content = file_get_contents($file);
$content = str_ireplace(
array(
'wordpress boilerplate',
'wordpress-boilerplate',
'wordpress_boilerplate',
'WordpressBoilerplate',
),
array(
$projectName,
$projectIdentifier,
str_replace('-', '_', $projectIdentifier),
str_replace('-', '', ucwords($projectIdentifier, '-')),
),
$content
);
file_put_contents($file, $content);
}
private static function updateComposerLock($composerFile, Composer $composer, IOInterface $io)
{
$lock = substr($composerFile, 0, -4).'lock';
$composerJson = file_get_contents($composerFile);
$lockFile = new JsonFile($lock, null, $io);
$locker = new Locker(
$io,
$lockFile,
$composer->getInstallationManager(),
$composerJson
);
$lockData = $locker->getLockData();
$lockData['content-hash'] = Locker::getContentHash($composerJson);
$lockFile->write($lockData);
}
private static function setupDeployment($type, $projectName, $projectIdentifier)
{
switch ($type) {
case 'ftp':
unlink(__DIR__ . '/.gitlab-ci.ssh.dist.yml');
unlink(__DIR__ . '/deploy.dist.php');
rename(__DIR__ . '/.gitlab-ci.ftp.dist.yml', __DIR__ . '/.gitlab-ci.yml');
self::replace(__DIR__ . '/.gitlab-ci.yml', $projectName, $projectIdentifier);
break;
case 'ssh':
unlink(__DIR__ . '/.gitlab-ci.ftp.dist.yml');
rename(__DIR__ . '/.gitlab-ci.ssh.dist.yml', __DIR__ . '/.gitlab-ci.yml');
rename(__DIR__ . '/deploy.dist.php', __DIR__ . '/deploy.php');
self::replace(__DIR__ . '/.gitlab-ci.yml', $projectName, $projectIdentifier);
self::replace(__DIR__ . '/deploy.php', $projectName, $projectIdentifier);
break;
case 'none':
default:
unlink(__DIR__ . '/.gitlab-ci.ftp.dist.yml');
unlink(__DIR__ . '/.gitlab-ci.ssh.dist.yml');
unlink(__DIR__ . '/deploy.dist.php');
break;
}
}
private static function setupWordpress($withWordpress, IOInterface $io)
{
if (!$withWordpress) {
return;
}
$io->write('Download latest wordpress version from wordpress.org...');
if (!copy('https://wordpress.org/latest.zip', __DIR__ . '/wordpress.zip')) {
return;
}
$io->write('Extract zip archive...');
$zip = new \ZipArchive;
if (true === $zip->open(__DIR__ . '/wordpress.zip')) {
$zip->extractTo(__DIR__ . '/public');
$zip->close();
}
// TODO: move wordpress (without wp-content) to public (after extract it will be in public/wordpress
// copy(__DIR__ . '/public/wp-config.dist.php', __DIR__ . '/public/wp-config.php');
unlink(__DIR__ . '/wordpress.zip');
}
}