improve package cache
This commit is contained in:
+36
-7
@@ -6,12 +6,38 @@ import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import * as fsutils from './fsutils';
|
||||
|
||||
function getPackageCacheKey(): string {
|
||||
function getProjectRootPath(): string {
|
||||
let projectRootPath = core.getInput('project-path');
|
||||
if (!projectRootPath) {
|
||||
projectRootPath = process.cwd();
|
||||
}
|
||||
projectRootPath = projectRootPath.trim();
|
||||
if (projectRootPath && projectRootPath !== '' && !path.isAbsolute(projectRootPath)) {
|
||||
projectRootPath = path.join(process.cwd(), projectRootPath);
|
||||
}
|
||||
return projectRootPath;
|
||||
}
|
||||
|
||||
async function getPackageCacheKey(): Promise<string> {
|
||||
let packageCacheKey = core.getInput('package-cache-key');
|
||||
if (!packageCacheKey) {
|
||||
packageCacheKey = '';
|
||||
}
|
||||
return `xmake-package-cache-${packageCacheKey}-${os.arch()}-${os.platform()}-${process.env.RUNNER_OS ?? 'unknown'}`;
|
||||
let packageCacheHash = '';
|
||||
const projectRootPath = getProjectRootPath();
|
||||
if (projectRootPath && projectRootPath !== '' && fsutils.isDir(projectRootPath)) {
|
||||
const options: ExecOptions = {};
|
||||
options.cwd = projectRootPath;
|
||||
options.listeners = {
|
||||
stdout: (data: Buffer) => {
|
||||
packageCacheHash += data.toString();
|
||||
},
|
||||
};
|
||||
await exec('xmake', ['repo', '--update']);
|
||||
await exec('xmake', ['l', 'utils.ci.packageskey'], options);
|
||||
packageCacheHash = packageCacheHash.trim();
|
||||
}
|
||||
return `xmake-package-cache-${packageCacheKey}-${packageCacheHash}-${os.arch()}-${os.platform()}-${process.env.RUNNER_OS ?? 'unknown'}`;
|
||||
}
|
||||
|
||||
async function getPackageCachePath(): Promise<string> {
|
||||
@@ -24,7 +50,6 @@ async function getPackageCachePath(): Promise<string> {
|
||||
};
|
||||
await exec('xmake', ['l', '-c', 'import("core.package.package"); print(package.installdir())'], options);
|
||||
packageCachePath = packageCachePath.trim();
|
||||
core.info(`packageCachePath: ${packageCachePath}`);
|
||||
return packageCachePath;
|
||||
}
|
||||
|
||||
@@ -39,7 +64,7 @@ export async function loadPackageCache(): Promise<void> {
|
||||
}
|
||||
|
||||
const packageCacheFolder = getPackageCacheFolder();
|
||||
const packageCacheKey = getPackageCacheKey();
|
||||
const packageCacheKey = await getPackageCacheKey();
|
||||
const packageCachePath = await getPackageCachePath();
|
||||
if (!packageCachePath || packageCachePath === '') {
|
||||
return;
|
||||
@@ -53,9 +78,13 @@ export async function loadPackageCache(): Promise<void> {
|
||||
await cache.restoreCache([packageCacheFolder], packageCacheKey);
|
||||
}
|
||||
if (fsutils.isFile(filepath)) {
|
||||
if (fsutils.isDir(packageCachePath)) {
|
||||
await io.rmRF(packageCachePath);
|
||||
}
|
||||
await io.cp(fullCachePath, packageCachePath, {
|
||||
recursive: true,
|
||||
});
|
||||
core.saveState('hitPackageCache', 'true');
|
||||
} else {
|
||||
core.warning(`No cached files found at path "${fullCachePath}".`);
|
||||
await io.rmRF(fullCachePath);
|
||||
@@ -70,19 +99,19 @@ export async function savePackageCache(): Promise<void> {
|
||||
}
|
||||
|
||||
const packageCacheFolder = getPackageCacheFolder();
|
||||
const packageCacheKey = getPackageCacheKey();
|
||||
const packageCacheKey = await getPackageCacheKey();
|
||||
const packageCachePath = await getPackageCachePath();
|
||||
if (!packageCachePath || packageCachePath === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (packageCacheFolder && process.env.GITHUB_WORKSPACE && fsutils.isDir(packageCachePath)) {
|
||||
const hitPackageCache = !!core.getState('hitPackageCache');
|
||||
if (!hitPackageCache && packageCacheFolder && process.env.GITHUB_WORKSPACE && fsutils.isDir(packageCachePath)) {
|
||||
const fullCachePath = path.join(process.env.GITHUB_WORKSPACE, packageCacheFolder);
|
||||
core.info(`Save package cache path: ${packageCachePath} to ${fullCachePath}, key: ${packageCacheKey}`);
|
||||
await io.cp(packageCachePath, fullCachePath, {
|
||||
recursive: true,
|
||||
});
|
||||
await exec('xmake', ['l', 'os.touch', path.join(fullCachePath, 'package_cache_saved.txt')]);
|
||||
await cache.saveCache([packageCacheFolder], packageCacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user