fix: install extraction dependencies

This commit is contained in:
Amin Yahyaabadi 2024-08-20 17:17:23 -07:00
parent f1968293bc
commit 41c74d00e3
No known key found for this signature in database
GPG Key ID: F52AF77F636088F0
8 changed files with 175 additions and 115 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,11 +1,60 @@
import { mkdirP } from "@actions/io"
import { grantUserWriteAccess } from "admina"
import { warning } from "ci-log"
import { info, warning } from "ci-log"
import { execa } from "execa"
import { installAptPack } from "setup-apt"
import which from "which"
import { setupSevenZip } from "../../sevenzip/sevenzip.js"
import { hasDnf } from "../env/hasDnf.js"
import { isArch } from "../env/isArch.js"
import { isUbuntu } from "../env/isUbuntu.js"
import { setupDnfPack } from "./setupDnfPack.js"
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",
}
export function getArchiveType(file: string): ArchiveType {
const ext = file.split(".").pop()
if (ext === "gz" || ext === "tgz") {
return ArchiveType.TarGz
}
if (ext === "xz" || ext === "txz") {
return ArchiveType.TarXz
}
if (ext === "zip") {
return ArchiveType.Zip
}
if (ext === "7z") {
return ArchiveType.SevenZip
}
// default to 7z
return ArchiveType.SevenZip
}
export function getExtractFunction(archiveType: ArchiveType) {
switch (archiveType) {
case ArchiveType.TarGz:
return extractTarByExe
case ArchiveType.TarXz:
return extractTarByExe
case ArchiveType.Zip:
return extractZip
default:
return extract7Zip
}
}
let sevenZip: string | undefined
/// Extract 7z using 7z
@ -38,6 +87,8 @@ export function extractZip(file: string, dest: string) {
}
export async function extractTarByExe(file: string, dest: string, stripComponents: number = 0, flags: string[] = []) {
await installTarDependencies(getArchiveType(file))
try {
await mkdirP(dest)
} catch {
@ -60,3 +111,38 @@ export async function extractTarByExe(file: string, dest: string, stripComponent
await grantUserWriteAccess(dest)
return dest
}
async function installTarDependencies(archiveType: ArchiveType) {
info("Installing tar extraction dependencies")
switch (archiveType) {
case ArchiveType.TarGz: {
if (process.platform === "linux") {
if (isArch()) {
await setupPacmanPack("gzip")
await setupPacmanPack("tar")
} else if (hasDnf()) {
await setupDnfPack([{ name: "gzip" }, { name: "tar" }])
} else if (isUbuntu()) {
await installAptPack([{ name: "gzip" }, { name: "tar" }])
}
}
break
}
case ArchiveType.TarXz: {
if (process.platform === "linux") {
if (isArch()) {
await setupPacmanPack("xz")
await setupPacmanPack("tar")
} else if (hasDnf()) {
await setupDnfPack([{ name: "xz" }, { name: "tar" }])
} else if (isUbuntu()) {
await installAptPack([{ name: "xz-utils" }, { name: "tar" }])
}
}
break
}
default:
throw new Error(`Unsupported archive type: ${archiveType} for tar extraction`)
}
}

View File

@ -7,13 +7,7 @@ import { chmod } from "fs/promises"
import { pathExists } from "path-exists"
import { join } from "patha"
import retry from "retry-as-promised"
import { installAptPack } from "setup-apt"
import { maybeGetInput, rcOptions } from "../../cli-options.js"
import { hasDnf } from "../env/hasDnf.js"
import { isArch } from "../env/isArch.js"
import { isUbuntu } from "../env/isUbuntu.js"
import { setupDnfPack } from "./setupDnfPack.js"
import { setupPacmanPack } from "./setupPacmanPack.js"
/** A type that describes a package */
export type PackageInfo = {
@ -36,8 +30,6 @@ export type InstallationInfo = {
bin?: string
}
let didInit: boolean = false
/**
* A function that:
*
@ -102,7 +94,7 @@ async function downloadExtractInstall(
version: string,
url: string,
setupDir: string,
extractFunction: ((file: string, dest: string) => Promise<unknown>) | undefined,
extractFunction: PackageInfo["extractFunction"],
arch: string,
) {
// download ane extract the package into the installation directory.
@ -154,28 +146,10 @@ async function extractPackage(
extractFunction: ((file: string, dest: string) => Promise<unknown>) | undefined,
) {
info(`Extracting ${downloaded} to ${setupDir}`)
await installExtractionDependencies()
await extractFunction?.(downloaded, setupDir)
}
async function installExtractionDependencies() {
if (!didInit) {
info("Installing extraction dependencies")
if (process.platform === "linux") {
if (isArch()) {
await Promise.all([setupPacmanPack("unzip"), setupPacmanPack("tar"), setupPacmanPack("xz")])
} else if (hasDnf()) {
await setupDnfPack([{ name: "unzip" }, { name: "tar" }, { name: "xz" }])
} else if (isUbuntu()) {
await installAptPack([{ name: "unzip" }, { name: "tar" }, { name: "xz-utils" }])
}
}
// eslint-disable-next-line require-atomic-updates
didInit = true
}
}
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") {