Skip to content

Commit

Permalink
Merge pull request #264 from aminya/powershell-ubuntu-24 [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored Aug 21, 2024
2 parents 35ec48a + c2afe42 commit e486e36
Show file tree
Hide file tree
Showing 17 changed files with 322 additions and 151 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ jobs:
- windows-2022
- ubuntu-24.04
- macos-13
- macos-12
# - macos-12
node:
- 22
pnpm:
Expand Down
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.

4 changes: 0 additions & 4 deletions src/cmake/cmake.ts
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -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`,
}
}
Expand All @@ -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`,
}
}
Expand All @@ -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`,
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/doxygen/doxygen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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`,
}
}
Expand All @@ -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`,
}
}
Expand Down
1 change: 0 additions & 1 deletion src/kcov/kcov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ function getDownloadKcovPackageInfo(version: string): PackageInfo {
extractedFolderName: "",
binRelativeDir: "usr/local/bin",
binFileName: addExeExt("kcov"),
extractFunction: extractTarByExe,
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/ninja/ninja.ts
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -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`,
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/powershell/__tests__/powershell.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { GITHUB_ACTIONS } from "ci-info"
import { testBin } from "../../utils/tests/test-helpers.js"
import { cleanupTmpDir, setupTmpDir, testBin } from "../../utils/tests/test-helpers.js"
import { getVersion } from "../../versions/versions.js"
import { setupPowershell } from "../powershell.js"

jest.setTimeout(300000)
describe("setup-powershell", () => {
let directory: string
beforeEach(async () => {
directory = await setupTmpDir("powershell")
process.env.CACHE_TOOLS = "true"
})

it("should setup powershell", async () => {
if (process.platform === "win32" && GITHUB_ACTIONS) {
// results in errors
return
}

const installInfo = await setupPowershell(getVersion("powershell", undefined), "", process.arch)
const installInfo = await setupPowershell(getVersion("powershell", undefined), directory, process.arch)

await testBin("pwsh", ["--version"], installInfo.binDir)
})

afterEach(async () => {
await cleanupTmpDir("ninja")
}, 100000)
})
60 changes: 59 additions & 1 deletion src/powershell/powershell.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,76 @@
import { execRootSync } from "admina"
import { error } from "ci-log"
import { addPath } from "envosman"
import { addExeExt } from "patha"
import { installAptPack } from "setup-apt"
import { rcOptions } from "../cli-options.js"
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 { type PackageInfo, setupBin } from "../utils/setup/setupBin.js"
import { setupBrewPack } from "../utils/setup/setupBrewPack.js"
import { setupChocoPack } from "../utils/setup/setupChocoPack.js"
import { setupDnfPack } from "../utils/setup/setupDnfPack.js"
import { setupPacmanPack } from "../utils/setup/setupPacmanPack.js"

/** Get the platform data for cmake */
function getPowerShellPackageInfo(version: string, platform: NodeJS.Platform, arch: string): PackageInfo {
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 `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 `https://github.com/PowerShell/PowerShell/releases/download/v${version}/powershell-${version}-${osArchStr}.tar.gz`
}
case "linux": {
const archMap = {
arm64: "linux-arm64",
arm: "linux-arm64",
arm32: "linux-arm32",
aarch64: "linux-arm64",
x64: "linux-x64",
} as Record<string, string | undefined>
const osArchStr = archMap[arch] ?? "linux-x64"
// TODO support musl
return `https://github.com/PowerShell/PowerShell/releases/download/v${version}/powershell-${version}-${osArchStr}.tar.gz`
}
default:
throw new Error(`Unsupported platform '${platform}'`)
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function setupPowershell(version: string, setupDir: string, arch: string) {
try {
return await setupBin("pwsh", version, getPowerShellPackageInfo, setupDir, arch)
} catch (err) {
error(`Failed to setup pwsh via download: ${err}. Trying package managers...`)
return setupPowershellSystem(version, setupDir, arch)
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function setupPowershell(version: string | undefined, _setupDir: string, _arch: string) {
export async function setupPowershellSystem(version: string | undefined, _setupDir: string, _arch: string) {
switch (process.platform) {
case "win32": {
await setupChocoPack("powershell-core", version)
Expand Down
5 changes: 1 addition & 4 deletions src/task/task.ts
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down Expand Up @@ -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}`,
}
}
Expand Down
106 changes: 103 additions & 3 deletions src/utils/setup/extract.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
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 {
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
}

if (ext === "xz" || ext === "txz") {
return ArchiveType.TarXz
}

if (ext === "zip") {
return ArchiveType.Zip
}

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:
return extractTarByExe
case ArchiveType.Zip:
return extractZip
default:
return extract7Zip
}
}

let sevenZip: string | undefined

/// Extract 7z using 7z
Expand All @@ -32,12 +88,21 @@ 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)
}

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 +125,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`)
}
}
Loading

0 comments on commit e486e36

Please sign in to comment.