Skip to content

Commit

Permalink
Add support for caching uv
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Feb 19, 2024
1 parent 871daa9 commit 0c8c22e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/cache-distributions/cache-factory.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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`);
}
Expand Down
36 changes: 36 additions & 0 deletions src/cache-distributions/uv-cache.ts
Original file line number Diff line number Diff line change
@@ -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
};
}
}

0 comments on commit 0c8c22e

Please sign in to comment.