Skip to content
Permalink
Browse files

Remove composer option from download module or theme (#4124)

  • Loading branch information...
harold20 authored and enzolutions committed Aug 2, 2019
1 parent 3f23f60 commit a51b7f411950df8b7ac9cfe106f81a8bab1f6066
Showing with 32 additions and 237 deletions.
  1. +1 −1 config/services/module.yml
  2. +22 −131 src/Command/Module/DownloadCommand.php
  3. +9 −25 src/Command/Theme/DownloadCommand.php
  4. +0 −80 src/Utils/DrupalApi.php
@@ -6,7 +6,7 @@ services:
- { name: drupal.command }
console.module_download:
class: Drupal\Console\Command\Module\DownloadCommand
arguments: ['@console.drupal_api', '@http_client', '@app.root', '@console.extension_manager', '@console.validator', '@console.site', '@console.configuration_manager', '@console.shell_process', '@console.root']
arguments: ['@console.drupal_api', '@http_client', '@app.root', '@console.extension_manager', '@console.validator', '@console.site']
tags:
- { name: drupal.command }
console.module_install:
@@ -7,32 +7,30 @@
namespace Drupal\Console\Command\Module;
use GuzzleHttp\Client;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Command\Shared\ProjectDownloadTrait;
use Drupal\Console\Utils\DrupalApi;
use GuzzleHttp\Client;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Utils\Validator;
use Drupal\Console\Utils\Site;
use Drupal\Console\Core\Utils\ConfigurationManager;
use Drupal\Console\Core\Utils\ShellProcess;
class DownloadCommand extends Command
{
use ProjectDownloadTrait;
/**
* @var DrupalApi
*/
* @var DrupalApi
*/
protected $drupalApi;
/**
* @var Client
*/
* @var Client
*/
protected $httpClient;
/**
@@ -41,29 +39,19 @@ class DownloadCommand extends Command
protected $appRoot;
/**
* @var Manager
*/
* @var Manager
*/
protected $extensionManager;
/**
* @var Validator
*/
* @var Validator
*/
protected $validator;
/**
* @var ConfigurationManager
*/
protected $configurationManager;
/**
* @var ShellProcess
*/
protected $shellProcess;
/**
* @var string
* @var Site
*/
protected $root;
protected $site;
/**
* DownloadCommand constructor.
@@ -74,30 +62,21 @@ class DownloadCommand extends Command
* @param Manager $extensionManager
* @param Validator $validator
* @param Site $site
* @param ConfigurationManager $configurationManager
* @param ShellProcess $shellProcess
* @param $root
*/
public function __construct(
DrupalApi $drupalApi,
Client $httpClient,
$appRoot,
Manager $extensionManager,
Validator $validator,
Site $site,
ConfigurationManager $configurationManager,
ShellProcess $shellProcess,
$root
Site $site
) {
$this->drupalApi = $drupalApi;
$this->httpClient = $httpClient;
$this->appRoot = $appRoot;
$this->extensionManager = $extensionManager;
$this->validator = $validator;
$this->site = $site;
$this->configurationManager = $configurationManager;
$this->shellProcess = $shellProcess;
$this->root = $root;
parent::__construct();
}
@@ -123,18 +102,6 @@ protected function configure()
InputOption::VALUE_NONE,
$this->trans('commands.module.download.options.latest')
)
->addOption(
'composer',
null,
InputOption::VALUE_NONE,
$this->trans('commands.module.install.options.composer')
)
->addOption(
'unstable',
null,
InputOption::VALUE_NONE,
$this->trans('commands.module.download.options.unstable')
)
->setAliases(['mod']);
}
@@ -143,23 +110,20 @@ protected function configure()
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$composer = $input->getOption('composer');
$module = $input->getArgument('module');
if (!$module) {
$module = $this->modulesQuestion();
$input->setArgument('module', $module);
}
if (!$composer) {
$path = $input->getOption('path');
if (!$path) {
$path = $this->getIo()->ask(
$this->trans('commands.module.download.questions.path'),
'modules/contrib'
);
$input->setOption('path', $path);
}
$path = $input->getOption('path');
if (!$path) {
$path = $this->getIo()->ask(
$this->trans('commands.module.download.questions.path'),
'modules/contrib'
);
$input->setOption('path', $path);
}
}
@@ -171,82 +135,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$modules = $input->getArgument('module');
$latest = $input->getOption('latest');
$path = $input->getOption('path');
$composer = $input->getOption('composer');
$unstable = true;
if ($composer) {
foreach ($modules as $module) {
if (!$latest) {
$versions = $this->drupalApi
->getPackagistModuleReleases($module, 10, $unstable);
if (!$versions) {
$this->getIo()->error(
sprintf(
$this->trans(
'commands.module.download.messages.no-releases'
),
$module
)
);
return 1;
} else {
$version = $this->getIo()->choice(
sprintf(
$this->trans(
'commands.site.new.questions.composer-release'
),
$module
),
$versions
);
}
} else {
$versions = $this->drupalApi
->getPackagistModuleReleases($module, 10, $unstable);
if (!$versions) {
$this->getIo()->error(
sprintf(
$this->trans(
'commands.module.download.messages.no-releases'
),
$module
)
);
return 1;
} else {
$version = current(
$this->drupalApi
->getPackagistModuleReleases($module, 1, $unstable)
);
}
}
// Register composer repository
$command = 'composer config repositories.drupal composer https://packages.drupal.org/8';
$this->shellProcess->exec($command, $this->root);
$command = sprintf(
'composer require drupal/%s:%s --prefer-dist --optimize-autoloader --sort-packages --update-no-dev',
$module,
$version
);
if ($this->shellProcess->exec($command, $this->root)) {
$this->getIo()->success(
sprintf(
$this->trans('commands.module.download.messages.composer'),
$module
)
);
}
}
} else {
$this->downloadModules($modules, $latest, $path);
}
return true;
$this->downloadModules($modules, $latest, $path);
return 1;
}
}
@@ -73,12 +73,7 @@ protected function configure()
InputArgument::OPTIONAL,
$this->trans('commands.theme.download.arguments.version')
)
->addOption(
'composer',
null,
InputOption::VALUE_NONE,
$this->trans('commands.theme.download.options.composer')
)->setAliases(['thd']);
->setAliases(['thd']);
}
/**
@@ -88,24 +83,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$theme = $input->getArgument('theme');
$version = $input->getArgument('version');
$composer = $input->getOption('composer');
if ($composer) {
if (!is_array($theme)) {
$theme = [$theme];
}
$this->get('chain_queue')->addCommand(
'module:download',
[
'module' => $theme,
'--composer' => true
],
true,
true
);
} else {
$this->downloadProject($theme, $version, 'theme');
if(!$version) {
return 1;
}
$this->downloadProject($theme, $version, 'theme');
return 1;
}
/**
@@ -115,9 +100,8 @@ protected function interact(InputInterface $input, OutputInterface $output)
{
$theme = $input->getArgument('theme');
$version = $input->getArgument('version');
$composer = $input->getOption('composer');
if (!$version && !$composer) {
if (!$version) {
$version = $this->releasesQuestion($theme);
$input->setArgument('version', $version);
}
@@ -232,86 +232,6 @@ public function downloadFile($url, $destination)
return file_exists($destination);
}
/**
* Gets Drupal modules releases from Packagist API.
*
* @param string $module
* @param int $limit
* @param bool $unstable
*
* @return array
*/
public function getPackagistModuleReleases($module, $limit = 10, $unstable = true)
{
if (!trim($module)) {
return [];
}
return $this->getComposerReleases(
sprintf(
'http://packagist.drupal-composer.org/packages/drupal/%s.json',
trim($module)
),
$limit,
$unstable
);
}
/**
* Gets Drupal releases from Packagist API.
*
* @param string $url
* @param int $limit
* @param bool $unstable
*
* @return array
*/
private function getComposerReleases($url, $limit = 10, $unstable = false)
{
if (!$url) {
return [];
}
$packagistResponse = $this->httpClient->getUrlAsString($url);
if ($packagistResponse->getStatusCode() != 200) {
throw new \Exception('Invalid path.');
}
try {
$packagistJson = json_decode(
$packagistResponse->getBody()->getContents()
);
} catch (\Exception $e) {
return [];
}
$versions = array_keys((array)$packagistJson->package->versions);
// Remove Drupal 7 versions
$i = 0;
foreach ($versions as $version) {
if (0 === strpos($version, "7.") || 0 === strpos($version, "dev-7.")) {
unset($versions[$i]);
}
$i++;
}
if (!$unstable) {
foreach ($versions as $key => $version) {
if (strpos($version, "-")) {
unset($versions[$key]);
}
}
}
if (is_array($versions)) {
return array_slice($versions, 0, $limit);
}
return [];
}
/**
* @Todo: Remove when issue https://www.drupal.org/node/2556025 get resolved
*

0 comments on commit a51b7f4

Please sign in to comment.
You can’t perform that action at this time.