Skip to content

Commit

Permalink
Merge pull request #318 from fwal/fwal/fix-version-lookup
Browse files Browse the repository at this point in the history
Improved version lookup
  • Loading branch information
fwal authored Oct 22, 2021
2 parents 6efa2ef + 20880e8 commit 06b289f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
25 changes: 25 additions & 0 deletions __tests__/get-version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { versionFromString } from "../src/get-version";

describe("version lookup", () => {
it("identifies version from swift version", async () => {
const version = versionFromString(
"Apple Swift version 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57)"
);
expect(version).toBe("5.4.2");
});

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

it("identifies version from swift-driver version", async () => {
const version = versionFromString(
"swift-driver version: 1.26.9 Apple Swift version 5.5 (swiftlang-1300.0.31.1 clang-1300.0.29.1)"
);
expect(version).toBe("5.5");
});
});
12 changes: 9 additions & 3 deletions src/get-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ export async function getVersion(

await exec(command, args, options);

if (error) {
throw new Error(error);
if (!output && error) {
throw new Error("Error getting swift version " + error);
}

const match = output.match(/(?<version>[0-9]+\.[0-9+]+(\.[0-9]+)?)/) || {
return versionFromString(output);
}

export function versionFromString(subject: string): string | null {
const match = subject.match(
/Apple\ Swift\ version (?<version>[0-9]+\.[0-9+]+(\.[0-9]+)?)/
) || {
groups: { version: null },
};

Expand Down

0 comments on commit 06b289f

Please sign in to comment.