fix: fix tool destination file names + automatic extract functions

This commit is contained in:
Amin Yahyaabadi 2024-08-21 15:10:46 -07:00
parent 41c74d00e3
commit 20c025d4d7
No known key found for this signature in database
GPG Key ID: F52AF77F636088F0
14 changed files with 60 additions and 74 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,6 @@
import { addExeExt } from "patha" import { addExeExt } from "patha"
import semverCoerce from "semver/functions/coerce" import semverCoerce from "semver/functions/coerce"
import semverLte from "semver/functions/lte" import semverLte from "semver/functions/lte"
import { extractTarByExe, extractZip } from "../utils/setup/extract.js"
import { type InstallationInfo, type PackageInfo, setupBin } from "../utils/setup/setupBin.js" import { type InstallationInfo, type PackageInfo, setupBin } from "../utils/setup/setupBin.js"
/** Get the platform data for cmake */ /** Get the platform data for cmake */
@ -21,7 +20,6 @@ function getCmakePackageInfo(version: string, platform: NodeJS.Platform, arch: s
binRelativeDir: "bin/", binRelativeDir: "bin/",
binFileName: addExeExt("cmake"), binFileName: addExeExt("cmake"),
extractedFolderName: folderName, extractedFolderName: folderName,
extractFunction: extractZip,
url: `https://github.com/Kitware/CMake/releases/download/v${version}/${folderName}.zip`, url: `https://github.com/Kitware/CMake/releases/download/v${version}/${folderName}.zip`,
} }
} }
@ -33,7 +31,6 @@ function getCmakePackageInfo(version: string, platform: NodeJS.Platform, arch: s
binRelativeDir: "CMake.app/Contents/bin/", binRelativeDir: "CMake.app/Contents/bin/",
binFileName: addExeExt("cmake"), binFileName: addExeExt("cmake"),
extractedFolderName: folderName, extractedFolderName: folderName,
extractFunction: extractTarByExe,
url: `https://github.com/Kitware/CMake/releases/download/v${version}/${folderName}.tar.gz`, url: `https://github.com/Kitware/CMake/releases/download/v${version}/${folderName}.tar.gz`,
} }
} }
@ -50,7 +47,6 @@ function getCmakePackageInfo(version: string, platform: NodeJS.Platform, arch: s
binRelativeDir: "bin/", binRelativeDir: "bin/",
binFileName: addExeExt("cmake"), binFileName: addExeExt("cmake"),
extractedFolderName: folderName, extractedFolderName: folderName,
extractFunction: extractTarByExe,
url: `https://github.com/Kitware/CMake/releases/download/v${version}/${folderName}.tar.gz`, url: `https://github.com/Kitware/CMake/releases/download/v${version}/${folderName}.tar.gz`,
} }
} }

View File

@ -3,7 +3,6 @@ import { addPath } from "envosman"
import { addExeExt, join } from "patha" import { addExeExt, join } from "patha"
import { installAptPack } from "setup-apt" import { installAptPack } from "setup-apt"
import { setupGraphviz } from "../graphviz/graphviz.js" import { setupGraphviz } from "../graphviz/graphviz.js"
import { extractTar, extractZip } from "../utils/setup/extract.js"
import { type InstallationInfo, type PackageInfo, setupBin } from "../utils/setup/setupBin.js" import { type InstallationInfo, type PackageInfo, setupBin } from "../utils/setup/setupBin.js"
import { setupBrewPack } from "../utils/setup/setupBrewPack.js" import { setupBrewPack } from "../utils/setup/setupBrewPack.js"
import { setupChocoPack } from "../utils/setup/setupChocoPack.js" import { setupChocoPack } from "../utils/setup/setupChocoPack.js"
@ -31,7 +30,6 @@ function getDoxygenPackageInfo(version: string, platform: NodeJS.Platform, _arch
binRelativeDir: "bin/", binRelativeDir: "bin/",
binFileName: addExeExt("doxygen"), binFileName: addExeExt("doxygen"),
extractedFolderName: folderName, extractedFolderName: folderName,
extractFunction: extractTar,
url: `https://www.doxygen.nl/files/${folderName}.linux.bin.tar.gz`, url: `https://www.doxygen.nl/files/${folderName}.linux.bin.tar.gz`,
} }
} }
@ -41,7 +39,6 @@ function getDoxygenPackageInfo(version: string, platform: NodeJS.Platform, _arch
binRelativeDir: "", binRelativeDir: "",
binFileName: addExeExt("doxygen"), binFileName: addExeExt("doxygen"),
extractedFolderName: folderName, extractedFolderName: folderName,
extractFunction: extractZip,
url: `https://www.doxygen.nl/files/${folderName}.windows.x64.bin.zip`, url: `https://www.doxygen.nl/files/${folderName}.windows.x64.bin.zip`,
} }
} }

View File

@ -23,7 +23,6 @@ function getDownloadKcovPackageInfo(version: string): PackageInfo {
extractedFolderName: "", extractedFolderName: "",
binRelativeDir: "usr/local/bin", binRelativeDir: "usr/local/bin",
binFileName: addExeExt("kcov"), binFileName: addExeExt("kcov"),
extractFunction: extractTarByExe,
} }
} }

View File

@ -1,5 +1,4 @@
import { addExeExt } from "patha" import { addExeExt } from "patha"
import { extractZip } from "../utils/setup/extract.js"
import { type InstallationInfo, type PackageInfo, setupBin } from "../utils/setup/setupBin.js" import { type InstallationInfo, type PackageInfo, setupBin } from "../utils/setup/setupBin.js"
/** Get the platform name Ninja uses in their download links */ /** Get the platform name Ninja uses in their download links */
@ -24,7 +23,6 @@ function getNinjaPackageInfo(version: string, platform: NodeJS.Platform, _arch:
binRelativeDir: "", binRelativeDir: "",
binFileName: addExeExt("ninja"), binFileName: addExeExt("ninja"),
extractedFolderName: "", extractedFolderName: "",
extractFunction: extractZip,
url: `https://github.com/ninja-build/ninja/releases/download/v${version}/ninja-${ninjaPlatform}.zip`, url: `https://github.com/ninja-build/ninja/releases/download/v${version}/ninja-${ninjaPlatform}.zip`,
} }
} }

View File

@ -8,7 +8,6 @@ import { hasDnf } from "../utils/env/hasDnf.js"
import { isArch } from "../utils/env/isArch.js" import { isArch } from "../utils/env/isArch.js"
import { isUbuntu } from "../utils/env/isUbuntu.js" import { isUbuntu } from "../utils/env/isUbuntu.js"
import { ubuntuVersion } from "../utils/env/ubuntu_version.js" import { ubuntuVersion } from "../utils/env/ubuntu_version.js"
import { extractTarByExe, extractZip } from "../utils/setup/extract.js"
import { type PackageInfo, setupBin } from "../utils/setup/setupBin.js" import { type PackageInfo, setupBin } from "../utils/setup/setupBin.js"
import { setupBrewPack } from "../utils/setup/setupBrewPack.js" import { setupBrewPack } from "../utils/setup/setupBrewPack.js"
import { setupChocoPack } from "../utils/setup/setupChocoPack.js" import { setupChocoPack } from "../utils/setup/setupChocoPack.js"
@ -17,34 +16,31 @@ import { setupPacmanPack } from "../utils/setup/setupPacmanPack.js"
/** Get the platform data for cmake */ /** Get the platform data for cmake */
function getPowerShellPackageInfo(version: string, platform: NodeJS.Platform, arch: string): PackageInfo { function getPowerShellPackageInfo(version: string, platform: NodeJS.Platform, arch: string): PackageInfo {
const binFileName = addExeExt("pwsh") return {
url: getPowershellUrl(platform, arch, version),
binRelativeDir: "",
binFileName: addExeExt("pwsh"),
extractedFolderName: "",
}
}
function getPowershellUrl(
platform: string,
arch: string,
version: string,
) {
switch (platform) { switch (platform) {
case "win32": { case "win32": {
const osArchStr = (["ia32", "x86", "i386", "x32"].includes(arch)) const osArchStr = (["ia32", "x86", "i386", "x32"].includes(arch))
? "win-x86" ? "win-x86"
: "win-x64" : "win-x64"
return { return `https://github.com/PowerShell/PowerShell/releases/download/v${version}/PowerShell-${version}-${osArchStr}.zip`
binRelativeDir: "",
binFileName,
extractedFolderName: "",
extractFunction: extractZip,
url:
`https://github.com/PowerShell/PowerShell/releases/download/v${version}/PowerShell-${version}-${osArchStr}.zip`,
}
} }
case "darwin": { case "darwin": {
const osArchStr = ["arm", "arm64"].includes(arch) ? "osx-arm64" : "osx-x64" const osArchStr = ["arm", "arm64"].includes(arch) ? "osx-arm64" : "osx-x64"
return { return `https://github.com/PowerShell/PowerShell/releases/download/v${version}/powershell-${version}-${osArchStr}.tar.gz`
binRelativeDir: "",
binFileName,
extractedFolderName: "",
extractFunction: extractTarByExe,
url:
`https://github.com/PowerShell/PowerShell/releases/download/v${version}/powershell-${version}-${osArchStr}.tar.gz`,
}
} }
case "linux": { case "linux": {
const archMap = { const archMap = {
@ -56,15 +52,7 @@ function getPowerShellPackageInfo(version: string, platform: NodeJS.Platform, ar
} as Record<string, string | undefined> } as Record<string, string | undefined>
const osArchStr = archMap[arch] ?? "linux-x64" const osArchStr = archMap[arch] ?? "linux-x64"
// TODO support musl // TODO support musl
return `https://github.com/PowerShell/PowerShell/releases/download/v${version}/powershell-${version}-${osArchStr}.tar.gz`
return {
binRelativeDir: "",
binFileName,
extractedFolderName: "",
extractFunction: extractTarByExe,
url:
`https://github.com/PowerShell/PowerShell/releases/download/v${version}/powershell-${version}-${osArchStr}.tar.gz`,
}
} }
default: default:
throw new Error(`Unsupported platform '${platform}'`) throw new Error(`Unsupported platform '${platform}'`)

View File

@ -1,5 +1,4 @@
import { addExeExt } from "patha" import { addExeExt } from "patha"
import { extractTarByExe, extractZip } from "../utils/setup/extract.js"
import { type InstallationInfo, type PackageInfo, setupBin } from "../utils/setup/setupBin.js" import { type InstallationInfo, type PackageInfo, setupBin } from "../utils/setup/setupBin.js"
/** Get the platform name task uses in their download links */ /** Get the platform name task uses in their download links */
@ -31,13 +30,11 @@ function getTaskArch(arch: string) {
function getTaskPackageInfo(version: string, platform: NodeJS.Platform, arch: string): PackageInfo { function getTaskPackageInfo(version: string, platform: NodeJS.Platform, arch: string): PackageInfo {
const taskPlatform = getTaskPlatform(platform) const taskPlatform = getTaskPlatform(platform)
const taskArch = getTaskArch(arch) const taskArch = getTaskArch(arch)
const isZip = platform === "win32" const extension = platform === "win32" ? "zip" : "tar.gz"
const extension = isZip ? "zip" : "tar.gz"
return { return {
binRelativeDir: "", binRelativeDir: "",
binFileName: addExeExt("task"), binFileName: addExeExt("task"),
extractedFolderName: "", extractedFolderName: "",
extractFunction: isZip ? extractZip : extractTarByExe,
url: `https://github.com/go-task/task/releases/download/v${version}/task_${taskPlatform}_${taskArch}.${extension}`, url: `https://github.com/go-task/task/releases/download/v${version}/task_${taskPlatform}_${taskArch}.${extension}`,
} }
} }

View File

@ -13,15 +13,20 @@ import { setupPacmanPack } from "./setupPacmanPack.js"
export { extractTar, extractXar } from "@actions/tool-cache" export { extractTar, extractXar } from "@actions/tool-cache"
export enum ArchiveType { export enum ArchiveType {
TarGz = "tar.gz", Tar = 0,
TarXz = "tar.xz", TarGz = 1,
Zip = "zip", TarXz = 2,
SevenZip = "7z", Zip = 3,
SevenZip = 4,
} }
export function getArchiveType(file: string): ArchiveType { export function getArchiveType(file: string): ArchiveType {
const ext = file.split(".").pop() const ext = file.split(".").pop()
if (ext === "tar") {
return ArchiveType.Tar
}
if (ext === "gz" || ext === "tgz") { if (ext === "gz" || ext === "tgz") {
return ArchiveType.TarGz return ArchiveType.TarGz
} }
@ -34,16 +39,18 @@ export function getArchiveType(file: string): ArchiveType {
return ArchiveType.Zip return ArchiveType.Zip
} }
if (ext === "7z") { if (ext === "7z" || ext === "exe") {
return ArchiveType.SevenZip return ArchiveType.SevenZip
} }
// default to 7z // default to 7z
warning(`Unknown archive type: ${ext}. Defaulting to 7z`)
return ArchiveType.SevenZip return ArchiveType.SevenZip
} }
export function getExtractFunction(archiveType: ArchiveType) { export function getExtractFunction(archiveType: ArchiveType) {
switch (archiveType) { switch (archiveType) {
case ArchiveType.Tar:
case ArchiveType.TarGz: case ArchiveType.TarGz:
return extractTarByExe return extractTarByExe
case ArchiveType.TarXz: case ArchiveType.TarXz:
@ -81,8 +88,15 @@ export function extractExe(file: string, dest: string) {
return extract7Zip(file, dest) return extract7Zip(file, dest)
} }
/// Extract Zip using 7z /// Extract Zip using unzip or 7z
export function extractZip(file: string, dest: string) { export async function extractZip(file: string, dest: string) {
// if unzip is available use it
if (which.sync("unzip", { nothrow: true }) !== null) {
await execa("unzip", [file, "-d", dest], { stdio: "inherit" })
await grantUserWriteAccess(dest)
return dest
}
return extract7Zip(file, dest) return extract7Zip(file, dest)
} }

View File

@ -1,4 +1,5 @@
import { tmpdir } from "os" import { tmpdir } from "os"
import { basename } from "path"
import { cacheDir, downloadTool, find } from "@actions/tool-cache" import { cacheDir, downloadTool, find } from "@actions/tool-cache"
import { GITHUB_ACTIONS } from "ci-info" import { GITHUB_ACTIONS } from "ci-info"
import { info, warning } from "ci-log" import { info, warning } from "ci-log"
@ -8,6 +9,7 @@ import { pathExists } from "path-exists"
import { join } from "patha" import { join } from "patha"
import retry from "retry-as-promised" import retry from "retry-as-promised"
import { maybeGetInput, rcOptions } from "../../cli-options.js" import { maybeGetInput, rcOptions } from "../../cli-options.js"
import { getArchiveType, getExtractFunction } from "./extract.js"
/** A type that describes a package */ /** A type that describes a package */
export type PackageInfo = { export type PackageInfo = {
@ -94,7 +96,7 @@ async function downloadExtractInstall(
version: string, version: string,
url: string, url: string,
setupDir: string, setupDir: string,
extractFunction: PackageInfo["extractFunction"], givenExtractFunction: PackageInfo["extractFunction"],
arch: string, arch: string,
) { ) {
// download ane extract the package into the installation directory. // download ane extract the package into the installation directory.
@ -102,7 +104,10 @@ async function downloadExtractInstall(
try { try {
const downloaded = await tryDownload(name, version, url) const downloaded = await tryDownload(name, version, url)
await extractPackage(downloaded, setupDir, extractFunction) info(`Extracting ${downloaded} to ${setupDir}`)
const extractFunction = givenExtractFunction ?? getExtractFunction(getArchiveType(url))
await extractFunction(downloaded, setupDir)
} catch (err) { } catch (err) {
throw new Error(`Failed to download ${name} ${version} ${arch} from ${url}: ${err}`) throw new Error(`Failed to download ${name} ${version} ${arch} from ${url}: ${err}`)
} }
@ -132,24 +137,16 @@ async function tryDownload(name: string, version: string, url: string) {
info(`Download ${name} ${version}`) info(`Download ${name} ${version}`)
// try to download the package 4 times with 2 seconds delay // try to download the package 4 times with 2 seconds delay
const downloaded = await retry( const downloaded = await retry(
() => { async () => {
return downloadTool(url) const downloadedFilePath = join(process.env.RUNNER_TEMP ?? tmpdir(), `${Date.now()}-${basename(url)}`)
return downloadTool(url, downloadedFilePath)
}, },
{ name: url, max: 4, backoffBase: 2000, report: (err) => info(err) }, { name: url, max: 4, backoffBase: 2000, report: (err) => info(err) },
) )
return downloaded return downloaded
} }
async function extractPackage(
downloaded: string,
setupDir: string,
extractFunction: ((file: string, dest: string) => Promise<unknown>) | undefined,
) {
info(`Extracting ${downloaded} to ${setupDir}`)
await extractFunction?.(downloaded, setupDir)
}
async function cacheInstallation(setupDir: string, name: string, version: string) { async function cacheInstallation(setupDir: string, name: string, version: string) {
// check if inside Github Actions. If so, cache the installation // check if inside Github Actions. If so, cache the installation
if (GITHUB_ACTIONS && typeof process.env.RUNNER_TOOL_CACHE === "string") { if (GITHUB_ACTIONS && typeof process.env.RUNNER_TOOL_CACHE === "string") {