Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default xcode version to contents of .xcode-version file #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 34 additions & 0 deletions __tests__/xcode-version-file.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
1 change: 0 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ inputs:
xcode-version:
description: 'Version of Xcode to use'
required: false
default: latest
runs:
using: 'node16'
main: 'dist/index.js'
Expand Down
65 changes: 64 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 12 additions & 1 deletion src/setup-xcode.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions src/xcode-version-file.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};