From 1c500a441411bb23b12fcc90ff0824316e3dec78 Mon Sep 17 00:00:00 2001
From: Sergey Dolin <dsame@github.com>
Date: Wed, 12 Jul 2023 00:28:42 +0200
Subject: [PATCH] Improve readability

---
 dist/setup/index.js | 31 ++++++++++++++++++-------------
 src/installer.ts    | 33 +++++++++++++++++++++------------
 2 files changed, 39 insertions(+), 25 deletions(-)

diff --git a/dist/setup/index.js b/dist/setup/index.js
index eef32c0..faf0ecd 100644
--- a/dist/setup/index.js
+++ b/dist/setup/index.js
@@ -61338,7 +61338,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
     return (mod && mod.__esModule) ? mod : { "default": mod };
 };
 Object.defineProperty(exports, "__esModule", ({ value: true }));
-exports.resolveStableVersionInput = exports.parseGoVersionFile = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.getManifest = exports.extractGoArchive = exports.getGo = void 0;
+exports.resolveStableVersionInput = exports.parseGoVersionFile = exports.makeSemver = exports.getVersionsDist = exports.findMatch = exports.getInfoFromManifest = exports.getManifest = exports.extractGoArchive = exports.addExecutablesToCache = exports.getGo = void 0;
 const tc = __importStar(__nccwpck_require__(7784));
 const core = __importStar(__nccwpck_require__(2186));
 const path = __importStar(__nccwpck_require__(1017));
@@ -61449,6 +61449,7 @@ function addExecutablesToCache(extPath, info, arch) {
         return cachedDir;
     });
 }
+exports.addExecutablesToCache = addExecutablesToCache;
 function installGoVersion(info, auth, arch) {
     return __awaiter(this, void 0, void 0, function* () {
         core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`);
@@ -61464,18 +61465,22 @@ function installGoVersion(info, auth, arch) {
             extPath = path.join(extPath, 'go');
         }
         if (isWindows) {
-            const oldCacheDir = process.env['RUNNER_TOOL_CACHE'] || '';
-            const tempCacheDir = oldCacheDir.replace('C:', 'D:').replace('c:', 'd:');
-            process.env['RUNNER_TOOL_CACHE'] = tempCacheDir;
-            const cachedDir = yield addExecutablesToCache(extPath, info, arch);
-            const lnkDest = cachedDir;
-            const lnkSrc = lnkDest.replace(tempCacheDir, oldCacheDir);
-            const lnkSrcDir = path.dirname(lnkSrc);
-            fs_1.default.mkdirSync(lnkSrcDir, { recursive: true });
-            fs_1.default.symlinkSync(lnkDest, lnkSrc, 'junction');
-            core.info(`Created link ${lnkSrc} => ${lnkDest}`);
-            process.env['RUNNER_TOOL_CACHE'] = oldCacheDir;
-            return cachedDir.replace(tempCacheDir, oldCacheDir);
+            const defaultToolCacheRoot = process.env['RUNNER_TOOL_CACHE'] || '';
+            const substitutedToolCacheRoot = defaultToolCacheRoot
+                .replace('C:', 'D:')
+                .replace('c:', 'd:');
+            // make toolcache root to be on drive d:
+            process.env['RUNNER_TOOL_CACHE'] = substitutedToolCacheRoot;
+            const actualToolCacheDir = yield addExecutablesToCache(extPath, info, arch);
+            // create a link from c: to d:
+            const lnkSrc = actualToolCacheDir.replace(substitutedToolCacheRoot, defaultToolCacheRoot);
+            fs_1.default.mkdirSync(path.dirname(lnkSrc), { recursive: true });
+            fs_1.default.symlinkSync(actualToolCacheDir, lnkSrc, 'junction');
+            core.info(`Created link ${lnkSrc} => ${actualToolCacheDir}`);
+            // restore toolcache root to default drive c:
+            process.env['RUNNER_TOOL_CACHE'] = defaultToolCacheRoot;
+            // make outer code to continue using toolcache as if it were installed on c:
+            return lnkSrc;
         }
         return yield addExecutablesToCache(extPath, info, arch);
     });
diff --git a/src/installer.ts b/src/installer.ts
index 537d4c2..8d2eaa7 100644
--- a/src/installer.ts
+++ b/src/installer.ts
@@ -202,20 +202,29 @@ async function installGoVersion(
   }
 
   if (isWindows) {
-    const oldCacheDir = process.env['RUNNER_TOOL_CACHE'] || '';
-    const tempCacheDir = oldCacheDir.replace('C:', 'D:').replace('c:', 'd:');
-    process.env['RUNNER_TOOL_CACHE'] = tempCacheDir;
+    const defaultToolCacheRoot = process.env['RUNNER_TOOL_CACHE'] || '';
+    const substitutedToolCacheRoot = defaultToolCacheRoot
+      .replace('C:', 'D:')
+      .replace('c:', 'd:');
+    // make toolcache root to be on drive d:
+    process.env['RUNNER_TOOL_CACHE'] = substitutedToolCacheRoot;
 
-    const cachedDir = await addExecutablesToCache(extPath, info, arch);
+    const actualToolCacheDir = await addExecutablesToCache(extPath, info, arch);
 
-    const lnkDest = cachedDir;
-    const lnkSrc = lnkDest.replace(tempCacheDir, oldCacheDir);
-    const lnkSrcDir = path.dirname(lnkSrc);
-    fs.mkdirSync(lnkSrcDir, {recursive: true});
-    fs.symlinkSync(lnkDest, lnkSrc, 'junction');
-    core.info(`Created link ${lnkSrc} => ${lnkDest}`);
-    process.env['RUNNER_TOOL_CACHE'] = oldCacheDir;
-    return cachedDir.replace(tempCacheDir, oldCacheDir);
+    // create a link from c: to d:
+    const lnkSrc = actualToolCacheDir.replace(
+      substitutedToolCacheRoot,
+      defaultToolCacheRoot
+    );
+    fs.mkdirSync(path.dirname(lnkSrc), {recursive: true});
+    fs.symlinkSync(actualToolCacheDir, lnkSrc, 'junction');
+    core.info(`Created link ${lnkSrc} => ${actualToolCacheDir}`);
+
+    // restore toolcache root to default drive c:
+    process.env['RUNNER_TOOL_CACHE'] = defaultToolCacheRoot;
+
+    // make outer code to continue using toolcache as if it were installed on c:
+    return lnkSrc;
   }
 
   return await addExecutablesToCache(extPath, info, arch);