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

Add parsing Swift version from file #665

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 3 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ author: Frederik Wallner
inputs:
swift-version:
description: Swift version to configure
required: true
default: '5.9.2'
swift-version-file:
description: File containing the Swift version
default: ".swift-version"
outputs:
version:
description: The full Swift version that was configured
Expand Down
52 changes: 52 additions & 0 deletions src/get-requested-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as core from "@actions/core";

import { readFile } from "fs";
import { join } from "path";

const semverRegex = /^((?:\d+)(?:\.\d+){0,2})$/;

async function readVersionFromFile(filePath: string) {
let version = "";

readFile(filePath, "utf8", (err, data) => {
if (err != null) throw err;

const parsedVersion = data.match(semverRegex)?.at(1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a semver package that's already used in the project that you can use to validate the string 🙂


if (parsedVersion == null)
throw new Error(
`Version provided at ${filePath} is in an unexpected format. Please refer to README.md when providing versions.`
);

version = parsedVersion;
});

return version;
}

export async function getRequestedVersion(): Promise<string> {
const swiftVersion = core.getInput("swift-version");
const swiftVersionFile = core.getInput("swift-version-file");

if (swiftVersion !== "" && swiftVersionFile !== "")
core.warning(
"Both swift-version as swift-version-file are set. Continuing with swift-version."
);

if (swiftVersion) {
return swiftVersion;
}

if (swiftVersionFile) {
const versionFilePath = join(
process.env.GITHUB_WORKSPACE!,
swiftVersionFile
);

return readVersionFromFile(versionFilePath);
}

throw new Error(
"Please set swift-version or swift-version-file in the action's configuration."
);
}
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { EOL } from "os";
import * as core from "@actions/core";

import * as system from "./os";
import * as versions from "./swift-versions";
import * as macos from "./macos-install";
import * as linux from "./linux-install";
import * as windows from "./windows-install";
import { getVersion } from "./get-version";
import { getRequestedVersion } from "./get-requested-version";

async function run() {
try {
const requestedVersion = core.getInput("swift-version", { required: true });
const requestedVersion = await getRequestedVersion();

let platform = await system.getSystem();
let version = versions.verify(requestedVersion, platform);
Expand Down