Browse Source

Début de commande pour générer un index

master
Matthieu Marcillaud 1 year ago
parent
commit
55a9278166
  1. 30
      old/Generator.php
  2. 42
      old/Loader/AbstractLoader.php
  3. 36
      old/Loader/Collection.php
  4. 9
      old/Loader/LoaderInterface.php
  5. 1
      src/Application.php
  6. 3
      src/Command/FromFile.php
  7. 62
      src/Command/MakeIndex.php
  8. 60
      src/Stage/IndexStage.php
  9. 1
      src/Stage/JsonAutodocStage.php

30
old/Generator.php

@ -1,30 +0,0 @@
<?php
namespace Spip\Autodoc;
use Spip\Autodoc\Loader\LoaderInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class Generator
{
private SymfonyStyle $io;
private LoaderInterface $loader;
public function __construct(SymfonyStyle $io)
{
$this->io = $io;
}
public function setLoader(LoaderInterface $loader)
{
$this->loader = $loader;
}
public function run()
{
$this->loader->load();
$directories = $this->loader->getDirectories();
dump($directories);
}
}

42
old/Loader/AbstractLoader.php

@ -1,42 +0,0 @@
<?php
namespace Spip\Autodoc\Loader;
use Spip\Autodoc\Loader\LoaderInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
abstract class AbstractLoader implements LoaderInterface
{
protected SymfonyStyle $io;
private string $inputDirectory;
public function __construct(SymfonyStyle $io)
{
$this->io = $io;
}
abstract public function load(): void;
/**
* Get the value of directories to document
*/
public function getDirectories(): array
{
return [ $this->inputDirectory ];
}
public function setInputDirectory(string $directory): self
{
$this->inputDirectory = $directory;
return $this;
}
/**
* Get the value of inputDirectory
*/
public function getInputDirectory(): string
{
return $this->inputDirectory;
}
}

36
old/Loader/Collection.php

@ -1,36 +0,0 @@
<?php
namespace Spip\Autodoc\Loader;
class Collection implements LoaderInterface
{
/** @var LoaderInterface[] */
private array $loaders;
public function add(LoaderInterface $loader)
{
$this->loaders[] = $loader;
}
public function load(): void
{
foreach ($this->loaders as $loader) {
$loader->load();
}
}
public function getDirectories(): array
{
$dirs = [];
foreach ($this->loaders as $loader) {
$dirs = [...$dirs, ...$loader->getDirectories()];
}
return $dirs;
}
/** @return LoaderInterface[] */
public function getLoaders(): array
{
return $this->loaders;
}
}

9
old/Loader/LoaderInterface.php

@ -1,9 +0,0 @@
<?php
namespace Spip\Autodoc\Loader;
interface LoaderInterface
{
public function getDirectories(): array;
public function load(): void;
}

1
src/Application.php

@ -23,5 +23,6 @@ class Application extends ConsoleApplication
$this->add(new Command\FromGit());
$this->add(new Command\FromDirectory());
$this->add(new Command\FromFile());
$this->add(new Command\MakeIndex());
}
}

3
src/Command/FromFile.php

@ -97,6 +97,9 @@ https://git.spip.net/spip-contrib-extensions/champs_extras_core@master;cextras
$this->pipeline($item);
}
// create index
return Command::SUCCESS;
}

62
src/Command/MakeIndex.php

@ -0,0 +1,62 @@
<?php
/*
* Commande d'exécution depuis un fichier autodoc.txt (de la Zone de SPIP par défaut)…
*/
namespace Spip\Autodoc\Command;
use League\Pipeline\InterruptibleProcessor;
use League\Pipeline\Pipeline;
use Spip\Autodoc\Context;
use Spip\Autodoc\Stage\IndexStage;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* Déclaration et exécution de l'application depuis un fichier de type autodoc.txt
*/
class MakeIndex extends Command
{
protected function configure()
{
$this
->setName('make:index')
->setDescription('Générer l’index des différentes documentations')
->setHelp('L’index se crée à partir des différents fichiers autodoc.json des documentations générées')
->addOption('outputs', 'o', InputOption::VALUE_OPTIONAL, 'Répertoire stockant toutes les documentations générées. Chemin absolu ou relatif au répertoire courant. <comment>Défaut : "var/output"</comment>')
->addOption('topnav', null, InputOption::VALUE_OPTIONAL, 'URL d’un fichier JS à charger dans le head html. <comment>Exemple: "//boussole.spip.net/?page=spipnav.js&lang=fr"</comment>')
->addOption('topnav_spip', null, InputOption::VALUE_NONE, 'Intègre le JS de la boussole SPIP en entête topnav.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$context = new Context($input, $output);
/** @var SymfonyStyle */
$io = $context->get('io');
$io->title("Autodoc " . $this->getName());
if ($input->getOption('outputs')) {
$context->set('output_base_directory', $input->getOption('outputs'));
}
$context->set('topnav', $input->getOption('topnav'));
$context->set('topnav_spip', $input->getOption('topnav_spip'));
$this->pipeline($context);
return Command::SUCCESS;
}
protected function pipeline(Context $context): void
{
$processor = new InterruptibleProcessor(fn (Context $context) => $context->empty('errors'));
$pipeline = (new Pipeline($processor))
->pipe(new IndexStage());
$pipeline->process($context);
}
}

60
src/Stage/IndexStage.php

@ -0,0 +1,60 @@
<?php
namespace Spip\Autodoc\Stage;
use Spip\Autodoc\Context;
use Spip\Autodoc\Exception\ContextException;
/** Create index.html page with links of all available documentations */
class IndexStage implements StageInterface {
public function __invoke(Context $context): Context
{
$context->add('stages', $this::class);
$context->get('logger')->debug("Pass: ".$this::class);
if (
!$context->get('output_base_directory')
or !is_dir($context->get('output_base_directory'))
) {
throw new ContextException(sprintf('Key "%s" needs to be a valid directory.', 'output_base_directory'));
}
$output_base_directory = $context->get('output_base_directory');
$list = $this->load($output_base_directory);
$sorted = $this->sort($list);
dump($sorted);
return $context;
}
/** @return array<string, stdClass> */
public function load(string $output_base_directory): array {
$list = [];
$projects = glob($output_base_directory . '/*/autodoc.json');
foreach ($projects as $project) {
$dir = basename(dirname($project));
$json = json_decode(file_get_contents($project));
$list[$dir] = $json;
}
return $list;
}
/** @return array<string, stdClass> */
public function sort(array $list): array {
$groups = [];
foreach ($list as $project) {
$organization = basename(dirname($project->urls->repository ?: '')) ?: 'other';
if ($organization === 'spip' and $project->prefix !== 'spip') {
$organization = 'spip-core';
}
$groups[$organization][] = $project;
}
foreach ($groups as $organization => &$group) {
usort($group, function ($a, $b) {
return $a->title <=> $b->title;
});
}
return $groups;
}
}

1
src/Stage/JsonAutodocStage.php

@ -30,6 +30,7 @@ class JsonAutodocStage implements StageInterface
$output_directory = $context->get('output_directory');
/** @var Package */
$package = $context->get('package');
// FIXME: This should be a real class. (and encoded / decoded with symfony/serializer ?)
$json = new stdClass();
$json->title = $context->get('title');
$json->prefix = $package->get('prefix');

Loading…
Cancel
Save