cache packages

This commit is contained in:
ruki
2025-01-15 22:49:46 +08:00 Unverified
parent 5ac639794f
commit 797cb16346
3 changed files with 115 additions and 87700 deletions
+82 -2
View File
@@ -5,7 +5,87 @@ import * as cache from '@actions/cache';
import * as os from 'os';
import * as fs from 'fs';
import * as path from 'path';
import * as fsutils from './fsutils';
export async function loadPackageCache(): Promise<void> {}
function getPackageCacheKey(): 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'
}`;
}
function getPackageCachePath(): string {
let packageCachePath = "";
await exec('xmake', ['l', 'core.package.package.installdir'], (error: Error | null, stdout: string, stderr: string) => {
if (error) {
core.info(`exec error: ${error}`);
return;
}
packageCachePath = stdout;
});
core.info(`packageCachePath: ${packageCachePath}`);
return packageCachePath;
}
function getPackageCacheFolder(): string {
return '.xmake-package-cache';
}
export async function loadPackageCache(): Promise<void> {
const packageCache = core.getBooleanInput('package-cache');
if (!packageCache) {
return;
}
const packageCacheFolder = getPackageCacheFolder();
const packageCacheKey = getPackageCacheKey();
const packageCachePath = getPackageCachePath();
if (!packageCachePath || packageCachePath === '') {
return;
}
if (packageCacheFolder && process.env.GITHUB_WORKSPACE) {
const fullCachePath = path.join(process.env.GITHUB_WORKSPACE, packageCacheFolder);
const filepath = path.join(fullCachePath, 'package_cache_saved.txt');
if (!fsutils.isFile(filepath)) {
core.info(`Restore package cache path: ${fullCachePath} to ${packageCachePath}, key: ${packageCacheKey}`);
await cache.restoreCache([packageCacheFolder], packageCacheKey);
}
if (fsutils.isFile(filepath)) {
await io.cp(fullCachePath, packageCachePath, {
recursive: true,
});
} else {
core.warning(`No cached files found at path "${fullCachePath}".`);
await io.rmRF(fullCachePath);
}
}
}
export async function savePackageCache(): Promise<void> {
const packageCache = core.getBooleanInput('package-cache');
if (!packageCache) {
return;
}
const packageCacheFolder = getPackageCacheFolder();
const packageCacheKey = getPackageCacheKey();
const packageCachePath = getPackageCachePath();
if (!packageCachePath || packageCachePath === '') {
return;
}
if (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);
}
}
export async function savePackageCache(): Promise<void> {}