fix: support adding environment variables on non GitHub Action

This commit is contained in:
Amin Yahyaabadi 2022-01-17 18:30:02 -08:00
parent 67d09d6064
commit c6a5745cec
9 changed files with 82 additions and 32 deletions

2
dist/setup_cpp.js vendored

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,4 +1,4 @@
import { exportVariable, info } from "@actions/core" import { info } from "@actions/core"
import { addPath } from "../utils/path/addPath" import { addPath } from "../utils/path/addPath"
import { existsSync } from "fs" import { existsSync } from "fs"
import { setupAptPack } from "../utils/setup/setupAptPack" import { setupAptPack } from "../utils/setup/setupAptPack"
@ -7,6 +7,7 @@ import { setupChocoPack } from "../utils/setup/setupChocoPack"
import semverMajor from "semver/functions/major" import semverMajor from "semver/functions/major"
import semverCoerce from "semver/functions/coerce" import semverCoerce from "semver/functions/coerce"
import { setupMacOSSDK } from "../macos-sdk/macos-sdk" import { setupMacOSSDK } from "../macos-sdk/macos-sdk"
import { addEnv } from "../utils/env/addEnv"
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function setupGcc(version: string, _setupDir: string, arch: string) { export async function setupGcc(version: string, _setupDir: string, arch: string) {
@ -92,22 +93,22 @@ async function activateGcc(version: string, binDir: string) {
// const ld = process.env.LD_LIBRARY_PATH ?? "" // const ld = process.env.LD_LIBRARY_PATH ?? ""
// const dyld = process.env.DYLD_LIBRARY_PATH ?? "" // const dyld = process.env.DYLD_LIBRARY_PATH ?? ""
// // Setup gcc as the compiler // // Setup gcc as the compiler
// exportVariable("LD_LIBRARY_PATH", `${installDir}/lib${path.delimiter}${ld}`) // addEnv("LD_LIBRARY_PATH", `${installDir}/lib${path.delimiter}${ld}`)
// exportVariable("DYLD_LIBRARY_PATH", `${installDir}/lib${path.delimiter}${dyld}`) // addEnv("DYLD_LIBRARY_PATH", `${installDir}/lib${path.delimiter}${dyld}`)
// exportVariable("CPATH", `${installDir}/lib/gcc/${majorVersion}/include`) // addEnv("CPATH", `${installDir}/lib/gcc/${majorVersion}/include`)
// exportVariable("LDFLAGS", `-L${installDir}/lib`) // addEnv("LDFLAGS", `-L${installDir}/lib`)
// exportVariable("CPPFLAGS", `-I${installDir}/include`) // addEnv("CPPFLAGS", `-I${installDir}/include`)
if (process.platform === "win32") { if (process.platform === "win32") {
exportVariable("CC", `${binDir}/gcc`) addEnv("CC", `${binDir}/gcc`)
exportVariable("CXX", `${binDir}/g++`) addEnv("CXX", `${binDir}/g++`)
} else { } else {
const majorVersion = semverMajor(semverCoerce(version) ?? version) const majorVersion = semverMajor(semverCoerce(version) ?? version)
if (majorVersion >= 5) { if (majorVersion >= 5) {
exportVariable("CC", `${binDir}/gcc-${majorVersion}`) addEnv("CC", `${binDir}/gcc-${majorVersion}`)
exportVariable("CXX", `${binDir}/g++-${majorVersion}`) addEnv("CXX", `${binDir}/g++-${majorVersion}`)
} else { } else {
exportVariable("CC", `${binDir}/gcc-${version}`) addEnv("CC", `${binDir}/gcc-${version}`)
exportVariable("CXX", `${binDir}/g++-${version}`) addEnv("CXX", `${binDir}/g++-${version}`)
} }
} }

View File

@ -1,4 +1,3 @@
import * as core from "@actions/core"
import * as path from "path" import * as path from "path"
import semverLte from "semver/functions/lte" import semverLte from "semver/functions/lte"
import semverMajor from "semver/functions/major" import semverMajor from "semver/functions/major"
@ -8,6 +7,8 @@ import { extractExe, extractTarByExe } from "../utils/setup/extract"
import { getSpecificVersionAndUrl, getVersions, semverCoerceIfInvalid } from "../utils/setup/version" import { getSpecificVersionAndUrl, getVersions, semverCoerceIfInvalid } from "../utils/setup/version"
import { setupMacOSSDK } from "../macos-sdk/macos-sdk" import { setupMacOSSDK } from "../macos-sdk/macos-sdk"
import { addBinExtension } from "../utils/extension/extension" import { addBinExtension } from "../utils/extension/extension"
import { addEnv } from "../utils/env/addEnv"
import { setOutput } from "@actions/core"
//================================================ //================================================
// Version // Version
@ -223,7 +224,7 @@ export function getUrl(platform: string, version: string): string | null | Promi
async function getLLVMPackageInfo(version: string, platform: NodeJS.Platform): Promise<PackageInfo> { async function getLLVMPackageInfo(version: string, platform: NodeJS.Platform): Promise<PackageInfo> {
const [specificVersion, url] = await getSpecificVersionAndUrl(VERSIONS, platform, version, getUrl) const [specificVersion, url] = await getSpecificVersionAndUrl(VERSIONS, platform, version, getUrl)
core.setOutput("version", specificVersion) setOutput("version", specificVersion)
return { return {
url, url,
extractedFolderName: "", extractedFolderName: "",
@ -252,26 +253,26 @@ export async function activateLLVM(directory: string, versionGiven: string) {
const ld = process.env.LD_LIBRARY_PATH ?? "" const ld = process.env.LD_LIBRARY_PATH ?? ""
const dyld = process.env.DYLD_LIBRARY_PATH ?? "" const dyld = process.env.DYLD_LIBRARY_PATH ?? ""
core.exportVariable("LLVM_PATH", directory) // the output of this action addEnv("LLVM_PATH", directory) // the output of this action
const llvmMajor = semverMajor(version) const llvmMajor = semverMajor(version)
// Setup LLVM as the compiler // Setup LLVM as the compiler
core.exportVariable("LD_LIBRARY_PATH", `${lib}${path.delimiter}${ld}`) addEnv("LD_LIBRARY_PATH", `${lib}${path.delimiter}${ld}`)
core.exportVariable("DYLD_LIBRARY_PATH", `${lib}${path.delimiter}${dyld}`) addEnv("DYLD_LIBRARY_PATH", `${lib}${path.delimiter}${dyld}`)
if (process.platform !== "win32") { if (process.platform !== "win32") {
// https://github.com/aminya/setup-cpp/issues/6 // https://github.com/aminya/setup-cpp/issues/6
core.exportVariable("CPATH", `${directory}/lib/clang/${llvmMajor}/include`) addEnv("CPATH", `${directory}/lib/clang/${llvmMajor}/include`)
} }
core.exportVariable("LDFLAGS", `-L${directory}/lib`) addEnv("LDFLAGS", `-L${directory}/lib`)
core.exportVariable("CPPFLAGS", `-I${directory}/include`) addEnv("CPPFLAGS", `-I${directory}/include`)
core.exportVariable("CC", `${directory}/bin/clang`) addEnv("CC", `${directory}/bin/clang`)
core.exportVariable("CXX", `${directory}/bin/clang++`) addEnv("CXX", `${directory}/bin/clang++`)
core.exportVariable("LIBRARY_PATH", `${directory}/lib`) addEnv("LIBRARY_PATH", `${directory}/lib`)
await setupMacOSSDK() await setupMacOSSDK()
} }

View File

@ -1,5 +1,6 @@
import { getExecOutput } from "@actions/exec" import { getExecOutput } from "@actions/exec"
import * as core from "@actions/core" import * as core from "@actions/core"
import { addEnv } from "../utils/env/addEnv"
export async function setupMacOSSDK() { export async function setupMacOSSDK() {
if (process.platform === "darwin") { if (process.platform === "darwin") {
@ -7,7 +8,7 @@ export async function setupMacOSSDK() {
const xcrun = await getExecOutput("xcrun --sdk macosx --show-sdk-path") const xcrun = await getExecOutput("xcrun --sdk macosx --show-sdk-path")
const sdkroot = xcrun.stdout || xcrun.stderr const sdkroot = xcrun.stdout || xcrun.stderr
if (sdkroot) { if (sdkroot) {
core.exportVariable("SDKROOT", sdkroot.trim()) addEnv("SDKROOT", sdkroot.trim())
} else { } else {
core.error(`SDKROOT not set`) core.error(`SDKROOT not set`)
} }

View File

@ -26,6 +26,7 @@ import { setupVcpkg } from "./vcpkg/vcpkg"
import { join } from "path" import { join } from "path"
import { setupVCVarsall } from "./vcvarsall/vcvarsall" import { setupVCVarsall } from "./vcvarsall/vcvarsall"
import { setupKcov } from "./kcov/kcov" import { setupKcov } from "./kcov/kcov"
import { addEnv } from "./utils/env/addEnv"
/** The setup functions */ /** The setup functions */
const setups = { const setups = {
@ -176,8 +177,8 @@ export async function main(args: string[]): Promise<number> {
case "appleclang": case "appleclang":
case "applellvm": { case "applellvm": {
core.info("Assuming apple-clang is already installed") core.info("Assuming apple-clang is already installed")
core.exportVariable("CC", "clang") addEnv("CC", "clang")
core.exportVariable("CXX", "clang++") addEnv("CXX", "clang++")
break break
} }
default: { default: {

46
src/utils/env/addEnv.ts vendored Normal file
View File

@ -0,0 +1,46 @@
import { exportVariable } from "@actions/core"
import * as core from "@actions/core"
import execa from "execa"
import { isGitHubCI } from "./isci"
import untildify from "untildify"
import { appendFileSync } from "fs"
/** An add path function that works locally or inside GitHub Actions */
export function addEnv(name: string, val: string | undefined) {
try {
if (isGitHubCI()) {
exportVariable(name, val)
} else {
addEnvSystem(name, val)
}
} catch (err) {
try {
core.error(err as Error)
return addEnvSystem(name, val)
} catch (err2) {
core.error(err2 as Error)
}
core.error(`Failed to export environment variable ${name}=${val}. You should add it manually.`)
}
}
function addEnvSystem(name: string, val: string | undefined) {
switch (process.platform) {
case "win32": {
execa.sync(`setx "${name}"="${val}"`)
core.info(`${name}="${val} was set in the environment."`)
return
}
case "linux":
case "darwin": {
const profile_path = untildify("~/.profile")
appendFileSync(profile_path, `\nexport ${name}="${val}\n`)
core.info(`${name}="${val} was added to "${profile_path}"`)
return
}
default: {
// fall through shell path modification
}
}
process.env[name] = val
}

View File

@ -5,7 +5,6 @@ import execa from "execa"
import { isGitHubCI } from "../env/isci" import { isGitHubCI } from "../env/isci"
import untildify from "untildify" import untildify from "untildify"
import { appendFileSync } from "fs" import { appendFileSync } from "fs"
// import { spawnSync } from "child_process"
/** An add path function that works locally or inside GitHub Actions */ /** An add path function that works locally or inside GitHub Actions */
export function addPath(path: string) { export function addPath(path: string) {
@ -30,13 +29,13 @@ function addPathSystem(path: string) {
switch (process.platform) { switch (process.platform) {
case "win32": { case "win32": {
execa.sync(`setx PATH=${path};%PATH%`) execa.sync(`setx PATH=${path};%PATH%`)
core.info(`${path} was added to the PATH.`)
return return
} }
case "linux": case "linux":
case "darwin": { case "darwin": {
const profile_path = untildify("~/.profile") const profile_path = untildify("~/.profile")
appendFileSync(profile_path, `\nexport PATH=${path}:$PATH\n`) appendFileSync(profile_path, `\nexport PATH=${path}:$PATH\n`)
// spawnSync(`source "${profile_path}"`, { shell: true })
core.info(`${path} was added to "${profile_path}"`) core.info(`${path} was added to "${profile_path}"`)
return return
} }

View File

@ -1,8 +1,9 @@
import { exportVariable, info } from "@actions/core" import { info } from "@actions/core"
import { existsSync } from "fs" import { existsSync } from "fs"
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
import { setupMSVCDevCmd } from "msvc-dev-cmd/lib.js" import { setupMSVCDevCmd } from "msvc-dev-cmd/lib.js"
import { addEnv } from "../utils/env/addEnv"
function getArch(arch: string): string { function getArch(arch: string): string {
switch (arch) { switch (arch) {
@ -31,7 +32,7 @@ export function setupVCVarsall(
) { ) {
if (VCTargetsPath !== undefined && existsSync(VCTargetsPath)) { if (VCTargetsPath !== undefined && existsSync(VCTargetsPath)) {
info(`Adding ${VCTargetsPath} to PATH`) info(`Adding ${VCTargetsPath} to PATH`)
exportVariable("VCTargetsPath", VCTargetsPath) addEnv("VCTargetsPath", VCTargetsPath)
} }
setupMSVCDevCmd(getArch(arch), sdk, toolset, uwp, spectre, vsversion) setupMSVCDevCmd(getArch(arch), sdk, toolset, uwp, spectre, vsversion)