Script to generate apps cache file
[gamestick-pjgsapi.git] / bin / generate-apps-cache.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * Generate cache file with information about apps
5  *
6  * Needed by:
7  * - www/api/rest/connect/stick/stick/generate.php
8  */
9 require_once __DIR__ . '/functions.php';
10 $cacheDir = dirname(__FILE__, 2) . '/cache/';
11 if (!is_dir($cacheDir)) {
12     if (!mkdir($cacheDir)) {
13         fwrite(STDERR, "Failed to create cache dir: $cacheDir\n");
14         exit(10);
15     }
16 }
17
18 if (!isset($argv[1])) {
19     fwrite(STDERR, "Pass the path to a directory with game data json files\n");
20     exit(1);
21 }
22 $gamesDir = $argv[1];
23 if (!is_dir($gamesDir)) {
24     fwrite(STDERR, 'Given path is not a directory: ' . $gamesDir . "\n");
25     exit(1);
26 }
27
28 $appsCacheFile         = $cacheDir . 'connect-apps.min.json';
29 $featuredAgesCacheFile = $cacheDir . 'connect-featured-ages.min.json';
30
31 $games = loadGames($gamesDir);
32 $connectGames = [];
33 foreach ($games as $gameData) {
34     $connectGames[] = convertGameDataForConnect($gameData);
35 }
36 file_put_contents(
37     $appsCacheFile,
38     json_encode($connectGames, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
39 );
40 ?>