From 203b15ee12f9b6f18de4f6575873d698be313557 Mon Sep 17 00:00:00 2001
From: Egor Tensin <Egor.Tensin@gmail.com>
Date: Sun, 14 Mar 2021 03:42:14 +0300
Subject: make it into a JavaScript action

This is a huge step back IMO, but I needed to be able to restore the
original PATH back as a "post" step.  Currently, composite actions don't
support post-actions, but JavaScript ones do.  I needed this due to a
bug: actions/cache wouldn't find Git's tar on windows-2016 (the one in
System32 would get used on windows-2019) if the PATH was cleaned up.
---
 action.yml   | 35 +++--------------------------------
 clean.js     | 39 +++++++++++++++++++++++++++++++++++++++
 package.json | 22 ++++++++++++++++++++++
 restore.js   | 15 +++++++++++++++
 4 files changed, 79 insertions(+), 32 deletions(-)
 create mode 100644 clean.js
 create mode 100644 package.json
 create mode 100644 restore.js

diff --git a/action.yml b/action.yml
index d83e0c5..8f3388d 100644
--- a/action.yml
+++ b/action.yml
@@ -11,38 +11,9 @@ inputs:
     default: 1
 
 runs:
-  using: composite
-  steps:
-    - run: |
-        New-Variable os -Value ('${{ runner.os }}') -Option Constant
-
-        New-Variable windows_host -Value ($os -eq 'Windows') -Option Constant
-
-        New-Variable dirs -Value ('${{ inputs.dirs }}') -Option Constant
-        New-Variable default -Value ('${{ inputs.default }}' -eq '1') -Option Constant
-
-        if ($windows_host) {
-          $sep = [IO.Path]::PathSeparator
-
-          $new_path = $dirs.Split($sep, [System.StringSplitOptions]::RemoveEmptyEntries)
-
-          if ($default) {
-              # This seems to be the default on new installations.
-              # Also, MSYS2 does this.
-              $new_path += @(
-                  'C:\Windows\system32',
-                  'C:\Windows',
-                  'C:\Windows\System32\Wbem',
-                  'C:\Windows\System32\WindowsPowerShell\v1.0\'
-              )
-          }
-
-          $new_path = $new_path -join $sep
-          echo "PATH=$new_path" >> $env:GITHUB_ENV
-        } else {
-          echo "::warning ::Not going to clean up PATH variable on $os"
-        }
-      shell: pwsh
+  using: node12
+  main: clean.js
+  post: restore.js
 
 branding:
   icon: star
diff --git a/clean.js b/clean.js
new file mode 100644
index 0000000..22dfdff
--- /dev/null
+++ b/clean.js
@@ -0,0 +1,39 @@
+const os = require('os');
+const path = require('path');
+const process = require('process');
+
+const core = require('@actions/core');
+
+try {
+    if (os.platform != 'win32') {
+        core.warning('Not going to clean up PATH variable on ${os.platform}');
+        process.exit();
+    }
+
+    let custom_paths = core.getInput('dirs');
+    custom_paths = custom_paths.split(path.delimiter).filter(function(p) {
+        return p.length != 0;
+    });
+
+    // This seems to be the default on new installations.
+    // Also, MSYS2 does this.
+    const default_paths = [
+        'C:\\Windows\\system32',
+        'C:\\Windows',
+        'C:\\Windows\\System32\\Wbem',
+        'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\',
+    ];
+
+    const add_default = core.getInput('default') == '1';
+
+    let new_path = custom_paths;
+    if (add_default) {
+        new_path = new_path.concat(default_paths);
+    }
+    new_path = new_path.join(path.delimiter);
+
+    core.exportVariable('ORIG_PATH', process.env.PATH);
+    core.exportVariable('PATH', new_path);
+} catch (error) {
+    core.setFailed(error.message);
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..6aefc52
--- /dev/null
+++ b/package.json
@@ -0,0 +1,22 @@
+{
+  "name": "cleanup-path",
+  "version": "2.0.0",
+  "description": "Clean up PATH on Windows workers",
+  "main": "clean.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/egor-tensin/cleanup-path.git"
+  },
+  "author": "Egor Tensin <Egor.Tensin@gmail.com>",
+  "license": "MIT",
+  "bugs": {
+    "url": "https://github.com/egor-tensin/cleanup-path/issues"
+  },
+  "homepage": "https://github.com/egor-tensin/cleanup-path",
+  "dependencies": {
+    "@actions/core": "^1.2.6"
+  }
+}
diff --git a/restore.js b/restore.js
new file mode 100644
index 0000000..794f6a5
--- /dev/null
+++ b/restore.js
@@ -0,0 +1,15 @@
+const os = require('os');
+const process = require('process');
+
+const core = require('@actions/core');
+
+try {
+    if (os.platform != 'win32') {
+        core.warning('Not going to restore PATH variable on ${os.platform}');
+        process.exit();
+    }
+
+    core.exportVariable('PATH', process.env.ORIG_PATH);
+} catch (error) {
+    core.setFailed(error.message);
+}
-- 
cgit v1.2.3