-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
readme-lint
executable file
·133 lines (122 loc) · 4.5 KB
/
readme-lint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env php
<?php
declare(strict_types=1);
/**
* This file is part of the guanguans/ai-commit.
*
* (c) guanguans <[email protected]>
*
* 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';
app()->singleton(OutputStyle::class, static function (): OutputStyle {
return new OutputStyle(new ArgvInput(), new ConsoleOutput());
});
(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);
};
}