Skip to content

Commit 8c43dcb

Browse files
author
ityaozm@gmail.com
committedNov 2, 2024·
ci(readme-lint): Improve README validation logic
- 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
1 parent e5b432a commit 8c43dcb

File tree

2 files changed

+118
-7
lines changed

2 files changed

+118
-7
lines changed
 

‎README-zh_TW.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
[![check & fix styling](https://github.com/guanguans/ai-commit/actions/workflows/php-cs-fixer.yml/badge.svg)](https://github.com/guanguans/ai-commit/actions)
1414
[![codecov](https://codecov.io/gh/guanguans/ai-commit/branch/main/graph/badge.svg?token=URGFAWS6S4)](https://codecov.io/gh/guanguans/ai-commit)
1515
[![Latest Stable Version](https://poser.pugx.org/guanguans/ai-commit/v)](https://packagist.org/packages/guanguans/ai-commit)
16-
![GitHub release (latest by date)](https://img.shields.io/github/v/release/guanguans/ai-commit)
16+
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/guanguans/ai-commit)](https://github.com/guanguans/ai-commit/releases/latest)
1717
[![Total Downloads](https://poser.pugx.org/guanguans/ai-commit/downloads)](https://packagist.org/packages/guanguans/ai-commit)
1818
[![License](https://poser.pugx.org/guanguans/ai-commit/license)](https://packagist.org/packages/guanguans/ai-commit)
1919

‎readme-lint

+117-6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,124 @@ declare(strict_types=1);
1010
*
1111
* This source file is subject to the MIT license that is bundled.
1212
*/
13+
14+
use GrahamCampbell\ResultType\Error;
15+
use GrahamCampbell\ResultType\Result;
16+
use GrahamCampbell\ResultType\Success;
17+
use Illuminate\Console\OutputStyle;
18+
use Illuminate\Pipeline\Pipeline;
19+
use Symfony\Component\Console\Input\ArgvInput;
20+
use Symfony\Component\Console\Output\ConsoleOutput;
21+
1322
require __DIR__.'/vendor/autoload.php';
1423

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

22-
exit(0);
28+
(new Pipeline(app()))
29+
->send(glob('README-*.md'))
30+
->through([
31+
pipeFor(static function (string $translatedReadme, string $readme): Result {
32+
if (count(file($translatedReadme)) !== count(file($readme))) {
33+
return Error::create("The file [$translatedReadme] has a different number of lines than [$readme]");
34+
}
35+
36+
return Success::create('ok');
37+
}),
38+
pipeFor(static function (string $translatedReadme, string $readme): Result {
39+
$translatedReadmeFile = file($translatedReadme);
40+
foreach (file($readme) as $lineNumber => $line) {
41+
/** @noinspection NotOptimalIfConditionsInspection */
42+
if (
43+
$line !== $translatedReadmeFile[$lineNumber]
44+
&& str($line)->trim()->isNotEmpty()
45+
&& str($line)->startsWith([
46+
// markdown title
47+
'#',
48+
'##',
49+
'###',
50+
'####',
51+
'#####',
52+
'######',
53+
// markdown list
54+
'-',
55+
'*',
56+
// markdown link
57+
'[',
58+
// markdown image
59+
'![',
60+
// markdown code
61+
'```',
62+
// markdown table
63+
'|-',
64+
'|',
65+
'-',
66+
// markdown blockquote
67+
'>',
68+
// markdown horizontal rule
69+
'---',
70+
// markdown bold
71+
'**',
72+
// markdown italic
73+
'*',
74+
// markdown strikethrough
75+
'~~',
76+
// markdown inline code
77+
'`',
78+
// markdown footnote
79+
'[^',
80+
// markdown superscript
81+
'^',
82+
// markdown subscript
83+
'_',
84+
// markdown escape
85+
'\\',
86+
// markdown html
87+
'<',
88+
// markdown comment
89+
'<!--',
90+
'[//]: # (',
91+
])
92+
&& ! str($translatedReadmeFile[$lineNumber])->startsWith(str($line)->before(' ')->append(' '))
93+
) {
94+
app(OutputStyle::class)->listing([
95+
$line,
96+
$translatedReadmeFile[$lineNumber],
97+
]);
98+
99+
return Error::create(sprintf(
100+
'The file [%s] has a different markdown line [%s] than [%s]',
101+
$translatedReadme,
102+
$lineNumber + 1,
103+
$readme
104+
));
105+
}
106+
}
107+
108+
return Success::create('ok');
109+
}),
110+
])
111+
->then(static function (): void {
112+
app(OutputStyle::class)->success('All readme files are ok');
113+
exit(0);
114+
});
115+
116+
/**
117+
* @param \Closure(string, string): \GrahamCampbell\ResultType\Result $checker
118+
*
119+
* @return \Closure(array, \Closure): array
120+
*/
121+
function pipeFor(Closure $checker): Closure
122+
{
123+
return static function (array $translatedReadmes, Closure $next) use ($checker): array {
124+
foreach ($translatedReadmes as $translatedReadme) {
125+
if ($value = $checker($translatedReadme, 'README.md')->error()->getOrElse(null)) {
126+
app(OutputStyle::class)->error($value);
127+
exit(1);
128+
}
129+
}
130+
131+
return $next($translatedReadmes);
132+
};
133+
}

0 commit comments

Comments
 (0)
Please sign in to comment.