SDL: Don't do 'git fetch' when we have a cache hit

From 0f04e1ac64b9bc272cfd40f7ee3fffed173153b8 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sun, 18 Jun 2023 20:41:49 +0200
Subject: [PATCH] Don't do 'git fetch' when we have a cache hit

---
 packed/index.js | 42 ++++++++++++++++++++++++------------------
 src/main.ts     | 17 +++++++++--------
 src/version.ts  | 49 ++++++++++++++++++++++++++++++++++++-------------
 3 files changed, 69 insertions(+), 39 deletions(-)

diff --git a/packed/index.js b/packed/index.js
index 35b73b319fdf..a8968611ab73 100644
--- a/packed/index.js
+++ b/packed/index.js
@@ -244,18 +244,16 @@ async function run() {
         shell: SHELL,
     });
     core.info(`setup-sdl state = ${STATE_HASH}`);
-    const SOURCE_DIR = `${SETUP_SDL_ROOT}/${STATE_HASH}/source`;
-    const BUILD_DIR = `${SETUP_SDL_ROOT}/${STATE_HASH}/build`;
     const PACKAGE_DIR = `${SETUP_SDL_ROOT}/${STATE_HASH}/package`;
-    await checkout_sdl_git_hash(GIT_HASH, SOURCE_DIR);
-    const SDL_VERSION = version_1.SdlVersion.detect_sdl_version_from_source_tree(SOURCE_DIR);
-    core.info(`SDL version is ${SDL_VERSION.toString()}`);
     const CACHE_KEY = `setup-sdl-${STATE_HASH}`;
     const CACHE_PATHS = [PACKAGE_DIR];
     // 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);
     if (!found_cache_key) {
         core.info("No match found in cache. Building SDL from scratch.");
+        const SOURCE_DIR = `${SETUP_SDL_ROOT}/${STATE_HASH}/source`;
+        const BUILD_DIR = `${SETUP_SDL_ROOT}/${STATE_HASH}/build`;
+        await checkout_sdl_git_hash(GIT_HASH, SOURCE_DIR);
         if (USE_NINJA) {
             await core.group(`Configuring Ninja`, async () => {
                 await (0, ninja_1.configure_ninja_build_tool)(SDL_BUILD_PLATFORM);
@@ -277,6 +275,8 @@ async function run() {
         // Pass a copy of CACHE_PATHS since cache.saveCache modifies/modified its arguments
         await cache.saveCache(CACHE_PATHS.slice(), CACHE_KEY);
     }
+    const SDL_VERSION = version_1.SdlVersion.detect_sdl_version_from_install_prefix(PACKAGE_DIR);
+    core.info(`SDL version is ${SDL_VERSION.toString()}`);
     core.exportVariable(`SDL${SDL_VERSION.major}_ROOT`, PACKAGE_DIR);
     core.setOutput("prefix", PACKAGE_DIR);
     core.setOutput("version", SDL_VERSION.toString());
@@ -537,22 +537,28 @@ class SdlVersion {
         return `${this.major}.${this.minor}.${this.patch}`;
     }
     static detect_sdl_version_from_source_tree(path) {
-        let SDL_version_h_path = null;
-        if (SDL_version_h_path == null) {
-            const sdl3_SDL_version_h_path = `${path}/include/SDL3/SDL_version.h`;
-            if (fs.existsSync(sdl3_SDL_version_h_path)) {
-                SDL_version_h_path = sdl3_SDL_version_h_path;
-            }
+        const sdl3_SDL_version_h_path = `${path}/include/SDL3/SDL_version.h`;
+        if (fs.existsSync(sdl3_SDL_version_h_path)) {
+            return this.extract_sdl_version_from_SDL_version_h(sdl3_SDL_version_h_path);
         }
-        if (SDL_version_h_path == null) {
-            const sdl3_SDL_version_h_path = `${path}/include/SDL_version.h`;
-            if (fs.existsSync(sdl3_SDL_version_h_path)) {
-                SDL_version_h_path = sdl3_SDL_version_h_path;
-            }
+        const sdl2_SDL_version_h_path = `${path}/include/SDL_version.h`;
+        if (fs.existsSync(sdl2_SDL_version_h_path)) {
+            return this.extract_sdl_version_from_SDL_version_h(sdl2_SDL_version_h_path);
         }
-        if (SDL_version_h_path == null) {
-            throw new util_1.SetupSdlError("Could not determine version of SDL source tree");
+        throw new util_1.SetupSdlError(`Could not find a SDL_version.h in the source tree (${path})`);
+    }
+    static detect_sdl_version_from_install_prefix(path) {
+        const sdl3_SDL_version_h_path = `${path}/include/SDL3/SDL_version.h`;
+        if (fs.existsSync(sdl3_SDL_version_h_path)) {
+            return this.extract_sdl_version_from_SDL_version_h(sdl3_SDL_version_h_path);
+        }
+        const sdl2_SDL_version_h_path = `${path}/include/SDL2/SDL_version.h`;
+        if (fs.existsSync(sdl2_SDL_version_h_path)) {
+            return this.extract_sdl_version_from_SDL_version_h(sdl2_SDL_version_h_path);
         }
+        throw new util_1.SetupSdlError(`Could not find a SDL_version.h in the prefix (${path})`);
+    }
+    static extract_sdl_version_from_SDL_version_h(SDL_version_h_path) {
         const SDL_version_h = fs.readFileSync(SDL_version_h_path, "utf8");
         const match_major = SDL_version_h.match(/#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)/);
         if (!match_major) {
diff --git a/src/main.ts b/src/main.ts
index 5d46c5a7a4e6..1109e1154bc7 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -235,16 +235,8 @@ async function run() {
   });
   core.info(`setup-sdl state = ${STATE_HASH}`);
 
-  const SOURCE_DIR = `${SETUP_SDL_ROOT}/${STATE_HASH}/source`;
-  const BUILD_DIR = `${SETUP_SDL_ROOT}/${STATE_HASH}/build`;
   const PACKAGE_DIR = `${SETUP_SDL_ROOT}/${STATE_HASH}/package`;
 
-  await checkout_sdl_git_hash(GIT_HASH, SOURCE_DIR);
-
-  const SDL_VERSION =
-    SdlVersion.detect_sdl_version_from_source_tree(SOURCE_DIR);
-  core.info(`SDL version is ${SDL_VERSION.toString()}`);
-
   const CACHE_KEY = `setup-sdl-${STATE_HASH}`;
   const CACHE_PATHS = [PACKAGE_DIR];
   // Pass a copy of CACHE_PATHS since cache.restoreCache modifies/modified its arguments
@@ -256,6 +248,11 @@ async function run() {
   if (!found_cache_key) {
     core.info("No match found in cache. Building SDL from scratch.");
 
+    const SOURCE_DIR = `${SETUP_SDL_ROOT}/${STATE_HASH}/source`;
+    const BUILD_DIR = `${SETUP_SDL_ROOT}/${STATE_HASH}/build`;
+
+    await checkout_sdl_git_hash(GIT_HASH, SOURCE_DIR);
+
     if (USE_NINJA) {
       await core.group(`Configuring Ninja`, async () => {
         await configure_ninja_build_tool(SDL_BUILD_PLATFORM);
@@ -281,6 +278,10 @@ async function run() {
     await cache.saveCache(CACHE_PATHS.slice(), CACHE_KEY);
   }
 
+  const SDL_VERSION =
+    SdlVersion.detect_sdl_version_from_install_prefix(PACKAGE_DIR);
+  core.info(`SDL version is ${SDL_VERSION.toString()}`);
+
   core.exportVariable(`SDL${SDL_VERSION.major}_ROOT`, PACKAGE_DIR);
   core.setOutput("prefix", PACKAGE_DIR);
   core.setOutput("version", SDL_VERSION.toString());
diff --git a/src/version.ts b/src/version.ts
index 18d34e5f24d9..deb42208d7eb 100644
--- a/src/version.ts
+++ b/src/version.ts
@@ -74,25 +74,48 @@ export class SdlVersion {
   }
 
   static detect_sdl_version_from_source_tree(path: string): SdlVersion {
-    let SDL_version_h_path: string | null = null;
+    const sdl3_SDL_version_h_path = `${path}/include/SDL3/SDL_version.h`;
+    if (fs.existsSync(sdl3_SDL_version_h_path)) {
+      return this.extract_sdl_version_from_SDL_version_h(
+        sdl3_SDL_version_h_path
+      );
+    }
 
-    if (SDL_version_h_path == null) {
-      const sdl3_SDL_version_h_path = `${path}/include/SDL3/SDL_version.h`;
-      if (fs.existsSync(sdl3_SDL_version_h_path)) {
-        SDL_version_h_path = sdl3_SDL_version_h_path;
-      }
+    const sdl2_SDL_version_h_path = `${path}/include/SDL_version.h`;
+    if (fs.existsSync(sdl2_SDL_version_h_path)) {
+      return this.extract_sdl_version_from_SDL_version_h(
+        sdl2_SDL_version_h_path
+      );
     }
 
-    if (SDL_version_h_path == null) {
-      const sdl3_SDL_version_h_path = `${path}/include/SDL_version.h`;
-      if (fs.existsSync(sdl3_SDL_version_h_path)) {
-        SDL_version_h_path = sdl3_SDL_version_h_path;
-      }
+    throw new SetupSdlError(
+      `Could not find a SDL_version.h in the source tree (${path})`
+    );
+  }
+
+  static detect_sdl_version_from_install_prefix(path: string): SdlVersion {
+    const sdl3_SDL_version_h_path = `${path}/include/SDL3/SDL_version.h`;
+    if (fs.existsSync(sdl3_SDL_version_h_path)) {
+      return this.extract_sdl_version_from_SDL_version_h(
+        sdl3_SDL_version_h_path
+      );
     }
-    if (SDL_version_h_path == null) {
-      throw new SetupSdlError("Could not determine version of SDL source tree");
+
+    const sdl2_SDL_version_h_path = `${path}/include/SDL2/SDL_version.h`;
+    if (fs.existsSync(sdl2_SDL_version_h_path)) {
+      return this.extract_sdl_version_from_SDL_version_h(
+        sdl2_SDL_version_h_path
+      );
     }
 
+    throw new SetupSdlError(
+      `Could not find a SDL_version.h in the prefix (${path})`
+    );
+  }
+
+  static extract_sdl_version_from_SDL_version_h(
+    SDL_version_h_path: string
+  ): SdlVersion {
     const SDL_version_h = fs.readFileSync(SDL_version_h_path, "utf8");
 
     const match_major = SDL_version_h.match(