summaryrefslogtreecommitdiff
path: root/prepare_static.php
blob: e3663f890ba310a0d6e1c62d85ea6a5b18ad9961 (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
#!/usr/bin/env php8.1
<?php

function gethash(string $path): string {
    return substr(sha1(file_get_contents($path)), 0, 8);
}

function sassc(string $src_dir, string $dst_dir, string $file): int {
    $cmd = 'sassc -t expanded '.escapeshellarg($src_dir.'/'.$file).' '.escapeshellarg($dst_dir.'/'.preg_replace('/\.scss$/', '.css', $file));
    exec($cmd, $output, $code);
    return $code;
}

require __DIR__.'/init.php';
global $config;

function build_static(): void {
    $css_dir = ROOT.'/htdocs/css';
    $hashes = [];

    if (!file_exists($css_dir))
        mkdir($css_dir);

    $files = ['common-bundle.scss', 'admin.scss'];
    foreach ($files as $file) {
        if (sassc(ROOT.'/htdocs/scss', $css_dir, $file) != 0)
            fwrite(STDERR, "error: could not compile $file\n");
    }

    foreach (['css', 'js'] as $type) {
        $reldir = ROOT.'/htdocs/';
        $files = glob_recursive($reldir.$type.'/*.'.$type);
        if (empty($files)) {
            continue;
        }
        foreach ($files as $file) {
            $name = preg_replace('/^'.preg_quote($reldir, '/').'/', '', $file);
            $hashes[$name] = gethash($file);
        }
    }

    $scfg = "<?php\n\n";
    $scfg .= "return ".var_export($hashes, true).";\n";

    file_put_contents(ROOT.'/config-static.php', $scfg);
}

build_static();