Skip to content

Commit

Permalink
Merge branch '11.x' into feature/Command_to_rebuild_all_display_config
Browse files Browse the repository at this point in the history
  • Loading branch information
ndf committed Dec 14, 2021
2 parents 162454d + a4f6869 commit 763bf3c
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 235 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ gh-pages
mkdocs.yml
#The Commands site output directory.
docs/commands
docs/generators
#The Doctum output directories
api
.doctum-cache
Expand Down
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ install:
- ps: appveyor-retry cinst -y curl
- SET PATH=C:\Program Files\curl;%PATH%
#which is only needed by the test suite.
- cinst -y which
- cinst --limit-output -y which
- SET PATH=C:\Program Files\which;%PATH%
- git clone -q https://github.com/acquia/DevDesktopCommon.git #For tar, cksum, ...
- SET PATH=%APPVEYOR_BUILD_FOLDER%/DevDesktopCommon/bintools-win/msys/bin;%PATH%
- SET PATH=C:\Program Files\MySql\MySQL Server 5.7\bin\;%PATH%
- choco search php --exact --all-versions -r
#Install PHP per https://blog.wyrihaximus.net/2016/11/running-php-unit-tests-on-windows-using-appveyor-and-chocolatey/
- ps: appveyor-retry cinst --limit-output --ignore-checksums -y php --version 7.4.25
- ps: appveyor-retry cinst --limit-output --ignore-checksums -y php --version 7.4.26
- cd c:\tools\php74
- copy php.ini-production php.ini

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"guzzlehttp/guzzle": "^6.3 || ^7.0",
"league/container": "^3.4",
"psr/log": "~1.0",
"psy/psysh": "~0.6",
"psy/psysh": "~0.11",
"symfony/event-dispatcher": "^4.0",
"symfony/finder": "^4.0 || ^5",
"symfony/polyfill-php80": "^1.23",
Expand Down Expand Up @@ -111,7 +111,7 @@
],
"api": "php $HOME/bin/doctum.phar --ansi --ignore-parse-errors update doctum-config.php",
"doctum-install": "mkdir -p $HOME/bin && curl --output $HOME/bin/doctum.phar https://doctum.long-term.support/releases/latest/doctum.phar && chmod +x $HOME/bin/doctum.phar",
"mk:docs": "./drush --uri=dev -v mk:docs --destination=commands",
"mk:docs": "./drush --uri=dev -v mk:docs",
"rector": "rector process src/Boot src/Preflight src/Runtime src/SiteAlias src/Symfony src/Config",
"sut": "./drush --uri=dev",
"sut:si": "./drush --uri=dev site:install testing --sites-subdir=dev --db-url=${UNISH_DB_URL:-mysql://root:password@mariadb}/unish_dev -v",
Expand Down
26 changes: 13 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions docs/contribute/unish.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ high quality, our tests are run on every push. See [CircleCi](https://circleci.c
1. Review the configuration settings in [tests/phpunit.xml.dist](https://github.com/drush-ops/drush/blob/11.x/tests/phpunit.xml.dist). If customization is needed, copy phpunit.xml.dist to phpunit.xml and edit away.
1. Run all test suites: `composer test`

## Docker
Drush's own tests may be run within provided Docker containers (see docker-compose.yml):
## DDEV
:octicons-tag-24: 11.0+

- Start containers: `docker-compose up -d`
- Run a test: `docker-compose exec drupal composer functional -- --filter testUserRole`
- To change configuration, copy `.env.example` to `.env`, edit to taste, and run `docker-compose up -d` again.
- See the [.env.example file](https://github.com/drush-ops/drush/blob/11.x/.env.example) for help on enabling Xdebug.
Drush's own tests may be run within provided Docker containers via [DDEV](https://ddev.readthedocs.io/en/stable/)

- Start containers: `ddev start`
- Run a test: `ddev exec drupal composer functional -- --filter testUserRole`
- See the [DDEV's XDebug docs](https://ddev.readthedocs.io/en/stable/users/step-debugging/) for help on enabling Xdebug.

## Advanced usage
- Run only one test suite
Expand All @@ -25,7 +26,7 @@ Drush's own tests may be run within provided Docker containers (see docker-compo
- Skip slow tests (usually those with network usage): `composer functional -- --exclude-group slow`
- XML results: `composer functional -- --log-junit results.xml`
- Ad-hoc testing with the SUT
- `composer sut:si`. See composer.json for details.
- `composer sut:si`. [See composer.json for details](https://github.com/drush-ops/drush/blob/dde2793453ed96cc8b9e5bbc1b66e3d8d26be1f6/composer.json#L117).
- `UNISH_DIRTY=1 composer functional -- --filter testUserRole`
- `./drush @sut.dev status`

Expand Down
3 changes: 2 additions & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ public function find($name)
$command = $this->bootstrapAndFind($name);
// Avoid exception when help is being built by https://github.com/bamarni/symfony-console-autocomplete.
// @todo Find a cleaner solution.
if (Drush::config()->get('runtime.argv')[1] !== 'help') {
$argv = Drush::config()->get('runtime.argv');
if (count($argv) > 1 && $argv[1] !== 'help') {
$this->checkObsolete($command);
}
return $command;
Expand Down
154 changes: 108 additions & 46 deletions src/Commands/core/MkCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,92 @@
namespace Drush\Commands\core;

use Consolidation\AnnotatedCommand\AnnotatedCommand;
use Consolidation\AnnotatedCommand\AnnotationData;
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
use Drush\Boot\AutoloaderAwareInterface;
use Drush\Boot\AutoloaderAwareTrait;
use Drush\Commands\DrushCommands;
use Drush\Commands\generate\ApplicationFactory;
use Drush\Commands\help\HelpCLIFormatter;
use Drush\Commands\help\ListCommands;
use Drush\Drush;
use Drush\SiteAlias\SiteAliasManagerAwareInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;
use Webmozart\PathUtil\Path;

class MkCommands extends DrushCommands implements SiteAliasManagerAwareInterface
class MkCommands extends DrushCommands implements SiteAliasManagerAwareInterface, AutoloaderAwareInterface
{
use SiteAliasManagerAwareTrait;
use AutoloaderAwareTrait;

/**
* Build a Markdown document for each Drush command thats available on a site.
* Build a Markdown document for each Drush command/generator that is available on a site.
*
* This command is an early step when building the www.drush.org static site. Adapt it to build a similar site listing the commands that are available on your site. Also see Drush's [Github Actions workflow](https://github.com/drush-ops/drush/blob/10.x/.github/workflows/main.yml).
*
* @option destination The path, relative to 'docs' dir, where command docs should be written.
*
* @command mk:docs
* @bootstrap max
* @usage drush mk:docs --destination=commands
* Build many .md files in the docs/commands directory.
* @usage drush mk:docs
* Build many .md files in the docs/commands and docs/generators directories.
*/
public function docs($options = ['destination' => self::REQ])
public function docs()
{
$dir_root = Drush::bootstrapManager()->getComposerRoot();
$dir_commands = Path::join($dir_root, 'docs', $options['destination']);
$this->prepare($dir_commands);
$destination = 'commands';
$destination_path = Path::join($dir_root, 'docs', $destination);
$this->prepare($destination_path);

$application = Drush::getApplication();
$all = $application->all();
$namespaced = ListCommands::categorize($all);
[$nav_commands, $pages_commands] = $this->writeContentFilesAndAddToNav($namespaced, $destination, $dir_root, $destination_path);
$this->writeAllMd($pages_commands, $destination_path);

// Write content files
$pages = $pages_all = $nav = [];
foreach ($namespaced as $category => $commands) {
foreach ($commands as $command) {
// Special case a single page
if (empty($pages_all)) {
$pages['all'] = $options['destination'] . '/all.md';
}
$destination = 'generators';
$destination_path = Path::join($dir_root, 'docs', $destination);
$this->prepare($destination_path);
$factory = new ApplicationFactory($this->logger(), $this->getConfig());
$factory->setAutoloader($this->autoloader());
$application_generate = $factory->create();
$all = $this->createAnnotatedCommands($application_generate, Drush::getApplication());
$namespaced = ListCommands::categorize($all);
[$nav_generators, $pages_generators] = $this->writeContentFilesAndAddToNav($namespaced, $destination, $dir_root, $destination_path);
$this->writeAllMd($pages_generators, $destination_path);

if ($command instanceof AnnotatedCommand) {
$command->optionsHook();
}
$body = self::appendPreamble($command, $dir_root);
$body .= self::appendUsages($command);
$body .= self::appendArguments($command);
$body .= self::appendOptions($command);
if ($command instanceof AnnotatedCommand) {
$body .= self::appendTopics($command, $dir_commands);
}
$body .= self::appendAliases($command);
$body .= self::appendPostAmble();
$filename = str_replace(':', '_', $command->getName()) . '.md';
$pages[$command->getName()] = $options['destination'] . "/$filename";
file_put_contents(Path::join($dir_commands, $filename), $body);
$this->writeYml($nav_commands, $nav_generators, $dir_root);
}

public function createAnnotatedCommands(Application $application_generate, Application $application_drush): array
{
$application = new Application('temp');
$definition = $application_drush->get('generate')->getDefinition();
foreach ($application_generate->all() as $command) {
$annotated = new AnnotatedCommand($command->getName());
foreach (['answer', 'destination', 'dry-run'] as $key) {
$options[$key] = $definition->getOption($key);
}
$this->logger()->info('Found {pages} pages in {cat}', ['pages' => count($pages), 'cat' => $category]);
$nav[] = [$category => $pages];
$pages_all = array_merge($pages_all, $pages);
unset($pages);
$annotated->addOptions($options);
$annotated->setDescription($command->getDescription());
$annotated->setHelp($command->getHelp());
$annotated->setAliases($command->getAliases());
$annotated->setTopics(['docs:generators']);
$values = [];
if ($command->getName() == 'entity:bundle-class') {
$values['version'] = '11.x';
}
$annotated->setAnnotationData(new AnnotationData($values));
// Hack, until we have https://github.com/consolidation/annotated-command/pull/247
$method = new \ReflectionMethod($annotated, 'addUsageOrExample');
$method->setAccessible(true);
$method->invoke($annotated, 'drush generate ' . $command->getName(), $command->getDescription());
$commands[$command->getName()] = $annotated;
}

$this->writeYml($nav, $dir_root);
$this->writeAllMd($pages_all, $dir_commands);
unset($commands['list'], $commands['help']);
return $commands;
}

protected static function appendPostAmble(): string
Expand Down Expand Up @@ -159,9 +174,9 @@ protected static function appendArguments(AnnotatedCommand $command): string

protected static function appendUsages(AnnotatedCommand $command): string
{
if ($examples = $command->getExampleUsages()) {
if ($usages = $command->getExampleUsages()) {
$body = "#### Examples\n\n";
foreach ($examples as $key => $value) {
foreach ($usages as $key => $value) {
$body .= '- <code>' . $key . '</code>. ' . self::cliTextToMarkdown($value) . "\n";
}
return "$body\n";
Expand All @@ -172,9 +187,10 @@ protected static function appendUsages(AnnotatedCommand $command): string
protected static function appendPreamble(AnnotatedCommand $command, $root): string
{
$path = Path::makeRelative($command->getAnnotationData()->get('_path'), $root);
$edit_url = $path ? "https://github.com/drush-ops/drush/blob/11.x/$path" : '';
$body = <<<EOT
---
edit_url: https://github.com/drush-ops/drush/blob/10.x/$path
edit_url: $edit_url
command: {$command->getName()}
---
Expand All @@ -192,10 +208,11 @@ protected static function appendPreamble(AnnotatedCommand $command, $root): stri
return $body;
}

protected function writeYml(array $nav, string $dest): void
protected function writeYml(array $nav_commands, $nav_generators, string $dest): void
{
$base = Yaml::parseFile(Path::join($dest, 'mkdocs_base.yml'));
$base['nav'][] = ['Commands' => $nav];
$base['nav'][] = ['Commands' => $nav_commands];
$base['nav'][] = ['Generators' => $nav_generators];
$yaml_nav = Yaml::dump($base, PHP_INT_MAX, 2);

// Remove invalid quotes that Symfony YAML adds/needs. https://github.com/symfony/symfony/blob/6.1/src/Symfony/Component/Yaml/Inline.php#L624
Expand All @@ -205,7 +222,7 @@ protected function writeYml(array $nav, string $dest): void
file_put_contents(Path::join($dest, 'mkdocs.yml'), $yaml_nav);
}

protected function writeAllMd(array $pages_all, string $dest): void
protected function writeAllMd(array $pages_all, string $destination_path): void
{
unset($pages_all['all']);
foreach ($pages_all as $name => $page) {
Expand All @@ -220,7 +237,7 @@ protected function writeAllMd(array $pages_all, string $dest): void
Press the ++slash++ key to Search for a command. Or use your browser's *Find in Page* feature.
EOT;
file_put_contents(Path::join($dest, 'all.md'), $preamble . implode("\n", $items));
file_put_contents(Path::join($destination_path, 'all.md'), $preamble . implode("\n", $items));
}

/**
Expand Down Expand Up @@ -287,4 +304,49 @@ public static function cliTextToMarkdown(string $text): string
{
return str_replace(['<info>', '</info>'], '*', $text);
}

/**
* @param array $namespaced
* @param string $destination
* @param string $dir_root
* @param string $destination_path
*
* @return array
*/
public function writeContentFilesAndAddToNav(array $namespaced, string $destination, string $dir_root, string $destination_path): array
{
// Write content files and add to nav.
$pages = $pages_all = $nav = [];
foreach ($namespaced as $category => $commands) {
foreach ($commands as $command) {
// Special case a single page
if (empty($pages_all)) {
$pages['all'] = $destination . '/all.md';
}

if ($command instanceof AnnotatedCommand) {
$command->optionsHook();
}
$body = self::appendPreamble($command, $dir_root);
$body .= self::appendUsages($command);
$body .= self::appendArguments($command);
$body .= self::appendOptions($command);
if ($command instanceof AnnotatedCommand) {
$body .= self::appendTopics($command, $destination_path);
}
$body .= self::appendAliases($command);
if ($destination == 'commands') {
$body .= self::appendPostAmble();
}
$filename = str_replace(':', '_', $command->getName()) . '.md';
$pages[$command->getName()] = $destination . "/$filename";
file_put_contents(Path::join($destination_path, $filename), $body);
}
$this->logger()->info('Found {pages} pages in {cat}', ['pages' => count($pages), 'cat' => $category]);
$nav[] = [$category => $pages];
$pages_all = array_merge($pages_all, $pages);
unset($pages);
}
return [$nav, $pages_all];
}
}

0 comments on commit 763bf3c

Please sign in to comment.