This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.php
451 lines (397 loc) · 10.2 KB
/
build.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<?php
/**
* extension source version
*/
$version = '1.7.3';
/**
* dist filename
*/
$distFilenamePrefix = 'woocommerce-';
$distFilenameSuffix = '';
/**
* path within zip file
* set to false to have source at the root of the zip file
* a requirement in some systems to make the zip file installable
* others require it to be lowercase without special characters
*/
// $distFilenameRootDirName = false;
/**
* Dragons below
* Do not change
*/
/**
* build config
*/
$placeholderName = 'Payment Gateway Cloud';
$hostname = $argv[1] ?? null;
$name = $argv[2] ?? null;
$debug = strpos(implode(' ', $argv), ' --debug') !== false;
$srcDir = 'src';
$buildDir = 'build';
$distDir = 'dist';
line();
highlight('Whitelabel Build Script');
/**
* check workspace
*/
if (!realpath($srcDir)) {
line();
error('Source directory does not exist');
exit;
}
/**
* validate name input
*/
if (empty($name)) {
line();
error('Name must not be empty');
usage();
exit;
}
/**
* validate domain input
*/
$hostname = filter_var($hostname, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
if (empty($hostname)) {
line();
error('Host name must be valid');
usage();
exit;
}
/**
* the zip file's name without extension
*/
$distFilename = $distFilenamePrefix . kebabCase($name) . '-' . $version . $distFilenameSuffix;
line(' '.$distFilename);
/**
* relative path within zip file
*/
$distFilenameRootDirName = ($distFilenameRootDirName ?? true) === false ? '' : identifierCase($name);
/**
* create replacement map
*/
$replacementMap = [
// X.Y.Z -> 1.1.0
'X.Y.Z' => $version,
// "gateway.paymentgateway.cloud" -> "gateway.myprovider.com" (client xml namespace and endpoints)
'gateway.paymentgateway.cloud' => $hostname,
// "sandbox.paymentgateway.cloud" -> "sandbox.myprovider.com" (client xml namespace and endpoints)
'sandbox.paymentgateway.cloud' => $hostname,
// "github.com/ixopay/pgc-woocommerce" -> "github.com/user/repo" (client xml namespace and endpoints)
'github.com/ixopay/pgc-woocommerce' => "github.com/user/repo",
// "Payment Gateway Cloud" -> "My Provider"
$placeholderName => $name,
// "PaymentGatewayCloud" -> "MyProvider" (namespaces and other identifiers)
pascalCase($placeholderName) => pascalCase($name),
// "paymentGatewayCloud" -> "myProvider"
camelCase($placeholderName) => camelCase($name),
// "paymentgatewaycloud" -> "myprovider"
identifierCase($placeholderName) => identifierCase($name),
// "payment-gateway-cloud" -> "my-provider"
kebabCase($placeholderName) => kebabCase($name),
// "payment_gateway_cloud" -> "my_provider"
snakeCase($placeholderName) => snakeCase($name),
// "PAYMENT_GATEWAY_CLOUD" -> "MY_PROVIDER" (constants)
constantCase($placeholderName) => constantCase($name),
];
/**
* print replacement map and prompt user if planned changes are ok
*/
line();
info('Replacements for file/folder names and contents:');
foreach ($replacementMap as $old => $new) {
line(' ' . $old . ' => ' . $new);
}
line();
prompt('This will clear any existing "' . $buildDir . '" folder and start the build. OK?');
/**
* Prefix composer autoloader names with a unique hash.
* This prevents conflicts in case two whitelabel plugins, which were both
* built from the same version of the source, are installed at the same time.
*/
$composerHashPrefix = md5(identifierCase($name) . '-' . $version);
$replacementMap = array_merge($replacementMap, [
'ComposerStaticInit' => 'ComposerStaticInit' . $composerHashPrefix,
'ComposerAutoloaderInit' => 'ComposerAutoloaderInit' . $composerHashPrefix,
]);
/**
* build
* clear existing build folder
* copy source to build folder
* applies replacement map to folder names, file names and file contents while at it
*/
deleteDir($buildDir);
info('Building...');
build($srcDir, $buildDir, $replacementMap);
success('Build complete');
/**
* create dist folder if needed
*/
if (!file_exists($distDir)) {
mkdir($distDir);
info('Created dist directory');
}
/**
* zip build to myprovider.zip
*/
info('Creating zip file...');
zipBuildToDist($buildDir, $distDir, $distFilename, $distFilenameRootDirName);
exit;
/**
* Helper functions below
*/
/**
* print usage info
*/
function usage()
{
warn('Usage: php build.php [hostname] [name]');
line('Example: php build.php gateway.mypaymentprovider.com "My Payment Provider"');
line();
}
/**
* @param string $string
* @return string
*/
function camelCase($string)
{
return lcfirst(pascalCase($string));
}
/**
* @param string $string
* @return string
*/
function kebabCase($string)
{
return str_replace('_', '-', snakeCase($string));
}
/**
* @param string $string
* @return string
*/
function pascalCase($string)
{
return ucfirst(str_replace(' ', '', ucwords(strtolower(preg_replace('/^a-z0-9]+/', ' ', $string)))));
}
/**
* @param string $string
* @return string
*/
function snakeCase($string)
{
return strtolower(str_replace(' ', '_', ucwords(preg_replace('/^a-z0-9]+/', ' ', $string))));
}
/**
* @param string $string
* @return mixed
*/
function identifierCase($string)
{
return strtolower(pascalCase($string));
}
/**
* @param string $string
* @return mixed
*/
function constantCase($string)
{
return strtoupper(snakeCase($string));
}
/**
* @param null $message
*/
function line($message = null)
{
echo $message . "\n";
}
/**
* @param null $message
*/
function error($message = null)
{
echo "\e[0;31m[ERROR] " . $message . "\e[0m\n";
}
/**
* @param null $message
*/
function warn($message = null)
{
echo "\e[0;33m[WARN] " . $message . "\e[0m\n";
}
/**
* @param null $message
*/
function info($message = null)
{
echo "\e[0;36m[INFO] " . $message . "\e[0m\n";
}
/**
* @param null $message
*/
function success($message = null)
{
echo "\e[0;32m[SUCCESS] " . $message . "\e[0m\n";
}
/**
* @param string $message
*/
function highlight($message)
{
echo "\e[0;34m" . $message . "\e[0m\n";
}
/**
* @param string $message
*/
function debug($message)
{
global $debug;
if ($debug) {
echo "\e[1;33m[DEBUG] " . $message . "\e[0m\n";
}
}
/**
* @param string $message
*/
function prompt($message)
{
echo "\e[0;35m" . $message . " [y/n]\e[0m\n";
$handle = fopen("php://stdin", "r");
$line = fgets($handle);
if (trim($line) !== 'y') {
warn('Abort');
exit;
}
fclose($handle);
}
/**
* @param string $src
* @param string $dst
* @param array $replacementMap
*/
function build($src, $dst, $replacementMap = [])
{
debug('Scan "' . $src . '"');
$dir = opendir($src);
@mkdir($dst);
while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
$srcFile = $src . '/' . $file;
$destFile = $dst . '/' . applyReplacements($file, $replacementMap);
if (is_dir($srcFile)) {
build($srcFile, $destFile, $replacementMap);
} else {
debug('Copy "' . $destFile . '" -> "' . $destFile . '"');
copy($srcFile, $destFile);
debug('Process "' . $destFile . '"');
replaceContents($destFile, $replacementMap);
}
}
}
closedir($dir);
}
/**
* @param string $file
* @param array $replacementMap
*/
function replaceContents($file, $replacementMap)
{
file_put_contents($file, applyReplacements(file_get_contents($file), $replacementMap));
}
/**
* @param string $string
* @param array $replacementMap
* @return string
*/
function applyReplacements($string, $replacementMap)
{
foreach ($replacementMap as $old => $new) {
$string = str_replace($old, $new, $string);
}
return $string;
}
/**
* @param string $dir
*/
function deleteDir($dir)
{
$dir = sanitizeDirInput($dir);
$dirPath = realpath(__DIR__ . '/' . $dir);
if (!$dirPath) {
return;
}
/**
* do not allow to delete this script's parent directory
*/
if ($dirPath == __DIR__) {
error('Deleting directory "' . $dir . '" is not allowed');
exit;
}
/**
* do not allow to delete any path that is not within this script's parent directory
*/
if (strpos($dirPath, __DIR__) !== 0) {
error('Deleting directory "' . $dir . '" is not allowed');
exit;
}
warn('Deleting ' . $dirPath);
$it = new RecursiveDirectoryIterator($dirPath, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it,
RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $file) {
if ($file->isDir()) {
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
rmdir($dirPath);
}
/**
* @param string $srcDir
* @param string $destDir
* @param string $filename
* @param string $rootDirectoryName the root directory's name within the zip file
*/
function zipBuildToDist($srcDir, $destDir, $filename, $rootDirectoryName)
{
$srcDir = sanitizeDirInput($srcDir);
$destDir = sanitizeDirInput($destDir);
$srcDirPath = realpath($srcDir);
$destDirPath = realpath($destDir);
$zipFilename = $filename . '.zip';
$zip = new ZipArchive();
$zip->open($destDirPath . '/' . $zipFilename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($srcDirPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
/**
* Skip directories (they would be added automatically)
*/
if (!$file->isDir()) {
/**
* Get real and relative path for current file
*/
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($srcDirPath) + 1);
$zip->addFile($filePath, $rootDirectoryName . '/' . $relativePath);
}
}
/**
* Zip archive will be created after closing object
*/
$zip->close();
success('Created file "' . $destDir . '/' . $zipFilename . '"');
}
/**
* do not allow to reference a directory outside of this script's path
*
* @param string $dir
* @return string
*/
function sanitizeDirInput($dir)
{
return ltrim(str_replace('../', '', $dir), '/');
}