Skip to content

Commit 9af887a

Browse files
mondrakeenzolutions
authored andcommitted
[module:install] Allow module installation with Drupal 8.8.x and enable Composer-based installation (#4167)
* Update services.yml * Fixes for module:install
1 parent ddb53e5 commit 9af887a

File tree

2 files changed

+94
-50
lines changed

2 files changed

+94
-50
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ before_script:
4545
# PHP 5.5.9 on TravisCI has a Sqlite version that fails
4646
# minimum requirements, so we install on MySql instead.
4747
export SIMPLETEST_DB="mysql://root:@localhost/drupal_travis_db#drupalconsole"
48+
export IMAGEMAGICK_MODULE="drupal/imagemagick:^2"
4849
else
4950
export DRUPAL_BRANCH="8.8.x"
5051
export SIMPLETEST_DB="sqlite://localhost/sites/default/files/.ht.sqlite#drupalconsole"
52+
export IMAGEMAGICK_MODULE="drupal/imagemagick:dev-3.x"
5153
fi
5254
5355
# Get Drupal via git, and install it via Composer.
@@ -66,6 +68,9 @@ before_script:
6668
script:
6769
# Install Drupal site via drupal/console and show site status.
6870
- 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
71+
- drupal module:install --composer token admin_toolbar "$IMAGEMAGICK_MODULE" --verbose --no-interaction
72+
- drupal debug:module
73+
- drupal config:override system.image --key=toolkit --value=imagemagick
6974
- drupal site:status -v
7075
# - cd $DRUPAL_PATH/vendor/drupal/console
7176
# - ../../phpunit/phpunit/phpunit

src/Command/Module/InstallCommand.php

Lines changed: 89 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -148,67 +148,106 @@ protected function execute(InputInterface $input, OutputInterface $output)
148148

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

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

157+
// When --composer is specified, build a command to Composer require
158+
// all the needed modules in one go. This will just download the
159+
// modules from the composer endpoint, not do any 'installation', in
160+
// Drupal terminology.
154161
if ($composer) {
155-
foreach ($module as $moduleItem) {
156-
$command = sprintf(
157-
'composer show drupal/%s ',
158-
$moduleItem
159-
);
160-
161-
$processBuilder = new ProcessBuilder([]);
162-
$processBuilder->setWorkingDirectory($this->appRoot);
163-
$processBuilder->setArguments(explode(' ', $command));
164-
$process = $processBuilder->getProcess();
165-
$process->setTty('true');
166-
$process->run();
167-
168-
if ($process->isSuccessful()) {
169-
$this->getIo()->info(
170-
sprintf(
171-
$this->trans('commands.module.install.messages.download-with-composer'),
172-
$moduleItem
173-
)
174-
);
162+
$composer_package_list = [];
163+
$module_list = [];
164+
foreach ($module as $item) {
165+
// Decompose each module item passed on the command line into
166+
// Composer-ready elements.
167+
$temp = explode('/', $item);
168+
if (count($temp) === 1) {
169+
$package_namespace = 'drupal';
170+
$package = $temp[0];
175171
} else {
176-
$this->getIo()->error(
177-
sprintf(
178-
$this->trans('commands.module.install.messages.not-installed-with-composer'),
179-
$moduleItem
180-
)
181-
);
182-
throw new \RuntimeException($process->getErrorOutput());
172+
$package_namespace = $temp[0];
173+
$package = $temp[1];
174+
}
175+
$temp = explode(':', $package);
176+
if (count($temp) === 1) {
177+
$package_constraint = null;
178+
} else {
179+
$package = $temp[0];
180+
$package_constraint = $temp[1];
183181
}
184-
}
185182

186-
$unInstalledModules = $module;
187-
} else {
188-
$resultList = $this->downloadModules($module, $latest);
189-
190-
$invalidModules = $resultList['invalid'];
191-
$unInstalledModules = $resultList['uninstalled'];
192-
193-
if ($invalidModules) {
194-
foreach ($invalidModules as $invalidModule) {
195-
unset($module[array_search($invalidModule, $module)]);
196-
$this->getIo()->error(
197-
sprintf(
198-
$this->trans('commands.module.install.messages.invalid-name'),
199-
$invalidModule
200-
)
201-
);
183+
// Add the Composer argument.
184+
$temp = "$package_namespace/$package";
185+
if (isset($package_constraint)) {
186+
$temp .= ':' . $package_constraint;
202187
}
203-
}
188+
$composer_package_list[] = $temp;
204189

205-
if (!$unInstalledModules) {
206-
$this->getIo()->warning($this->trans('commands.module.install.messages.nothing'));
190+
// Add the module to the list of those to be Drupal-installed.
191+
if ($package_namespace === 'drupal') {
192+
$module_list[] = $package;
193+
}
194+
}
195+
$module = $module_list;
196+
197+
// Run the Composer require command.
198+
$command = array_merge(['composer', 'require'], $composer_package_list);
199+
$this->getIo()->info('Executing... ' . implode(' ', $command));
200+
$processBuilder = new ProcessBuilder([]);
201+
$processBuilder->setWorkingDirectory($this->appRoot);
202+
$processBuilder->setArguments($command);
203+
$processBuilder->inheritEnvironmentVariables();
204+
$process = $processBuilder->getProcess();
205+
$process->setTty(true);
206+
$process->run();
207+
208+
if ($process->isSuccessful()) {
209+
$this->getIo()->info(
210+
sprintf(
211+
$this->trans('commands.module.install.messages.download-with-composer'),
212+
implode(', ', $composer_package_list)
213+
)
214+
);
215+
} else {
216+
$this->getIo()->error(
217+
sprintf(
218+
$this->trans('commands.module.install.messages.not-installed-with-composer'),
219+
implode(', ', $composer_package_list)
220+
)
221+
);
222+
throw new \RuntimeException($process->getErrorOutput());
223+
}
224+
}
207225

208-
return 0;
226+
// Build the list of modules to be installed, skipping those that are
227+
// installed already.
228+
$resultList = $this->downloadModules($module, $latest);
229+
$invalidModules = $resultList['invalid'];
230+
$unInstalledModules = $resultList['uninstalled'];
231+
232+
if ($invalidModules) {
233+
foreach ($invalidModules as $invalidModule) {
234+
unset($module[array_search($invalidModule, $module)]);
235+
$this->getIo()->error(
236+
sprintf(
237+
$this->trans('commands.module.install.messages.invalid-name'),
238+
$invalidModule
239+
)
240+
);
209241
}
210242
}
211243

244+
// If no modules need to be installed, warn and exit.
245+
if (!$unInstalledModules) {
246+
$this->getIo()->warning($this->trans('commands.module.install.messages.nothing'));
247+
return 0;
248+
}
249+
250+
// Install the needed modules.
212251
try {
213252
$this->getIo()->comment(
214253
sprintf(

0 commit comments

Comments
 (0)