Code improvemnt: Rm CacheDistributor.cacheDependencyPath field and pass back from computeKeys instead, simplifying CacheDistributor.restoreCache error check. Rm now uneeded constant.ts file.

This commit is contained in:
Sam Pinkus 2025-01-08 16:37:25 +10:00
parent 3efe843c7b
commit 5ed506eb2d
5 changed files with 16 additions and 18 deletions

View File

@ -1,6 +1,5 @@
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',
@ -11,24 +10,19 @@ export enum State {
abstract class CacheDistributor {
protected CACHE_KEY_PREFIX = 'setup-python';
protected abstract readonly packageManager: string;
protected abstract readonly cacheDependencyPath: string;
protected abstract getCacheGlobalDirectories(): Promise<string[]>;
protected abstract computeKeys(): Promise<{
primaryKey: string;
restoreKey: string[] | undefined;
cacheDependencyPath: string;
}>;
protected async handleLoadedCache() {}
public async restoreCache() {
const {primaryKey, restoreKey} = await this.computeKeys();
const {primaryKey, restoreKey, cacheDependencyPath} = 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(',');
const file = cacheDependencyPath.split('\n').join(',');
throw new Error(
`No file in ${process.cwd()} matched to [${file}], make sure you have checked out the target repository`
);

View File

@ -1 +0,0 @@
export const CACHE_DEPENDENCY_BACKUP_PATH = '**/pyproject.toml';

View File

@ -8,10 +8,9 @@ import os from 'os';
import CacheDistributor from './cache-distributor';
import {getLinuxInfo, IS_LINUX, IS_WINDOWS} from '../utils';
import {CACHE_DEPENDENCY_BACKUP_PATH} from './constants';
class PipCache extends CacheDistributor {
private cacheDependencyBackupPath: string = CACHE_DEPENDENCY_BACKUP_PATH;
private cacheDependencyBackupPath = '**/pyproject.toml';
protected readonly packageManager = 'pip';
constructor(
@ -60,9 +59,12 @@ class PipCache extends CacheDistributor {
}
protected async computeKeys() {
const hash =
(await glob.hashFiles(this.cacheDependencyPath)) ||
(await glob.hashFiles(this.cacheDependencyBackupPath));
let cacheDependencyPath = this.cacheDependencyPath;
let hash = await glob.hashFiles(this.cacheDependencyPath);
if(!hash) {
hash = await glob.hashFiles(this.cacheDependencyBackupPath);
cacheDependencyPath = this.cacheDependencyBackupPath;
}
let primaryKey = '';
let restoreKey = '';
@ -77,7 +79,8 @@ class PipCache extends CacheDistributor {
return {
primaryKey,
restoreKey: [restoreKey]
restoreKey: [restoreKey],
cacheDependencyPath,
};
}
}

View File

@ -38,7 +38,8 @@ class PipenvCache extends CacheDistributor {
const restoreKey = undefined;
return {
primaryKey,
restoreKey
restoreKey,
cacheDependencyPath: this.cacheDependencyPath,
};
}
}

View File

@ -54,7 +54,8 @@ class PoetryCache extends CacheDistributor {
const restoreKey = undefined;
return {
primaryKey,
restoreKey
restoreKey,
cacheDependencyPath: this.cacheDependencyPath,
};
}