Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
21 / 21 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
GenerateBuildManifest | |
100.00% |
21 / 21 |
|
100.00% |
6 / 6 |
8 | |
100.00% |
1 / 1 |
handle | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
hashOutputPath | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
hashSourcePath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getManifestPath | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
jsonEncodeOutput | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
setOutput | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Hyde\Framework\Actions\PostBuildTasks; |
6 | |
7 | use Hyde\Hyde; |
8 | use Hyde\Facades\Config; |
9 | use Hyde\Pages\Concerns\HydePage; |
10 | use Hyde\Framework\Features\BuildTasks\PostBuildTask; |
11 | use Illuminate\Console\OutputStyle; |
12 | use Illuminate\Support\Collection; |
13 | |
14 | use function Hyde\unixsum_file; |
15 | use function file_put_contents; |
16 | use function file_exists; |
17 | use function json_encode; |
18 | use function now; |
19 | |
20 | /** |
21 | * The build manifest contains a list of all pages and their source and output paths. |
22 | * |
23 | * While not used by the framework, it's useful for addon services to know which page were built, and when. |
24 | * The hashes are so that the addon services can determine if a page has changed since the last build. |
25 | * |
26 | * The manifest is stored in the `app/storage/framework/cache` directory by default, as some users |
27 | * may not want to commit the manifest file to their repository or their deployed site. |
28 | * However, a great alternate location is in `_site/build-manifest.json`, |
29 | * if you don't mind it the file being publicly accessible. |
30 | */ |
31 | class GenerateBuildManifest extends PostBuildTask |
32 | { |
33 | public static string $message = 'Generating build manifest'; |
34 | |
35 | public function handle(): void |
36 | { |
37 | $pages = new Collection(); |
38 | |
39 | /** @var \Hyde\Pages\Concerns\HydePage $page */ |
40 | foreach (Hyde::pages() as $page) { |
41 | $pages->put($page->getRouteKey(), [ |
42 | 'source_path' => $page->getSourcePath(), |
43 | 'output_path' => $page->getOutputPath(), |
44 | 'source_hash' => $this->hashSourcePath($page), |
45 | 'output_hash' => $this->hashOutputPath($page), |
46 | ]); |
47 | } |
48 | |
49 | file_put_contents($this->getManifestPath(), $this->jsonEncodeOutput($pages)); |
50 | } |
51 | |
52 | protected function hashOutputPath(HydePage $page): ?string |
53 | { |
54 | $path = Hyde::sitePath($page->getOutputPath()); |
55 | |
56 | return file_exists($path) ? unixsum_file($path) : null; |
57 | } |
58 | |
59 | protected function hashSourcePath(HydePage $page): string |
60 | { |
61 | return unixsum_file(Hyde::path($page->getSourcePath())); |
62 | } |
63 | |
64 | protected function getManifestPath(): string |
65 | { |
66 | return Hyde::path(Config::getString( |
67 | 'hyde.build_manifest_path', |
68 | 'app/storage/framework/cache/build-manifest.json' |
69 | )); |
70 | } |
71 | |
72 | protected function jsonEncodeOutput(Collection $pages): string |
73 | { |
74 | return json_encode([ |
75 | 'generated' => now(), |
76 | 'pages' => $pages, |
77 | ], JSON_PRETTY_PRINT); |
78 | } |
79 | |
80 | public function setOutput(OutputStyle $output): void |
81 | { |
82 | // Disable output |
83 | } |
84 | } |