Skip to content

Commit

Permalink
feature #97 Add a debug CLI command (ogizanagi)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 0.x-dev branch.

Discussion
----------

Add a debug CLI command

Introduce a new `debug:stenope:content` that'll be useful in multiple ways, to debug, list, filter and inspect contents managed by Stenope:

## List

```bash
bin/console debug:stenope:content "App\Model\Page"
```

![Capture d’écran 2021-05-28 à 16 40 01](https://user-images.githubusercontent.com/2211145/120000690-5fba9480-bfd3-11eb-957a-db473f718fd6.png)

## Show

```bash
bin/console debug:stenope:content "App\Model\Index" README
```

![Capture d’écran 2021-05-28 à 16 37 51](https://user-images.githubusercontent.com/2211145/120000413-110cfa80-bfd3-11eb-83dc-67e0d7298359.png)

(change verbosity to display more)

## Order & Filter

```bash
bin/console debug:stenope:content "App\Model\Article" \
   --filter="not:outdated" \
   --filter="slug contains:rebranding" \
   --order="desc:date"
```

![Capture d’écran 2021-05-28 à 16 37 03](https://user-images.githubusercontent.com/2211145/120000280-f33f9580-bfd2-11eb-9ef2-1c70fe6c91d3.png)

---

There are some minor improvments / changes included in this PR as well:
- handle fragment (anchors) when linking content between them ([see how we reference another document with an anchor](https://github.com/StenopePHP/Stenope/pull/97/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R58) in the documentation changes in this PR)
- added the ability to provide a filter function for a specific property (it allows the `--filter=field contains:text` feature exposed in the command)
- added the `TableOfContent` class. Small BC break (change your typehints from `Headline[]` to `TableOfContent`), but it allows to dump a simpler representation of the TOC when inspecting a content in non-verbose mode. It may also be useful later in case we need to introduce more data/config inside the TOC.
- `ContentManager::getContents` now returns the slugs as keys

### TODO

- [x] Add some functional tests for the debug command
- [x] Add unit & functional tests for resolving links with anchors
- [x] Add unit tests for filtering with a callable for a specific property
- [x] Add tests to reflect the keys for `ContentManager::getContents`
- Later: interactively ask the class if not provided
- Later: allow partial classname if no conflict and ask between matches otherwise
- Later: dedicated & more powerful parser classes for orders & filters

Commits
-------

0279673 Add a debug CLI command
  • Loading branch information
ogizanagi committed Jun 2, 2021
2 parents b754d9d + 0279673 commit 0a01b1b
Show file tree
Hide file tree
Showing 27 changed files with 625 additions and 95 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Stenope is not a ready-to-use bloging system: but you could quickly _write your
### Features

- [CLI usage](doc/cli.md)
- [Debug contents](doc/cli.md#debug)
- [Loading and parsing content](doc/loading-content.md)
- [Supported formats](doc/supported-formats.md)
- [Supported sources](doc/supported-sources.md)
Expand Down
8 changes: 8 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Stenope\Bundle\Builder\PageList;
use Stenope\Bundle\Builder\Sitemap;
use Stenope\Bundle\Command\BuildCommand;
use Stenope\Bundle\Command\DebugCommand;
use Stenope\Bundle\ContentManager;
use Stenope\Bundle\Decoder\HtmlDecoder;
use Stenope\Bundle\Decoder\MarkdownDecoder;
Expand Down Expand Up @@ -68,6 +69,13 @@
->set(ContentProviderFactory::class)->args(['$factories' => tagged_iterator(tags\content_provider_factory)])
->set(LocalFilesystemProviderFactory::class)->tag(tags\content_provider_factory)

// Debug
->set(DebugCommand::class)->args([
'$manager' => service(ContentManager::class),
'$stopwatch' => service('stenope.build.stopwatch'),
])
->tag('console.command', ['command' => DebugCommand::getDefaultName()])

// Build
->set(BuildCommand::class)->args([
'$builder' => service(Builder::class),
Expand Down
2 changes: 1 addition & 1 deletion doc/app/README → doc/app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Development

### Depenencies
### Dependencies

make install

Expand Down
2 changes: 1 addition & 1 deletion doc/app/composer.lock

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

1 change: 1 addition & 0 deletions doc/app/config/packages/stenope.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ stenope:
- vendor
patterns:
- '*.md'
depth: <=1

App\Model\Page:
type: files
Expand Down
5 changes: 2 additions & 3 deletions doc/app/src/Model/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

namespace App\Model;

use Stenope\Bundle\TableOfContent\Headline;
use Stenope\Bundle\TableOfContent\TableOfContent;

class Page
{
public string $title;
public string $slug;
public string $content;
/** @var Headline[] */
public array $tableOfContent = [];
public ?TableOfContent $tableOfContent = null;
public \DateTimeInterface $created;
public \DateTimeInterface $lastModified;
}
26 changes: 24 additions & 2 deletions doc/cli.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# CLI

## Build

The build command:

```shell
bin/console stenope:build [options] [--] [<buildDir>]
```

## Building into a specific directory
### Building into a specific directory

Use the `buildDir` argument:

```shell
bin/console stenope:build ./static
```

## Options
### Options

| Option | Description |
| -- | -- | -- |
Expand All @@ -24,3 +26,23 @@ bin/console stenope:build ./static
| `--no-sitemap` | Don't build the sitemap | |
| `--no-expose` | Don't expose the public directory | |
| `--ignore-content-not-found` | Ignore content not found errors | |

## Debug

There is a command to list, filter, sort out and display content managed by Stenope:

```shell
bin/console debug:stenope:content [options] [--] <class> [<id>]
```

E.g:

```shell
bin/console debug:stenope:content "App\Model\Article" --filter="not:outdated" --filter="slug contains:symfony" --order="desc:publishedAt"
```

```shell
bin/console debug:stenope:content "App\Model\Author" ogi
```

Use `--help` for more details and usage samples.
4 changes: 4 additions & 0 deletions doc/loading-content.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ $tagedMobileArticles = $this->manager->getContents(
);
```

## Debug

See [CLI - Debug](./cli.md#debug)

## Advanced usage and extension

### Register a custom denormalizer
Expand Down
1 change: 0 additions & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Symfony\Component\Mime\MimeTypesInterface;
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Stopwatch\Stopwatch;
use Twig\Environment;
Expand Down
52 changes: 2 additions & 50 deletions src/Command/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Stenope\Bundle\Builder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -19,13 +18,11 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Terminal;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Stopwatch\StopwatchEvent;

/**
* Build Command
*/
class BuildCommand extends Command
{
use StopwatchHelperTrait;

protected static $defaultName = 'stenope:build';

private Builder $builder;
Expand All @@ -46,7 +43,6 @@ protected function configure(): void
{
$this
->setDescription('Build static website')
->setHelp('...')
->addArgument(
'buildDir',
InputArgument::OPTIONAL,
Expand Down Expand Up @@ -159,50 +155,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int

return Command::SUCCESS;
}

public static function formatTimePrecision($secs)
{
static $timeFormats = [
[0, 'sec', 1, 2],
[2, 'secs', 1, 2],
[60, '1 min'],
[120, 'mins', 60],
[3600, '1 hr'],
[7200, 'hrs', 3600],
[86400, '1 day'],
[172800, 'days', 86400],
];

foreach ($timeFormats as $index => $format) {
if ($secs >= $format[0]) {
if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
|| $index == \count($timeFormats) - 1
) {
switch (\count($format)) {
case 2:
return $format[1];

case 4:
return round($secs / $format[2], $format[3]) . ' ' . $format[1];

default:
return floor($secs / $format[2]) . ' ' . $format[1];
}
}
}
}
}

public static function formatEvent(StopwatchEvent $event): string
{
return sprintf(
'Start time: %s — End time: %s — Duration: %s — Memory used: %s',
date('H:i:s', ($event->getOrigin() + $event->getStartTime()) / 1000),
date('H:i:s', ($event->getOrigin() + $event->getEndTime()) / 1000),
static::formatTimePrecision($event->getDuration() / 1000),
Helper::formatMemory($event->getMemory())
);
}
}

/**
Expand Down
Loading

0 comments on commit 0a01b1b

Please sign in to comment.