From 4907cdfe90a7aa2c98ef4bc8d5415ef8a38bf079 Mon Sep 17 00:00:00 2001 From: James Hegedus Date: Fri, 17 Mar 2023 13:51:59 +0000 Subject: [PATCH] feat: update asdf repo if it exists (#539) Co-authored-by: Sakamoto, Kazunori --- .github/workflows/build.yml | 6 +---- .github/workflows/test.yml | 30 ++++++++++++++++++++++++ install/main.js | 46 ++++++++++++++++++++++--------------- lefthook.yaml | 2 ++ package.json | 3 ++- plugin-test/main.js | 40 ++++++++++++++++++++------------ plugins-add/main.js | 46 ++++++++++++++++++++++--------------- setup/main.js | 40 ++++++++++++++++++++------------ src/setup/index.ts | 33 ++++++++++++++++++-------- 9 files changed, 164 insertions(+), 82 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fb6cf26..e110237 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -36,8 +36,4 @@ jobs: shell: bash run: | yarn build - if [ "$(git status --porcelain | wc -l)" -gt "0" ]; then - echo "Detected uncommitted changes after build. See status below:" - git diff - exit 1 - fi + yarn build:check_uncommitted_changes diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d3841c1..8fa9ea8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,6 +24,36 @@ jobs: echo PATH="${PATH}" asdf --version + asdf_updates_clone_if_installed: + strategy: + fail-fast: false + matrix: + os: ["macos-latest", "ubuntu-latest"] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - name: "Setup: should clone repo" + uses: ./setup + - run: asdf version + - name: + "Simulate self-hosted runner and remove /home/runner/.asdf/bin/asdf + after setup (:shrug:)" + if: ${{ matrix.os == 'ubuntu-latest' }} + run: | + rm -rf /home/runner/.asdf/bin/asdf + - name: + "Simulate self-hosted runner and remove /Users/runner/.asdf/bin/asdf + after setup (:shrug:)" + if: ${{ matrix.os == 'macos-latest' }} + run: | + rm -rf /Users/runner/.asdf/bin/asdf + - name: "Setup: should update repo" + uses: ./setup + # NB: how is it that the hosted runners get to this point in the code without ~/.asdf/bin/asdf + # the below execution fails because the checkout doesn't seem to fix the rm -rf. + # Comment out for now & trust in the OP of #394 + # - run: asdf version + plugin_is_tested: strategy: fail-fast: false diff --git a/install/main.js b/install/main.js index 4afc292..ae402a0 100644 --- a/install/main.js +++ b/install/main.js @@ -519,7 +519,7 @@ var require_file_command = __commonJS({ }; Object.defineProperty(exports, "__esModule", { value: true }); exports.prepareKeyValueMessage = exports.issueFileCommand = void 0; - var fs2 = __importStar(require("fs")); + var fs3 = __importStar(require("fs")); var os2 = __importStar(require("os")); var uuid_1 = (init_esm_node(), __toCommonJS(esm_node_exports)); var utils_1 = require_utils(); @@ -528,10 +528,10 @@ var require_file_command = __commonJS({ if (!filePath) { throw new Error(`Unable to find environment variable for file command ${command}`); } - if (!fs2.existsSync(filePath)) { + if (!fs3.existsSync(filePath)) { throw new Error(`Missing file at path: ${filePath}`); } - fs2.appendFileSync(filePath, `${utils_1.toCommandValue(message)}${os2.EOL}`, { + fs3.appendFileSync(filePath, `${utils_1.toCommandValue(message)}${os2.EOL}`, { encoding: "utf8" }); } @@ -2269,9 +2269,9 @@ var require_io_util = __commonJS({ var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCmdPath = exports.tryGetExecutablePath = exports.isRooted = exports.isDirectory = exports.exists = exports.IS_WINDOWS = exports.unlink = exports.symlink = exports.stat = exports.rmdir = exports.rename = exports.readlink = exports.readdir = exports.mkdir = exports.lstat = exports.copyFile = exports.chmod = void 0; - var fs2 = __importStar(require("fs")); + var fs3 = __importStar(require("fs")); var path2 = __importStar(require("path")); - _a = fs2.promises, exports.chmod = _a.chmod, exports.copyFile = _a.copyFile, exports.lstat = _a.lstat, exports.mkdir = _a.mkdir, exports.readdir = _a.readdir, exports.readlink = _a.readlink, exports.rename = _a.rename, exports.rmdir = _a.rmdir, exports.stat = _a.stat, exports.symlink = _a.symlink, exports.unlink = _a.unlink; + _a = fs3.promises, exports.chmod = _a.chmod, exports.copyFile = _a.copyFile, exports.lstat = _a.lstat, exports.mkdir = _a.mkdir, exports.readdir = _a.readdir, exports.readlink = _a.readlink, exports.rename = _a.rename, exports.rmdir = _a.rmdir, exports.stat = _a.stat, exports.symlink = _a.symlink, exports.unlink = _a.unlink; exports.IS_WINDOWS = process.platform === "win32"; function exists(fsPath) { return __awaiter(this, void 0, void 0, function* () { @@ -3276,12 +3276,13 @@ var exec5 = __toESM(require_exec()); // src/plugins-add/index.ts var core2 = __toESM(require_core()); var exec3 = __toESM(require_exec()); -var fs = __toESM(require("fs")); +var fs2 = __toESM(require("fs")); // src/setup/index.ts var core = __toESM(require_core()); var exec = __toESM(require_exec()); var io = __toESM(require_io()); +var fs = __toESM(require("fs")); var os = __toESM(require("os")); var path = __toESM(require("path")); async function setupAsdf() { @@ -3298,17 +3299,26 @@ async function setupAsdf() { if (skip) { return; } - core.info(`Cloning asdf into ASDF_DIR: ${asdfDir}`); const branch = core.getInput("asdf_branch", { required: true }); - await exec.exec("git", [ - "clone", - "--depth", - "1", - "--branch", - branch, - "https://github.com/asdf-vm/asdf.git", - asdfDir - ]); + if (fs.existsSync(asdfDir)) { + core.info(`Updating asdf in ASDF_DIR "${asdfDir}" on branch "${branch}"`); + const opts = { cwd: asdfDir }; + await exec.exec("git", ["remote", "set-branches", "origin", branch], opts); + await exec.exec("git", ["fetch", "--depth", "1", "origin", branch], opts); + await exec.exec("git", ["checkout", "-B", branch, "origin"], opts); + } else { + core.info(`Cloning asdf into ASDF_DIR "${asdfDir}" on branch "${branch}"`); + await exec.exec("git", [ + "clone", + "--depth", + "1", + "--branch", + branch, + "--single-branch", + "https://github.com/asdf-vm/asdf.git", + asdfDir + ]); + } } // src/plugins-add/index.ts @@ -3338,11 +3348,11 @@ async function pluginsAdd() { await setupAsdf(); let toolVersions = core2.getInput("tool_versions", { required: false }); if (toolVersions) { - await fs.promises.writeFile(".tool-versions", toolVersions, { + await fs2.promises.writeFile(".tool-versions", toolVersions, { encoding: "utf8" }); } else { - toolVersions = await fs.promises.readFile(".tool-versions", { + toolVersions = await fs2.promises.readFile(".tool-versions", { encoding: "utf8" }); } diff --git a/lefthook.yaml b/lefthook.yaml index 4eb66d9..4760388 100644 --- a/lefthook.yaml +++ b/lefthook.yaml @@ -4,6 +4,8 @@ pre-push: run: yarn run lint format: run: yarn fmt:check + check_uncommitted_build_changes: + run: yarn build:check_uncommitted_changes pre-commit: commands: diff --git a/package.json b/package.json index a6eb906..549dc90 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "fmt:check": "prettier --check .", "typecheck": "tsc", "lint": "eslint \"**/*.ts\" --cache", - "build": "esbuild src/install/main.ts src/plugin-test/main.ts src/plugins-add/main.ts src/setup/main.ts --bundle --outdir=. --target=node16 --platform=node" + "build": "esbuild src/install/main.ts src/plugin-test/main.ts src/plugins-add/main.ts src/setup/main.ts --bundle --outdir=. --target=node16 --platform=node", + "build:check_uncommitted_changes": "bash -c 'if [ $(git status --porcelain | grep --extended-regexp \"src|install|plugin-test|plugins-add\" | wc -l) -gt 0 ]; then printf \"* %s\\n\" \"Detected uncommitted changes after build. See status below:\"; git diff; exit 1; fi'" }, "dependencies": { "@actions/core": "^1.10.0", diff --git a/plugin-test/main.js b/plugin-test/main.js index 618fc32..57cd192 100644 --- a/plugin-test/main.js +++ b/plugin-test/main.js @@ -519,7 +519,7 @@ var require_file_command = __commonJS({ }; Object.defineProperty(exports, "__esModule", { value: true }); exports.prepareKeyValueMessage = exports.issueFileCommand = void 0; - var fs = __importStar(require("fs")); + var fs2 = __importStar(require("fs")); var os2 = __importStar(require("os")); var uuid_1 = (init_esm_node(), __toCommonJS(esm_node_exports)); var utils_1 = require_utils(); @@ -528,10 +528,10 @@ var require_file_command = __commonJS({ if (!filePath) { throw new Error(`Unable to find environment variable for file command ${command}`); } - if (!fs.existsSync(filePath)) { + if (!fs2.existsSync(filePath)) { throw new Error(`Missing file at path: ${filePath}`); } - fs.appendFileSync(filePath, `${utils_1.toCommandValue(message)}${os2.EOL}`, { + fs2.appendFileSync(filePath, `${utils_1.toCommandValue(message)}${os2.EOL}`, { encoding: "utf8" }); } @@ -2269,9 +2269,9 @@ var require_io_util = __commonJS({ var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCmdPath = exports.tryGetExecutablePath = exports.isRooted = exports.isDirectory = exports.exists = exports.IS_WINDOWS = exports.unlink = exports.symlink = exports.stat = exports.rmdir = exports.rename = exports.readlink = exports.readdir = exports.mkdir = exports.lstat = exports.copyFile = exports.chmod = void 0; - var fs = __importStar(require("fs")); + var fs2 = __importStar(require("fs")); var path2 = __importStar(require("path")); - _a = fs.promises, exports.chmod = _a.chmod, exports.copyFile = _a.copyFile, exports.lstat = _a.lstat, exports.mkdir = _a.mkdir, exports.readdir = _a.readdir, exports.readlink = _a.readlink, exports.rename = _a.rename, exports.rmdir = _a.rmdir, exports.stat = _a.stat, exports.symlink = _a.symlink, exports.unlink = _a.unlink; + _a = fs2.promises, exports.chmod = _a.chmod, exports.copyFile = _a.copyFile, exports.lstat = _a.lstat, exports.mkdir = _a.mkdir, exports.readdir = _a.readdir, exports.readlink = _a.readlink, exports.rename = _a.rename, exports.rmdir = _a.rmdir, exports.stat = _a.stat, exports.symlink = _a.symlink, exports.unlink = _a.unlink; exports.IS_WINDOWS = process.platform === "win32"; function exists(fsPath) { return __awaiter(this, void 0, void 0, function* () { @@ -3277,6 +3277,7 @@ var exec3 = __toESM(require_exec()); var core = __toESM(require_core()); var exec = __toESM(require_exec()); var io = __toESM(require_io()); +var fs = __toESM(require("fs")); var os = __toESM(require("os")); var path = __toESM(require("path")); async function setupAsdf() { @@ -3293,17 +3294,26 @@ async function setupAsdf() { if (skip) { return; } - core.info(`Cloning asdf into ASDF_DIR: ${asdfDir}`); const branch = core.getInput("asdf_branch", { required: true }); - await exec.exec("git", [ - "clone", - "--depth", - "1", - "--branch", - branch, - "https://github.com/asdf-vm/asdf.git", - asdfDir - ]); + if (fs.existsSync(asdfDir)) { + core.info(`Updating asdf in ASDF_DIR "${asdfDir}" on branch "${branch}"`); + const opts = { cwd: asdfDir }; + await exec.exec("git", ["remote", "set-branches", "origin", branch], opts); + await exec.exec("git", ["fetch", "--depth", "1", "origin", branch], opts); + await exec.exec("git", ["checkout", "-B", branch, "origin"], opts); + } else { + core.info(`Cloning asdf into ASDF_DIR "${asdfDir}" on branch "${branch}"`); + await exec.exec("git", [ + "clone", + "--depth", + "1", + "--branch", + branch, + "--single-branch", + "https://github.com/asdf-vm/asdf.git", + asdfDir + ]); + } } // src/plugin-test/index.ts diff --git a/plugins-add/main.js b/plugins-add/main.js index e7e4a19..cf045a3 100644 --- a/plugins-add/main.js +++ b/plugins-add/main.js @@ -519,7 +519,7 @@ var require_file_command = __commonJS({ }; Object.defineProperty(exports, "__esModule", { value: true }); exports.prepareKeyValueMessage = exports.issueFileCommand = void 0; - var fs2 = __importStar(require("fs")); + var fs3 = __importStar(require("fs")); var os2 = __importStar(require("os")); var uuid_1 = (init_esm_node(), __toCommonJS(esm_node_exports)); var utils_1 = require_utils(); @@ -528,10 +528,10 @@ var require_file_command = __commonJS({ if (!filePath) { throw new Error(`Unable to find environment variable for file command ${command}`); } - if (!fs2.existsSync(filePath)) { + if (!fs3.existsSync(filePath)) { throw new Error(`Missing file at path: ${filePath}`); } - fs2.appendFileSync(filePath, `${utils_1.toCommandValue(message)}${os2.EOL}`, { + fs3.appendFileSync(filePath, `${utils_1.toCommandValue(message)}${os2.EOL}`, { encoding: "utf8" }); } @@ -2269,9 +2269,9 @@ var require_io_util = __commonJS({ var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCmdPath = exports.tryGetExecutablePath = exports.isRooted = exports.isDirectory = exports.exists = exports.IS_WINDOWS = exports.unlink = exports.symlink = exports.stat = exports.rmdir = exports.rename = exports.readlink = exports.readdir = exports.mkdir = exports.lstat = exports.copyFile = exports.chmod = void 0; - var fs2 = __importStar(require("fs")); + var fs3 = __importStar(require("fs")); var path2 = __importStar(require("path")); - _a = fs2.promises, exports.chmod = _a.chmod, exports.copyFile = _a.copyFile, exports.lstat = _a.lstat, exports.mkdir = _a.mkdir, exports.readdir = _a.readdir, exports.readlink = _a.readlink, exports.rename = _a.rename, exports.rmdir = _a.rmdir, exports.stat = _a.stat, exports.symlink = _a.symlink, exports.unlink = _a.unlink; + _a = fs3.promises, exports.chmod = _a.chmod, exports.copyFile = _a.copyFile, exports.lstat = _a.lstat, exports.mkdir = _a.mkdir, exports.readdir = _a.readdir, exports.readlink = _a.readlink, exports.rename = _a.rename, exports.rmdir = _a.rmdir, exports.stat = _a.stat, exports.symlink = _a.symlink, exports.unlink = _a.unlink; exports.IS_WINDOWS = process.platform === "win32"; function exists(fsPath) { return __awaiter(this, void 0, void 0, function* () { @@ -3272,12 +3272,13 @@ var core3 = __toESM(require_core()); // src/plugins-add/index.ts var core2 = __toESM(require_core()); var exec3 = __toESM(require_exec()); -var fs = __toESM(require("fs")); +var fs2 = __toESM(require("fs")); // src/setup/index.ts var core = __toESM(require_core()); var exec = __toESM(require_exec()); var io = __toESM(require_io()); +var fs = __toESM(require("fs")); var os = __toESM(require("os")); var path = __toESM(require("path")); async function setupAsdf() { @@ -3294,17 +3295,26 @@ async function setupAsdf() { if (skip) { return; } - core.info(`Cloning asdf into ASDF_DIR: ${asdfDir}`); const branch = core.getInput("asdf_branch", { required: true }); - await exec.exec("git", [ - "clone", - "--depth", - "1", - "--branch", - branch, - "https://github.com/asdf-vm/asdf.git", - asdfDir - ]); + if (fs.existsSync(asdfDir)) { + core.info(`Updating asdf in ASDF_DIR "${asdfDir}" on branch "${branch}"`); + const opts = { cwd: asdfDir }; + await exec.exec("git", ["remote", "set-branches", "origin", branch], opts); + await exec.exec("git", ["fetch", "--depth", "1", "origin", branch], opts); + await exec.exec("git", ["checkout", "-B", branch, "origin"], opts); + } else { + core.info(`Cloning asdf into ASDF_DIR "${asdfDir}" on branch "${branch}"`); + await exec.exec("git", [ + "clone", + "--depth", + "1", + "--branch", + branch, + "--single-branch", + "https://github.com/asdf-vm/asdf.git", + asdfDir + ]); + } } // src/plugins-add/index.ts @@ -3334,11 +3344,11 @@ async function pluginsAdd() { await setupAsdf(); let toolVersions = core2.getInput("tool_versions", { required: false }); if (toolVersions) { - await fs.promises.writeFile(".tool-versions", toolVersions, { + await fs2.promises.writeFile(".tool-versions", toolVersions, { encoding: "utf8" }); } else { - toolVersions = await fs.promises.readFile(".tool-versions", { + toolVersions = await fs2.promises.readFile(".tool-versions", { encoding: "utf8" }); } diff --git a/setup/main.js b/setup/main.js index c60d7d6..58ab65b 100644 --- a/setup/main.js +++ b/setup/main.js @@ -519,7 +519,7 @@ var require_file_command = __commonJS({ }; Object.defineProperty(exports, "__esModule", { value: true }); exports.prepareKeyValueMessage = exports.issueFileCommand = void 0; - var fs = __importStar(require("fs")); + var fs2 = __importStar(require("fs")); var os2 = __importStar(require("os")); var uuid_1 = (init_esm_node(), __toCommonJS(esm_node_exports)); var utils_1 = require_utils(); @@ -528,10 +528,10 @@ var require_file_command = __commonJS({ if (!filePath) { throw new Error(`Unable to find environment variable for file command ${command}`); } - if (!fs.existsSync(filePath)) { + if (!fs2.existsSync(filePath)) { throw new Error(`Missing file at path: ${filePath}`); } - fs.appendFileSync(filePath, `${utils_1.toCommandValue(message)}${os2.EOL}`, { + fs2.appendFileSync(filePath, `${utils_1.toCommandValue(message)}${os2.EOL}`, { encoding: "utf8" }); } @@ -2269,9 +2269,9 @@ var require_io_util = __commonJS({ var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCmdPath = exports.tryGetExecutablePath = exports.isRooted = exports.isDirectory = exports.exists = exports.IS_WINDOWS = exports.unlink = exports.symlink = exports.stat = exports.rmdir = exports.rename = exports.readlink = exports.readdir = exports.mkdir = exports.lstat = exports.copyFile = exports.chmod = void 0; - var fs = __importStar(require("fs")); + var fs2 = __importStar(require("fs")); var path2 = __importStar(require("path")); - _a = fs.promises, exports.chmod = _a.chmod, exports.copyFile = _a.copyFile, exports.lstat = _a.lstat, exports.mkdir = _a.mkdir, exports.readdir = _a.readdir, exports.readlink = _a.readlink, exports.rename = _a.rename, exports.rmdir = _a.rmdir, exports.stat = _a.stat, exports.symlink = _a.symlink, exports.unlink = _a.unlink; + _a = fs2.promises, exports.chmod = _a.chmod, exports.copyFile = _a.copyFile, exports.lstat = _a.lstat, exports.mkdir = _a.mkdir, exports.readdir = _a.readdir, exports.readlink = _a.readlink, exports.rename = _a.rename, exports.rmdir = _a.rmdir, exports.stat = _a.stat, exports.symlink = _a.symlink, exports.unlink = _a.unlink; exports.IS_WINDOWS = process.platform === "win32"; function exists(fsPath) { return __awaiter(this, void 0, void 0, function* () { @@ -3273,6 +3273,7 @@ var core2 = __toESM(require_core()); var core = __toESM(require_core()); var exec = __toESM(require_exec()); var io = __toESM(require_io()); +var fs = __toESM(require("fs")); var os = __toESM(require("os")); var path = __toESM(require("path")); async function setupAsdf() { @@ -3289,17 +3290,26 @@ async function setupAsdf() { if (skip) { return; } - core.info(`Cloning asdf into ASDF_DIR: ${asdfDir}`); const branch = core.getInput("asdf_branch", { required: true }); - await exec.exec("git", [ - "clone", - "--depth", - "1", - "--branch", - branch, - "https://github.com/asdf-vm/asdf.git", - asdfDir - ]); + if (fs.existsSync(asdfDir)) { + core.info(`Updating asdf in ASDF_DIR "${asdfDir}" on branch "${branch}"`); + const opts = { cwd: asdfDir }; + await exec.exec("git", ["remote", "set-branches", "origin", branch], opts); + await exec.exec("git", ["fetch", "--depth", "1", "origin", branch], opts); + await exec.exec("git", ["checkout", "-B", branch, "origin"], opts); + } else { + core.info(`Cloning asdf into ASDF_DIR "${asdfDir}" on branch "${branch}"`); + await exec.exec("git", [ + "clone", + "--depth", + "1", + "--branch", + branch, + "--single-branch", + "https://github.com/asdf-vm/asdf.git", + asdfDir + ]); + } } // src/setup/main.ts diff --git a/src/setup/index.ts b/src/setup/index.ts index 3f8bc22..f77d496 100644 --- a/src/setup/index.ts +++ b/src/setup/index.ts @@ -1,6 +1,7 @@ import * as core from "@actions/core"; import * as exec from "@actions/exec"; import * as io from "@actions/io"; +import * as fs from "fs"; import * as os from "os"; import * as path from "path"; @@ -9,24 +10,36 @@ export async function setupAsdf(): Promise { if (asdfPath) { return; } + const asdfDir = path.join(os.homedir(), ".asdf"); core.exportVariable("ASDF_DIR", asdfDir); core.exportVariable("ASDF_DATA_DIR", asdfDir); core.addPath(`${asdfDir}/bin`); core.addPath(`${asdfDir}/shims`); + const skip = core.getBooleanInput("skip_install", { required: true }); if (skip) { return; } - core.info(`Cloning asdf into ASDF_DIR: ${asdfDir}`); + const branch = core.getInput("asdf_branch", { required: true }); - await exec.exec("git", [ - "clone", - "--depth", - "1", - "--branch", - branch, - "https://github.com/asdf-vm/asdf.git", - asdfDir, - ]); + if (fs.existsSync(asdfDir)) { + core.info(`Updating asdf in ASDF_DIR "${asdfDir}" on branch "${branch}"`); + const opts = { cwd: asdfDir }; + await exec.exec("git", ["remote", "set-branches", "origin", branch], opts); + await exec.exec("git", ["fetch", "--depth", "1", "origin", branch], opts); + await exec.exec("git", ["checkout", "-B", branch, "origin"], opts); + } else { + core.info(`Cloning asdf into ASDF_DIR "${asdfDir}" on branch "${branch}"`); + await exec.exec("git", [ + "clone", + "--depth", + "1", + "--branch", + branch, + "--single-branch", + "https://github.com/asdf-vm/asdf.git", + asdfDir, + ]); + } }