setup-uv/src/download/download-version.ts

67 lines
2 KiB
TypeScript
Raw Normal View History

2024-08-23 23:58:26 +02:00
import * as core from '@actions/core'
import * as tc from '@actions/tool-cache'
2024-08-24 00:45:44 +02:00
import * as path from 'path'
2024-08-24 00:27:50 +02:00
import {OWNER, REPO, TOOL_CACHE_NAME} from '../utils/utils'
2024-08-23 23:58:26 +02:00
import {Architecture, Platform} from '../utils/platforms'
import {validateChecksum} from './checksum/checksum'
2024-08-24 09:12:22 +02:00
import * as fs from 'fs'
import * as util from 'util'
const readdir = util.promisify(fs.readdir)
2024-08-23 23:58:26 +02:00
export function tryGetFromToolCache(
arch: Architecture,
version: string
): string | undefined {
core.debug(`Trying to get uv from tool cache for ${version}...`)
const cachedVersions = tc.findAllVersions(TOOL_CACHE_NAME, arch)
core.debug(`Cached versions: ${cachedVersions}`)
return tc.find(TOOL_CACHE_NAME, version, arch)
}
export async function downloadVersion(
platform: Platform,
arch: Architecture,
version: string,
checkSum: string | undefined,
githubToken: string | undefined
): Promise<string> {
2024-08-24 00:45:44 +02:00
const artifact = `uv-${arch}-${platform}`
let downloadUrl = `https://github.com/${OWNER}/${REPO}/releases/download/${version}/${artifact}`
2024-08-23 23:58:26 +02:00
if (platform === 'pc-windows-msvc') {
downloadUrl += '.zip'
} else {
downloadUrl += '.tar.gz'
}
core.info(`Downloading uv from "${downloadUrl}" ...`)
const downloadPath = await tc.downloadTool(
downloadUrl,
2024-08-24 00:27:50 +02:00
undefined,
2024-08-23 23:58:26 +02:00
githubToken
)
await validateChecksum(checkSum, downloadPath, arch, platform, version)
2024-08-24 00:27:50 +02:00
let extractedDir: string
2024-08-23 23:58:26 +02:00
if (platform === 'pc-windows-msvc') {
2024-08-24 00:27:50 +02:00
extractedDir = await tc.extractZip(downloadPath)
2024-08-24 09:12:22 +02:00
const files = await readdir(extractedDir)
core.info(
`Contents of extracted directory ${extractedDir}: ${files.join(', ')}`
)
const uvDir = path.join(extractedDir, artifact)
const uvfiles = await readdir(uvDir)
core.info(`Contents of directory ${uvDir}: ${uvfiles.join(', ')}`)
2024-08-23 23:58:26 +02:00
} else {
2024-08-24 00:27:50 +02:00
extractedDir = await tc.extractTar(downloadPath)
2024-08-23 23:58:26 +02:00
}
2024-08-24 00:40:34 +02:00
2024-08-24 00:45:44 +02:00
return await tc.cacheDir(
path.join(extractedDir, artifact),
TOOL_CACHE_NAME,
version,
arch
)
2024-08-23 23:58:26 +02:00
}