setup-python/src/cache-distributions/cache-distributor.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-10-27 09:52:29 +03:00
import * as cache from '@actions/cache';
import * as core from '@actions/core';
export enum State {
STATE_CACHE_PRIMARY_KEY = 'cache-primary-key',
CACHE_MATCHED_KEY = 'cache-matched-key',
CACHE_PATHS = 'cache-paths'
}
abstract class CacheDistributor {
protected CACHE_KEY_PREFIX = 'setup-python';
2021-11-09 10:41:35 +03:00
constructor(protected packageManager: string, protected cacheDependencyPath: string) {}
2021-10-27 09:52:29 +03:00
protected abstract getCacheGlobalDirectories(): Promise<string[]>;
protected abstract computeKeys(): Promise<{
primaryKey: string;
restoreKey: string[] | undefined;
}>;
public async restoreCache() {
const {primaryKey, restoreKey} = await this.computeKeys();
if (primaryKey.endsWith('-')) {
throw new Error(
2021-11-03 13:10:35 +03:00
`No file in ${process.cwd()} matched to [${this.cacheDependencyPath
2021-10-27 09:52:29 +03:00
.split('\n')
.join(',')}], make sure you have checked out the target repository`
);
}
2021-11-03 13:10:35 +03:00
const cachePath = await this.getCacheGlobalDirectories();
core.saveState(State.CACHE_PATHS, cachePath);
core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey);
2021-10-27 09:52:29 +03:00
const matchedKey = await cache.restoreCache(
cachePath,
primaryKey,
restoreKey
);
2021-11-03 13:10:35 +03:00
2021-10-27 09:52:29 +03:00
if (matchedKey) {
core.saveState(State.CACHE_MATCHED_KEY, matchedKey);
core.info(`Cache restored from key: ${matchedKey}`);
} else {
2021-11-09 10:41:35 +03:00
core.info(`${this.packageManager} cache is not found`);
2021-10-27 09:52:29 +03:00
}
}
}
export default CacheDistributor;