refactor
This commit is contained in:
parent
a43b52f8f8
commit
ab441cce53
106
index.js
106
index.js
|
@ -17,62 +17,70 @@ function getJSON (opts) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function downloadZig (version) {
|
function extForPlatform (platform) {
|
||||||
|
return {
|
||||||
|
linux: 'tar.xz',
|
||||||
|
darwin: 'tar.xz',
|
||||||
|
win32: 'zip'
|
||||||
|
}[platform]
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveCommit (platform, version) {
|
||||||
|
const ext = extForPlatform(platform)
|
||||||
|
const addrhost = {
|
||||||
|
linux: 'linux-x86_64',
|
||||||
|
darwin: 'macos-x86_64',
|
||||||
|
win32: 'windows-x86_64'
|
||||||
|
}[platform]
|
||||||
|
|
||||||
|
const downloadUrl = `https://ziglang.org/builds/zig-${addrhost}-${version}.${ext}`
|
||||||
|
const variantName = `zig-${addrhost}-${version}`
|
||||||
|
|
||||||
|
return { downloadUrl, variantName }
|
||||||
|
}
|
||||||
|
|
||||||
|
async function resolveVersion (platform, version) {
|
||||||
|
const ext = extForPlatform(platform)
|
||||||
const host = {
|
const host = {
|
||||||
linux: 'x86_64-linux',
|
linux: 'x86_64-linux',
|
||||||
darwin: 'x86_64-macos',
|
darwin: 'x86_64-macos',
|
||||||
win32: 'x86_64-windows'
|
win32: 'x86_64-windows'
|
||||||
}[os.platform()] || os.platform()
|
}[platform] || platform
|
||||||
const ext = {
|
|
||||||
linux: 'tar.xz',
|
|
||||||
darwin: 'tar.xz',
|
|
||||||
win32: 'zip'
|
|
||||||
}[os.platform()]
|
|
||||||
|
|
||||||
if (version.includes('+')) {
|
const index = await getJSON({ url: 'https://ziglang.org/download/index.json' })
|
||||||
// use exact commit hash
|
|
||||||
const addrhost = {
|
|
||||||
linux: 'linux-x86_64',
|
|
||||||
darwin: 'macos-x86_64',
|
|
||||||
win32: 'windows-x86_64'
|
|
||||||
}[os.platform()]
|
|
||||||
const downloadUrl = `https://ziglang.org/builds/zig-${addrhost}-${version}.${ext}`
|
|
||||||
const variantName = `zig-${addrhost}-${version}`
|
|
||||||
|
|
||||||
const downloadPath = await cache.downloadTool(downloadUrl)
|
const availableVersions = Object.keys(index)
|
||||||
const zigPath = ext === 'zip'
|
const useVersion = semver.valid(version)
|
||||||
? await cache.extractZip(downloadPath)
|
? semver.maxSatisfying(availableVersions.filter((v) => semver.valid(v)), version)
|
||||||
: await cache.extractTar(downloadPath, undefined, 'x')
|
: null
|
||||||
|
|
||||||
const binPath = path.join(zigPath, variantName)
|
const meta = index[useVersion || version]
|
||||||
const cachePath = await cache.cacheDir(binPath, 'zig', variantName)
|
if (!meta || !meta[host]) {
|
||||||
|
throw new Error(`Could not find version ${version} for platform ${host}`)
|
||||||
return cachePath
|
|
||||||
} else {
|
|
||||||
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 ${version} for platform ${host}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
const variantName = path.basename(meta[host].tarball).replace(`.${ext}`, '')
|
|
||||||
|
|
||||||
const downloadPath = await cache.downloadTool(meta[host].tarball)
|
|
||||||
const zigPath = ext === 'zip'
|
|
||||||
? await cache.extractZip(downloadPath)
|
|
||||||
: await cache.extractTar(downloadPath, undefined, 'x')
|
|
||||||
|
|
||||||
const binPath = path.join(zigPath, variantName)
|
|
||||||
const cachePath = await cache.cacheDir(binPath, 'zig', variantName)
|
|
||||||
|
|
||||||
return cachePath
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const downloadUrl = meta[host].tarball
|
||||||
|
const variantName = path.basename(meta[host].tarball).replace(`.${ext}`, '')
|
||||||
|
|
||||||
|
return { downloadUrl, variantName }
|
||||||
|
}
|
||||||
|
|
||||||
|
async function downloadZig (platform, version) {
|
||||||
|
const ext = extForPlatform(platform)
|
||||||
|
|
||||||
|
const { downloadUrl, variantName } = version.includes('+')
|
||||||
|
? resolveCommit(platform, version)
|
||||||
|
: await resolveVersion(platform, version)
|
||||||
|
|
||||||
|
const downloadPath = await cache.downloadTool(downloadUrl)
|
||||||
|
const zigPath = ext === 'zip'
|
||||||
|
? await cache.extractZip(downloadPath)
|
||||||
|
: await cache.extractTar(downloadPath, undefined, 'x')
|
||||||
|
|
||||||
|
const binPath = path.join(zigPath, variantName)
|
||||||
|
const cachePath = await cache.cacheDir(binPath, 'zig', variantName)
|
||||||
|
|
||||||
|
return cachePath
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main () {
|
async function main () {
|
||||||
|
@ -84,7 +92,7 @@ async function main () {
|
||||||
|
|
||||||
let zigPath = cache.find('zig', version)
|
let zigPath = cache.find('zig', version)
|
||||||
if (!zigPath) {
|
if (!zigPath) {
|
||||||
zigPath = await downloadZig(version)
|
zigPath = await downloadZig(os.platform(), version)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the `zig` binary to the $PATH
|
// Add the `zig` binary to the $PATH
|
||||||
|
|
Loading…
Reference in New Issue
Block a user