aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/linters/src/Support/FileFinder.php
blob: 50dabcd837fbd2f814556e45709bef99a6a14c0b (plain)
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
<?php

declare(strict_types=1);

namespace Shirabe\Lint\Support;

final class FileFinder
{
    // Returns absolute paths to each crate's Cargo.toml (crates/*/Cargo.toml), sorted.
    public static function cargoTomls(string $rootDir): array
    {
        $paths = glob("{$rootDir}/crates/*/Cargo.toml") ?: [];
        sort($paths);

        return $paths;
    }

    /** @return list<string> absolute paths to *.rs files under crates/, sorted */
    public static function rustFiles(string $rootDir): array
    {
        return self::walk("{$rootDir}/crates", static fn (string $path): bool => str_ends_with($path, '.rs'));
    }

    // Returns absolute paths to mod.rs files under each crate's src/ tree
    // (crates/*/src/**/mod.rs), sorted.
    public static function modRsFiles(string $rootDir): array
    {
        $found = [];

        foreach (glob("{$rootDir}/crates/*", GLOB_ONLYDIR) ?: [] as $crateDir) {
            $srcDir = "{$crateDir}/src";
            if (!is_dir($srcDir)) {
                continue;
            }

            foreach (self::walk($srcDir, static fn (string $path): bool => basename($path) === 'mod.rs') as $path) {
                $found[] = $path;
            }
        }

        sort($found);

        return $found;
    }

    /** @return list<string> */
    private static function walk(string $baseDir, callable $predicate): array
    {
        if (!is_dir($baseDir)) {
            return [];
        }

        $found = [];
        $iterator = new \RecursiveIteratorIterator(
            new \RecursiveDirectoryIterator($baseDir, \FilesystemIterator::SKIP_DOTS),
        );
        foreach ($iterator as $file) {
            /** @var \SplFileInfo $file */
            if ($file->isFile() && $predicate($file->getPathname())) {
                $found[] = $file->getPathname();
            }
        }

        sort($found);

        return $found;
    }
}