Skip to content

Commit

Permalink
Remove composer option from download module or theme (#4124)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjuarez20 authored and enzolutions committed Aug 2, 2019
1 parent 3f23f60 commit a51b7f4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 237 deletions.
2 changes: 1 addition & 1 deletion config/services/module.yml
Expand Up @@ -6,7 +6,7 @@ services:
- { name: drupal.command } - { name: drupal.command }
console.module_download: console.module_download:
class: Drupal\Console\Command\Module\DownloadCommand 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: tags:
- { name: drupal.command } - { name: drupal.command }
console.module_install: console.module_install:
Expand Down
153 changes: 22 additions & 131 deletions src/Command/Module/DownloadCommand.php
Expand Up @@ -7,32 +7,30 @@


namespace Drupal\Console\Command\Module; namespace Drupal\Console\Command\Module;


use GuzzleHttp\Client;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\Command; use Drupal\Console\Core\Command\Command;
use Drupal\Console\Command\Shared\ProjectDownloadTrait; use Drupal\Console\Command\Shared\ProjectDownloadTrait;
use Drupal\Console\Utils\DrupalApi; use Drupal\Console\Utils\DrupalApi;
use GuzzleHttp\Client;
use Drupal\Console\Extension\Manager; use Drupal\Console\Extension\Manager;
use Drupal\Console\Utils\Validator; use Drupal\Console\Utils\Validator;
use Drupal\Console\Utils\Site; use Drupal\Console\Utils\Site;
use Drupal\Console\Core\Utils\ConfigurationManager;
use Drupal\Console\Core\Utils\ShellProcess;


class DownloadCommand extends Command class DownloadCommand extends Command
{ {
use ProjectDownloadTrait; use ProjectDownloadTrait;


/** /**
* @var DrupalApi * @var DrupalApi
*/ */
protected $drupalApi; protected $drupalApi;


/** /**
* @var Client * @var Client
*/ */
protected $httpClient; protected $httpClient;


/** /**
Expand All @@ -41,29 +39,19 @@ class DownloadCommand extends Command
protected $appRoot; protected $appRoot;


/** /**
* @var Manager * @var Manager
*/ */
protected $extensionManager; protected $extensionManager;


/** /**
* @var Validator * @var Validator
*/ */
protected $validator; protected $validator;


/** /**
* @var ConfigurationManager * @var Site
*/
protected $configurationManager;

/**
* @var ShellProcess
*/
protected $shellProcess;

/**
* @var string
*/ */
protected $root; protected $site;


/** /**
* DownloadCommand constructor. * DownloadCommand constructor.
Expand All @@ -74,30 +62,21 @@ class DownloadCommand extends Command
* @param Manager $extensionManager * @param Manager $extensionManager
* @param Validator $validator * @param Validator $validator
* @param Site $site * @param Site $site
* @param ConfigurationManager $configurationManager
* @param ShellProcess $shellProcess
* @param $root
*/ */
public function __construct( public function __construct(
DrupalApi $drupalApi, DrupalApi $drupalApi,
Client $httpClient, Client $httpClient,
$appRoot, $appRoot,
Manager $extensionManager, Manager $extensionManager,
Validator $validator, Validator $validator,
Site $site, Site $site
ConfigurationManager $configurationManager,
ShellProcess $shellProcess,
$root
) { ) {
$this->drupalApi = $drupalApi; $this->drupalApi = $drupalApi;
$this->httpClient = $httpClient; $this->httpClient = $httpClient;
$this->appRoot = $appRoot; $this->appRoot = $appRoot;
$this->extensionManager = $extensionManager; $this->extensionManager = $extensionManager;
$this->validator = $validator; $this->validator = $validator;
$this->site = $site; $this->site = $site;
$this->configurationManager = $configurationManager;
$this->shellProcess = $shellProcess;
$this->root = $root;
parent::__construct(); parent::__construct();
} }


Expand All @@ -123,18 +102,6 @@ protected function configure()
InputOption::VALUE_NONE, InputOption::VALUE_NONE,
$this->trans('commands.module.download.options.latest') $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']); ->setAliases(['mod']);
} }


Expand All @@ -143,23 +110,20 @@ protected function configure()
*/ */
protected function interact(InputInterface $input, OutputInterface $output) protected function interact(InputInterface $input, OutputInterface $output)
{ {
$composer = $input->getOption('composer');
$module = $input->getArgument('module'); $module = $input->getArgument('module');


if (!$module) { if (!$module) {
$module = $this->modulesQuestion(); $module = $this->modulesQuestion();
$input->setArgument('module', $module); $input->setArgument('module', $module);
} }


if (!$composer) { $path = $input->getOption('path');
$path = $input->getOption('path'); if (!$path) {
if (!$path) { $path = $this->getIo()->ask(
$path = $this->getIo()->ask( $this->trans('commands.module.download.questions.path'),
$this->trans('commands.module.download.questions.path'), 'modules/contrib'
'modules/contrib' );
); $input->setOption('path', $path);
$input->setOption('path', $path);
}
} }
} }


Expand All @@ -171,82 +135,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$modules = $input->getArgument('module'); $modules = $input->getArgument('module');
$latest = $input->getOption('latest'); $latest = $input->getOption('latest');
$path = $input->getOption('path'); $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;
} }
} }
34 changes: 9 additions & 25 deletions src/Command/Theme/DownloadCommand.php
Expand Up @@ -73,12 +73,7 @@ protected function configure()
InputArgument::OPTIONAL, InputArgument::OPTIONAL,
$this->trans('commands.theme.download.arguments.version') $this->trans('commands.theme.download.arguments.version')
) )
->addOption( ->setAliases(['thd']);
'composer',
null,
InputOption::VALUE_NONE,
$this->trans('commands.theme.download.options.composer')
)->setAliases(['thd']);
} }


/** /**
Expand All @@ -88,24 +83,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
{ {
$theme = $input->getArgument('theme'); $theme = $input->getArgument('theme');
$version = $input->getArgument('version'); $version = $input->getArgument('version');
$composer = $input->getOption('composer');

if(!$version) {
if ($composer) { return 1;
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');
} }

$this->downloadProject($theme, $version, 'theme');

return 1;
} }


/** /**
Expand All @@ -115,9 +100,8 @@ protected function interact(InputInterface $input, OutputInterface $output)
{ {
$theme = $input->getArgument('theme'); $theme = $input->getArgument('theme');
$version = $input->getArgument('version'); $version = $input->getArgument('version');
$composer = $input->getOption('composer');


if (!$version && !$composer) { if (!$version) {
$version = $this->releasesQuestion($theme); $version = $this->releasesQuestion($theme);
$input->setArgument('version', $version); $input->setArgument('version', $version);
} }
Expand Down
80 changes: 0 additions & 80 deletions src/Utils/DrupalApi.php
Expand Up @@ -232,86 +232,6 @@ public function downloadFile($url, $destination)
return file_exists($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 * @Todo: Remove when issue https://www.drupal.org/node/2556025 get resolved
* *
Expand Down

0 comments on commit a51b7f4

Please sign in to comment.