Skip to content

Commit 0c8c22e

Browse files
committed
Add support for caching uv
See https://github.com/astral-sh/uv
1 parent 871daa9 commit 0c8c22e

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/cache-distributions/cache-factory.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import PipCache from './pip-cache';
22
import PipenvCache from './pipenv-cache';
33
import PoetryCache from './poetry-cache';
4+
import UvCache from './uv-cache';
45

56
export enum PackageManagers {
67
Pip = 'pip',
78
Pipenv = 'pipenv',
8-
Poetry = 'poetry'
9+
Poetry = 'poetry',
10+
Uv = 'uv'
911
}
1012

1113
export function getCacheDistributor(
@@ -20,6 +22,8 @@ export function getCacheDistributor(
2022
return new PipenvCache(pythonVersion, cacheDependencyPath);
2123
case PackageManagers.Poetry:
2224
return new PoetryCache(pythonVersion, cacheDependencyPath);
25+
case PackageManagers.Uv:
26+
return new UvCache(pythonVersion, cacheDependencyPath);
2327
default:
2428
throw new Error(`Caching for '${packageManager}' is not supported`);
2529
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as glob from '@actions/glob';
2+
import * as os from 'os';
3+
import * as path from 'path';
4+
5+
import CacheDistributor from './cache-distributor';
6+
7+
export default class UvCache extends CacheDistributor {
8+
constructor(
9+
private pythonVersion: string,
10+
protected patterns: string = '**/requirements.txt'
11+
) {
12+
super('uv', patterns);
13+
}
14+
15+
protected async getCacheGlobalDirectories() {
16+
if (process.platform === 'win32') {
17+
// `LOCALAPPDATA` should always be defined,
18+
// but we can't just join `undefined`
19+
// into the path in case it's not.
20+
return [
21+
path.join(process.env['LOCALAPPDATA'] ?? os.homedir(), 'uv', 'cache')
22+
];
23+
}
24+
return [path.join(os.homedir(), '.cache/uv')];
25+
}
26+
27+
protected async computeKeys() {
28+
const hash = await glob.hashFiles(this.patterns);
29+
const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
30+
const restoreKey = undefined;
31+
return {
32+
primaryKey,
33+
restoreKey
34+
};
35+
}
36+
}

0 commit comments

Comments
 (0)