forked from anilozmen/clean-code-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.travis-build.php
70 lines (59 loc) · 2.2 KB
/
.travis-build.php
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
<?php
$readMeFilepath = __DIR__ . '/README.md';
$readMeFile = new SplFileObject($readMeFilepath);
$readMeFile->setFlags(SplFileObject::DROP_NEW_LINE);
$cliRedBackground = "\033[37;41m";
$cliReset = "\033[0m";
$exitStatus = 0;
$indentationSteps = 3;
$manIndex = 0;
$linesWithSpaces = [];
$tableOfContentsStarted = null;
$currentTableOfContentsChapters = [];
$chaptersFound = [];
foreach ($readMeFile as $lineNumber => $line) {
if (preg_match('/\s$/', $line)) {
$linesWithSpaces[] = sprintf('%5s: %s', 1 + $lineNumber, $line);
}
if (preg_match('/^(?<depth>##+)\s(?<title>.+)/', $line, $matches)) {
if (null === $tableOfContentsStarted) {
$tableOfContentsStarted = true;
continue;
}
$tableOfContentsStarted = false;
$chaptersFound[] = sprintf('%s [%s](#%s)',
strlen($matches['depth']) === 2
? sprintf(' %s.', ++$manIndex)
: ' *'
,
$matches['title'],
preg_replace(['/ /', '/[^-\w]+/'], ['-', ''], strtolower($matches['title']))
);
}
if ($tableOfContentsStarted === true && isset($line[0])) {
$currentTableOfContentsChapters[] = $line;
}
}
if (count($linesWithSpaces)) {
fwrite(STDERR, sprintf("${cliRedBackground}The following lines end with a space character:${cliReset}\n%s\n\n",
implode(PHP_EOL, $linesWithSpaces)
));
$exitStatus = 1;
}
$currentTableOfContentsChaptersFilename = __DIR__ . '/current-chapters';
$chaptersFoundFilename = __DIR__ . '/chapters-found';
file_put_contents($currentTableOfContentsChaptersFilename, implode(PHP_EOL, $currentTableOfContentsChapters));
file_put_contents($chaptersFoundFilename, implode(PHP_EOL, $chaptersFound));
$tableOfContentsDiff = shell_exec(sprintf('diff --unified %s %s',
escapeshellarg($currentTableOfContentsChaptersFilename),
escapeshellarg($chaptersFoundFilename)
));
@ unlink($currentTableOfContentsChaptersFilename);
@ unlink($chaptersFoundFilename);
if (!empty($tableOfContentsDiff)) {
fwrite(STDERR, sprintf("${cliRedBackground}The table of contents is not aligned:${cliReset}\n%s\n\n",
$tableOfContentsDiff
));
$exitStatus = 1;
}
exit($exitStatus);