Skip to content

Commit

Permalink
[module:install] Allow module installation with Drupal 8.8.x and enab…
Browse files Browse the repository at this point in the history
…le Composer-based installation (#4167)

* Update services.yml

* Fixes for module:install
  • Loading branch information
mondrake authored and enzolutions committed Oct 10, 2019
1 parent ddb53e5 commit 9af887a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ before_script:
# PHP 5.5.9 on TravisCI has a Sqlite version that fails
# minimum requirements, so we install on MySql instead.
export SIMPLETEST_DB="mysql://root:@localhost/drupal_travis_db#drupalconsole"
export IMAGEMAGICK_MODULE="drupal/imagemagick:^2"
else
export DRUPAL_BRANCH="8.8.x"
export SIMPLETEST_DB="sqlite://localhost/sites/default/files/.ht.sqlite#drupalconsole"
export IMAGEMAGICK_MODULE="drupal/imagemagick:dev-3.x"
fi
# Get Drupal via git, and install it via Composer.
Expand All @@ -66,6 +68,9 @@ before_script:
script:
# Install Drupal site via drupal/console and show site status.
- drupal site:install standard $SIMPLETEST_DB --langcode=en --site-name="Drupal 8 Site Install" [email protected] --account-name=admin [email protected] --account-pass=admin --no-interaction
- drupal module:install --composer token admin_toolbar "$IMAGEMAGICK_MODULE" --verbose --no-interaction
- drupal debug:module
- drupal config:override system.image --key=toolkit --value=imagemagick
- drupal site:status -v
# - cd $DRUPAL_PATH/vendor/drupal/console
# - ../../phpunit/phpunit/phpunit
Expand Down
139 changes: 89 additions & 50 deletions src/Command/Module/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,67 +148,106 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->site->loadLegacyFile('/core/includes/bootstrap.inc');

// check module's requirements
$this->moduleRequirement($module);
// Check module's requirements, only for Drupal below 8.7.7. From there
// on, we just use Drupal's API.
if (version_compare(\Drupal::VERSION, '8.7.7') < 0) {
$this->moduleRequirement($module);
}

// When --composer is specified, build a command to Composer require
// all the needed modules in one go. This will just download the
// modules from the composer endpoint, not do any 'installation', in
// Drupal terminology.
if ($composer) {
foreach ($module as $moduleItem) {
$command = sprintf(
'composer show drupal/%s ',
$moduleItem
);

$processBuilder = new ProcessBuilder([]);
$processBuilder->setWorkingDirectory($this->appRoot);
$processBuilder->setArguments(explode(' ', $command));
$process = $processBuilder->getProcess();
$process->setTty('true');
$process->run();

if ($process->isSuccessful()) {
$this->getIo()->info(
sprintf(
$this->trans('commands.module.install.messages.download-with-composer'),
$moduleItem
)
);
$composer_package_list = [];
$module_list = [];
foreach ($module as $item) {
// Decompose each module item passed on the command line into
// Composer-ready elements.
$temp = explode('/', $item);
if (count($temp) === 1) {
$package_namespace = 'drupal';
$package = $temp[0];
} else {
$this->getIo()->error(
sprintf(
$this->trans('commands.module.install.messages.not-installed-with-composer'),
$moduleItem
)
);
throw new \RuntimeException($process->getErrorOutput());
$package_namespace = $temp[0];
$package = $temp[1];
}
$temp = explode(':', $package);
if (count($temp) === 1) {
$package_constraint = null;
} else {
$package = $temp[0];
$package_constraint = $temp[1];
}
}

$unInstalledModules = $module;
} else {
$resultList = $this->downloadModules($module, $latest);

$invalidModules = $resultList['invalid'];
$unInstalledModules = $resultList['uninstalled'];

if ($invalidModules) {
foreach ($invalidModules as $invalidModule) {
unset($module[array_search($invalidModule, $module)]);
$this->getIo()->error(
sprintf(
$this->trans('commands.module.install.messages.invalid-name'),
$invalidModule
)
);
// Add the Composer argument.
$temp = "$package_namespace/$package";
if (isset($package_constraint)) {
$temp .= ':' . $package_constraint;
}
}
$composer_package_list[] = $temp;

if (!$unInstalledModules) {
$this->getIo()->warning($this->trans('commands.module.install.messages.nothing'));
// Add the module to the list of those to be Drupal-installed.
if ($package_namespace === 'drupal') {
$module_list[] = $package;
}
}
$module = $module_list;

// Run the Composer require command.
$command = array_merge(['composer', 'require'], $composer_package_list);
$this->getIo()->info('Executing... ' . implode(' ', $command));
$processBuilder = new ProcessBuilder([]);
$processBuilder->setWorkingDirectory($this->appRoot);
$processBuilder->setArguments($command);
$processBuilder->inheritEnvironmentVariables();
$process = $processBuilder->getProcess();
$process->setTty(true);
$process->run();

if ($process->isSuccessful()) {
$this->getIo()->info(
sprintf(
$this->trans('commands.module.install.messages.download-with-composer'),
implode(', ', $composer_package_list)
)
);
} else {
$this->getIo()->error(
sprintf(
$this->trans('commands.module.install.messages.not-installed-with-composer'),
implode(', ', $composer_package_list)
)
);
throw new \RuntimeException($process->getErrorOutput());
}
}

return 0;
// Build the list of modules to be installed, skipping those that are
// installed already.
$resultList = $this->downloadModules($module, $latest);
$invalidModules = $resultList['invalid'];
$unInstalledModules = $resultList['uninstalled'];

if ($invalidModules) {
foreach ($invalidModules as $invalidModule) {
unset($module[array_search($invalidModule, $module)]);
$this->getIo()->error(
sprintf(
$this->trans('commands.module.install.messages.invalid-name'),
$invalidModule
)
);
}
}

// If no modules need to be installed, warn and exit.
if (!$unInstalledModules) {
$this->getIo()->warning($this->trans('commands.module.install.messages.nothing'));
return 0;
}

// Install the needed modules.
try {
$this->getIo()->comment(
sprintf(
Expand Down

0 comments on commit 9af887a

Please sign in to comment.