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 semverCoerce from "semver/functions/coerce"
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"
/** Get the platform data for cmake */
@ -21,7 +20,6 @@ function getCmakePackageInfo(version: string, platform: NodeJS.Platform, arch: s
binRelativeDir: "bin/",
binFileName: addExeExt("cmake"),
extractedFolderName: folderName,
extractFunction: extractZip,
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/",
binFileName: addExeExt("cmake"),
extractedFolderName: folderName,
extractFunction: extractTarByExe,
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/",
binFileName: addExeExt("cmake"),
extractedFolderName: folderName,
extractFunction: extractTarByExe,
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 { installAptPack } from "setup-apt"
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 { setupBrewPack } from "../utils/setup/setupBrewPack.js"
import { setupChocoPack } from "../utils/setup/setupChocoPack.js"
@ -31,7 +30,6 @@ function getDoxygenPackageInfo(version: string, platform: NodeJS.Platform, _arch
binRelativeDir: "bin/",
binFileName: addExeExt("doxygen"),
extractedFolderName: folderName,
extractFunction: extractTar,
url: `https://www.doxygen.nl/files/${folderName}.linux.bin.tar.gz`,
}
}
@ -41,7 +39,6 @@ function getDoxygenPackageInfo(version: string, platform: NodeJS.Platform, _arch
binRelativeDir: "",
binFileName: addExeExt("doxygen"),
extractedFolderName: folderName,
extractFunction: extractZip,
url: `https://www.doxygen.nl/files/${folderName}.windows.x64.bin.zip`,
}
}

View File

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

View File

@ -1,5 +1,4 @@
import { addExeExt } from "patha"
import { extractZip } from "../utils/setup/extract.js"
import { type InstallationInfo, type PackageInfo, setupBin } from "../utils/setup/setupBin.js"
/** Get the platform name Ninja uses in their download links */
@ -24,7 +23,6 @@ function getNinjaPackageInfo(version: string, platform: NodeJS.Platform, _arch:
binRelativeDir: "",
binFileName: addExeExt("ninja"),
extractedFolderName: "",
extractFunction: extractZip,
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 { isUbuntu } from "../utils/env/isUbuntu.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 { setupBrewPack } from "../utils/setup/setupBrewPack.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 */
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) {
case "win32": {
const osArchStr = (["ia32", "x86", "i386", "x32"].includes(arch))
? "win-x86"
: "win-x64"
return {
binRelativeDir: "",
binFileName,
extractedFolderName: "",
extractFunction: extractZip,
url:
`https://github.com/PowerShell/PowerShell/releases/download/v${version}/PowerShell-${version}-${osArchStr}.zip`,
}
return `https://github.com/PowerShell/PowerShell/releases/download/v${version}/PowerShell-${version}-${osArchStr}.zip`
}
case "darwin": {
const osArchStr = ["arm", "arm64"].includes(arch) ? "osx-arm64" : "osx-x64"
return {
binRelativeDir: "",
binFileName,
extractedFolderName: "",
extractFunction: extractTarByExe,
url:
`https://github.com/PowerShell/PowerShell/releases/download/v${version}/powershell-${version}-${osArchStr}.tar.gz`,
}
return `https://github.com/PowerShell/PowerShell/releases/download/v${version}/powershell-${version}-${osArchStr}.tar.gz`
}
case "linux": {
const archMap = {
@ -56,15 +52,7 @@ function getPowerShellPackageInfo(version: string, platform: NodeJS.Platform, ar
} as Record<string, string | undefined>
const osArchStr = archMap[arch] ?? "linux-x64"
// TODO support musl
return {
binRelativeDir: "",
binFileName,
extractedFolderName: "",
extractFunction: extractTarByExe,
url:
`https://github.com/PowerShell/PowerShell/releases/download/v${version}/powershell-${version}-${osArchStr}.tar.gz`,
}
return `https://github.com/PowerShell/PowerShell/releases/download/v${version}/powershell-${version}-${osArchStr}.tar.gz`
}
default:
throw new Error(`Unsupported platform '${platform}'`)

View File

@ -1,5 +1,4 @@
import { addExeExt } from "patha"
import { extractTarByExe, extractZip } from "../utils/setup/extract.js"
import { type InstallationInfo, type PackageInfo, setupBin } from "../utils/setup/setupBin.js"
/** 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 {
const taskPlatform = getTaskPlatform(platform)
const taskArch = getTaskArch(arch)
const isZip = platform === "win32"
const extension = isZip ? "zip" : "tar.gz"
const extension = platform === "win32" ? "zip" : "tar.gz"
return {
binRelativeDir: "",
binFileName: addExeExt("task"),
extractedFolderName: "",
extractFunction: isZip ? extractZip : extractTarByExe,
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 enum ArchiveType {
TarGz = "tar.gz",
TarXz = "tar.xz",
Zip = "zip",
SevenZip = "7z",
Tar = 0,
TarGz = 1,
TarXz = 2,
Zip = 3,
SevenZip = 4,
}
export function getArchiveType(file: string): ArchiveType {
const ext = file.split(".").pop()
if (ext === "tar") {
return ArchiveType.Tar
}
if (ext === "gz" || ext === "tgz") {
return ArchiveType.TarGz
}
@ -34,16 +39,18 @@ export function getArchiveType(file: string): ArchiveType {
return ArchiveType.Zip
}
if (ext === "7z") {
if (ext === "7z" || ext === "exe") {
return ArchiveType.SevenZip
}
// default to 7z
warning(`Unknown archive type: ${ext}. Defaulting to 7z`)
return ArchiveType.SevenZip
}
export function getExtractFunction(archiveType: ArchiveType) {
switch (archiveType) {
case ArchiveType.Tar:
case ArchiveType.TarGz:
return extractTarByExe
case ArchiveType.TarXz:
@ -81,8 +88,15 @@ export function extractExe(file: string, dest: string) {
return extract7Zip(file, dest)
}
/// Extract Zip using 7z
export function extractZip(file: string, dest: string) {
/// Extract Zip using unzip or 7z
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)
}

View File

@ -1,4 +1,5 @@
import { tmpdir } from "os"
import { basename } from "path"
import { cacheDir, downloadTool, find } from "@actions/tool-cache"
import { GITHUB_ACTIONS } from "ci-info"
import { info, warning } from "ci-log"
@ -8,6 +9,7 @@ import { pathExists } from "path-exists"
import { join } from "patha"
import retry from "retry-as-promised"
import { maybeGetInput, rcOptions } from "../../cli-options.js"
import { getArchiveType, getExtractFunction } from "./extract.js"
/** A type that describes a package */
export type PackageInfo = {
@ -94,7 +96,7 @@ async function downloadExtractInstall(
version: string,
url: string,
setupDir: string,
extractFunction: PackageInfo["extractFunction"],
givenExtractFunction: PackageInfo["extractFunction"],
arch: string,
) {
// download ane extract the package into the installation directory.
@ -102,7 +104,10 @@ async function downloadExtractInstall(
try {
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) {
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}`)
// try to download the package 4 times with 2 seconds delay
const downloaded = await retry(
() => {
return downloadTool(url)
async () => {
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) },
)
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) {
// check if inside Github Actions. If so, cache the installation
if (GITHUB_ACTIONS && typeof process.env.RUNNER_TOOL_CACHE === "string") {