Skip to content

Commit

Permalink
fix: toolcache (it never worked)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Sep 5, 2024
1 parent 0ff5c0f commit eb61069
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
2 changes: 1 addition & 1 deletion dist/setup/index.js

Large diffs are not rendered by default.

34 changes: 20 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { cacheDir, downloadTool, extractZip, find as findTool } from '@actions/t
import { mkdirP, rmRF } from '@actions/io';
import { coerce } from 'semver';
import { SVNClient } from '@taiyosen/easy-svn';
import { downloadAsText, fileExists, isDir, isGHES } from './utils';
import { downloadAsText, isDir, isGHES } from './utils';
import { getWordPressDownloadUrl, getWordPressTestLibraryBaseUrl, resolveWordPressVersion } from './wputils';

interface Inputs {
Expand Down Expand Up @@ -62,6 +62,14 @@ function resolveSemVer(version: string, inputs: Inputs): void {
}
}

async function cacheTool(path: string, name: string, inputs: Inputs): Promise<void> {
if (inputs.has_toolcache) {
info(`📦 Caching ${path} as ${name} ${inputs.semver}…`);
const dir = await cacheDir(path, name, inputs.semver!);
info(`ℹ️ Saved cache to ${dir}`);
}
}

/**
* Find the cached version of the given tool.
*
Expand All @@ -72,18 +80,18 @@ function resolveSemVer(version: string, inputs: Inputs): void {
* @returns {Promise<boolean>} True if the tool was found in the cache, false otherwise.
*/
async function findCached(name: string, tool: string, inputs: Inputs): Promise<boolean> {
if (!inputs.has_toolcache && inputs.semver) {
if (inputs.has_toolcache && inputs.semver) {
info(`ℹ️ Checking tool cache for ${tool} ${inputs.semver}…`);
const cachePath = findTool(tool, inputs.semver);
if (cachePath) {
const resolvedPath = resolve(cachePath);

const configFile = join(resolvedPath, 'wp-tests-config-sample.php');
if (await fileExists(configFile)) {
info(`🚀 Using cached ${name} from ${resolvedPath}`);
await symlink(resolvedPath, `${inputs.dir}/${tool}`);
return true;
}
info(`🚀 Using cached ${name} from ${resolvedPath}`);
await symlink(resolvedPath, `${inputs.dir}/${tool}`);
return true;
}

info(`😔 ${tool} ${inputs.semver} was not found in the tool cache`);
}

if (inputs.has_cache) {
Expand All @@ -94,6 +102,7 @@ async function findCached(name: string, tool: string, inputs: Inputs): Promise<b

if (result) {
info(`🚀 Using cached ${name}, key is ${key}`);
await cacheTool(join(inputs.dir, tool), tool, inputs);
return true;
}

Expand Down Expand Up @@ -121,9 +130,8 @@ async function downloadWordPress(url: string, inputs: Inputs): Promise<void> {
info(`📥 Downloading WordPress…`);
const file = await downloadTool(url, dest);
const targetDir = await extractZip(file, inputs.dir);
if (inputs.has_toolcache) {
await cacheDir(`${targetDir}/wordpress`, 'wordpress', inputs.semver!);
}

await cacheTool(`${targetDir}/wordpress`, 'wordpress', inputs);
} finally {
await rmRF(dest);
}
Expand Down Expand Up @@ -157,9 +165,7 @@ async function downloadTestLibrary(url: string, inputs: Inputs): Promise<void> {
writeFile(join(inputs.dir, 'wordpress-tests-lib', 'wp-tests-config-sample.php'), config),
]);

if (inputs.has_toolcache) {
await cacheDir(`${inputs.dir}/wordpress-tests-lib`, 'wordpress-tests-lib', inputs.semver!);
}
await cacheTool(`${inputs.dir}/wordpress-tests-lib`, 'wordpress-tests-lib', inputs);
}

/**
Expand Down
16 changes: 0 additions & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,6 @@ export async function isDir(path: string): Promise<boolean> {
}
}

/**
* Check if a given path is a file.
*
* @async
* @param {string} path The path to check.
* @returns {Promise<boolean>} A promise that resolves to true if the path is a file, false otherwise.
*/
export async function fileExists(path: string): Promise<boolean> {
try {
const stats = await stat(path);
return stats.isFile();
} catch {
return false;
}
}

/**
* Download the content of a URL as a text string.
*
Expand Down

0 comments on commit eb61069

Please sign in to comment.