Permalink
Cannot retrieve contributors at this time
66 lines (55 sloc)
1.96 KB
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
setup-python/src/cache-distributions/cache-distributor.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as cache from '@actions/cache'; | |
import * as core from '@actions/core'; | |
import {CACHE_DEPENDENCY_BACKUP_PATH} from './constants'; | |
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'; | |
constructor( | |
protected packageManager: string, | |
protected cacheDependencyPath: string | |
) {} | |
protected abstract getCacheGlobalDirectories(): Promise<string[]>; | |
protected abstract computeKeys(): Promise<{ | |
primaryKey: string; | |
restoreKey: string[] | undefined; | |
}>; | |
protected async handleLoadedCache() {} | |
public async restoreCache() { | |
const {primaryKey, restoreKey} = await this.computeKeys(); | |
if (primaryKey.endsWith('-')) { | |
const file = | |
this.packageManager === 'pip' | |
? `${this.cacheDependencyPath | |
.split('\n') | |
.join(',')} or ${CACHE_DEPENDENCY_BACKUP_PATH}` | |
: this.cacheDependencyPath.split('\n').join(','); | |
throw new Error( | |
`No file in ${process.cwd()} matched to [${file}], make sure you have checked out the target repository` | |
); | |
} | |
const cachePath = await this.getCacheGlobalDirectories(); | |
core.saveState(State.CACHE_PATHS, cachePath); | |
core.saveState(State.STATE_CACHE_PRIMARY_KEY, primaryKey); | |
const matchedKey = await cache.restoreCache( | |
cachePath, | |
primaryKey, | |
restoreKey | |
); | |
await this.handleLoadedCache(); | |
this.handleMatchResult(matchedKey, primaryKey); | |
} | |
public handleMatchResult(matchedKey: string | undefined, primaryKey: string) { | |
if (matchedKey) { | |
core.saveState(State.CACHE_MATCHED_KEY, matchedKey); | |
core.info(`Cache restored from key: ${matchedKey}`); | |
} else { | |
core.info(`${this.packageManager} cache is not found`); | |
} | |
core.setOutput('cache-hit', matchedKey === primaryKey); | |
} | |
} | |
export default CacheDistributor; |