Browse Source

DocumentationGenerator - make re-using of previously generated docs mechanism available not only for tests, but also for DocumentationCommand

Also report errors of dev-tools/doc.php script and add success message after docs regeneration.
Dariusz Ruminski 4 years ago
parent
commit
d7f33fac52

+ 2 - 0
dev-tools/doc.php

@@ -11,6 +11,8 @@
  * with this source code in the file LICENSE.
  */
 
+error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED);
+
 require __DIR__.'/../vendor/autoload.php';
 
 use PhpCsFixer\Console\Command\DocumentationCommand;

+ 17 - 4
src/Console/Command/DocumentationCommand.php

@@ -58,6 +58,8 @@ final class DocumentationCommand extends Command
         $this->generateFixersDocs($fixers);
         $this->generateRuleSetsDocs($fixers);
 
+        $output->writeln('Docs updated.');
+
         return 0;
     }
 
@@ -65,18 +67,29 @@ final class DocumentationCommand extends Command
     {
         $filesystem = new Filesystem();
 
-        /** @var SplFileInfo $file */
-        foreach ((new Finder())->files()->in($this->generator->getFixersDocumentationDirectoryPath()) as $file) {
-            $filesystem->remove($file->getPathname());
-        }
+        // Array of existing fixer docs.
+        // We first override existing files, and then we will delete files that are no longer needed.
+        // We cannot remove all files first, as generation of docs is re-using existing docs to extract code-samples for
+        // VersionSpecificCodeSample under incompatible PHP version.
+        $docForFixerRelativePaths = [];
 
         foreach ($fixers as $fixer) {
+            $docForFixerRelativePaths[] = $this->generator->getFixerDocumentationFileRelativePath($fixer);
             $filesystem->dumpFile(
                 $this->generator->getFixerDocumentationFilePath($fixer),
                 $this->generator->generateFixerDocumentation($fixer)
             );
         }
 
+        /** @var SplFileInfo $file */
+        foreach (
+            (new Finder())->files()
+                ->in($this->generator->getFixersDocumentationDirectoryPath())
+                ->notPath($docForFixerRelativePaths) as $file
+        ) {
+            $filesystem->remove($file->getPathname());
+        }
+
         $index = $this->generator->getFixersDocumentationIndexFilePath();
 
         if (false === @file_put_contents($index, $this->generator->generateFixersDocumentationIndex($fixers))) {

+ 13 - 5
src/Documentation/DocumentationGenerator.php

@@ -129,11 +129,7 @@ RST;
                 $attributes = '';
             }
 
-            $path = Preg::replace(
-                '#^'.preg_quote($this->getFixersDocumentationDirectoryPath(), '#').'/#',
-                './',
-                $this->getFixerDocumentationFilePath($fixer)
-            );
+            $path = './'.$this->getFixerDocumentationFileRelativePath($fixer);
 
             $documentation .= <<<RST
 
@@ -159,6 +155,18 @@ RST;
         ).'.rst';
     }
 
+    /**
+     * @return string
+     */
+    public function getFixerDocumentationFileRelativePath(FixerInterface $fixer)
+    {
+        return Preg::replace(
+            '#^'.preg_quote($this->getFixersDocumentationDirectoryPath(), '#').'/#',
+            '',
+            $this->getFixerDocumentationFilePath($fixer)
+        );
+    }
+
     /**
      * @return string
      */