Skip to content

Commit

Permalink
fix: install extraction dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Aug 21, 2024
1 parent f196829 commit 41c74d0
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 115 deletions.
56 changes: 28 additions & 28 deletions dist/actions/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/actions/setup-cpp.js.map

Large diffs are not rendered by default.

56 changes: 28 additions & 28 deletions dist/legacy/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/legacy/setup-cpp.js.map

Large diffs are not rendered by default.

56 changes: 28 additions & 28 deletions dist/modern/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/modern/setup-cpp.js.map

Large diffs are not rendered by default.

88 changes: 87 additions & 1 deletion src/utils/setup/extract.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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`)
}
}
28 changes: 1 addition & 27 deletions src/utils/setup/setupBin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -36,8 +30,6 @@ export type InstallationInfo = {
bin?: string
}

let didInit: boolean = false

/**
* A function that:
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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") {
Expand Down

0 comments on commit 41c74d0

Please sign in to comment.