-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcomponents.php
More file actions
616 lines (521 loc) · 14.5 KB
/
Copy pathcomponents.php
File metadata and controls
616 lines (521 loc) · 14.5 KB
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/usr/bin/php
<?php
/**
* Components manager.
*
* @copyright 2015 Fernando Val
* @author Fernando Val <fernando.val@gmail.com>
*
* @version 5.0.3
*
* This is script is not a Composer plugin.
*
* This post install/update script for Composer is not a packager version number.
* It is a helper program to copy (and minify) component files from the download
* destination directories to final folders in the web server accessible tree.
* Than you can use your favorite package manager like Composer, NPM, Yarn, etc.
*
* The "components.json" file will be loaded if it exists. Then the list of
* components listed inside "components" entry.
*
* The following attributes will be used:
*
* "source" - (only in components.json) The source folder where to find the files;
* "target" - Destination folder to the files;
* "ignore-subdirs" - If true all files will be sabed in same folder;
* "minify" - "on" or "off" to minify or not the Javascript and CSS files;
* "files" - The array of files or the file to be copied. Wildcards accepted.
*
* If there is no "files" defined for every component, their bower.json file is
* used by this script to decide which files will be copied.
*
* NOTE: To minify CSS and JS files, is recommended the use of the Minify class
* by Matthias Mullie.
* https://github.com/matthiasmullie/minify
*/
define('DS', DIRECTORY_SEPARATOR);
define('LF', "\n");
define('CS_GREEN', "\033[32m");
define('CS_RED', "\033[31m");
define('CS_RESET', "\033[0m");
define('TAB', ' ');
define('LOCK_FILE', __DIR__ . DS . 'components.lock');
define('BOWER_FILE', 'bower.json');
/**
* Gets the component source path.
*
* Also checks if the destination is defined.
*
* @param array $data
*
* @return string
*/
function checkComponentPath(array $data): string
{
if (!isset($data['source'])) {
fatalError(TAB . 'Component source path undefined.');
}
// Component sub directory
$path = __DIR__ . DS . implode(DS, explode('/', $data['source']));
// Check component's source path
if (!is_dir($path)) {
echo TAB, CS_RED, 'Component\'s "', $path, '" does not exists.', CS_RESET, LF;
return '';
}
// Check compnent's configuration
if (!isset($data['target'])) {
echo TAB, CS_RED, 'Target directory not defined.', CS_RESET, LF;
return '';
}
return $path;
}
/**
* Copies all files from a directory.
*
* @param string $path
* @param string $dest
* @param string $minify
*
* @return array
*/
function copyDir(string $path, string $dest, string $minify): array
{
$installed = [];
$objects = scandir($path);
foreach ($objects as $file) {
if ($file == '.' || $file == '..') {
continue;
}
$installed = array_merge(
$installed,
recursiveCopy($path . DS . $file, $dest . DS . $file, $minify)
);
}
return $installed;
}
/**
* Copy a file.
*
* @param string $path
* @param string $dest
* @param string $minify
*
* @return void
*/
function copyFile(string $path, string $dest, string $minify): void
{
// The source is a file
$dir = dirname($dest);
grantDestination($dir);
// Copy only if source is new or newer
if (is_file($dest) && filemtime($path) < filemtime($dest)) {
return;
} elseif (!realCopy($path, $dest, $minify)) {
echo TAB, CS_RED, '[ERROR] Copying (', $path, ') to (', $dest, ')', CS_RESET, LF;
}
}
/**
* Removes an empty directory.
*
* @param array $dir
*
* @return void
*/
function delDir(array $dir): void
{
$type = $dir['type'] ?? '';
$path = $dir['path'] ?? '';
if ($type !== 'd' || !$path) {
return;
} elseif (is_dir($path) && count(getDir($path)) === 0 && !rmdir($path)) {
echo TAB, CS_RED, 'Fail to delete "', $path, '" file.', CS_RESET, LF;
}
}
/**
* Deletes a file.
*
* @param array $file
*
* @return void
*/
function delFile(array $file): void
{
$type = $file['type'] ?? '';
$path = $file['path'] ?? '';
if ($type !== 'f' || !$path) {
return;
} elseif (is_file($path) && !unlink($path)) {
echo TAB, CS_RED, 'Fail to delete "', $path, '" file.', CS_RESET, LF;
}
$dir = dirname($path);
$files = glob($dir . DS . '{.[!.],}*', GLOB_BRACE);
if (is_array($files) && !count($files)) {
delDir([
'type' => 'd',
'path' => $dir,
]);
}
}
/**
* Verifies all installed components that is no more listed inside Json.
*
* @param array $components
*
* @return void
*/
function delRemovedComponents(array $components): void
{
$installed = loadLockFile();
// Verify if any component was removed
foreach (array_reverse($installed) as $name => $files) {
if (isset($components[$name])) {
continue;
}
echo ' - Deleting ', CS_GREEN, $name, CS_RESET, ' files', LF;
foreach (array_reverse($files) as $file) {
delDir($file);
delFile($file);
}
}
}
/**
* Terminates the program with an error message.
*
* @param string $error
*
* @return void
*
* @SuppressWarnings(PHPMD.ExitExpression)
*/
function fatalError(string $error): void
{
echo CS_RED, $error, CS_RESET, LF;
exit(1);
}
/**
* Gets the list of files from Bower Json.
*
* @param string $path
*
* @return array
*/
function getBowerMain(string $path): array
{
if (!file_exists($path . DS . BOWER_FILE)) {
return ['*'];
}
$bower = loadJson($path . DS . BOWER_FILE);
if (!isset($bower['main'])) {
echo TAB, CS_RED, 'Main section does not exists in "' . $path . DS . BOWER_FILE . '" file.', CS_RESET, LF;
return [];
}
return is_array($bower['main']) ? $bower['main'] : [$bower['main']];
}
/**
* Gets the list of files of the component.
*
* @param array $data
* @param string $path
*
* @return array
*/
function getComponentFiles(array $data, string $path): array
{
if (isset($data['files'])) {
return is_array($data['files']) ? $data['files'] : [$data['files']];
}
return getBowerMain($path);
}
/**
* Returns an array with directory content without . and .. special dirs.
*
* @param string $path
*
* @return array
*/
function getDir(string $path): array
{
return array_filter(
scandir($path),
fn ($file) => $file !== '.' && $file !== '..'
);
}
/**
* Gets the destination folder for the component.
*
* Creates the folder if does not exists.
*
* @param string $component
* @param array $data
*
* @return string
*/
function getDestinantion(string $component, array $data)
{
if (!is_string($data['target'])) {
fatalError(TAB . 'No destination defined for "' . $component . '" component.');
}
$destination = __DIR__ . DS . implode(DS, explode('/', $data['target']));
if (!is_dir($destination) && !mkdir($destination, 0775, true)) {
fatalError(TAB . 'Can\'t create "' . $destination . '" directory.');
}
return $destination;
}
/**
* Grants the existance of the directory.
*
* @param string $dir
*
* @return void
*/
function grantDestination(string $dir): void
{
if (is_dir($dir)) {
return;
} elseif (!mkdir($dir, 0775, true)) {
echo TAB, CS_RED, 'Can\'t create "', $dir, '" directory.', CS_RESET, LF;
}
}
/**
* Loads the components.json file and returns an array with components list.
*
* @return array
*/
function loadComponentsJson(): array
{
$jsonpath = __DIR__ . DS . 'components.json';
if (!file_exists($jsonpath)) {
return [];
}
$json = loadJson($jsonpath);
if (!is_array($json['components'] ?? null)) {
fatalError('Syntax error in components.json');
}
$components = [];
foreach ($json['components'] as $name => $data) {
$components[$name] = $data;
}
return $components;
}
/**
* Parses the Json file.
*
* @param string $json
*
* @return array
*/
function loadJson(string $filepath): array
{
$jsonstr = file_get_contents($filepath);
if (!$jsonstr) {
fatalError('Can\'t open ' . $filepath . ' file.');
}
$parsed = json_decode($jsonstr, true);
$error = json_last_error();
if ($error === JSON_ERROR_NONE) {
return $parsed;
}
$jsonErrors = [
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded',
JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded',
JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given',
];
fatalError($jsonErrors[$error] ?? 'Unknown error occurred');
}
/**
* Loads the components.lock file.
*
* @return array
*/
function loadLockFile(): array
{
if (!file_exists(LOCK_FILE)) {
return [];
}
$lock = file_get_contents(LOCK_FILE);
if (!$lock) {
fatalError('Can\'t open ' . LOCK_FILE . ' file.');
}
return unserialize($lock);
}
/**
* Minify a file using Mattias Mullie's component.
*
* @param string $buffer
* @param string $ext
*
* @return string
*/
function matthiasMullie(string $buffer, string $ext): string
{
if ($ext !== 'css' && $ext !== 'js') {
echo TAB, CS_RED, '[WARNING] Invalid minify method: ', $ext, CS_RESET, LF;
return $buffer;
}
$minifier = 'css'
? new MatthiasMullie\Minify\CSS($buffer)
: new MatthiasMullie\Minify\JS($buffer);
return $minifier->minify();
}
/**
* Minify the file if turned on.
*
* @param string $buffer
* @param string $minify
*
* @return string
*/
function minifyFile(string $buffer, string $minify): string
{
if ($minify == 'off') {
return $buffer;
}
if (class_exists('MatthiasMullie\Minify\Minify')) {
return matthiasMullie($buffer, $minify);
}
// Matthias Mullie's Minify class not found. I Will try by myself but this is not the best way.
switch ($minify) {
case 'css':
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
$buffer = str_replace(["\r\n", "\r", "\n", "\t", ' ', TAB, ' '], '', $buffer);
$buffer = preg_replace(['(( )+{)', '({( )+)'], '{', $buffer);
$buffer = preg_replace(['(( )+})', '(}( )+)', '(;( )*})'], '}', $buffer);
$buffer = preg_replace(['(;( )+)', '(( )+;)'], ';', $buffer);
break;
case 'js':
$buffer = preg_replace("/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/", '', $buffer);
$buffer = str_replace(["\r\n", "\r", "\t", "\n", ' ', TAB, ' '], '', $buffer);
$buffer = preg_replace(['(( )+\))', '(\)( )+)'], ')', $buffer);
break;
default:
echo TAB, CS_RED, '[WARNING] Invalid minify method: ', $minify, CS_RESET, LF;
}
return $buffer;
}
/**
* Copy file minifyint if necessary.
*
* @param string $source
* @param string $destiny
* @param string $minify
*
* @return bool
*/
function realCopy(string $source, string $destiny, string $minify = 'auto')
{
if ($minify == 'auto' || $minify == 'on') {
$minify = (substr($source, -4) == '.css' ? 'css' : (substr($source, -3) == '.js' ? 'js' : 'off'));
}
$buffer = file_get_contents($source);
if ($buffer == false) {
echo TAB, CS_RED, '[ERROR] Failed to open ', $source, CS_RESET, LF;
return false;
}
$buffer = minifyFile($buffer, $minify);
$return = file_put_contents($destiny, minifyFile($buffer, $minify));
if ($return !== false) {
chmod($destiny, 0664);
}
return $return;
}
/**
* Copy files or directory recursively.
*
* @param string $path
* @param string $dest
* @param string $minify
* @param string $component
*
* @return array
*/
function recursiveCopy(string $path, string $dest, string $minify): array
{
$installed = [];
if (is_dir($path)) {
// The source is a directory
return array_merge(
$installed,
copyDir($path, $dest, $minify)
);
} elseif (is_file($path)) {
copyFile($path, $dest, $minify);
$installed[] = [
'path' => $dest,
'type' => 'f',
];
return $installed;
}
/*
* Oh! Is a wildcard path.
*/
$dest = dirname($dest);
foreach (glob($path) as $filename) {
$installed = array_merge(
$installed,
recursiveCopy($filename, $dest . DS . basename($filename), $minify)
);
}
return $installed;
}
/**
* Adds Composer autoload if exists.
*
* @return void
*/
function requireComposerAutoload(): void
{
$composer = loadJson(__DIR__ . DS . 'composer.json');
$vendor = isset($composer['config']) && isset($composer['config']['vendor-dir'])
? $composer['config']['vendor-dir']
: 'vendor';
if (file_exists($vendor . DS . 'autoload.php')) {
require $vendor . DS . 'autoload.php';
}
}
/** @var array components list */
$components = loadComponentsJson();
/** @var array installed components list */
$installed = [];
echo CS_GREEN, 'Starting the installation of the extra components', CS_RESET, LF;
requireComposerAutoload();
delRemovedComponents($components);
// Process every component
foreach ($components as $name => $data) {
echo ' - Processing ', CS_GREEN, $name, CS_RESET, ' files', LF;
$installed[$name] = [];
$path = checkComponentPath($data);
if (!$path) {
continue;
}
// Component properties
$files = getComponentFiles($data, $path);
$noSubdirs = $data['ignore-subdirs'] ?? false;
$minify = $data['minify'] ?? 'off';
$destination = getDestinantion($name, $data);
$installed[$name][] = [
'path' => $destination,
'type' => 'd',
];
foreach ($files as $file) {
$file = implode(DS, explode('/', $file));
$dstFile = $file;
if ($noSubdirs) {
$dstFile = explode('/', $file);
$dstFile = array_pop($dstFile);
}
$installed[$name] = array_merge(
$installed[$name],
recursiveCopy($path . DS . $file, $destination . DS . $dstFile, $minify)
);
}
}
// Write the lock file
echo CS_GREEN, 'Writing lock file', CS_RESET, LF;
if (!file_put_contents(LOCK_FILE, serialize($installed))) {
fatalError('Can\'t write ' . LOCK_FILE . ' file.');
}