Skip to content

Commit

Permalink
ci(readme-lint): Improve README validation logic
Browse files Browse the repository at this point in the history
- Refactor README file validation to use a pipeline structure
- Introduce a new `pipeFor` function for cleaner processing
- Utilize `GrahamCampbellResultType` for error handling
- Ensure all README files match the line count of README.md
- Provide clearer error messages for mismatched files
  • Loading branch information
[email protected] committed Nov 2, 2024
1 parent e5b432a commit 8c43dcb
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README-zh_TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[![check & fix styling](https://github.com/guanguans/ai-commit/actions/workflows/php-cs-fixer.yml/badge.svg)](https://github.com/guanguans/ai-commit/actions)
[![codecov](https://codecov.io/gh/guanguans/ai-commit/branch/main/graph/badge.svg?token=URGFAWS6S4)](https://codecov.io/gh/guanguans/ai-commit)
[![Latest Stable Version](https://poser.pugx.org/guanguans/ai-commit/v)](https://packagist.org/packages/guanguans/ai-commit)
![GitHub release (latest by date)](https://img.shields.io/github/v/release/guanguans/ai-commit)
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/guanguans/ai-commit)](https://github.com/guanguans/ai-commit/releases/latest)
[![Total Downloads](https://poser.pugx.org/guanguans/ai-commit/downloads)](https://packagist.org/packages/guanguans/ai-commit)
[![License](https://poser.pugx.org/guanguans/ai-commit/license)](https://packagist.org/packages/guanguans/ai-commit)

Expand Down
123 changes: 117 additions & 6 deletions readme-lint
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,124 @@ declare(strict_types=1);
*
* This source file is subject to the MIT license that is bundled.
*/

use GrahamCampbell\ResultType\Error;
use GrahamCampbell\ResultType\Result;
use GrahamCampbell\ResultType\Success;
use Illuminate\Console\OutputStyle;
use Illuminate\Pipeline\Pipeline;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;

require __DIR__.'/vendor/autoload.php';

collect(glob('README-*.md'))->each(static function (string $file): void {
if (count(file($file)) !== count(file('README.md'))) {
echo "The file [$file] has a different number of lines than [README.md]\n";
exit(1);
}
app()->singleton(OutputStyle::class, static function (): OutputStyle {
return new OutputStyle(new ArgvInput(), new ConsoleOutput());
});

exit(0);
(new Pipeline(app()))
->send(glob('README-*.md'))
->through([
pipeFor(static function (string $translatedReadme, string $readme): Result {
if (count(file($translatedReadme)) !== count(file($readme))) {
return Error::create("The file [$translatedReadme] has a different number of lines than [$readme]");
}

return Success::create('ok');
}),
pipeFor(static function (string $translatedReadme, string $readme): Result {
$translatedReadmeFile = file($translatedReadme);
foreach (file($readme) as $lineNumber => $line) {
/** @noinspection NotOptimalIfConditionsInspection */
if (
$line !== $translatedReadmeFile[$lineNumber]
&& str($line)->trim()->isNotEmpty()
&& str($line)->startsWith([
// markdown title
'#',
'##',
'###',
'####',
'#####',
'######',
// markdown list
'-',
'*',
// markdown link
'[',
// markdown image
'![',
// markdown code
'```',
// markdown table
'|-',
'|',
'-',
// markdown blockquote
'>',
// markdown horizontal rule
'---',
// markdown bold
'**',
// markdown italic
'*',
// markdown strikethrough
'~~',
// markdown inline code
'`',
// markdown footnote
'[^',
// markdown superscript
'^',
// markdown subscript
'_',
// markdown escape
'\\',
// markdown html
'<',
// markdown comment
'<!--',
'[//]: # (',
])
&& ! str($translatedReadmeFile[$lineNumber])->startsWith(str($line)->before(' ')->append(' '))
) {
app(OutputStyle::class)->listing([
$line,
$translatedReadmeFile[$lineNumber],
]);

return Error::create(sprintf(
'The file [%s] has a different markdown line [%s] than [%s]',
$translatedReadme,
$lineNumber + 1,
$readme
));
}
}

return Success::create('ok');
}),
])
->then(static function (): void {
app(OutputStyle::class)->success('All readme files are ok');
exit(0);
});

/**
* @param \Closure(string, string): \GrahamCampbell\ResultType\Result $checker
*
* @return \Closure(array, \Closure): array
*/
function pipeFor(Closure $checker): Closure
{
return static function (array $translatedReadmes, Closure $next) use ($checker): array {
foreach ($translatedReadmes as $translatedReadme) {
if ($value = $checker($translatedReadme, 'README.md')->error()->getOrElse(null)) {
app(OutputStyle::class)->error($value);
exit(1);
}
}

return $next($translatedReadmes);
};
}

0 comments on commit 8c43dcb

Please sign in to comment.