Skip to content

Commit a63db63

Browse files
committed
New set of core functions
1 parent d7fe7b9 commit a63db63

File tree

11 files changed

+91
-152
lines changed

11 files changed

+91
-152
lines changed

__tests__/get-version.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
import { versionFromString } from "../src/get-version";
1+
import { versionFromString } from "../src/swift";
22

33
describe("version lookup", () => {
44
it("identifies version from swift version", async () => {
55
const version = versionFromString(
6-
"Apple Swift version 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57)"
6+
"Apple Swift version 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57)",
77
);
88
expect(version).toBe("5.4.2");
99
});
1010

1111
it("identifies version from swift version with target", async () => {
1212
const version = versionFromString(
1313
`Apple Swift version 5.5 (swiftlang-1300.0.31.1 clang-1300.0.29.1)
14-
Target: x86_64-apple-macosx11.0`
14+
Target: x86_64-apple-macosx11.0`,
1515
);
1616
expect(version).toBe("5.5");
1717
});
1818

1919
it("identifies version from swift-driver version", async () => {
2020
const version = versionFromString(
21-
"swift-driver version: 1.26.9 Apple Swift version 5.5 (swiftlang-1300.0.31.1 clang-1300.0.29.1)"
21+
"swift-driver version: 1.26.9 Apple Swift version 5.5 (swiftlang-1300.0.31.1 clang-1300.0.29.1)",
2222
);
2323
expect(version).toBe("5.5");
2424
});
2525

2626
it("identifies version from swift version on linux", async () => {
2727
const version = versionFromString(
28-
"Swift version 5.5.1 (swift-5.5.1-RELEASE)"
28+
"Swift version 5.5.1 (swift-5.5.1-RELEASE)",
2929
);
3030
expect(version).toBe("5.5.1");
3131
});

__tests__/gpg.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as exec from "@actions/exec";
2-
import { setupKeys, verify, refreshKeys } from "../src/gpg";
2+
import { refreshKeys } from "../src/core";
33

44
jest.mock("@actions/exec");
55

@@ -58,7 +58,7 @@ describe("gpg", () => {
5858
await refreshKeys();
5959
} catch (e) {
6060
expect(e).toEqual(
61-
new Error("Failed to refresh keys from any server in the pool.")
61+
new Error("Failed to refresh keys from any server in the pool."),
6262
);
6363
}
6464
});

__tests__/os.test.ts

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as os from "../src/os";
1+
import { getOS } from "../src/core";
22

33
jest.mock("getos");
44

@@ -8,45 +8,17 @@ describe("os resolver", () => {
88
it("finds matching system and version", async () => {
99
setSystem({ os: "linux", dist: "Ubuntu", release: "22.04" });
1010

11-
let ubuntu = await os.getSystem();
12-
expect(ubuntu.os).toBe(os.OS.Ubuntu);
13-
expect(ubuntu.version).toBe("22.04");
14-
expect(ubuntu.name).toBe("Ubuntu");
11+
let ubuntu = await getOS();
12+
expect(ubuntu).toBe("linux");
1513

1614
setSystem({ os: "darwin", dist: "macOS", release: "latest" });
1715

18-
let mac = await os.getSystem();
19-
expect(mac.os).toBe(os.OS.MacOS);
20-
expect(mac.version).toBe("latest");
21-
expect(mac.name).toBe("macOS");
16+
let mac = await getOS();
17+
expect(mac).toBe("darwin");
2218

2319
setSystem({ os: "win32", dist: "Windows", release: "latest" });
2420

25-
let windows = await os.getSystem();
26-
expect(windows.os).toBe(os.OS.Windows);
27-
expect(windows.version).toBe("latest");
28-
expect(windows.name).toBe("Windows");
29-
});
30-
31-
it("throws an error if the os is not supported", async () => {
32-
setSystem({ os: "windows", dist: "Microsoft Windows 10", release: "10.0" });
33-
expect.assertions(1);
34-
try {
35-
await os.getSystem();
36-
} catch (e) {
37-
expect(e).toEqual(new Error('"windows" is not a supported platform'));
38-
}
39-
});
40-
41-
it("throws an error if the version is not supported", async () => {
42-
setSystem({ os: "linux", dist: "Ubuntu", release: "15.04" });
43-
expect.assertions(1);
44-
try {
45-
await os.getSystem();
46-
} catch (e) {
47-
expect(e).toEqual(
48-
new Error('Version "15.04" of Ubuntu is not supported')
49-
);
50-
}
21+
let windows = await getOS();
22+
expect(windows).toBe("win32");
5123
});
5224
});

src/core/cmd.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { debug } from "@actions/core";
2+
import { exec } from "@actions/exec";
3+
4+
/**
5+
* Runs a command and returns the output
6+
* @param command Command to run
7+
* @param args Arguments to pass to the command
8+
* @returns Output of the command
9+
*/
10+
export async function cmd(command: string, ...args: string[]) {
11+
let output = "";
12+
let error = "";
13+
14+
const options = {
15+
listeners: {
16+
stdout: (data: Buffer) => {
17+
output += data.toString();
18+
},
19+
stderr: (data: Buffer) => {
20+
error += data.toString();
21+
},
22+
},
23+
};
24+
25+
debug(`Running command: ${command} ${args.join(" ")}`);
26+
27+
await exec(command, args, options);
28+
29+
if (!output && error) {
30+
throw new Error("Error running command " + error);
31+
}
32+
33+
debug(`Command output: ${output}`);
34+
35+
return output;
36+
}

src/gpg.ts renamed to src/core/gpg.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as toolCache from "@actions/tool-cache";
55
export async function setupKeys() {
66
core.debug("Fetching verification keys");
77
let path = await toolCache.downloadTool(
8-
"https://swift.org/keys/all-keys.asc"
8+
"https://swift.org/keys/all-keys.asc",
99
);
1010

1111
core.debug("Importing verification keys");
@@ -47,7 +47,7 @@ function refreshKeysFromServer(server: string): Promise<boolean> {
4747
.then((code) => code === 0)
4848
.catch((error) => {
4949
core.warning(
50-
`An error occurred when trying to refresh keys from ${server}: ${error}`
50+
`An error occurred when trying to refresh keys from ${server}: ${error}`,
5151
);
5252
return false;
5353
});

src/core/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./cmd";
2+
export * from "./gpg";
3+
export * from "./os";

src/core/os.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import getos from "getos";
2+
3+
export async function getOS() {
4+
const detectedSystem = await new Promise<getos.Os>((resolve, reject) => {
5+
getos((error, os) => {
6+
os ? resolve(os) : reject(error || "No OS detected");
7+
});
8+
});
9+
10+
return detectedSystem.os;
11+
}

src/get-version.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/os.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/swift/current-version.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { cmd } from "../core";
2+
3+
/**
4+
* Get the current swift version
5+
* @returns Current swift version
6+
*/
7+
export async function currentVersion() {
8+
const output = await cmd("swift", "--version");
9+
return versionFromString(output);
10+
}
11+
12+
export function versionFromString(subject: string): string | null {
13+
const match = subject.match(
14+
/Swift\ version (?<version>[0-9]+\.[0-9+]+(\.[0-9]+)?)/,
15+
) || {
16+
groups: { version: null },
17+
};
18+
19+
if (!match.groups || !match.groups.version) {
20+
return null;
21+
}
22+
23+
return match.groups.version;
24+
}

0 commit comments

Comments
 (0)