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 cache from '@actions/cache';
import * as core from '@actions/core'; import * as core from '@actions/core';
import {CACHE_DEPENDENCY_BACKUP_PATH} from './constants';
export enum State { export enum State {
STATE_CACHE_PRIMARY_KEY = 'cache-primary-key', STATE_CACHE_PRIMARY_KEY = 'cache-primary-key',
@ -11,24 +10,19 @@ export enum State {
abstract class CacheDistributor { abstract class CacheDistributor {
protected CACHE_KEY_PREFIX = 'setup-python'; protected CACHE_KEY_PREFIX = 'setup-python';
protected abstract readonly packageManager: string; protected abstract readonly packageManager: string;
protected abstract readonly cacheDependencyPath: string;
protected abstract getCacheGlobalDirectories(): Promise<string[]>; protected abstract getCacheGlobalDirectories(): Promise<string[]>;
protected abstract computeKeys(): Promise<{ protected abstract computeKeys(): Promise<{
primaryKey: string; primaryKey: string;
restoreKey: string[] | undefined; restoreKey: string[] | undefined;
cacheDependencyPath: string;
}>; }>;
protected async handleLoadedCache() {} protected async handleLoadedCache() {}
public async restoreCache() { public async restoreCache() {
const {primaryKey, restoreKey} = await this.computeKeys(); const {primaryKey, restoreKey, cacheDependencyPath} = await this.computeKeys();
if (primaryKey.endsWith('-')) { if (primaryKey.endsWith('-')) {
const file = const file = cacheDependencyPath.split('\n').join(',');
this.packageManager === 'pip'
? `${this.cacheDependencyPath
.split('\n')
.join(',')} or ${CACHE_DEPENDENCY_BACKUP_PATH}`
: this.cacheDependencyPath.split('\n').join(',');
throw new Error( throw new Error(
`No file in ${process.cwd()} matched to [${file}], make sure you have checked out the target repository` `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 CacheDistributor from './cache-distributor';
import {getLinuxInfo, IS_LINUX, IS_WINDOWS} from '../utils'; import {getLinuxInfo, IS_LINUX, IS_WINDOWS} from '../utils';
import {CACHE_DEPENDENCY_BACKUP_PATH} from './constants';
class PipCache extends CacheDistributor { class PipCache extends CacheDistributor {
private cacheDependencyBackupPath: string = CACHE_DEPENDENCY_BACKUP_PATH; private cacheDependencyBackupPath = '**/pyproject.toml';
protected readonly packageManager = 'pip'; protected readonly packageManager = 'pip';
constructor( constructor(
@ -60,9 +59,12 @@ class PipCache extends CacheDistributor {
} }
protected async computeKeys() { protected async computeKeys() {
const hash = let cacheDependencyPath = this.cacheDependencyPath;
(await glob.hashFiles(this.cacheDependencyPath)) || let hash = await glob.hashFiles(this.cacheDependencyPath);
(await glob.hashFiles(this.cacheDependencyBackupPath)); if(!hash) {
hash = await glob.hashFiles(this.cacheDependencyBackupPath);
cacheDependencyPath = this.cacheDependencyBackupPath;
}
let primaryKey = ''; let primaryKey = '';
let restoreKey = ''; let restoreKey = '';
@ -77,7 +79,8 @@ class PipCache extends CacheDistributor {
return { return {
primaryKey, primaryKey,
restoreKey: [restoreKey] restoreKey: [restoreKey],
cacheDependencyPath,
}; };
} }
} }

View File

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

View File

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