function UnpackCommand::execute

File

composer/Plugin/RecipeUnpack/UnpackCommand.php, line 47

Class

UnpackCommand
The "drupal:recipe-unpack" command class.

Namespace

Drupal\Composer\Plugin\RecipeUnpack

Code

protected function execute(InputInterface $input, OutputInterface $output) : int {
  $composer = $this->requireComposer();
  $io = $this->getIO();
  $local_repo = $composer->getRepositoryManager()
    ->getLocalRepository();
  $package_names = $input->getArgument('recipes') ?? [];
  // If no recipes are provided unpack all recipes that are required by the
  // root package.
  if (empty($package_names)) {
    foreach ($composer->getPackage()
      ->getRequires() as $link) {
      $package = $local_repo->findPackage($link->getTarget(), $link->getConstraint());
      if ($package->getType() === Plugin::RECIPE_PACKAGE_TYPE) {
        $package_names[] = $package->getName();
      }
    }
    if (empty($package_names)) {
      $io->write('<info>No recipes to unpack.</info>');
      return 0;
    }
  }
  $manager = new UnpackManager($composer, $io);
  $unpack_collection = new UnpackCollection();
  foreach ($package_names as $package_name) {
    if (!$manager->isRootDependency($package_name)) {
      $io->error(sprintf('<info>%s</info> not found in the root composer.json.', $package_name));
      return 1;
    }
    $packages = $local_repo->findPackages($package_name);
    $package = reset($packages);
    if (!$package instanceof Package) {
      $io->error(sprintf('<info>%s</info> does not resolve to a package.', $package_name));
      return 1;
    }
    if ($package->getType() !== Plugin::RECIPE_PACKAGE_TYPE) {
      $io->error(sprintf('<info>%s</info> is not a recipe.', $package->getPrettyName()));
      return 1;
    }
    if ($manager->unpackOptions
      ->isIgnored($package)) {
      $io->error(sprintf('<info>%s</info> is in the extra.drupal-recipe-unpack.ignore list.', $package->getName()));
      return 1;
    }
    if (UnpackManager::isDevRequirement($package)) {
      $io->warning(sprintf('<info>%s</info> is present in the require-dev key. Unpacking will move the recipe\'s dependencies to the require key.', $package->getName()));
      if ($io->isInteractive() && !$io->askConfirmation('<info>Do you want to continue</info> [<comment>yes</comment>]?')) {
        return 0;
      }
    }
    $unpack_collection->add($package);
  }
  $manager->unpack($unpack_collection);
  return 0;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.