Update: Added function that find binary.
Some checks failed
Test Action / test (push) Has been cancelled

This commit is contained in:
2026-01-11 01:32:12 +01:00
parent 95fbe95099
commit 2928ae62d3
3 changed files with 88 additions and 46 deletions

View File

@@ -8,6 +8,9 @@ inputs:
file-name: file-name:
description: 'The name or regex pattern (prefixed with ~) of the asset file to download.' description: 'The name or regex pattern (prefixed with ~) of the asset file to download.'
required: false required: false
binary-name:
description: 'The name or regex pattern (prefixed with ~) of the binary to search for within the asset. Defaults to the repository name.'
required: false
file-type: file-type:
description: 'The type of the file to be downloaded (archive, package, or custom regex).' description: 'The type of the file to be downloaded (archive, package, or custom regex).'
required: false required: false

64
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -4,10 +4,41 @@ import * as path from 'path';
import * as os from 'os'; import * as os from 'os';
import * as fs from 'fs'; import * as fs from 'fs';
function findBinary(dir: string, pattern: string | RegExp, debug: boolean): string | undefined {
const items = fs.readdirSync(dir);
if (debug) {
core.info(`Searching for binary in ${dir}...`);
items.forEach(item => core.info(` - ${item}`));
}
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
const found = findBinary(fullPath, pattern, debug);
if (found) return found;
} else {
let isMatch = false;
if (pattern instanceof RegExp) {
isMatch = pattern.test(item);
} else {
isMatch = item === pattern;
// On Windows, also check for .exe extension if the pattern doesn't have it
if (!isMatch && process.platform === 'win32' && !pattern.toLowerCase().endsWith('.exe')) {
isMatch = item.toLowerCase() === `${pattern.toLowerCase()}.exe`;
}
}
if (isMatch) return fullPath;
}
}
return undefined;
}
async function run() { async function run() {
try { try {
const repoName = core.getInput('repo-name', { required: true }); const repoName = core.getInput('repo-name', { required: true });
let fileName = core.getInput('file-name'); let fileName = core.getInput('file-name');
const binaryInput = core.getInput('binary-name');
const fileType = core.getInput('file-type') || 'archive'; const fileType = core.getInput('file-type') || 'archive';
const debug = core.getBooleanInput('debug'); const debug = core.getBooleanInput('debug');
const token = core.getInput('token') || process.env.GITHUB_TOKEN; const token = core.getInput('token') || process.env.GITHUB_TOKEN;
@@ -111,6 +142,7 @@ async function run() {
const version = data.tag_name.replace(/^v/, ''); const version = data.tag_name.replace(/^v/, '');
const toolName = repoName.split('/').pop() || repoName; const toolName = repoName.split('/').pop() || repoName;
const binaryName = binaryInput || toolName;
// Check if the tool is already in the cache // Check if the tool is already in the cache
const cachedDir = tc.find(toolName, version, arch); const cachedDir = tc.find(toolName, version, arch);
@@ -153,22 +185,29 @@ async function run() {
} }
} }
// Handle nested directories in archives // Find the binary within the extracted/prepared directory
if (/\.(zip|tar(\.gz)?|tgz|7z)$/i.test(nameLower)) { let binaryPattern: string | RegExp;
const items = fs.readdirSync(toolDir); if (binaryName.startsWith('~')) {
binaryPattern = new RegExp(binaryName.substring(1), 'i');
if (debug) { core.info(`Searching for binary matching regex: ${binaryName.substring(1)}`);
core.info(`Contents of ${toolDir}:`); } else {
items.forEach(item => core.info(` - ${item}`)); binaryPattern = binaryName;
core.info(`Searching for binary named: ${binaryName}`);
} }
if (items.length === 1 && fs.statSync(path.join(toolDir, items[0])).isDirectory()) { const binaryPath = findBinary(toolDir, binaryPattern, debug);
core.info(`Detected single root directory in archive, descending into: ${items[0]}`); if (!binaryPath) {
toolDir = path.join(toolDir, items[0]); throw new Error(`Could not find binary "${binaryName}" in the extracted asset.`);
}
} }
core.info(`Tool extracted/located at ${toolDir}`); // The tool directory is the one containing the binary
toolDir = path.dirname(binaryPath);
core.info(`Binary found at ${binaryPath}. Setting tool directory to ${toolDir}`);
// Make binary executable just in case it's not
if (process.platform !== 'win32') {
fs.chmodSync(binaryPath, '755');
}
// Cache the tool // Cache the tool
const finalCachedDir = await tc.cacheDir(toolDir, toolName, version, arch); const finalCachedDir = await tc.cacheDir(toolDir, toolName, version, arch);