From 7caf0ddff86bbfbcd2edfd3a61aa969d7e2ca8b1 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Wed, 29 Jun 2022 19:06:33 -0700 Subject: [PATCH] feat: add the cached isArch function --- src/ccache/ccache.ts | 4 ++-- src/cppcheck/cppcheck.ts | 4 ++-- src/doxygen/doxygen.ts | 6 +++--- src/gcc/gcc.ts | 6 +++--- src/graphviz/graphviz.ts | 4 ++-- src/kcov/kcov.ts | 5 +++-- src/llvm/llvm.ts | 4 ++-- src/make/make.ts | 4 ++-- src/python/python.ts | 4 ++-- src/sevenzip/sevenzip.ts | 4 ++-- src/utils/env/isArch.ts | 12 ++++++++++++ src/utils/setup/setupBin.ts | 4 ++-- src/utils/setup/setupPipPack.ts | 3 ++- src/vcpkg/vcpkg.ts | 3 ++- 14 files changed, 41 insertions(+), 26 deletions(-) create mode 100644 src/utils/env/isArch.ts diff --git a/src/ccache/ccache.ts b/src/ccache/ccache.ts index 6d53fc98..d421ab42 100644 --- a/src/ccache/ccache.ts +++ b/src/ccache/ccache.ts @@ -2,7 +2,7 @@ import { setupAptPack } from "../utils/setup/setupAptPack" import { setupPacmanPack } from "../utils/setup/setupPacmanPack" import { setupBrewPack } from "../utils/setup/setupBrewPack" import { setupChocoPack } from "../utils/setup/setupChocoPack" -import which from "which" +import { isArch } from "../utils/env/isArch" // eslint-disable-next-line @typescript-eslint/no-unused-vars export function setupCcache(version: string, _setupDir: string, _arch: string) { @@ -14,7 +14,7 @@ export function setupCcache(version: string, _setupDir: string, _arch: string) { return setupBrewPack("ccache", version) } case "linux": { - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { return setupPacmanPack("ccache", version) } return setupAptPack("ccache", version) diff --git a/src/cppcheck/cppcheck.ts b/src/cppcheck/cppcheck.ts index f903c1f9..f4c2800e 100644 --- a/src/cppcheck/cppcheck.ts +++ b/src/cppcheck/cppcheck.ts @@ -3,7 +3,7 @@ import { setupAptPack } from "../utils/setup/setupAptPack" import { setupPacmanPack } from "../utils/setup/setupPacmanPack" import { setupBrewPack } from "../utils/setup/setupBrewPack" import { setupChocoPack } from "../utils/setup/setupChocoPack" -import which from "which" +import { isArch } from "../utils/env/isArch" // eslint-disable-next-line @typescript-eslint/no-unused-vars export async function setupCppcheck(version: string | undefined, _setupDir: string, _arch: string) { @@ -17,7 +17,7 @@ export async function setupCppcheck(version: string | undefined, _setupDir: stri return setupBrewPack("cppcheck", version) } case "linux": { - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { return setupPacmanPack("cppcheck", version) } return setupAptPack("cppcheck", version) diff --git a/src/doxygen/doxygen.ts b/src/doxygen/doxygen.ts index f8981575..941fac3e 100644 --- a/src/doxygen/doxygen.ts +++ b/src/doxygen/doxygen.ts @@ -11,7 +11,7 @@ import { setupGraphviz } from "../graphviz/graphviz" import { getVersion } from "../default_versions" import { existsSync } from "fs" import { join } from "path" -import which from "which" +import { isArch } from "../utils/env/isArch" /** Get the platform data for cmake */ // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -59,13 +59,13 @@ export async function setupDoxygen(version: string, setupDir: string, arch: stri case "linux": { let installationInfo: InstallationInfo if (version === "") { - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { installationInfo = setupPacmanPack("doxygen", undefined) } else { installationInfo = setupAptPack("doxygen", undefined) } } else { - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { installationInfo = setupPacmanPack("doxygen", version) } else { try { diff --git a/src/gcc/gcc.ts b/src/gcc/gcc.ts index 0e1ec689..2a455272 100644 --- a/src/gcc/gcc.ts +++ b/src/gcc/gcc.ts @@ -13,7 +13,7 @@ import { isGitHubCI } from "../utils/env/isci" import { addBinExtension } from "../utils/extension/extension" import { InstallationInfo, PackageInfo, setupBin } from "../utils/setup/setupBin" import { extract7Zip } from "../utils/setup/extract" -import which from "which" +import { isArch } from "../utils/env/isArch" interface MingwInfo { releaseName: string @@ -81,7 +81,7 @@ export async function setupGcc(version: string, setupDir: string, arch: string) } case "linux": { if (arch === "x64") { - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { installationInfo = setupPacmanPack("gcc", version) } else { setupAptPack("gcc", version, ["ppa:ubuntu-toolchain-r/test"]) @@ -89,7 +89,7 @@ export async function setupGcc(version: string, setupDir: string, arch: string) } } else { info(`Install g++-multilib because gcc for ${arch} was requested`) - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { setupPacmanPack("gcc-multilib", version) } else { setupAptPack("gcc-multilib", version, ["ppa:ubuntu-toolchain-r/test"]) diff --git a/src/graphviz/graphviz.ts b/src/graphviz/graphviz.ts index a44bfd4f..b595e346 100644 --- a/src/graphviz/graphviz.ts +++ b/src/graphviz/graphviz.ts @@ -4,7 +4,7 @@ import { setupPacmanPack } from "../utils/setup/setupPacmanPack" import { InstallationInfo } from "../utils/setup/setupBin" import { setupBrewPack } from "../utils/setup/setupBrewPack" import { setupChocoPack } from "../utils/setup/setupChocoPack" -import which from "which" +import { isArch } from "../utils/env/isArch" // eslint-disable-next-line @typescript-eslint/no-unused-vars export async function setupGraphviz(version: string, _setupDir: string, _arch: string) { @@ -17,7 +17,7 @@ export async function setupGraphviz(version: string, _setupDir: string, _arch: s return setupBrewPack("graphviz", version) } case "linux": { - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { return setupPacmanPack("graphviz", version) } return setupAptPack("graphviz", version) diff --git a/src/kcov/kcov.ts b/src/kcov/kcov.ts index 01fa112c..ed77c437 100644 --- a/src/kcov/kcov.ts +++ b/src/kcov/kcov.ts @@ -10,6 +10,7 @@ import { extractTarByExe } from "../utils/setup/extract" import { setupAptPack } from "../utils/setup/setupAptPack" import { setupPacmanPack } from "../utils/setup/setupPacmanPack" import { PackageInfo, setupBin } from "../utils/setup/setupBin" +import { isArch } from "../utils/env/isArch" function getKcovPackageInfo(version: string): PackageInfo { const version_number = parseInt(version.replace(/^v/, ""), 10) @@ -43,7 +44,7 @@ async function buildKcov(file: string, dest: string) { await setupCmake(getVersion("cmake", undefined), join(untildify(""), "cmake"), "") } if (process.platform === "linux") { - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { setupPacmanPack("libdwarf") setupPacmanPack("libcurl-openssl") } else { @@ -60,7 +61,7 @@ async function buildKcov(file: string, dest: string) { export async function setupKcov(version: string, setupDir: string, arch: string) { switch (process.platform) { case "linux": { - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { // TODO install kcov ? setupPacmanPack("kcov") const installationInfo = await setupBin("kcov", version, getKcovPackageInfo, setupDir, arch) setupPacmanPack("binutils") diff --git a/src/llvm/llvm.ts b/src/llvm/llvm.ts index a1089219..9fb081ca 100644 --- a/src/llvm/llvm.ts +++ b/src/llvm/llvm.ts @@ -20,7 +20,7 @@ import { existsSync } from "fs" import { isGitHubCI } from "../utils/env/isci" import { setupGcc } from "../gcc/gcc" import { getVersion } from "../default_versions" -import which from "which" +import { isArch } from "../utils/env/isArch" //================================================ // Version @@ -286,7 +286,7 @@ async function _setupLLVM(version: string, setupDir: string, arch: string) { if (process.platform === "linux") { // install llvm build dependencies await setupGcc(getVersion("gcc", undefined), "", arch) // using llvm requires ld, an up to date libstdc++, etc. So, install gcc first - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { // setupPacmanPack("ncurses") // TODO: install libtinfo ? } else { diff --git a/src/make/make.ts b/src/make/make.ts index e2609304..4af79790 100644 --- a/src/make/make.ts +++ b/src/make/make.ts @@ -3,7 +3,7 @@ import { setupAptPack } from "../utils/setup/setupAptPack" import { setupPacmanPack } from "../utils/setup/setupPacmanPack" import { setupBrewPack } from "../utils/setup/setupBrewPack" import { setupChocoPack } from "../utils/setup/setupChocoPack" -import which from "which" +import { isArch } from "../utils/env/isArch" // eslint-disable-next-line @typescript-eslint/no-unused-vars export async function setupMake(version: string, _setupDir: string, _arch: string) { @@ -17,7 +17,7 @@ export async function setupMake(version: string, _setupDir: string, _arch: strin return { binDir: "/usr/local/opt/make/libexec/gnubin" } } case "linux": { - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { return setupPacmanPack("make", version) } return setupAptPack("make", version) diff --git a/src/python/python.ts b/src/python/python.ts index ad5ede31..cd8249ad 100644 --- a/src/python/python.ts +++ b/src/python/python.ts @@ -5,7 +5,7 @@ import { setupBrewPack } from "../utils/setup/setupBrewPack" import { setupChocoPack } from "../utils/setup/setupChocoPack" import { isGitHubCI } from "../utils/env/isci" import { warning, info } from "../utils/io/io" -import which from "which" +import { isArch } from "../utils/env/isArch" export async function setupPython(version: string, setupDir: string, arch: string) { if (!isGitHubCI()) { @@ -40,7 +40,7 @@ export async function setupPythonViaSystem(version: string, setupDir: string, _a return setupBrewPack("python3", version) } case "linux": { - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { const installInfo = setupPacmanPack("python", version) setupPacmanPack("python-pip") return installInfo diff --git a/src/sevenzip/sevenzip.ts b/src/sevenzip/sevenzip.ts index 69378871..d6f1ebe2 100644 --- a/src/sevenzip/sevenzip.ts +++ b/src/sevenzip/sevenzip.ts @@ -2,7 +2,7 @@ import { setupAptPack } from "../utils/setup/setupAptPack" import { setupPacmanPack } from "../utils/setup/setupPacmanPack" import { setupBrewPack } from "../utils/setup/setupBrewPack" import { setupChocoPack } from "../utils/setup/setupChocoPack" -import which from "which" +import { isArch } from "../utils/env/isArch" // eslint-disable-next-line @typescript-eslint/no-unused-vars export function setupSevenZip(version: string, _setupDir: string, _arch: string) { @@ -14,7 +14,7 @@ export function setupSevenZip(version: string, _setupDir: string, _arch: string) return setupBrewPack("p7zip", version) } case "linux": { - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { return setupPacmanPack("p7zip", version) } return setupAptPack("p7zip-full", version) diff --git a/src/utils/env/isArch.ts b/src/utils/env/isArch.ts new file mode 100644 index 00000000..d575728e --- /dev/null +++ b/src/utils/env/isArch.ts @@ -0,0 +1,12 @@ +import which from "which" + +let isArchCache: undefined | boolean = undefined + +export function isArch(): boolean { + if (isArchCache === undefined) { + // detect arch by checking if pacman exists + isArchCache = which.sync("pacman", { nothrow: true }) !== null + } + + return isArchCache +} diff --git a/src/utils/setup/setupBin.ts b/src/utils/setup/setupBin.ts index 75e6558d..e78a05f0 100644 --- a/src/utils/setup/setupBin.ts +++ b/src/utils/setup/setupBin.ts @@ -7,7 +7,7 @@ import { tmpdir } from "os" import { isGitHubCI } from "../env/isci" import { setupAptPack } from "./setupAptPack" import { setupPacmanPack } from "./setupPacmanPack" -import which from "which" +import { isArch } from "../env/isArch" /** A type that describes a package */ export type PackageInfo = { @@ -90,7 +90,7 @@ export async function setupBin( if (!didInit) { if (process.platform === "linux") { // extraction dependencies - if (which.sync("pacman", { nothrow: true })) { + if (isArch()) { setupPacmanPack("unzip") setupPacmanPack("tar") setupPacmanPack("xz") diff --git a/src/utils/setup/setupPipPack.ts b/src/utils/setup/setupPipPack.ts index e44ff694..fd86e297 100644 --- a/src/utils/setup/setupPipPack.ts +++ b/src/utils/setup/setupPipPack.ts @@ -11,6 +11,7 @@ import { getVersion } from "../../default_versions" import { InstallationInfo } from "./setupBin" import { setupAptPack } from "./setupAptPack" import { setupPacmanPack } from "./setupPacmanPack" +import { isArch } from "../env/isArch" let python: string | undefined let binDir: string | undefined @@ -43,7 +44,7 @@ export async function setupPipPack(name: string, version?: string): Promise