9
9
use Composer \Package \CompletePackage ;
10
10
use Composer \Package \Package ;
11
11
use Composer \Package \PackageInterface ;
12
+ use Composer \Plugin \Capable ;
12
13
use Composer \Plugin \PluginInterface ;
13
14
use Composer \Script \Event ;
14
15
use Composer \Script \ScriptEvents ;
15
16
use Composer \Util \Filesystem ;
16
17
use Symfony \Component \Finder \Finder ;
17
18
use Symfony \Component \Finder \Glob ;
19
+ use Composer \Plugin \Capability \CommandProvider ;
18
20
19
21
/**
20
22
* The Drupal libraries installer plugin.
21
23
*/
22
- class Plugin implements PluginInterface, EventSubscriberInterface {
24
+ class Plugin implements PluginInterface, Capable, EventSubscriberInterface {
23
25
24
26
/**
25
27
* The installed-libraries.json lock file schema version.
26
28
*
27
29
* @var string
28
30
*/
29
- const SCHEMA_VERSION = '1.0 ' ;
31
+ const SCHEMA_VERSION = '1.1 ' ;
30
32
31
33
/**
32
34
* The composer package name.
@@ -94,14 +96,24 @@ public static function getSubscribedEvents() {
94
96
return [
95
97
ScriptEvents::POST_INSTALL_CMD => 'install ' ,
96
98
ScriptEvents::POST_UPDATE_CMD => 'install ' ,
99
+ InstallLibrariesEvent::INSTALL_LIBRARIES => 'install ' ,
100
+ ];
101
+ }
102
+
103
+ /**
104
+ * {@inheritDoc}
105
+ */
106
+ public function getCapabilities () {
107
+ return [
108
+ CommandProvider::class => PluginCommandProvider::class,
97
109
];
98
110
}
99
111
100
112
/**
101
113
* Upon running composer install or update, install the drupal libraries.
102
114
*
103
- * @param \Composer\Script\Event $event
104
- * The composer install/update event.
115
+ * @param \Composer\Script\Event|\Zodiacmedia\DrupalLibrariesInstaller\InstallLibrariesEvent $event
116
+ * The composer install/update/install-drupal-libraries event.
105
117
*
106
118
* @throws \Exception
107
119
*/
@@ -209,6 +221,7 @@ protected function processPackage(array $processed_drupal_libraries, array $drup
209
221
// Install each library.
210
222
foreach ($ extra ['drupal-libraries ' ] as $ library => $ library_definition ) {
211
223
$ ignore_patterns = [];
224
+ $ rename = [];
212
225
$ sha1checksum = NULL ;
213
226
if (is_string ($ library_definition )) {
214
227
// Simple format.
@@ -224,6 +237,7 @@ protected function processPackage(array $processed_drupal_libraries, array $drup
224
237
$ version = $ library_definition ['version ' ] ?? $ version ;
225
238
$ distribution_type = $ library_definition ['type ' ] ?? $ distribution_type ;
226
239
$ ignore_patterns = $ library_definition ['ignore ' ] ?? $ ignore_patterns ;
240
+ $ rename = $ library_definition ['rename ' ] ?? $ rename ;
227
241
$ sha1checksum = $ library_definition ['shasum ' ] ?? $ sha1checksum ;
228
242
}
229
243
@@ -251,8 +265,12 @@ protected function processPackage(array $processed_drupal_libraries, array $drup
251
265
'url ' => $ url ,
252
266
'type ' => $ distribution_type ,
253
267
'ignore ' => $ ignore_patterns ,
268
+ 'rename ' => $ rename ,
254
269
'package ' => $ package ->getName (),
255
270
];
271
+ if (empty ($ rename )) {
272
+ unset($ applied_library ['rename ' ]);
273
+ }
256
274
if (isset ($ sha1checksum )) {
257
275
$ applied_library ['shasum ' ] = $ sha1checksum ;
258
276
}
@@ -294,6 +312,7 @@ protected function downloadLibraries(array $processed_libraries, array $applied_
294
312
$ library_package = $ this ->getLibraryPackage ($ library_name , $ processed_library );
295
313
296
314
$ ignore_patterns = $ processed_library ['ignore ' ];
315
+ $ rename = $ processed_library ['rename ' ] ?? NULL ;
297
316
$ install_path = $ this ->installationManager ->getInstallPath ($ library_package );
298
317
if (
299
318
(
@@ -306,7 +325,7 @@ protected function downloadLibraries(array $processed_libraries, array $applied_
306
325
// - wasn't in the lock file.
307
326
// - doesn't match what is in the lock file.
308
327
// - doesn't exist on disk.
309
- $ this ->downloadPackage ($ library_package , $ install_path , $ ignore_patterns );
328
+ $ this ->downloadPackage ($ library_package , $ install_path , $ ignore_patterns, $ rename );
310
329
}
311
330
}
312
331
}
@@ -320,8 +339,10 @@ protected function downloadLibraries(array $processed_libraries, array $applied_
320
339
* The package install path.
321
340
* @param array $ignore_patterns
322
341
* File patterns to ignore.
342
+ * @param array|null $rename
343
+ * Array mapping of files/folders to rename.
323
344
*/
324
- protected function downloadPackage (Package $ library_package , $ install_path , array $ ignore_patterns ) {
345
+ protected function downloadPackage (Package $ library_package , $ install_path , array $ ignore_patterns, array $ rename = NULL ) {
325
346
// Let composer download and unpack the library for us!
326
347
$ this ->downloadManager ->download ($ library_package , $ install_path );
327
348
@@ -364,6 +385,30 @@ function ($file) use ($patterns) {
364
385
$ this ->fileSystem ->remove ($ file_pathname );
365
386
}
366
387
}
388
+
389
+ if ($ rename ) {
390
+ foreach ($ rename as $ original_file => $ destination_file ) {
391
+ $ original_file = $ this ->fileSystem ->normalizePath ("$ install_path/ $ original_file " );
392
+ $ destination_file = $ this ->fileSystem ->normalizePath ("$ install_path/ $ destination_file " );
393
+ if (strpos ($ original_file , $ install_path ) !== 0 ) {
394
+ $ this ->io ->writeError (" - Could not rename <info> $ original_file</info> as it is outside the library directory. " );
395
+ }
396
+ elseif (strpos ($ destination_file , $ install_path ) !== 0 ) {
397
+ $ this ->io ->writeError (" - Could not rename <info> $ destination_file</info> as it is outside the library directory. " );
398
+ }
399
+ elseif (!file_exists ($ original_file )) {
400
+ $ this ->io ->writeError (" - Could not rename <info> $ original_file</info> as it does not exist " );
401
+ }
402
+ elseif (file_exists ($ destination_file )) {
403
+ $ this ->io ->writeError (" - Could not rename <info> $ original_file</info> as the destination file <info> $ destination_file</info> already exists " );
404
+ }
405
+ else {
406
+ $ this ->io ->writeError (" - Renaming <info> $ original_file</info> to <info> $ destination_file</info> " );
407
+ // Attempt to move the file over.
408
+ $ this ->fileSystem ->rename ($ original_file , $ destination_file );
409
+ }
410
+ }
411
+ }
367
412
}
368
413
369
414
/**
@@ -378,7 +423,15 @@ function ($file) use ($patterns) {
378
423
* The pseudo-package for the library.
379
424
*/
380
425
protected function getLibraryPackage ($ library_name , array $ library_definition ) {
381
- $ library_package_name = 'drupal-library/ ' . $ library_name ;
426
+ if (strpos ($ library_name , '/ ' )) {
427
+ // The library name already contains a '/', add the "drupal-library_"
428
+ // prefix to it so that it can be configured to a custom path through its
429
+ // vendor name.
430
+ $ library_package_name = "drupal-library_ $ library_name " ;
431
+ }
432
+ else {
433
+ $ library_package_name = 'drupal-library/ ' . $ library_name ;
434
+ }
382
435
$ library_package = new Package (
383
436
$ library_package_name , $ library_definition ['version ' ], $ library_definition ['version ' ]
384
437
);
0 commit comments