@@ -10,13 +10,124 @@ declare(strict_types=1);
10
10
*
11
11
* This source file is subject to the MIT license that is bundled.
12
12
*/
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
+
13
22
require __DIR__ .'/vendor/autoload.php ' ;
14
23
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 ());
20
26
});
21
27
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