Skip to content

Commit

Permalink
Merge pull request #547 from ec-europa/feature/DQA-4585
Browse files Browse the repository at this point in the history
DQA-4585: Add command to install dependencies.
  • Loading branch information
jonhy81 authored Aug 24, 2022
2 parents 59676df + fca2b41 commit 9997ce8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/TaskRunner/Commands/BuildCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public function buildDev(array $options = [
$runner_bin = $this->getBin('run');
$tasks[] = $this->taskExecStack()
->stopOnFail()
->exec("$runner_bin toolkit:install-dependencies")
->exec("$runner_bin drupal:settings-setup --root=$root");

// Double check presence of required folders.
Expand Down
7 changes: 7 additions & 0 deletions src/TaskRunner/Commands/TestsCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,13 @@ public function toolkitBehat(array $options = [
])
{
$tasks = [];

if (Toolkit::isCiCd()) {
$this->taskExecStack()
->exec($this->getBin('run') . ' toolkit:install-dependencies')
->run();
}

$behatBin = $this->getBin('behat');
$defaultProfile = $this->getConfig()->get('toolkit.test.behat.profile');

Expand Down
81 changes: 81 additions & 0 deletions src/TaskRunner/Commands/ToolCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OpenEuropa\TaskRunner\Tasks\ProcessConfigFile\loadTasks;
use Robo\Contract\VerbosityThresholdInterface;
use Robo\ResultData;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputOption;
use OpenEuropa\TaskRunner\Commands\AbstractCommands;
use Symfony\Component\Yaml\Yaml;
Expand Down Expand Up @@ -1510,4 +1511,84 @@ public function getQaProjectInformation($project_id)

return false;
}

/**
* Install packages present in the opts.yml file under extra_pkgs section.
*
* @command toolkit:install-dependencies
*
* @option print Shows output from apt commands.
*/
public function toolkitInstallDependencies(array $options = [
'print' => InputOption::VALUE_NONE,
])
{
$this->io()->title('Installing dependencies');
$return = 0;
if (!file_exists('.opts.yml')) {
return $return;
}
$opts = Yaml::parseFile('.opts.yml');
$packages = $opts['extra_pkgs'] ?? [];
if (empty($packages)) {
$this->output()->writeln('No packages found, skipping.');
return $return;
}

$print = $options['print'] !== InputOption::VALUE_NONE;
$verbose = $print ? VerbosityThresholdInterface::VERBOSITY_NORMAL : VerbosityThresholdInterface::VERBOSITY_DEBUG;
$data = $install = [];

// The command apt list needs the apt update to run.
$this->taskExec('apt-get update')
->setVerbosityThreshold($verbose)->run();

foreach ($packages as $package) {
$info = $this->taskExec("apt list $package")
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_DEBUG)
->run()->getMessage();
// The package is installed if output contains '[installed]'. If
// the name is not in the output the package was not found.
if (strpos($info, '[installed]') !== false) {
$data[$package] = 'already installed';
} elseif (strpos($info, $package) === false) {
$data[$package] = 'not found, skip install';
} else {
$install[] = $package;
}
if ($print) {
$this->output()->writeln(["Running apt list $package", $info]);
}
}

if (!empty($install)) {
// Install the missing packages.
foreach ($install as $package) {
$this->taskExec("apt-get install -y --no-install-recommends $package")
->setVerbosityThreshold($verbose)->run();

// Check if the package was installed.
$info = $this->taskExec("apt list $package")
->setVerbosityThreshold(VerbosityThresholdInterface::VERBOSITY_DEBUG)
->run()->getMessage();
if (strpos($info, '[installed]') !== false) {
$data[$package] = 'installed';
} else {
$data[$package] = 'fail';
$return = 1;
}
if ($print) {
$this->output()->writeln(["Running apt list $package", $info]);
}
}
}

$table = new Table($this->io());
$table->setHeaders(['Package', 'Status']);
foreach ($data as $package => $status) {
$table->addRow([$package, $status]);
}
$table->render();
return $return;
}
}

0 comments on commit 9997ce8

Please sign in to comment.