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
|
<?php
declare(strict_types=1);
namespace Shirabe\Lint\Linters;
use Shirabe\Lint\Linter;
use Shirabe\Lint\Support\FileFinder;
use Shirabe\Lint\Support\Paths;
final class SortedDependencies implements Linter
{
public function name(): string
{
return 'sorted_dependencies';
}
public function failureIntro(): string
{
return "Found unsorted `[dependencies]` / `[dev-dependencies]` in Cargo.toml.\n"
. 'Entries must be alphabetical, with `shirabe-*` crates listed before others:';
}
public function check(string $rootDir, array $excludes): array
{
$errors = [];
foreach (FileFinder::cargoTomls($rootDir) as $path) {
$relative = Paths::relativeTo($rootDir, $path);
if (in_array($relative, $excludes, true)) {
continue;
}
$sections = $this->parseDepSections(file_get_contents($path));
foreach (['dependencies', 'dev-dependencies'] as $section) {
$deps = $sections[$section] ?? [];
if ($deps === []) {
continue;
}
$expected = $this->sortDepNames($deps);
if ($deps === $expected) {
continue;
}
$errors[] = "{$relative} [{$section}]\n"
. ' actual: ' . implode(', ', $deps) . "\n"
. ' expected: ' . implode(', ', $expected);
}
}
return $errors;
}
/** @return array<string, list<string>> */
private function parseDepSections(string $content): array
{
$sections = [];
$current = null;
foreach (explode("\n", $content) as $line) {
$stripped = rtrim($line, "\r\n");
if (preg_match('/\A\s*\[([^\]]+)\]\s*\z/', $stripped, $m)) {
$current = $m[1];
$sections[$current] ??= [];
continue;
}
if ($current !== null && preg_match('/\A([A-Za-z0-9_-]+)\s*[.=]/', $stripped, $m)) {
$sections[$current][] = $m[1];
}
}
return $sections;
}
/**
* @param list<string> $deps
* @return list<string>
*/
private function sortDepNames(array $deps): array
{
$shirabe = [];
$other = [];
foreach ($deps as $dep) {
if (str_starts_with($dep, 'shirabe-')) {
$shirabe[] = $dep;
} else {
$other[] = $dep;
}
}
sort($shirabe);
sort($other);
return array_merge($shirabe, $other);
}
}
|