From 0c8c22e3bf43b109bc8d93c4c28bc86c248ba301 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Mon, 19 Feb 2024 13:30:09 +0200 Subject: [PATCH] Add support for caching `uv` See https://github.com/astral-sh/uv --- src/cache-distributions/cache-factory.ts | 6 +++- src/cache-distributions/uv-cache.ts | 36 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/cache-distributions/uv-cache.ts diff --git a/src/cache-distributions/cache-factory.ts b/src/cache-distributions/cache-factory.ts index 7becf5325..66d4b9dbb 100644 --- a/src/cache-distributions/cache-factory.ts +++ b/src/cache-distributions/cache-factory.ts @@ -1,11 +1,13 @@ import PipCache from './pip-cache'; import PipenvCache from './pipenv-cache'; import PoetryCache from './poetry-cache'; +import UvCache from './uv-cache'; export enum PackageManagers { Pip = 'pip', Pipenv = 'pipenv', - Poetry = 'poetry' + Poetry = 'poetry', + Uv = 'uv' } export function getCacheDistributor( @@ -20,6 +22,8 @@ export function getCacheDistributor( return new PipenvCache(pythonVersion, cacheDependencyPath); case PackageManagers.Poetry: return new PoetryCache(pythonVersion, cacheDependencyPath); + case PackageManagers.Uv: + return new UvCache(pythonVersion, cacheDependencyPath); default: throw new Error(`Caching for '${packageManager}' is not supported`); } diff --git a/src/cache-distributions/uv-cache.ts b/src/cache-distributions/uv-cache.ts new file mode 100644 index 000000000..b9ca6cd96 --- /dev/null +++ b/src/cache-distributions/uv-cache.ts @@ -0,0 +1,36 @@ +import * as glob from '@actions/glob'; +import * as os from 'os'; +import * as path from 'path'; + +import CacheDistributor from './cache-distributor'; + +export default class UvCache extends CacheDistributor { + constructor( + private pythonVersion: string, + protected patterns: string = '**/requirements.txt' + ) { + super('uv', patterns); + } + + protected async getCacheGlobalDirectories() { + if (process.platform === 'win32') { + // `LOCALAPPDATA` should always be defined, + // but we can't just join `undefined` + // into the path in case it's not. + return [ + path.join(process.env['LOCALAPPDATA'] ?? os.homedir(), 'uv', 'cache') + ]; + } + return [path.join(os.homedir(), '.cache/uv')]; + } + + protected async computeKeys() { + const hash = await glob.hashFiles(this.patterns); + const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`; + const restoreKey = undefined; + return { + primaryKey, + restoreKey + }; + } +}