mirror of
https://github.com/astral-sh/setup-uv.git
synced 2025-04-18 22:47:05 -05:00
Initial commit
This commit is contained in:
commit
18498fc78f
61 changed files with 261875 additions and 0 deletions
63
src/cache/restore-cache.ts
vendored
Normal file
63
src/cache/restore-cache.ts
vendored
Normal file
|
@ -0,0 +1,63 @@
|
|||
import * as cache from '@actions/cache'
|
||||
import * as glob from '@actions/glob'
|
||||
import * as core from '@actions/core'
|
||||
import path from 'path'
|
||||
import {cacheDependencyGlob, cacheLocalPath, cacheSuffix} from '../utils/inputs'
|
||||
import {getArch, getPlatform} from '../utils/platforms'
|
||||
|
||||
export const STATE_CACHE_KEY = 'cache-key'
|
||||
export const STATE_CACHE_MATCHED_KEY = 'cache-matched-key'
|
||||
const CACHE_VERSION = '1'
|
||||
const fullCacheDependencyGlob = `${process.env['GITHUB_WORKSPACE']}${path.sep}${cacheDependencyGlob}`
|
||||
|
||||
export async function restoreCache(version: string): Promise<void> {
|
||||
const cacheKey = await computeKeys(version)
|
||||
|
||||
let matchedKey: string | undefined
|
||||
core.info(
|
||||
`Trying to restore uv cache from GitHub Actions cache with key: ${cacheKey}`
|
||||
)
|
||||
try {
|
||||
matchedKey = await cache.restoreCache([cacheLocalPath], cacheKey)
|
||||
} catch (err) {
|
||||
const message = (err as Error).message
|
||||
core.warning(message)
|
||||
core.setOutput('cache-hit', false)
|
||||
return
|
||||
}
|
||||
|
||||
core.saveState(STATE_CACHE_KEY, cacheKey)
|
||||
|
||||
handleMatchResult(matchedKey, cacheKey)
|
||||
}
|
||||
|
||||
async function computeKeys(version: string): Promise<string> {
|
||||
let cacheDependencyPathHash = '-'
|
||||
if (fullCacheDependencyGlob !== '') {
|
||||
cacheDependencyPathHash += await glob.hashFiles(fullCacheDependencyGlob)
|
||||
if (cacheDependencyPathHash === '-') {
|
||||
throw new Error(
|
||||
`No file in ${process.cwd()} matched to [${cacheDependencyGlob}], make sure you have checked out the target repository`
|
||||
)
|
||||
}
|
||||
}
|
||||
const suffix = cacheSuffix ? `-${cacheSuffix}` : ''
|
||||
return `setup-uv-${CACHE_VERSION}-${getArch()}-${getPlatform()}-${version}${cacheDependencyPathHash}${suffix}`
|
||||
}
|
||||
|
||||
function handleMatchResult(
|
||||
matchedKey: string | undefined,
|
||||
primaryKey: string
|
||||
): void {
|
||||
if (!matchedKey) {
|
||||
core.info(`No GitHub Actions cache found for key: ${primaryKey}`)
|
||||
core.setOutput('cache-hit', false)
|
||||
return
|
||||
}
|
||||
|
||||
core.saveState(STATE_CACHE_MATCHED_KEY, matchedKey)
|
||||
core.info(
|
||||
`uv cache restored from GitHub Actions cache with key: ${matchedKey}`
|
||||
)
|
||||
core.setOutput('cache-hit', true)
|
||||
}
|
55
src/download/checksum/checksum.ts
Normal file
55
src/download/checksum/checksum.ts
Normal file
|
@ -0,0 +1,55 @@
|
|||
import * as fs from 'fs'
|
||||
import * as crypto from 'crypto'
|
||||
|
||||
import * as core from '@actions/core'
|
||||
import {KNOWN_CHECKSUMS} from './known-checksums'
|
||||
import {Architecture, Platform} from '../../utils/platforms'
|
||||
|
||||
export async function validateChecksum(
|
||||
checkSum: string | undefined,
|
||||
downloadPath: string,
|
||||
arch: Architecture,
|
||||
platform: Platform,
|
||||
version: string
|
||||
): Promise<void> {
|
||||
let isValid = true
|
||||
if (checkSum !== undefined && checkSum !== '') {
|
||||
isValid = await validateFileCheckSum(downloadPath, checkSum)
|
||||
} else {
|
||||
core.debug(`Checksum not provided. Checking known checksums.`)
|
||||
const key = `${arch}-${platform}-${version}`
|
||||
if (key in KNOWN_CHECKSUMS) {
|
||||
const knownChecksum = KNOWN_CHECKSUMS[`${arch}-${platform}-${version}`]
|
||||
core.debug(`Checking checksum for ${arch}-${platform}-${version}.`)
|
||||
isValid = await validateFileCheckSum(downloadPath, knownChecksum)
|
||||
} else {
|
||||
core.debug(`No known checksum found for ${key}.`)
|
||||
}
|
||||
}
|
||||
|
||||
if (!isValid) {
|
||||
throw new Error(`Checksum for ${downloadPath} did not match ${checkSum}.`)
|
||||
}
|
||||
core.debug(`Checksum for ${downloadPath} is valid.`)
|
||||
}
|
||||
|
||||
async function validateFileCheckSum(
|
||||
filePath: string,
|
||||
expected: string
|
||||
): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const hash = crypto.createHash('sha256')
|
||||
const stream = fs.createReadStream(filePath)
|
||||
stream.on('error', err => reject(err))
|
||||
stream.on('data', chunk => hash.update(chunk))
|
||||
stream.on('end', () => {
|
||||
const actual = hash.digest('hex')
|
||||
resolve(actual === expected)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function isknownVersion(version: string): boolean {
|
||||
const pattern = new RegExp(`^.*-.*-${version}$`)
|
||||
return Object.keys(KNOWN_CHECKSUMS).some(key => pattern.test(key))
|
||||
}
|
3015
src/download/checksum/known-checksums.ts
Normal file
3015
src/download/checksum/known-checksums.ts
Normal file
File diff suppressed because it is too large
Load diff
39
src/download/checksum/update-known-checksums.ts
Normal file
39
src/download/checksum/update-known-checksums.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import {promises as fs} from 'fs'
|
||||
import * as tc from '@actions/tool-cache'
|
||||
export async function updateChecksums(
|
||||
filePath: string,
|
||||
downloadUrls: string[]
|
||||
): Promise<void> {
|
||||
await fs.rm(filePath)
|
||||
await fs.appendFile(
|
||||
filePath,
|
||||
'// AUTOGENERATED_DO_NOT_EDIT\nexport const KNOWN_CHECKSUMS: {[key: string]: string} = {\n'
|
||||
)
|
||||
let firstLine = true
|
||||
for (const downloadUrl of downloadUrls) {
|
||||
const content = await downloadAssetContent(downloadUrl)
|
||||
const checksum = content.split(' ')[0].trim()
|
||||
const key = getKey(downloadUrl)
|
||||
if (!firstLine) {
|
||||
await fs.appendFile(filePath, ',\n')
|
||||
}
|
||||
await fs.appendFile(filePath, ` '${key}':\n '${checksum}'`)
|
||||
firstLine = false
|
||||
}
|
||||
await fs.appendFile(filePath, '}\n')
|
||||
}
|
||||
|
||||
function getKey(downloadUrl: string): string {
|
||||
// https://github.com/astral-sh/uv/releases/download/0.3.2/uv-aarch64-apple-darwin.tar.gz.sha256
|
||||
const parts = downloadUrl.split('/')
|
||||
const fileName = parts[parts.length - 1]
|
||||
const name = fileName.split('.')[0].split('uv-')[1]
|
||||
const version = parts[parts.length - 2]
|
||||
return `${name}-${version}`
|
||||
}
|
||||
|
||||
async function downloadAssetContent(downloadUrl: string): Promise<string> {
|
||||
const downloadPath = await tc.downloadTool(downloadUrl)
|
||||
const content = await fs.readFile(downloadPath, 'utf8')
|
||||
return content
|
||||
}
|
64
src/download/download-latest.ts
Normal file
64
src/download/download-latest.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
import * as core from '@actions/core'
|
||||
import * as tc from '@actions/tool-cache'
|
||||
import * as path from 'path'
|
||||
import * as exec from '@actions/exec'
|
||||
import {Architecture, Platform} from '../utils/platforms'
|
||||
import {validateChecksum} from './checksum/checksum'
|
||||
import {OWNER, REPO} from '../utils/utils'
|
||||
|
||||
export async function downloadLatest(
|
||||
platform: Platform,
|
||||
arch: Architecture,
|
||||
checkSum: string | undefined,
|
||||
githubToken: string | undefined
|
||||
): Promise<{downloadDir: string; version: string}> {
|
||||
const binary = `uv-${arch}-${platform}`
|
||||
let downloadUrl = `https://github.com/${OWNER}/${REPO}/releases/latest/download/${binary}`
|
||||
if (platform === 'pc-windows-msvc') {
|
||||
downloadUrl += '.zip'
|
||||
} else {
|
||||
downloadUrl += '.tar.gz'
|
||||
}
|
||||
core.info(`Downloading uv from "${downloadUrl}" ...`)
|
||||
|
||||
const downloadDir = `${process.cwd()}${path.sep}uv`
|
||||
const downloadPath = await tc.downloadTool(
|
||||
downloadUrl,
|
||||
downloadDir,
|
||||
githubToken
|
||||
)
|
||||
let uvExecutablePath: string
|
||||
let extracted: string
|
||||
if (platform === 'pc-windows-msvc') {
|
||||
extracted = await tc.extractZip(downloadPath)
|
||||
uvExecutablePath = path.join(extracted, 'uv.exe')
|
||||
} else {
|
||||
extracted = await tc.extractTar(downloadPath)
|
||||
uvExecutablePath = path.join(extracted, 'uv')
|
||||
}
|
||||
const version = await getVersion(uvExecutablePath)
|
||||
await validateChecksum(checkSum, downloadPath, arch, platform, version)
|
||||
|
||||
return {downloadDir, version}
|
||||
}
|
||||
|
||||
async function getVersion(uvExecutablePath: string): Promise<string> {
|
||||
// Parse the output of `uv --version` to get the version
|
||||
// The output looks like
|
||||
// uv 0.3.1 (be17d132a 2024-08-21)
|
||||
|
||||
const options: exec.ExecOptions = {
|
||||
silent: !core.isDebug()
|
||||
}
|
||||
const execArgs = ['--version']
|
||||
|
||||
let output = ''
|
||||
options.listeners = {
|
||||
stdout: (data: Buffer) => {
|
||||
output += data.toString()
|
||||
}
|
||||
}
|
||||
await exec.exec(uvExecutablePath, execArgs, options)
|
||||
const parts = output.split(' ')
|
||||
return parts[1]
|
||||
}
|
48
src/download/download-version.ts
Normal file
48
src/download/download-version.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
import * as core from '@actions/core'
|
||||
import * as tc from '@actions/tool-cache'
|
||||
import {OWNER, REPO, TOOL_CACHE_NAME} from '../utils/utils'
|
||||
import path from 'path'
|
||||
import {Architecture, Platform} from '../utils/platforms'
|
||||
import {validateChecksum} from './checksum/checksum'
|
||||
|
||||
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> {
|
||||
const binary = `uv-${arch}-${platform}`
|
||||
let downloadUrl = `https://github.com/${OWNER}/${REPO}/releases/download/${version}/${binary}`
|
||||
if (platform === 'pc-windows-msvc') {
|
||||
downloadUrl += '.zip'
|
||||
} else {
|
||||
downloadUrl += '.tar.gz'
|
||||
}
|
||||
core.info(`Downloading uv from "${downloadUrl}" ...`)
|
||||
|
||||
const downloadDir = `${process.cwd()}${path.sep}uv`
|
||||
const downloadPath = await tc.downloadTool(
|
||||
downloadUrl,
|
||||
downloadDir,
|
||||
githubToken
|
||||
)
|
||||
await validateChecksum(checkSum, downloadPath, arch, platform, version)
|
||||
|
||||
if (platform === 'pc-windows-msvc') {
|
||||
await tc.extractZip(downloadPath)
|
||||
} else {
|
||||
tc.extractTar(downloadPath)
|
||||
}
|
||||
return downloadPath
|
||||
}
|
36
src/save-cache.ts
Normal file
36
src/save-cache.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
import * as cache from '@actions/cache'
|
||||
import * as core from '@actions/core'
|
||||
import {STATE_CACHE_MATCHED_KEY, STATE_CACHE_KEY} from './cache/restore-cache'
|
||||
import {cacheLocalPath, enableCache} from './utils/inputs'
|
||||
|
||||
export async function run(): Promise<void> {
|
||||
try {
|
||||
if (enableCache) {
|
||||
await saveCache()
|
||||
}
|
||||
} catch (error) {
|
||||
const err = error as Error
|
||||
core.setFailed(err.message)
|
||||
}
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
async function saveCache(): Promise<void> {
|
||||
const cacheKey = core.getState(STATE_CACHE_KEY)
|
||||
const matchedKey = core.getState(STATE_CACHE_MATCHED_KEY)
|
||||
|
||||
if (!cacheKey) {
|
||||
core.warning('Error retrieving cache key from state.')
|
||||
return
|
||||
} else if (matchedKey === cacheKey) {
|
||||
// no change in target directories
|
||||
core.info(`Cache hit occurred on key ${cacheKey}, not saving cache.`)
|
||||
return
|
||||
}
|
||||
core.info(`Saving cache path: ${cacheLocalPath}`)
|
||||
await cache.saveCache([cacheLocalPath], cacheKey)
|
||||
|
||||
core.info(`cache saved with the key: ${cacheKey}`)
|
||||
}
|
||||
|
||||
run()
|
98
src/setup-uv.ts
Normal file
98
src/setup-uv.ts
Normal file
|
@ -0,0 +1,98 @@
|
|||
import * as core from '@actions/core'
|
||||
import * as path from 'path'
|
||||
import {downloadVersion, tryGetFromToolCache} from './download/download-version'
|
||||
import {restoreCache} from './cache/restore-cache'
|
||||
|
||||
import {downloadLatest} from './download/download-latest'
|
||||
import {Architecture, getArch, getPlatform, Platform} from './utils/platforms'
|
||||
import {
|
||||
cacheLocalPath,
|
||||
checkSum,
|
||||
enableCache,
|
||||
githubToken,
|
||||
version
|
||||
} from './utils/inputs'
|
||||
|
||||
async function run(): Promise<void> {
|
||||
const platform = getPlatform()
|
||||
const arch = getArch()
|
||||
|
||||
try {
|
||||
if (platform === undefined) {
|
||||
throw new Error(`Unsupported platform: ${process.platform}`)
|
||||
}
|
||||
if (arch === undefined) {
|
||||
throw new Error(`Unsupported architecture: ${process.arch}`)
|
||||
}
|
||||
const installedVersion = await setupUv(
|
||||
platform,
|
||||
arch,
|
||||
version,
|
||||
checkSum,
|
||||
githubToken
|
||||
)
|
||||
|
||||
addMatchers()
|
||||
setCacheDir(cacheLocalPath)
|
||||
|
||||
if (enableCache) {
|
||||
await restoreCache(installedVersion)
|
||||
}
|
||||
} catch (err) {
|
||||
core.setFailed((err as Error).message)
|
||||
}
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
async function setupUv(
|
||||
platform: Platform,
|
||||
arch: Architecture,
|
||||
versionInput: string,
|
||||
checkSum: string | undefined,
|
||||
githubToken: string | undefined
|
||||
): Promise<string> {
|
||||
let installedPath: string | undefined
|
||||
let downloadDir: string
|
||||
let version: string
|
||||
if (versionInput === 'latest') {
|
||||
const result = await downloadLatest(platform, arch, checkSum, githubToken)
|
||||
version = result.version
|
||||
downloadDir = result.downloadDir
|
||||
} else {
|
||||
version = versionInput
|
||||
installedPath = tryGetFromToolCache(arch, versionInput)
|
||||
if (installedPath) {
|
||||
core.info(`Found uv in tool-cache for ${versionInput}`)
|
||||
return version
|
||||
}
|
||||
downloadDir = await downloadVersion(
|
||||
platform,
|
||||
arch,
|
||||
versionInput,
|
||||
checkSum,
|
||||
githubToken
|
||||
)
|
||||
}
|
||||
|
||||
addUvToPath(downloadDir)
|
||||
core.setOutput('uv-version', version)
|
||||
core.info(`Successfully installed uv version ${version}`)
|
||||
return version
|
||||
}
|
||||
|
||||
function addUvToPath(cachedPath: string): void {
|
||||
core.addPath(cachedPath)
|
||||
core.info(`Added ${cachedPath} to the path`)
|
||||
}
|
||||
|
||||
function setCacheDir(cacheLocalPath: string): void {
|
||||
core.exportVariable('UV_CACHE_DIR', cacheLocalPath)
|
||||
core.info(`Set UV_CACHE_DIR to ${cacheLocalPath}`)
|
||||
}
|
||||
|
||||
function addMatchers(): void {
|
||||
const matchersPath = path.join(__dirname, `..${path.sep}..`, '.github')
|
||||
core.info(`##[add-matcher]${path.join(matchersPath, 'python.json')}`)
|
||||
}
|
||||
|
||||
run()
|
65
src/update-default-version.ts
Normal file
65
src/update-default-version.ts
Normal file
|
@ -0,0 +1,65 @@
|
|||
import * as github from '@actions/github'
|
||||
import * as core from '@actions/core'
|
||||
|
||||
import {OWNER, REPO} from './utils/utils'
|
||||
import {createReadStream, promises as fs} from 'fs'
|
||||
import * as readline from 'readline'
|
||||
import * as semver from 'semver'
|
||||
|
||||
import {updateChecksums} from './download/checksum/update-known-checksums'
|
||||
|
||||
async function run(): Promise<void> {
|
||||
const checksumFilePath = process.argv.slice(2)[0]
|
||||
const defaultVersionFilePath = process.argv.slice(2)[1]
|
||||
const github_token = process.argv.slice(2)[2]
|
||||
|
||||
const octokit = github.getOctokit(github_token)
|
||||
|
||||
const response = await octokit.paginate(octokit.rest.repos.listReleases, {
|
||||
owner: OWNER,
|
||||
repo: REPO
|
||||
})
|
||||
const downloadUrls: string[] = response.flatMap(release =>
|
||||
release.assets
|
||||
.filter(asset => asset.name.endsWith('.sha256'))
|
||||
.map(asset => asset.browser_download_url)
|
||||
)
|
||||
await updateChecksums(checksumFilePath, downloadUrls)
|
||||
|
||||
const latestVersion = response
|
||||
.map(release => release.tag_name)
|
||||
.sort(semver.rcompare)[0]
|
||||
core.setOutput('latest-version', latestVersion)
|
||||
await updateDefaultVersion(defaultVersionFilePath, latestVersion)
|
||||
}
|
||||
|
||||
async function updateDefaultVersion(
|
||||
filePath: string,
|
||||
latestVersion: string
|
||||
): Promise<void> {
|
||||
const fileStream = createReadStream(filePath)
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: fileStream
|
||||
})
|
||||
|
||||
let foundDescription = false
|
||||
const lines = []
|
||||
|
||||
for await (let line of rl) {
|
||||
if (
|
||||
!foundDescription &&
|
||||
line.includes("description: 'The version of uv to install'")
|
||||
) {
|
||||
foundDescription = true
|
||||
} else if (foundDescription && line.includes('default: ')) {
|
||||
line = line.replace(/'[^']*'/, `'${latestVersion}'`)
|
||||
foundDescription = false
|
||||
}
|
||||
lines.push(line)
|
||||
}
|
||||
|
||||
await fs.writeFile(filePath, lines.join('\n'))
|
||||
}
|
||||
|
||||
run()
|
9
src/utils/inputs.ts
Normal file
9
src/utils/inputs.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import * as core from '@actions/core'
|
||||
|
||||
export const version = core.getInput('version')
|
||||
export const checkSum = core.getInput('checksum')
|
||||
export const enableCache = core.getInput('enable-cache') === 'true'
|
||||
export const cacheSuffix = core.getInput('cache-suffix') || ''
|
||||
export const cacheLocalPath = core.getInput('cache-local-path')
|
||||
export const githubToken = core.getInput('github-token')
|
||||
export const cacheDependencyGlob = core.getInput('cache-dependency-glob')
|
33
src/utils/platforms.ts
Normal file
33
src/utils/platforms.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
export type Platform =
|
||||
| 'unknown-linux-gnu'
|
||||
| 'unknown-linux-musl'
|
||||
| 'unknown-linux-musleabihf'
|
||||
| 'apple-darwin'
|
||||
| 'pc-windows-msvc'
|
||||
export type Architecture = 'i686' | 'x86_64' | 'aarch64'
|
||||
|
||||
export function getArch(): Architecture | undefined {
|
||||
const arch = process.arch
|
||||
const archMapping: {[key: string]: Architecture} = {
|
||||
ia32: 'i686',
|
||||
x64: 'x86_64',
|
||||
arm64: 'aarch64'
|
||||
}
|
||||
|
||||
if (arch in archMapping) {
|
||||
return archMapping[arch]
|
||||
}
|
||||
}
|
||||
|
||||
export function getPlatform(): Platform | undefined {
|
||||
const platform = process.platform
|
||||
const platformMapping: {[key: string]: Platform} = {
|
||||
linux: 'unknown-linux-gnu',
|
||||
darwin: 'apple-darwin',
|
||||
win32: 'pc-windows-msvc'
|
||||
}
|
||||
|
||||
if (platform in platformMapping) {
|
||||
return platformMapping[platform]
|
||||
}
|
||||
}
|
3
src/utils/utils.ts
Normal file
3
src/utils/utils.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export const REPO = 'uv'
|
||||
export const OWNER = 'astral-sh'
|
||||
export const TOOL_CACHE_NAME = 'uv'
|
Loading…
Add table
Add a link
Reference in a new issue