|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ExampleDrupalLibrariesProject\composer; |
| 4 | + |
| 5 | +use Composer\Script\Event; |
| 6 | +use Composer\Util\Filesystem; |
| 7 | +use Composer\Util\Platform; |
| 8 | + |
| 9 | +/** |
| 10 | + * The drupal-libraries-installer example project script handler. |
| 11 | + * |
| 12 | + * Attempts to symlink to the parent repository during development when |
| 13 | + * "extra.symlink-root-project" is true. |
| 14 | + * |
| 15 | + * Mainly useful for testing certain behaviours without having to duplicate |
| 16 | + * code or commit every time in order for composer to pull it in. |
| 17 | + * |
| 18 | + * Composer restricts symlinking to a path containing the current project. |
| 19 | + * So we have to handle it manually with some hacky code. |
| 20 | + * |
| 21 | + * It's definitely not adviced as you could accidentally discard changes in |
| 22 | + * your root project during an install/update if the appropriate event hooks |
| 23 | + * are not triggered. |
| 24 | + */ |
| 25 | +class ScriptHandler { |
| 26 | + |
| 27 | + protected const PACKAGE_NAME = 'zodiacmedia/drupal-libraries-installer'; |
| 28 | + |
| 29 | + /** |
| 30 | + * Attempt to prepare for the post install/update. |
| 31 | + * |
| 32 | + * @param \Composer\Script\Event $event |
| 33 | + * The composer event. |
| 34 | + */ |
| 35 | + public static function preInstall(Event $event) { |
| 36 | + $composer = $event->getComposer(); |
| 37 | + $root_package = $composer->getPackage(); |
| 38 | + $locker = $composer->getLocker(); |
| 39 | + // Always attempt to clean up if: |
| 40 | + // - the lock file is not present. |
| 41 | + // - the lock file is outdated. |
| 42 | + // - in the pre-update hook. |
| 43 | + $originating_event = $event->getOriginatingEvent(); |
| 44 | + $originating_event_name = $originating_event ? $originating_event->getName() : NULL; |
| 45 | + $needs_updating = $originating_event_name === 'pre-update-cmd' || !$locker->isLocked() || !$locker->isFresh(); |
| 46 | + $should_symlink = empty($root_package->getExtra()['symlink-root-project']); |
| 47 | + if ($needs_updating || $should_symlink) { |
| 48 | + // Don't symlink the root project. Attempt to remove any existing symlink. |
| 49 | + $filesystem = new Filesystem(); |
| 50 | + $destination = static::getPackageDestination(); |
| 51 | + $destination = $filesystem->normalizePath($destination); |
| 52 | + |
| 53 | + $is_junction = $filesystem->isJunction($destination); |
| 54 | + $is_symlink = is_link($destination); |
| 55 | + if ($is_junction || $is_symlink) { |
| 56 | + $io = $event->getIO(); |
| 57 | + if ($is_junction) { |
| 58 | + $io->writeError(sprintf('Removing existing junction for <info>%s</info>', static::PACKAGE_NAME)); |
| 59 | + } |
| 60 | + elseif ($is_symlink) { |
| 61 | + $io->writeError(sprintf('Removing existing symlink for <info>%s</info>', static::PACKAGE_NAME)); |
| 62 | + } |
| 63 | + $filesystem->removeDirectory($destination); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Attempt to symlink to the parent repository during development. |
| 70 | + * |
| 71 | + * @param \Composer\Script\Event $event |
| 72 | + * The composer event. |
| 73 | + */ |
| 74 | + public static function postInstall(Event $event) { |
| 75 | + $root_package = $event->getComposer()->getPackage(); |
| 76 | + if (empty($root_package->getExtra()['symlink-root-project'])) { |
| 77 | + // Do nothing. |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + $io = $event->getIO(); |
| 82 | + $filesystem = new Filesystem(); |
| 83 | + $link = implode(DIRECTORY_SEPARATOR, ['..', '..', '..', '..']); |
| 84 | + $destination = static::getPackageDestination(); |
| 85 | + $destination = $filesystem->normalizePath($destination); |
| 86 | + if (Platform::isWindows()) { |
| 87 | + if (!$filesystem->isJunction($destination)) { |
| 88 | + $io->writeError(sprintf('Creating junction for <info>%s</info>', static::PACKAGE_NAME)); |
| 89 | + $filesystem->removeDirectory($destination); |
| 90 | + $filesystem->junction($link, $destination); |
| 91 | + } |
| 92 | + } |
| 93 | + else { |
| 94 | + if (!$filesystem->isSymlinkedDirectory($destination)) { |
| 95 | + $io->writeError(sprintf('Creating symlink for <info>%s</info>', static::PACKAGE_NAME)); |
| 96 | + // Attempt to remove the existing directory. |
| 97 | + $filesystem->removeDirectory($destination); |
| 98 | + if (!static::createSymlink($link, $destination)) { |
| 99 | + throw new \RuntimeException(sprintf('Failed to create a symlink for "%s"', static::PACKAGE_NAME)); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Returns the destination package directory. |
| 107 | + */ |
| 108 | + protected static function getPackageDestination() { |
| 109 | + return implode(DIRECTORY_SEPARATOR, [ |
| 110 | + getcwd(), |
| 111 | + 'vendor', |
| 112 | + 'zodiacmedia', |
| 113 | + 'drupal-libraries-installer', |
| 114 | + ]); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Creates a symlink to the root project. |
| 119 | + */ |
| 120 | + protected static function createSymlink($target, $link) { |
| 121 | + if (!function_exists('symlink')) { |
| 122 | + return FALSE; |
| 123 | + } |
| 124 | + |
| 125 | + return @symlink($target, $link); |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments