Skip to content

Commit

Permalink
Add a new mk:docs command to auto-build an official Drush Commands we…
Browse files Browse the repository at this point in the history
…b site (#4472)
  • Loading branch information
weitzman committed Jul 24, 2020
1 parent e4c9087 commit 643e8a6
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 33 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/commands.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Build Commands Site

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches: [master]
# pull_request:
# branches: [master]

jobs:
build:
name: Build and Deploy
runs-on: ubuntu-latest
steps:
- name: Checkout master
uses: actions/checkout@v2

- name: Checkout gh-pages branch
uses: actions/checkout@v2
with:
ref: gh-pages
path: gh-pages

- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: '3.8'

- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install mkdocs
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.3
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite
coverage: none

- name: Install Composer dependencies
run: composer install --prefer-dist --no-interaction --no-suggest

- name: Install Drupal
run: composer sut:si
env:
UNISH_DB_URL: sqlite://sites/default/files/.ht.sqlite

- name: Drush status
run: ./drush --uri=dev st

- name: Drush mk:docs
run: composer mk:docs

- name: Mkdocs build
run: cd build-commands && mkdocs build -v --site-dir ../gh-pages/commands/10.x

- name: Commit and Push to gh-pages
run: |
cd gh-pages
git config --local user.email "actions@github.com"
git config --local user.name "GitHub Action"
git add -A .
if git diff-index --quiet HEAD --; then
echo "No changes..."
else
git commit -m "[CI] build commands static site"
git push
fi
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ box.phar
drush.phar
#The mkdocs output directory.
site
#The Commands site output directory.
build-commands
#The sami output directories
api
.sami-cache
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
Drush is a command line shell and Unix scripting interface for Drupal. Drush core ships with lots of useful commands for interacting with code like modules/themes/profiles. Similarly, it runs update.php, executes SQL queries and DB migrations, and misc utilities like run cron or clear cache. Developers love the `generate` command, which jump starts your coding project by writing ready-to-customize PHP and YML files. Drush can be extended by [3rd party commandfiles](https://www.drupal.org/project/project_module?f[2]=im_vid_3%3A4654).
Drush is a command line shell and Unix scripting interface for Drupal. Drush core ships with lots of [useful commands](https://www.drush.org/commands/10.x/) for interacting with code like modules/themes/profiles. Similarly, it runs update.php, executes SQL queries and DB migrations, and misc utilities like run cron or clear cache. Developers love the `generate` command, which jump starts your coding project by writing ready-to-customize PHP and YML files. Drush can be extended by [3rd party commandfiles](https://www.drupal.org/project/project_module?f[2]=im_vid_3%3A4654).

[![Latest Stable Version](https://poser.pugx.org/drush/drush/v/stable.png)](https://packagist.org/packages/drush/drush) [![Total Downloads](https://poser.pugx.org/drush/drush/downloads.png)](https://packagist.org/packages/drush/drush) [![License](https://poser.pugx.org/drush/drush/license.png)](https://packagist.org/packages/drush/drush) <a href="https://circleci.com/gh/drush-ops/drush"><img src="https://circleci.com/gh/drush-ops/drush.svg?style=shield"></a> [![Documentation Status](https://readthedocs.org/projects/drush/badge/?version=master)](https://readthedocs.org/projects/drush/?badge=master) [![Twitter](https://img.shields.io/badge/Twitter-%40DrushCli-blue.svg)](https://twitter.com/intent/user?screen_name=DrushCli)

Resources
-----------
* [Installing (and Upgrading)](http://docs.drush.org/en/master/install/)
* [General Documentation](http://docs.drush.org)
* [Installing (and Upgrading)](http://docs.drush.org/en/master/install/) ([Drush 8](https://docs.drush.org/en/8.x/install/))
* [General Documentation](http://docs.drush.org) ([Drush 8](https://docs.drush.org/en/8.x/install/))
* [Commands](https://www.drush.org/commands/10.x/)
* [API Documentation](http://www.drush.org/api/master/index.html)
* [Drush packages available via Composer](https://packagist.org/search/?type=drupal-drush)
* [A list of modules that include Drush integration](https://www.drupal.org/project/project_module?f[2]=im_vid_3%3A4654&solrsort=ds_project_latest_release+desc)
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@
],
"api": "PATH=$HOME/bin:$PATH sami.phar --ansi update sami-config.php",
"sami-install": "mkdir -p $HOME/bin && curl --output $HOME/bin/sami.phar http://get.sensiolabs.org/sami.phar && chmod +x $HOME/bin/sami.phar",
"mk:docs": "./drush --uri=dev -v mk:docs --destination=../build-commands",
"sut": "./drush --uri=dev",
"sut:si": "./drush site:install testing --uri=dev --sites-subdir=dev --db-url=${UNISH_DB_URL:-mysql://root:password@mariadb}/unish_dev -v",
"sut:si": "./drush --uri=dev site:install testing --sites-subdir=dev --db-url=${UNISH_DB_URL:-mysql://root:password@mariadb}/unish_dev -v",
"phpunit": "php -d sendmail_path='true' vendor/bin/phpunit --colors=always --configuration tests",
"unit": "composer phpunit -- --testsuite unit",
"integration": "composer phpunit -- --testsuite integration",
Expand Down
27 changes: 0 additions & 27 deletions docs/index.md

This file was deleted.

1 change: 1 addition & 0 deletions docs/index.md
Binary file added misc/favicon.ico
Binary file not shown.
177 changes: 177 additions & 0 deletions src/Commands/core/MkCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

namespace Drush\Commands\core;

use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
use Drush\Commands\DrushCommands;
use Drush\Commands\help\HelpCLIFormatter;
use Drush\Commands\help\ListCommands;
use Drush\Drush;
use Drush\SiteAlias\SiteAliasManagerAwareInterface;
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
{

use SiteAliasManagerAwareTrait;

/**
* Build an mkdocs site.
*
* @option destination The target directory where files should be written. A relative path starts at Drupal root.
*
* @command mk:docs
* @bootstrap max
* @hidden
* @usage drush mk:docs --destination=/tmp/build
* Build an mkdocs site at /tmp/build directory.
*/
public function docs($options = ['destination' => self::REQ])
{
$this->prepare($options['destination']);

$application = Drush::getApplication();
$all = $application->all();
$namespaced = ListCommands::categorize($all);

// Write content files
$pages = $nav = [];
foreach ($namespaced as $category => $commands) {
foreach ($commands as $command) {
$name = $command->getName();
$filename = str_replace(':', '_', $name) . '.md';
$pages[] = $filename;
$body = "# $name\n\n";
if ($command->getDescription()) {
$body .= $command->getDescription() ."\n\n";
if ($command->getHelp()) {
$body .= $command->getHelp(). "\n\n";
}
}
if ($examples = $command->getExampleUsages()) {
$body .= "#### Examples\n\n";
foreach ($examples as $key => $value) {
$body .= '- <code>' . $key . '</code>. ' . $value . "\n";
}
$body .= "\n";
}
if ($args = $command->getDefinition()->getArguments()) {
$body .= "#### Arguments\n\n";
foreach ($args as $arg) {
$body .= '- **' . $arg->getName() . '**. ' . $arg->getDescription() . "\n";
}
$body .= "\n";
}
if ($opts = $command->getDefinition()->getOptions()) {
$body .= "#### Options\n\n";
$body .= "!!! note \"Tip\"\n\n An option value without square brackets is mandatory. Any default value is listed at description end.\n\n";
foreach ($opts as $opt) {
// @todo more rich key and default value
$opt_array = self::optionToArray($opt);
$body .= '- **' . HelpCLIFormatter::formatOptionKeys($opt_array) . '**. ' . HelpCLIFormatter::formatOptionDescription($opt_array) . "\n";
}
$body .= "\n";
}
if ($topics = $command->getTopics()) {
$body .= "#### Topics\n\n";
foreach ($topics as $value) {
$body .= "- `drush $value`\n";
}
$body .= "\n";
}
if ($aliases = $command->getAliases()) {
$body .= "#### Aliases\n\n";
foreach ($aliases as $value) {
$body .= '- ' . $value . "\n";
}
$body .= "\n";
}
file_put_contents(Path::join($options['destination'], 'docs', $filename), $body);
}
$this->logger()->info('Found {pages} pages in {cat}', ['pages' => count($pages), 'cat' => $category]);
$nav[] = [$category => $pages];
unset($pages);
}

$this->writeyml($nav, $options['destination']);
}

/**
* Write mkdocs.yml.
*
* @param $nav
* @param $dest
*/
protected function writeyml($nav, $dest)
{
// Write yml file.
$mkdocs = [
'site_name' => 'Drush Commands',
'site_author' => 'Moshe Weitzman',
'repo_name' => 'GitHub',
'repo_url' => 'https://github.com/drush-ops/drush',
'edit_uri' => '',
'theme' => [
'name' => 'readthedocs',
],
'site_url' => 'http://commands.drush.org',
'markdown_extensions' => [
['toc' => [
'toc_depth' => 0,
'permalink' => true,
]],
['admonition' => []],
],
'nav' => $nav,
];
$yaml = Yaml::dump($mkdocs, PHP_INT_MAX, 2);
file_put_contents(Path::join($dest, 'mkdocs.yml'), $yaml);
}

/**
* Empty target directories.
*
* @param $destination
*/
protected function prepare($destination)
{
$fs = new Filesystem();
$dest = $destination;
if ($fs->exists($dest)) {
drush_delete_dir_contents($dest);
}
$fs->mkdir($dest);
$docs_dir = Path::join($dest, 'docs');
$fs->mkdir($docs_dir);
$favicon_dir = Path::join($dest, 'docs', 'img');
$fs->mkdir($favicon_dir);
$fs->copy('../misc/favicon.ico', Path::join($favicon_dir, 'favicon.ico'));
$fs->copy('../docs/index.md', Path::join($docs_dir, 'index.md'));
// $fs->copy('../drush_logo-black.png', Path::join($docs_dir, 'logo.png'));
}

/**
* Build an array since thats what HelpCLIFormatter expects.
*
* @param \Symfony\Component\Console\Input\InputOption $opt
*
* @return array
*/
public static function optionToArray(InputOption $opt)
{
$return = [
'name' => '--' . $opt->getName(),
'accept_value' => $opt->acceptValue(),
'is_value_required' => $opt->isValueRequired(),
'shortcut' => $opt->getShortcut(),
'description' => $opt->getDescription(),
];
if ($opt->getDefault()) {
$return['defaults'] = (array)$opt->getDefault();
}
return $return;
}
}
2 changes: 1 addition & 1 deletion src/Commands/help/ListCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function renderListRaw($namespaced)
* @param Command[] $all
* @param string $separator
*
* @return array
* @return Command[]
*/
public static function categorize($all, $separator = ':')
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/pm/SecurityUpdateCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected function calculateSecurityUpdates($composer_lock_data, $security_advis
* @return UnstructuredData
* @throws \Exception
* @command pm:security-php
* @aliases sec-php,pm:security-php
* @aliases sec-php,pm-security-php
* @bootstrap none
*
* @usage drush pm:security-php --format=json
Expand Down
3 changes: 3 additions & 0 deletions sut/drush/Commands/FixtureCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class FixtureCommands extends DrushCommands
*
* @command unit-eval
* @bootstrap max
* @hidden
*/
public function drushUnitEval($code)
{
Expand All @@ -28,6 +29,7 @@ public function drushUnitEval($code)
/**
* Return options as function result.
* @command unit-return-options
* @hidden
*/
public function drushUnitReturnOptions($arg = '', $options = ['x' => 'y', 'data' => [], 'format' => 'yaml'])
{
Expand All @@ -38,6 +40,7 @@ public function drushUnitReturnOptions($arg = '', $options = ['x' => 'y', 'data'
/**
* Return original argv as function result.
* @command unit-return-argv
* @hidden
*/
public function drushUnitReturnArgv(array $args)
{
Expand Down

0 comments on commit 643e8a6

Please sign in to comment.