Skip to content

Commit

Permalink
add command to list broken xrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Oct 7, 2024
1 parent d4a83a8 commit 995c591
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
2 changes: 2 additions & 0 deletions dev/google-cloud
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Dev\Command\AddComponentCommand;
use Google\Cloud\Dev\Command\ComponentInfoCommand;
use Google\Cloud\Dev\Command\DocFxCommand;
use Google\Cloud\Dev\Command\ListBrokenXrefsCommand;
use Google\Cloud\Dev\Command\RepoInfoCommand;
use Google\Cloud\Dev\Command\ReleaseInfoCommand;
use Google\Cloud\Dev\Command\SplitCommand;
Expand All @@ -45,6 +46,7 @@ $app = new Application;
$app->add(new AddComponentCommand($rootDirectory));
$app->add(new ComponentInfoCommand());
$app->add(new DocFxCommand());
$app->add(new ListBrokenXrefsCommand());
$app->add(new RepoInfoCommand());
$app->add(new ReleaseInfoCommand());
$app->add(new SplitCommand($rootDirectory));
Expand Down
67 changes: 67 additions & 0 deletions dev/src/Command/ListBrokenXrefsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright 2024 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Dev\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use Google\Cloud\Dev\Component;

/**
* @internal
*/
class ListBrokenXrefsCommand extends Command
{
const BROKEN_REFS_REGEX = '/\[(\w+)\] Broken xref in (.*) \((.*)\): (.*)/';

protected function configure()
{
$this->setName('list-broken-xrefs')
->setDescription('List all the broken xrefs in the documentation using ".kokoro/docs/publish.sh"')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
foreach (Component::getComponents() as $component) {
$input = new ArrayInput($f = [
'command' => 'docfx',
'--component' => $component->getName(),
]);

$buffer = new BufferedOutput(OutputInterface::VERBOSITY_DEBUG);
$returnCode = $this->getApplication()->doRun($input, $buffer);
foreach (explode("\n", $buffer->fetch()) as $line) {
if (preg_match(self::BROKEN_REFS_REGEX, $line, $matches)) {
list(, $component, $file, $ref, $brokenText) = $matches;
$link = sprintf(
'=HYPERLINK("https://github.com/googleapis/googleapis/blob/master/%s", "%s")',
$file,
$file
);
$output->writeln(implode(',', [$component, $link, $ref, $brokenText]));
}
}
}

return 0;
}
}
4 changes: 2 additions & 2 deletions dev/src/DocFx/Node/ClassNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ public function getProtoFileName(string $ref = null): string|null
if ($ref1 && $ref2) {
if (false !== stripos($line, $ref1)
&& false !== stripos($lines[$i+1], $ref2)) {
return $proto . '#' . ($i + 1);
return $proto . '#L' . ($i + 1);
}
} elseif (false !== stripos($line, $ref)) {
return $proto . '#' . ($i + 1);
return $proto . '#L' . ($i + 1);
}
}

Expand Down
1 change: 1 addition & 0 deletions dev/src/DocFx/XrefValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Google\Cloud\Core\Logger\AppEngineFlexFormatter;
use Google\Cloud\Core\Logger\AppEngineFlexFormatterV2;
use Google\Cloud\Dev\Component;

/**
* @internal
Expand Down

0 comments on commit 995c591

Please sign in to comment.