From 3ddd3133a61a211bbfa1ce5ee8376e05106813d7 Mon Sep 17 00:00:00 2001 From: Andrii Vasyliev Date: Fri, 19 May 2017 15:22:49 +0200 Subject: [PATCH] added showing packages hierarchy tree with `showDepsTree()` --- src/Plugin.php | 63 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index fca69af..fe98e5a 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -20,6 +20,7 @@ use Composer\Script\Event; use Composer\Script\ScriptEvents; use Composer\Util\Filesystem; +use Symfony\Component\Console\Formatter\OutputFormatterStyle; /** * Plugin class. @@ -60,6 +61,11 @@ class Plugin implements PluginInterface, EventSubscriberInterface 'params' => [], ]; + /** + * @var array package name => configs as listed in `composer.json` + */ + protected $originalFiles = []; + protected $aliases = []; protected $extensions = []; @@ -112,6 +118,7 @@ public function onPostAutoloadDump(Event $event) $this->io->writeError('Assembling config files'); $this->initAutoload(); $this->scanPackages(); + $this->showDepsTree(); $builder = new Builder($this->files); $builder->setAddition(['aliases' => $this->aliases]); @@ -144,6 +151,7 @@ protected function processPackage(CompletePackageInterface $package) { $extra = $package->getExtra(); $files = isset($extra[self::EXTRA_OPTION_NAME]) ? $extra[self::EXTRA_OPTION_NAME] : null; + $this->originalFiles[$package->getPrettyName()] = $files; if ($package->getType() !== self::YII2_PACKAGE_TYPE && is_null($files)) { return; @@ -311,7 +319,8 @@ public function getPackages() protected $plainList = []; /** - * Ordered list of package. Order @see findPackages. + * Ordered list of package in form: package => depth + * For order description @see findPackages. */ protected $orderedList = []; @@ -331,13 +340,8 @@ public function findPackages() $this->orderedList = []; $this->iteratePackage($root, true); - if ($this->io->isVerbose()) { - $indent = ' - '; - $packages = $indent . implode("\n$indent", $this->orderedList); - $this->io->writeError($packages); - } $res = []; - foreach ($this->orderedList as $name) { + foreach (array_keys($this->orderedList) as $name) { $res[] = $this->plainList[$name]; } @@ -349,7 +353,7 @@ public function findPackages() * @param PackageInterface $package to iterate * @param bool $includingDev process development dependencies, defaults to not process */ - public function iteratePackage(PackageInterface $package, $includingDev = false) + protected function iteratePackage(PackageInterface $package, $includingDev = false) { $name = $package->getPrettyName(); @@ -361,13 +365,19 @@ public function iteratePackage(PackageInterface $package, $includingDev = false) $processed[$name] = 1; } + /// package depth in dependency hierarchy + static $depth = 0; + $depth++; + $this->iterateDependencies($package); if ($includingDev) { $this->iterateDependencies($package, true); } if (!isset($this->orderedList[$name])) { - $this->orderedList[$name] = $name; + $this->orderedList[$name] = $depth; } + + $depth--; } /** @@ -375,7 +385,7 @@ public function iteratePackage(PackageInterface $package, $includingDev = false) * @param PackageInterface $package * @param bool $dev which dependencies to iterate: true - dev, default - general */ - public function iterateDependencies(PackageInterface $package, $dev = false) + protected function iterateDependencies(PackageInterface $package, $dev = false) { $path = $this->preparePath($package, 'composer.json'); if (file_exists($path)) { @@ -386,12 +396,43 @@ public function iterateDependencies(PackageInterface $package, $dev = false) $deps = $dev ? $package->getDevRequires() : $package->getRequires(); } foreach (array_keys($deps) as $target) { - if (isset($this->plainList[$target]) && !isset($this->orderedList[$target])) { + if (isset($this->plainList[$target]) && empty($this->orderedList[$target])) { $this->iteratePackage($this->plainList[$target]); } } } + protected function showDepsTree() + { + if (!$this->io->isVerbose()) { + return; + } + + $this->initStyles(); + foreach (array_reverse($this->orderedList) as $name => $depth) { + $deps = $this->originalFiles[$name]; + $color = $this->colors[$depth]; + $indent = str_repeat(' ', $depth - 1); + $package = $this->plainList[$name]; + $showdeps = $deps ? '[' . implode(',', array_keys($deps)) . ']' : ''; + $this->io->write(sprintf('%s - <%s>%s %s %s', $indent, $color, $name, $color, $package->getFullPrettyVersion(), $showdeps)); + } + } + + protected $colors = ['red', 'green', 'yellow', 'cyan', 'magenta', 'blue']; + + protected function initStyles() + { + $ref = new \ReflectionProperty(get_class($this->io), 'output'); + $ref->setAccessible(true); + $output = $ref->getValue($this->io); + + foreach ($this->colors as $color) { + $style = new OutputFormatterStyle($color); + $output->getFormatter()->setStyle($color, $style); + } + } + /** * Get absolute path to package base dir. * @return string