Skip to content

Commit 3f61aef

Browse files
committed
Add final completion check
1 parent a3599b1 commit 3f61aef

File tree

6 files changed

+88
-50
lines changed

6 files changed

+88
-50
lines changed

lib/get-version.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
const exec_1 = require("@actions/exec");
13+
function getVersion(command = 'swift', args = ['--version']) {
14+
return __awaiter(this, void 0, void 0, function* () {
15+
let output = '';
16+
let error = '';
17+
const options = {
18+
listeners: {
19+
stdout: (data) => {
20+
output += data.toString();
21+
},
22+
stderr: (data) => {
23+
error += data.toString();
24+
}
25+
}
26+
};
27+
yield exec_1.exec(command, args, options);
28+
if (error) {
29+
throw new Error(error);
30+
}
31+
const match = output.match(/(?<version>[0-9]+\.[0-9+]+(\.[0-9]+)?)/) || { groups: { version: null } };
32+
if (!match.groups || !match.groups.version) {
33+
return null;
34+
}
35+
return match.groups.version;
36+
});
37+
}
38+
exports.getVersion = getVersion;

lib/macos-install.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const io = __importStar(require("@actions/io"));
2222
const path = __importStar(require("path"));
2323
const exec_1 = require("@actions/exec");
2424
const swift_versions_1 = require("./swift-versions");
25+
const get_version_1 = require("./get-version");
2526
function install(version, system) {
2627
return __awaiter(this, void 0, void 0, function* () {
2728
const toolchainName = `swift ${version}`;
@@ -49,27 +50,7 @@ function install(version, system) {
4950
exports.install = install;
5051
function toolchainVersion(requestedVersion) {
5152
return __awaiter(this, void 0, void 0, function* () {
52-
let output = '';
53-
let error = '';
54-
const options = {
55-
listeners: {
56-
stdout: (data) => {
57-
output += data.toString();
58-
},
59-
stderr: (data) => {
60-
error += data.toString();
61-
}
62-
}
63-
};
64-
yield exec_1.exec('xcrun', ['--toolchain', requestedVersion, '--run', 'swift', '--version'], options);
65-
if (error) {
66-
throw new Error(error);
67-
}
68-
const match = output.match(/(?<version>[0-9]+\.[0-9+]+(\.[0-9]+)?)/) || { groups: { version: null } };
69-
if (!match.groups || !match.groups.version) {
70-
return null;
71-
}
72-
return match.groups.version;
53+
return yield get_version_1.getVersion('xcrun', ['--toolchain', requestedVersion, '--run', 'swift', '--version']);
7354
});
7455
}
7556
function download({ url }) {

lib/main.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ const system = __importStar(require("./os"));
2121
const versions = __importStar(require("./swift-versions"));
2222
const macos = __importStar(require("./macos-install"));
2323
const linux = __importStar(require("./linux-install"));
24+
const get_version_1 = require("./get-version");
2425
function run() {
2526
return __awaiter(this, void 0, void 0, function* () {
26-
let version = versions.verify(core.getInput('swift-version', { required: true }));
27+
const requestedVersion = core.getInput('swift-version', { required: true });
28+
let version = versions.verify(requestedVersion);
2729
let platform = yield system.getSystem();
2830
switch (platform.os) {
2931
case system.OS.MacOS:
@@ -33,6 +35,10 @@ function run() {
3335
yield linux.install(version, platform);
3436
break;
3537
}
38+
const current = yield get_version_1.getVersion();
39+
if (current !== requestedVersion) {
40+
core.error('Failed to setup requested swift version');
41+
}
3642
});
3743
}
3844
run();

src/get-version.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { exec } from '@actions/exec'
2+
3+
export async function getVersion(command: string = 'swift', args: string[] = ['--version']) {
4+
let output = ''
5+
let error = ''
6+
7+
const options = {
8+
listeners: {
9+
stdout: (data: Buffer) => {
10+
output += data.toString()
11+
},
12+
stderr: (data: Buffer) => {
13+
error += data.toString()
14+
}
15+
}
16+
}
17+
18+
await exec(command, args, options)
19+
20+
if (error) {
21+
throw new Error(error)
22+
}
23+
24+
const match = output.match(/(?<version>[0-9]+\.[0-9+]+(\.[0-9]+)?)/) || { groups: { version: null } }
25+
26+
if (!match.groups || !match.groups.version) {
27+
return null
28+
}
29+
30+
return match.groups.version
31+
}

src/macos-install.ts

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as path from 'path'
55
import { exec } from '@actions/exec'
66
import { System } from './os'
77
import { swiftPackage, Package } from './swift-versions'
8+
import { getVersion } from './get-version'
89

910
export async function install(version: string, system: System) {
1011

@@ -38,33 +39,7 @@ export async function install(version: string, system: System) {
3839
}
3940

4041
async function toolchainVersion(requestedVersion: string) {
41-
let output = ''
42-
let error = ''
43-
44-
const options = {
45-
listeners: {
46-
stdout: (data: Buffer) => {
47-
output += data.toString()
48-
},
49-
stderr: (data: Buffer) => {
50-
error += data.toString()
51-
}
52-
}
53-
}
54-
55-
await exec('xcrun', ['--toolchain', requestedVersion, '--run', 'swift', '--version'], options)
56-
57-
if (error) {
58-
throw new Error(error)
59-
}
60-
61-
const match = output.match(/(?<version>[0-9]+\.[0-9+]+(\.[0-9]+)?)/) || { groups: { version: null } }
62-
63-
if (!match.groups || !match.groups.version) {
64-
return null
65-
}
66-
67-
return match.groups.version
42+
return await getVersion('xcrun', ['--toolchain', requestedVersion, '--run', 'swift', '--version'])
6843
}
6944

7045
async function download({ url }: Package) {

src/main.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import * as system from './os'
33
import * as versions from './swift-versions'
44
import * as macos from './macos-install'
55
import * as linux from './linux-install'
6+
import { getVersion } from './get-version'
67

78
async function run() {
8-
let version = versions.verify(core.getInput('swift-version', { required: true }))
9+
const requestedVersion = core.getInput('swift-version', { required: true })
10+
let version = versions.verify(requestedVersion)
911

1012
let platform = await system.getSystem()
1113
switch (platform.os) {
@@ -16,6 +18,11 @@ async function run() {
1618
await linux.install(version, platform)
1719
break
1820
}
21+
22+
const current = await getVersion()
23+
if (current !== requestedVersion) {
24+
core.error('Failed to setup requested swift version')
25+
}
1926
}
2027

2128
run();

0 commit comments

Comments
 (0)