Skip to content

Commit 5ed506e

Browse files
committed
Code improvemnt: Rm CacheDistributor.cacheDependencyPath field and pass back from computeKeys instead, simplifying CacheDistributor.restoreCache error check. Rm now uneeded constant.ts file.
1 parent 3efe843 commit 5ed506e

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

Diff for: src/cache-distributions/cache-distributor.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as cache from '@actions/cache';
22
import * as core from '@actions/core';
3-
import {CACHE_DEPENDENCY_BACKUP_PATH} from './constants';
43

54
export enum State {
65
STATE_CACHE_PRIMARY_KEY = 'cache-primary-key',
@@ -11,24 +10,19 @@ export enum State {
1110
abstract class CacheDistributor {
1211
protected CACHE_KEY_PREFIX = 'setup-python';
1312
protected abstract readonly packageManager: string;
14-
protected abstract readonly cacheDependencyPath: string;
1513

1614
protected abstract getCacheGlobalDirectories(): Promise<string[]>;
1715
protected abstract computeKeys(): Promise<{
1816
primaryKey: string;
1917
restoreKey: string[] | undefined;
18+
cacheDependencyPath: string;
2019
}>;
2120
protected async handleLoadedCache() {}
2221

2322
public async restoreCache() {
24-
const {primaryKey, restoreKey} = await this.computeKeys();
23+
const {primaryKey, restoreKey, cacheDependencyPath} = await this.computeKeys();
2524
if (primaryKey.endsWith('-')) {
26-
const file =
27-
this.packageManager === 'pip'
28-
? `${this.cacheDependencyPath
29-
.split('\n')
30-
.join(',')} or ${CACHE_DEPENDENCY_BACKUP_PATH}`
31-
: this.cacheDependencyPath.split('\n').join(',');
25+
const file = cacheDependencyPath.split('\n').join(',');
3226
throw new Error(
3327
`No file in ${process.cwd()} matched to [${file}], make sure you have checked out the target repository`
3428
);

Diff for: src/cache-distributions/constants.ts

-1
This file was deleted.

Diff for: src/cache-distributions/pip-cache.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import os from 'os';
88

99
import CacheDistributor from './cache-distributor';
1010
import {getLinuxInfo, IS_LINUX, IS_WINDOWS} from '../utils';
11-
import {CACHE_DEPENDENCY_BACKUP_PATH} from './constants';
1211

1312
class PipCache extends CacheDistributor {
14-
private cacheDependencyBackupPath: string = CACHE_DEPENDENCY_BACKUP_PATH;
13+
private cacheDependencyBackupPath = '**/pyproject.toml';
1514
protected readonly packageManager = 'pip';
1615

1716
constructor(
@@ -60,9 +59,12 @@ class PipCache extends CacheDistributor {
6059
}
6160

6261
protected async computeKeys() {
63-
const hash =
64-
(await glob.hashFiles(this.cacheDependencyPath)) ||
65-
(await glob.hashFiles(this.cacheDependencyBackupPath));
62+
let cacheDependencyPath = this.cacheDependencyPath;
63+
let hash = await glob.hashFiles(this.cacheDependencyPath);
64+
if(!hash) {
65+
hash = await glob.hashFiles(this.cacheDependencyBackupPath);
66+
cacheDependencyPath = this.cacheDependencyBackupPath;
67+
}
6668
let primaryKey = '';
6769
let restoreKey = '';
6870

@@ -77,7 +79,8 @@ class PipCache extends CacheDistributor {
7779

7880
return {
7981
primaryKey,
80-
restoreKey: [restoreKey]
82+
restoreKey: [restoreKey],
83+
cacheDependencyPath,
8184
};
8285
}
8386
}

Diff for: src/cache-distributions/pipenv-cache.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class PipenvCache extends CacheDistributor {
3838
const restoreKey = undefined;
3939
return {
4040
primaryKey,
41-
restoreKey
41+
restoreKey,
42+
cacheDependencyPath: this.cacheDependencyPath,
4243
};
4344
}
4445
}

Diff for: src/cache-distributions/poetry-cache.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class PoetryCache extends CacheDistributor {
5454
const restoreKey = undefined;
5555
return {
5656
primaryKey,
57-
restoreKey
57+
restoreKey,
58+
cacheDependencyPath: this.cacheDependencyPath,
5859
};
5960
}
6061

0 commit comments

Comments
 (0)