Skip to content

Commit b8b0c2a

Browse files
authored
feat: added supporting new swift toolchains without action release (#259)
1 parent 840a162 commit b8b0c2a

File tree

16 files changed

+92621
-92982
lines changed

16 files changed

+92621
-92982
lines changed

.github/changelog/pre_commit_hook.js

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,8 @@
11
'use strict'
2-
const path = require('path');
3-
const fs = require('fs').promises;
42
const core = require('@actions/core');
5-
const {exec, getExecOutput} = require('@actions/exec');
6-
const yaml = require('js-yaml');
7-
const semver = require('semver');
8-
const PackageJson = require('@npmcli/package-json');
9-
10-
const SWIFT_ORG_BUILD = path.join('swiftorg', '_data', 'builds');
11-
12-
async function swiftorgCommit() {
13-
const gitOptions = {cwd: 'swiftorg'};
14-
const {stdout} = await getExecOutput('git', ['rev-parse', '--verify', 'HEAD'], gitOptions);
15-
return stdout.trim();
16-
}
17-
18-
async function latestRelease() {
19-
const swiftRelease = path.join(SWIFT_ORG_BUILD, 'swift_releases.yml');
20-
const releaseData = await fs.readFile(swiftRelease, 'utf-8');
21-
const releases = yaml.load(releaseData);
22-
return releases[releases.length - 1];
23-
}
24-
25-
async function latestDevRelease() {
26-
const buildEntries = await fs.readdir(SWIFT_ORG_BUILD, { withFileTypes: true });
27-
const devBranchRegex = /swift-([^-]*)-branch/;
28-
const devDirs = buildEntries.flatMap(entry => {
29-
if (!entry.isDirectory() || !devBranchRegex.exec(entry.name)) {
30-
return [];
31-
}
32-
return entry.name;
33-
}).sort((dir1, dir2) => {
34-
const ver1 = devBranchRegex.exec(dir1)[1].replace('_', '.');
35-
const ver2 = devBranchRegex.exec(dir2)[1].replace('_', '.');
36-
return semver.gt(semver.coerce(ver2), semver.coerce(ver1)) ? 1 : -1;
37-
});
38-
const devVer = devBranchRegex.exec(devDirs[0])[1].replace('_', '.');
39-
const xcodeSnapshot = path.join(SWIFT_ORG_BUILD,devDirs[0], 'xcode.yml');
40-
const devReleaseData = await fs.readFile(xcodeSnapshot, 'utf-8');
41-
const devReleases = yaml.load(devReleaseData);
42-
return { name: devVer, date: devReleases[0].date, tag: devReleases[0].dir };
43-
}
44-
45-
async function latestSnapshot() {
46-
const xcodeSnapshot = path.join(SWIFT_ORG_BUILD, 'development', 'xcode.yml');
47-
const devSnapshotsData = await fs.readFile(xcodeSnapshot, 'utf-8');
48-
const snapshots = yaml.load(devSnapshotsData);
49-
return { date: snapshots[0].date, tag: snapshots[0].dir };
50-
}
3+
const {exec} = require('@actions/exec');
514

525
exports.preCommit = async (props) => {
53-
const commit = await swiftorgCommit();
54-
const release = await latestRelease();
55-
const dev = await latestDevRelease();
56-
const snapshot = await latestSnapshot();
57-
58-
const swiftorg = { commit: commit, release: release, dev: dev, snapshot: snapshot };
59-
const pkgJson = await PackageJson.load('./');
60-
core.info(`Updating swiftorg metadata to "${JSON.stringify(swiftorg)}"`);
61-
pkgJson.update({ swiftorg: swiftorg });
62-
await pkgJson.save();
63-
646
core.startGroup(`Bundling`);
657
await exec('npm install');
668
await exec('npm run package');

.github/dependabot.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ updates:
77
commit-message:
88
prefix: 'ci(Deps): '
99

10-
- package-ecosystem: gitsubmodule
11-
directory: /
12-
schedule:
13-
interval: daily
14-
commit-message:
15-
prefix: 'build(swift-org-website): '
16-
1710
- package-ecosystem: npm
1811
directory: /
1912
schedule:

.github/utils/update_metadata.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
'use strict'
2+
const path = require('path');
3+
const fs = require('fs').promises;
4+
const core = require('@actions/core');
5+
const {exec,getExecOutput} = require('@actions/exec');
6+
const yaml = require('js-yaml');
7+
const semver = require('semver');
8+
const https = require('https');
9+
10+
const SWIFT_ORG = 'swiftorg';
11+
const SWIFT_ORG_BUILD = path.join(SWIFT_ORG, '_data', 'builds');
12+
const SWIFT_ORG_CWD = {cwd: SWIFT_ORG};
13+
14+
async function swiftorgCommit() {
15+
const {stdout} = await getExecOutput('git', ['rev-parse', '--verify', 'HEAD'], SWIFT_ORG_CWD);
16+
return stdout.trim();
17+
}
18+
19+
async function latestRelease() {
20+
const swiftRelease = path.join(SWIFT_ORG_BUILD, 'swift_releases.yml');
21+
const releaseData = await fs.readFile(swiftRelease, 'utf-8');
22+
const releases = yaml.load(releaseData);
23+
return releases[releases.length - 1];
24+
}
25+
26+
async function latestDevRelease() {
27+
const buildEntries = await fs.readdir(SWIFT_ORG_BUILD, { withFileTypes: true });
28+
const devBranchRegex = /swift-([^-]*)-branch/;
29+
const devDirs = buildEntries.flatMap(entry => {
30+
if (!entry.isDirectory() || !devBranchRegex.exec(entry.name)) {
31+
return [];
32+
}
33+
return entry.name;
34+
}).sort((dir1, dir2) => {
35+
const ver1 = devBranchRegex.exec(dir1)[1].replace('_', '.');
36+
const ver2 = devBranchRegex.exec(dir2)[1].replace('_', '.');
37+
return semver.gt(semver.coerce(ver2), semver.coerce(ver1)) ? 1 : -1;
38+
});
39+
const devVer = devBranchRegex.exec(devDirs[0])[1].replace('_', '.');
40+
const xcodeSnapshot = path.join(SWIFT_ORG_BUILD,devDirs[0], 'xcode.yml');
41+
const devReleaseData = await fs.readFile(xcodeSnapshot, 'utf-8');
42+
const devReleases = yaml.load(devReleaseData);
43+
return { name: devVer, date: devReleases[0].date, tag: devReleases[0].dir };
44+
}
45+
46+
async function latestSnapshot() {
47+
const xcodeSnapshot = path.join(SWIFT_ORG_BUILD, 'development', 'xcode.yml');
48+
const devSnapshotsData = await fs.readFile(xcodeSnapshot, 'utf-8');
49+
const snapshots = yaml.load(devSnapshotsData);
50+
return { date: snapshots[0].date, tag: snapshots[0].dir };
51+
}
52+
53+
exports.update = async () => {
54+
const commit = await swiftorgCommit();
55+
const release = await latestRelease();
56+
const dev = await latestDevRelease();
57+
const snapshot = await latestSnapshot();
58+
59+
const swiftorg = { commit: commit, release: release, dev: dev, snapshot: snapshot };
60+
const data = JSON.stringify(swiftorg);
61+
core.info(`Updating swiftorg metadata to "${data}"`);
62+
const metadata = path.join('pages', 'metadata.json');
63+
await fs.mkdir(path.dirname(metadata), {recursive: true});
64+
await fs.writeFile(metadata, data, 'utf-8');
65+
return data;
66+
};
67+
68+
exports.currentData = async () => {
69+
return new Promise((resolve, reject) => {
70+
https.get(
71+
'https://swiftylab.github.io/setup-swift/metadata.json',
72+
res => {
73+
const {statusCode} = res
74+
const contentType = res.headers['content-type']
75+
76+
let error
77+
if (statusCode !== 200) {
78+
error = new Error(`Request Failed Status Code: '${statusCode}'`)
79+
} else if (!contentType?.startsWith('application/json')) {
80+
error = new Error(`Invalid content-type: ${contentType}`)
81+
}
82+
83+
if (error) {
84+
core.error(error.message)
85+
res.resume()
86+
reject(error)
87+
return
88+
}
89+
90+
let rawData = ''
91+
res.setEncoding('utf8')
92+
res.on('data', chunk => {
93+
rawData += chunk
94+
})
95+
res.on('end', () => {
96+
try {
97+
const parsedData = JSON.parse(rawData)
98+
core.debug(`Recieved swift.org metadata: "${rawData}"`)
99+
resolve(parsedData)
100+
} catch (e) {
101+
core.error(`Parsing swift.org metadata error: '${e}'`)
102+
reject(e)
103+
}
104+
})
105+
}
106+
)
107+
})
108+
}
109+
110+
exports.fetch = async () => {
111+
let checkoutData;
112+
if (process.env.SETUPSWIFT_SWIFTORG_METADATA) {
113+
checkoutData = JSON.parse(process.env.SETUPSWIFT_SWIFTORG_METADATA);
114+
}
115+
if (!checkoutData || !checkoutData.commit) {
116+
checkoutData = await this.currentData();
117+
}
118+
const origin = 'https://github.com/apple/swift-org-website.git'
119+
const ref = checkoutData.commit
120+
await exec('git', ['init', SWIFT_ORG])
121+
await exec('git', ['fetch', origin, ref, '--depth=1', '--no-tags'], SWIFT_ORG_CWD)
122+
await exec('git', ['checkout', 'FETCH_HEAD', '--detach'], SWIFT_ORG_CWD)
123+
}

.github/workflows/approve.yml

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

0 commit comments

Comments
 (0)