Skip to content

Commit e1d6dd2

Browse files
committed
Support Mach nominated versions in build.zig.zon
Mach are using the `mach_zig_version` key as an unofficial way to signify a required Mach nominated version. It's unclear whether the compiler will continue to accept this long-term, but at least for now, it makes sense for us to interpret it.
1 parent 9ac9efd commit e1d6dd2

File tree

1 file changed

+62
-41
lines changed

1 file changed

+62
-41
lines changed

common.js

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const VERSIONS_JSON = 'https://ziglang.org/download/index.json';
99
const MACH_VERSIONS_JSON = 'https://pkg.machengine.org/zig/index.json';
1010
const CACHE_PREFIX = "setup-zig-global-cache-";
1111

12+
// Mach uses `mach_zig_version` in `build.zig.zon` to signify Mach nominated versions.
13+
// See: https://github.com/marler8997/anyzig?tab=readme-ov-file#mach-versions-and-download-mirror
14+
const MACH_ZIG_VERSION_REGEX = /\.\s*mach_zig_version\s*=\s*"(.*?)"/;
1215
const MINIMUM_ZIG_VERSION_REGEX = /\.\s*minimum_zig_version\s*=\s*"(.*?)"/;
1316

1417
let _cached_version = null;
@@ -21,14 +24,22 @@ async function getVersion() {
2124
if (raw === '') {
2225
try {
2326
const zon = await fs.promises.readFile('build.zig.zon', 'utf8');
24-
const match = MINIMUM_ZIG_VERSION_REGEX.exec(zon);
2527

28+
// Look for `mach_zig_version` first
29+
let match = MACH_ZIG_VERSION_REGEX.exec(zon);
30+
if (match !== null) {
31+
_cached_version = await getMachVersion(match[1]);
32+
return _cached_version;
33+
}
34+
35+
// Else, look for `mach_zig_version` first
36+
match = MINIMUM_ZIG_VERSION_REGEX.exec(zon);
2637
if (match !== null) {
2738
_cached_version = match[1];
2839
return _cached_version;
2940
}
3041

31-
core.info('Failed to find minimum_zig_version in build.zig.zon (using latest)');
42+
core.info('Failed to find `mach_zig_version` or `minimum_zig_version` in build.zig.zon (using latest)');
3243
} catch (e) {
3344
core.info(`Failed to read build.zig.zon (using latest): ${e}`);
3445
}
@@ -37,54 +48,64 @@ async function getVersion() {
3748
}
3849

3950
if (raw === 'master') {
40-
const resp = await fetch(VERSIONS_JSON);
41-
const versions = await resp.json();
42-
_cached_version = versions['master'].version;
51+
_cached_version = await getMasterVersion();
4352
} else if (raw === 'latest') {
44-
const resp = await fetch(VERSIONS_JSON);
45-
const versions = await resp.json();
46-
let latest = null;
47-
let latest_major;
48-
let latest_minor;
49-
let latest_patch;
50-
for (const version in versions) {
51-
if (version === 'master') continue;
52-
const [major_str, minor_str, patch_str] = version.split('.')
53-
const major = Number(major_str);
54-
const minor = Number(minor_str);
55-
const patch = Number(patch_str);
56-
if (latest === null) {
57-
latest = version;
58-
latest_major = major;
59-
latest_minor = minor;
60-
latest_patch = patch;
61-
continue;
62-
}
63-
if (major > latest_major ||
64-
(major == latest_major && minor > latest_minor) ||
65-
(major == latest_major && minor == latest_minor && patch > latest_patch))
66-
{
67-
latest = version;
68-
latest_major = major;
69-
latest_minor = minor;
70-
latest_patch = patch;
71-
}
72-
}
73-
_cached_version = latest;
53+
_cached_version = await getLatestVersion();
7454
} else if (raw.includes("mach")) {
75-
const resp = await fetch(MACH_VERSIONS_JSON);
76-
const versions = await resp.json();
77-
if (!(raw in versions)) {
78-
throw new Error(`Mach nominated version '${raw}' not found`);
79-
}
80-
_cached_version = versions[raw].version;
55+
_cached_version = await getMachVersion(raw);
8156
} else {
8257
_cached_version = raw;
8358
}
8459

8560
return _cached_version;
8661
}
8762

63+
async function getMachVersion(raw) {
64+
const resp = await fetch(MACH_VERSIONS_JSON);
65+
const versions = await resp.json();
66+
if (!(raw in versions)) {
67+
throw new Error(`Mach nominated version '${raw}' not found`);
68+
}
69+
return versions[raw].version;
70+
}
71+
async function getMasterVersion() {
72+
const resp = await fetch(VERSIONS_JSON);
73+
const versions = await resp.json();
74+
return versions['master'].version;
75+
}
76+
async function getLatestVersion() {
77+
const resp = await fetch(VERSIONS_JSON);
78+
const versions = await resp.json();
79+
let latest = null;
80+
let latest_major;
81+
let latest_minor;
82+
let latest_patch;
83+
for (const version in versions) {
84+
if (version === 'master') continue;
85+
const [major_str, minor_str, patch_str] = version.split('.')
86+
const major = Number(major_str);
87+
const minor = Number(minor_str);
88+
const patch = Number(patch_str);
89+
if (latest === null) {
90+
latest = version;
91+
latest_major = major;
92+
latest_minor = minor;
93+
latest_patch = patch;
94+
continue;
95+
}
96+
if (major > latest_major ||
97+
(major == latest_major && minor > latest_minor) ||
98+
(major == latest_major && minor == latest_minor && patch > latest_patch))
99+
{
100+
latest = version;
101+
latest_major = major;
102+
latest_minor = minor;
103+
latest_patch = patch;
104+
}
105+
}
106+
return latest;
107+
}
108+
88109
async function getTarballName() {
89110
const version = await getVersion();
90111

0 commit comments

Comments
 (0)