From 36a72691bc85109cbb6adc43005062ee047e3551 Mon Sep 17 00:00:00 2001 From: Jonas Boberg Date: Wed, 4 Jan 2023 10:50:01 +0900 Subject: [PATCH] Default xcode version to contents of .xcode-version file at root of workspace. If the file doesn't exist, default to 'latest', as before. --- README.md | 2 + __tests__/xcode-version-file.test.ts | 34 +++++++++++++++ action.yml | 1 - dist/index.js | 65 +++++++++++++++++++++++++++- src/setup-xcode.ts | 13 +++++- src/xcode-version-file.ts | 18 ++++++++ 6 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 __tests__/xcode-version-file.test.ts create mode 100644 src/xcode-version-file.ts diff --git a/README.md b/README.md index 6e7889d..0d1681f 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ The list of all available versions can be found in [runner-images](https://githu - SemVer examples: `10`, `11.4`, `12.0`, `11.7.0`, `^11.7.0` (find more examples in [SemVer cheatsheet](https://devhints.io/semver)) - `-beta` suffix after SemVer will only select among beta releases that GitHub actions has installed - If sets a specific version, wraps it to single quotes in YAML like `'12.0'` to pass it as string because GitHub trimmes trailing `.0` from numbers +- If the `xcode-version` argument is omitted, the version is read from the file `.xcode-version` at the root of the repository +- `xcode-version` defaults to `latest` when no argument or `.xcode-version` file is present. # Usage diff --git a/__tests__/xcode-version-file.test.ts b/__tests__/xcode-version-file.test.ts new file mode 100644 index 0000000..aabad9a --- /dev/null +++ b/__tests__/xcode-version-file.test.ts @@ -0,0 +1,34 @@ +import fs from "fs"; +import os from "os"; +import path from "path"; +import * as xcodeVersionFile from "../src/xcode-version-file"; + +describe("getXcodeVersionFromDotFile", () => { + let tmpDirPath: string + + beforeEach(() => { + tmpDirPath = fs.mkdtempSync(path.join(os.tmpdir(), "test-")); + }); + + afterEach(() => { + fs.rmSync(tmpDirPath, { recursive: true, force: true }); + }); + + it("reads the version from .xcode-version file at root of workspace", () => { + const envMock = { + 'GITHUB_WORKSPACE': tmpDirPath + }; + const version = "1.0"; + fs.writeFileSync(path.join(tmpDirPath, ".xcode-version"), version); + const xcodeVersion = xcodeVersionFile.getXcodeVersionFromDotFile(envMock); + expect(xcodeVersion).toBe(version); + }); + + it("returns undefined if the .xcode-version file does not exist", () => { + const envMock = { + 'GITHUB_WORKSPACE': path.join(tmpDirPath, "non-existing-directory") + }; + const xcodeVersion = xcodeVersionFile.getXcodeVersionFromDotFile(envMock); + expect(xcodeVersion).toBeUndefined(); + }); +}); \ No newline at end of file diff --git a/action.yml b/action.yml index 6e4d474..3779b32 100644 --- a/action.yml +++ b/action.yml @@ -5,7 +5,6 @@ inputs: xcode-version: description: 'Version of Xcode to use' required: false - default: latest runs: using: 'node16' main: 'dist/index.js' diff --git a/dist/index.js b/dist/index.js index 3a35059..43b49a3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -32,12 +32,23 @@ var __importStar = (this && this.__importStar) || function (mod) { Object.defineProperty(exports, "__esModule", ({ value: true })); const core = __importStar(__nccwpck_require__(2186)); const xcode_selector_1 = __nccwpck_require__(8865); +const xcode_version_file_1 = __nccwpck_require__(9972); const run = () => { try { if (process.platform !== "darwin") { throw new Error(`This task is intended only for macOS platform. It can't be run on '${process.platform}' platform`); } - const versionSpec = core.getInput("xcode-version", { required: false }); + const defaultVersionSpec = "latest"; + const versionSpec = (() => { + var _a; + const inputValue = core.getInput("xcode-version", { required: false }); + if (inputValue !== "") { + return inputValue; + } + else { + return (_a = (0, xcode_version_file_1.getXcodeVersionFromDotFile)(process.env)) !== null && _a !== void 0 ? _a : defaultVersionSpec; + } + })(); core.info(`Switching Xcode to version '${versionSpec}'...`); const selector = new xcode_selector_1.XcodeSelector(); if (core.isDebug()) { @@ -233,6 +244,58 @@ const getXcodeVersionInfo = (xcodeRootPath) => { exports.getXcodeVersionInfo = getXcodeVersionInfo; +/***/ }), + +/***/ 9972: +/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { + +"use strict"; + +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.getXcodeVersionFromDotFile = void 0; +const fs = __importStar(__nccwpck_require__(7147)); +const core = __importStar(__nccwpck_require__(2186)); +const path = __importStar(__nccwpck_require__(1017)); +const getXcodeVersionFromDotFile = (env) => { + const githubWorkspace = env.GITHUB_WORKSPACE; + if (!githubWorkspace) { + throw new Error("$GITHUB_WORKSPACE is not set"); + } + const xcodeVersionFilePath = path.join(githubWorkspace, ".xcode-version"); + try { + return fs.readFileSync(xcodeVersionFilePath).toString().trimEnd(); + } + catch (err) { + core.debug("No .xcode-version file in repository root"); + return undefined; + } +}; +exports.getXcodeVersionFromDotFile = getXcodeVersionFromDotFile; + + /***/ }), /***/ 7351: diff --git a/src/setup-xcode.ts b/src/setup-xcode.ts index ca659fe..c0a0d17 100644 --- a/src/setup-xcode.ts +++ b/src/setup-xcode.ts @@ -1,5 +1,6 @@ import * as core from "@actions/core"; import { XcodeSelector } from "./xcode-selector"; +import { getXcodeVersionFromDotFile } from "./xcode-version-file"; const run = (): void => { try { @@ -9,7 +10,17 @@ const run = (): void => { ); } - const versionSpec = core.getInput("xcode-version", { required: false }); + const defaultVersionSpec = "latest"; + + const versionSpec = (() => { + const inputValue = core.getInput("xcode-version", { required: false }); + if (inputValue !== "") { + return inputValue; + } else { + return getXcodeVersionFromDotFile(process.env) ?? defaultVersionSpec; + } + })(); + core.info(`Switching Xcode to version '${versionSpec}'...`); const selector = new XcodeSelector(); diff --git a/src/xcode-version-file.ts b/src/xcode-version-file.ts new file mode 100644 index 0000000..d5308e7 --- /dev/null +++ b/src/xcode-version-file.ts @@ -0,0 +1,18 @@ +import * as fs from "fs"; +import * as core from "@actions/core"; +import * as path from "path"; + +export const getXcodeVersionFromDotFile = (env: NodeJS.ProcessEnv): string | undefined => { + const githubWorkspace = env.GITHUB_WORKSPACE; + if (!githubWorkspace) { + throw new Error("$GITHUB_WORKSPACE is not set"); + } + const xcodeVersionFilePath = path.join(githubWorkspace, ".xcode-version"); + + try { + return fs.readFileSync(xcodeVersionFilePath).toString().trimEnd(); + } catch (err) { + core.debug("No .xcode-version file in repository root"); + return undefined; + } +};