Passed
Push — master ( 922213...2aed56 )
by Caen
03:16 queued 12s
created

getCssVersion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
nc 3
nop 1
dl 0
loc 15
c 1
b 0
f 0
cc 3
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
require_once __DIR__ . '/minima.php';
6
7
exit(main(function (): int {
8
    $baseDir = __DIR__.'/../../';
9
10
    if ($this->hasOption('inject-version')) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
11
        $package = json_decode(file_get_contents($baseDir.'package.json'), true);
12
        $version = $package['version'];
13
        $css = file_get_contents($baseDir.'dist/hyde.css');
14
15
        if (str_contains($css, '/*! HydeFront')) {
16
            $this->error('Version already injected in dist/hyde.css');
17
            return 1;
18
        }
19
20
        $template = '/*! HydeFront v{{ $version }} | MIT License | https://hydephp.com*/';
21
        $versionString = str_replace('{{ $version }}', $version, $template);
22
        $css = "$versionString\n$css";
23
        file_put_contents($baseDir.'dist/hyde.css', $css);
24
25
        return 0;
26
    }
27
28
    $this->info('Verifying build files...');
29
    $exitCode = 0;
30
31
    $package = json_decode(file_get_contents($baseDir.'package.json'), true);
32
    $version = $package['version'];
33
    $this->line("Found version '$version' in package.json");
34
35
    $hydeCssVersion = getCssVersion($baseDir.'dist/hyde.css');
36
    $this->line("Found version '$hydeCssVersion' in dist/hyde.css");
37
38
    $appCssVersion = getCssVersion($baseDir.'dist/app.css');
39
    $this->line("Found version '$appCssVersion' in dist/app.css");
40
41
    if ($this->hasOption('fix')) {
42
        $this->info('Fixing build files...');
43
44
        if ($version !== $hydeCssVersion) {
45
            $this->line(' > Updating dist/hyde.css...');
46
            $contents = file_get_contents($baseDir.'dist/hyde.css');
47
            $contents = str_replace($hydeCssVersion, $version, $contents);
48
            file_put_contents($baseDir.'dist/hyde.css', $contents);
49
            $filesChanged = true;
50
        }
51
52
        if ($version !== $appCssVersion) {
53
            $this->line(' > Updating dist/app.css...');
54
            $contents = file_get_contents($baseDir.'dist/app.css');
55
            $contents = str_replace($appCssVersion, $version, $contents);
56
            file_put_contents($baseDir.'dist/app.css', $contents);
57
            $filesChanged = true;
58
        }
59
60
        if (isset($filesChanged)) {
61
            $this->info('Build files fixed');
62
63
            // Run the script again to verify the changes, but without the --fix option
64
            $this->info('Verifying build files again...');
65
            $this->line('---');
66
            passthru('php packages/hydefront/.github/scripts/post-build.php', $verifyExitCode);
67
            return $verifyExitCode;
68
        } else {
69
            $this->warning('Nothing to fix!');
70
            return 0;
71
        }
72
    }
73
74
    if ($version !== $hydeCssVersion) {
75
        $this->error('Version mismatch in package.json and dist/hyde.css:');
76
        $this->warning("Expected hyde.css to have version '$version', but found '$hydeCssVersion'");
77
        $exitCode = 1;
78
    }
79
80
    if ($version !== $appCssVersion) {
81
        $this->error('Version mismatch in package.json and dist/app.css:');
82
        $this->warning("Expected app.css to have version '$version', but found '$appCssVersion'");
83
        $exitCode = 1;
84
    }
85
86
    if ($exitCode > 0) {
87
        $this->error('Exiting with errors.');
88
    } else {
89
        $this->info('Build files verified. All looks good!');
90
    }
91
92
    return $exitCode;
93
}));
94
95
function getCssVersion(string $path): string
96
{
97
    $contents = file_get_contents($path);
98
    $prefix = '/*! HydeFront v';
99
    if (! str_starts_with($contents, $prefix)) {
100
        throw new Exception('Invalid CSS file');
101
    }
102
    $contents = substr($contents, strlen($prefix));
103
    // Get everything before  |
104
    $pipePos = strpos($contents, '|');
105
    if ($pipePos === false) {
106
        throw new Exception('Invalid CSS file');
107
    }
108
    $contents = substr($contents, 0, $pipePos);
109
    return trim($contents);
110
}
111