setup-sdl: Cache ninja with cache instead of tool-cache

From 8543b222d12842c951cd28c67cdac848ee10c431 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Mon, 19 Jun 2023 18:14:07 +0200
Subject: [PATCH] Cache ninja with cache instead of tool-cache

---
 packed/index.js     | 32 ++++++++++++++++++--------------
 src/ninja.ts        | 29 ++++++++++++++++-------------
 src/version.test.ts |  1 -
 3 files changed, 34 insertions(+), 28 deletions(-)

diff --git a/packed/index.js b/packed/index.js
index 92db0cc..cacc72d 100644
--- a/packed/index.js
+++ b/packed/index.js
@@ -570,6 +570,7 @@ exports.configure_ninja_build_tool = exports.get_ninja_download_url = void 0;
 var fs = __importStar(__nccwpck_require__(7147));
 var path = __importStar(__nccwpck_require__(1017));
 var process = __importStar(__nccwpck_require__(7282));
+var cache = __importStar(__nccwpck_require__(7799));
 var core = __importStar(__nccwpck_require__(2186));
 var tc = __importStar(__nccwpck_require__(7784));
 var constants_1 = __nccwpck_require__(7077);
@@ -592,30 +593,33 @@ function get_ninja_download_url(platform, version) {
 exports.get_ninja_download_url = get_ninja_download_url;
 function configure_ninja_build_tool(platform) {
     return __awaiter(this, void 0, void 0, function () {
-        var ninja_dir, cache_name, ninja_directory, ninja_url, ninja_zip_path, ninja_extract_folder;
+        var ninja_directory, CACHE_KEY, CACHE_PATHS, found_cache_key, ninja_url, ninja_zip_path;
         return __generator(this, function (_a) {
             switch (_a.label) {
                 case 0:
-                    ninja_dir = "".concat((0, platform_1.get_platform_root_directory)(platform), "/ninja");
-                    fs.mkdirSync(ninja_dir, { recursive: true });
-                    cache_name = "sdl-".concat(platform);
-                    ninja_directory = tc.find(cache_name, constants_1.NINJA_VERSION);
-                    if (!!ninja_directory) return [3 /*break*/, 4];
-                    core.info("Could not find ninja ".concat(constants_1.NINJA_VERSION, " in the cache."));
+                    ninja_directory = "".concat((0, platform_1.get_platform_root_directory)(platform), "/ninja");
+                    fs.mkdirSync(ninja_directory, { recursive: true });
+                    CACHE_KEY = "ninja-".concat(platform, "-").concat(constants_1.NINJA_VERSION);
+                    CACHE_PATHS = [ninja_directory];
+                    return [4 /*yield*/, cache.restoreCache(CACHE_PATHS.slice(), CACHE_KEY, ["ninja-{platform}"])];
+                case 1:
+                    found_cache_key = _a.sent();
+                    if (!!found_cache_key) return [3 /*break*/, 5];
+                    core.info("Could not find ninja in the cache.");
                     ninja_url = get_ninja_download_url(platform, constants_1.NINJA_VERSION);
                     core.info("Downloading ".concat(ninja_url, "."));
                     return [4 /*yield*/, tc.downloadTool(ninja_url)];
-                case 1:
+                case 2:
                     ninja_zip_path = _a.sent();
                     core.info("Extracting ".concat(ninja_zip_path, "."));
-                    return [4 /*yield*/, tc.extractZip(ninja_zip_path, ninja_dir)];
-                case 2:
-                    ninja_extract_folder = _a.sent();
-                    return [4 /*yield*/, tc.cacheDir(ninja_extract_folder, cache_name, constants_1.NINJA_VERSION)];
+                    return [4 /*yield*/, tc.extractZip(ninja_zip_path, ninja_directory)];
                 case 3:
-                    ninja_directory = _a.sent();
-                    _a.label = 4;
+                    _a.sent();
+                    return [4 /*yield*/, cache.saveCache(CACHE_PATHS.slice(), CACHE_KEY)];
                 case 4:
+                    _a.sent();
+                    _a.label = 5;
+                case 5:
                     process.env.PATH = ninja_directory + path.delimiter + process.env.PATH;
                     return [2 /*return*/];
             }
diff --git a/src/ninja.ts b/src/ninja.ts
index 1b76275..8bf86ce 100644
--- a/src/ninja.ts
+++ b/src/ninja.ts
@@ -2,6 +2,7 @@ import * as fs from "fs";
 import * as path from "path";
 import * as process from "process";
 
+import * as cache from "@actions/cache";
 import * as core from "@actions/core";
 import * as tc from "@actions/tool-cache";
 
@@ -28,26 +29,28 @@ export function get_ninja_download_url(
 }
 
 export async function configure_ninja_build_tool(platform: SdlBuildPlatform) {
-  const ninja_dir = `${get_platform_root_directory(platform)}/ninja`;
-  fs.mkdirSync(ninja_dir, { recursive: true });
+  const ninja_directory = `${get_platform_root_directory(platform)}/ninja`;
+  fs.mkdirSync(ninja_directory, { recursive: true });
 
-  const cache_name = `sdl-${platform}`;
+  const CACHE_KEY = `ninja-${platform}-${NINJA_VERSION}`;
+  const CACHE_PATHS = [ninja_directory];
+  // Pass a copy of CACHE_PATHS since cache.restoreCache modifies/modified its arguments
+  const found_cache_key = await cache.restoreCache(
+    CACHE_PATHS.slice(),
+    CACHE_KEY,
+    [`ninja-{platform}`]
+  );
 
-  let ninja_directory = tc.find(cache_name, NINJA_VERSION);
-
-  if (!ninja_directory) {
-    core.info(`Could not find ninja ${NINJA_VERSION} in the cache.`);
+  if (!found_cache_key) {
+    core.info(`Could not find ninja in the cache.`);
 
     const ninja_url = get_ninja_download_url(platform, NINJA_VERSION);
     core.info(`Downloading ${ninja_url}.`);
     const ninja_zip_path = await tc.downloadTool(ninja_url);
     core.info(`Extracting ${ninja_zip_path}.`);
-    const ninja_extract_folder = await tc.extractZip(ninja_zip_path, ninja_dir);
-    ninja_directory = await tc.cacheDir(
-      ninja_extract_folder,
-      cache_name,
-      NINJA_VERSION
-    );
+    await tc.extractZip(ninja_zip_path, ninja_directory);
+
+    await cache.saveCache(CACHE_PATHS.slice(), CACHE_KEY);
   }
 
   process.env.PATH = ninja_directory + path.delimiter + process.env.PATH;
diff --git a/src/version.test.ts b/src/version.test.ts
index ca259db..9a55536 100644
--- a/src/version.test.ts
+++ b/src/version.test.ts
@@ -103,7 +103,6 @@ describe("test finding a release", () => {
   });
 
   test(`test finding latest non-prerelease 2 release`, () => {
-    console.log(SdlRelease.get_releases());
     const v = new SdlVersion({ major: 2, minor: 0, patch: 0 });
     const rel = SdlRelease.find_release(v, false, SdlReleaseType.Latest);
     expect(rel).not.toBeNull();