2020-11-11 11:38:41 +00:00
|
|
|
const path = require('path')
|
|
|
|
const get = require('simple-get').concat
|
|
|
|
const semver = require('semver')
|
|
|
|
|
|
|
|
function extForPlatform (platform) {
|
|
|
|
return {
|
|
|
|
linux: 'tar.xz',
|
|
|
|
darwin: 'tar.xz',
|
|
|
|
win32: 'zip'
|
|
|
|
}[platform]
|
|
|
|
}
|
|
|
|
|
2024-04-26 07:39:29 +00:00
|
|
|
function resolveCommit (arch, platform, version) {
|
2020-11-11 11:38:41 +00:00
|
|
|
const ext = extForPlatform(platform)
|
2024-04-26 07:39:29 +00:00
|
|
|
const resolvedOs = {
|
|
|
|
linux: 'linux',
|
|
|
|
darwin: 'macos',
|
|
|
|
win32: 'windows'
|
2020-11-11 11:38:41 +00:00
|
|
|
}[platform]
|
|
|
|
|
2024-04-26 07:39:29 +00:00
|
|
|
const resolvedArch = {
|
|
|
|
arm: 'armv7a',
|
|
|
|
arm64: 'aarch64',
|
|
|
|
ppc64: 'powerpc64',
|
|
|
|
riscv64: 'riscv64',
|
2024-04-26 07:42:30 +00:00
|
|
|
x64: 'x86_64'
|
|
|
|
}[arch]
|
2024-04-26 07:39:29 +00:00
|
|
|
|
|
|
|
const downloadUrl = `https://ziglang.org/builds/zig-${resolvedOs}-${resolvedArch}-${version}.${ext}`
|
|
|
|
const variantName = `zig-${resolvedOs}-${resolvedArch}-${version}`
|
2020-11-11 11:38:41 +00:00
|
|
|
|
2023-05-18 12:30:28 +00:00
|
|
|
return { downloadUrl, variantName, version }
|
2020-11-11 11:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getJSON (opts) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
get({ ...opts, json: true }, (err, req, data) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve(data)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-26 07:39:29 +00:00
|
|
|
async function resolveVersion (arch, platform, version) {
|
2020-11-11 11:38:41 +00:00
|
|
|
const ext = extForPlatform(platform)
|
2024-04-26 07:39:29 +00:00
|
|
|
const resolvedOs = {
|
|
|
|
linux: 'linux',
|
|
|
|
darwin: 'macos',
|
|
|
|
win32: 'windows'
|
|
|
|
}[platform]
|
|
|
|
|
|
|
|
const resolvedArch = {
|
|
|
|
arm: 'armv7a',
|
|
|
|
arm64: 'aarch64',
|
|
|
|
ppc64: 'powerpc64',
|
|
|
|
riscv64: 'riscv64',
|
2024-04-26 07:42:30 +00:00
|
|
|
x64: 'x86_64'
|
|
|
|
}[arch]
|
2024-04-26 07:39:29 +00:00
|
|
|
|
|
|
|
const host = `${resolvedArch}-${resolvedOs}`
|
2020-11-11 11:38:41 +00:00
|
|
|
|
|
|
|
const index = await getJSON({ url: 'https://ziglang.org/download/index.json' })
|
|
|
|
|
|
|
|
const availableVersions = Object.keys(index)
|
|
|
|
const useVersion = semver.valid(version)
|
|
|
|
? semver.maxSatisfying(availableVersions.filter((v) => semver.valid(v)), version)
|
|
|
|
: null
|
|
|
|
|
|
|
|
const meta = index[useVersion || version]
|
|
|
|
if (!meta || !meta[host]) {
|
|
|
|
throw new Error(`Could not find version ${useVersion || version} for platform ${host}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const downloadUrl = meta[host].tarball
|
|
|
|
const variantName = path.basename(meta[host].tarball).replace(`.${ext}`, '')
|
|
|
|
|
2023-05-18 12:30:28 +00:00
|
|
|
return { downloadUrl, variantName, version: useVersion || version }
|
2020-11-11 11:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
extForPlatform,
|
|
|
|
resolveCommit,
|
|
|
|
resolveVersion
|
|
|
|
}
|