chore: move addPath function

This commit is contained in:
Amin Yahyaabadi 2022-02-28 21:54:49 -08:00
parent a19fcfecdf
commit 1af7af9c53
14 changed files with 59 additions and 67 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

@ -3,7 +3,7 @@ import execa from "execa"
import { existsSync } from "fs" import { existsSync } from "fs"
import { dirname } from "path" import { dirname } from "path"
import which from "which" import which from "which"
import { addPath } from "../utils/path/addPath" import { addPath } from "../utils/env/addEnv"
import { InstallationInfo } from "../utils/setup/setupBin" import { InstallationInfo } from "../utils/setup/setupBin"
let binDir: string | undefined let binDir: string | undefined

View File

@ -1,4 +1,4 @@
import { addPath } from "../utils/path/addPath" import { addPath } from "../utils/env/addEnv"
import { setupAptPack } from "../utils/setup/setupAptPack" import { setupAptPack } from "../utils/setup/setupAptPack"
import { setupBrewPack } from "../utils/setup/setupBrewPack" import { setupBrewPack } from "../utils/setup/setupBrewPack"
import { setupChocoPack } from "../utils/setup/setupChocoPack" import { setupChocoPack } from "../utils/setup/setupChocoPack"

View File

@ -1,4 +1,4 @@
import { addPath } from "../utils/path/addPath" import { addPath } from "../utils/env/addEnv"
import { setupAptPack } from "../utils/setup/setupAptPack" import { setupAptPack } from "../utils/setup/setupAptPack"
import { InstallationInfo, PackageInfo, setupBin } from "../utils/setup/setupBin" import { InstallationInfo, PackageInfo, setupBin } from "../utils/setup/setupBin"
import { setupBrewPack } from "../utils/setup/setupBrewPack" import { setupBrewPack } from "../utils/setup/setupBrewPack"

View File

@ -1,4 +1,4 @@
import { addPath } from "../utils/path/addPath" import { addPath } from "../utils/env/addEnv"
import { existsSync } from "fs" import { existsSync } from "fs"
import { setupAptPack } from "../utils/setup/setupAptPack" import { setupAptPack } from "../utils/setup/setupAptPack"
import { setupBrewPack } from "../utils/setup/setupBrewPack" import { setupBrewPack } from "../utils/setup/setupBrewPack"

View File

@ -1,4 +1,4 @@
import { addPath } from "../utils/path/addPath" import { addPath } from "../utils/env/addEnv"
import { setupAptPack } from "../utils/setup/setupAptPack" import { setupAptPack } from "../utils/setup/setupAptPack"
import { InstallationInfo } from "../utils/setup/setupBin" import { InstallationInfo } from "../utils/setup/setupBin"
import { setupBrewPack } from "../utils/setup/setupBrewPack" import { setupBrewPack } from "../utils/setup/setupBrewPack"

View File

@ -1,4 +1,4 @@
import { addPath } from "../utils/path/addPath" import { addPath } from "../utils/env/addEnv"
import { setupChocoPack } from "../utils/setup/setupChocoPack" import { setupChocoPack } from "../utils/setup/setupChocoPack"
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars

View File

@ -1,5 +1,5 @@
import * as core from "@actions/core" import * as core from "@actions/core"
import { addPath } from "../utils/path/addPath" import { addPath } from "../utils/env/addEnv"
import { setupAptPack } from "../utils/setup/setupAptPack" import { setupAptPack } from "../utils/setup/setupAptPack"
import { setupBrewPack } from "../utils/setup/setupBrewPack" import { setupBrewPack } from "../utils/setup/setupBrewPack"
import { setupChocoPack } from "../utils/setup/setupChocoPack" import { setupChocoPack } from "../utils/setup/setupChocoPack"

View File

@ -1,10 +1,10 @@
import { exportVariable } from "@actions/core" import { exportVariable, addPath as ghAddPath, info } from "@actions/core"
import * as core from "@actions/core"
import { isGitHubCI } from "./isci" import { isGitHubCI } from "./isci"
import { untildify_user as untildify } from "../path/untildify" import { untildify_user as untildify } from "../path/untildify"
import { appendFileSync } from "fs" import { appendFileSync } from "fs"
import { error } from "../io/io" import { error } from "../io/io"
import { execPowershell } from "../exec/powershell" import { execPowershell } from "../exec/powershell"
import { delimiter } from "path"
/** An add path function that works locally or inside GitHub Actions */ /** An add path function that works locally or inside GitHub Actions */
export function addEnv(name: string, val: string | undefined) { export function addEnv(name: string, val: string | undefined) {
@ -31,7 +31,7 @@ function addEnvSystem(name: string, valGiven: string | undefined) {
case "win32": { case "win32": {
// We do not use `execa.sync(`setx PATH "${path};%PATH%"`)` because of its character limit // We do not use `execa.sync(`setx PATH "${path};%PATH%"`)` because of its character limit
execPowershell(`[Environment]::SetEnvironmentVariable("${name}", "${val}", "User")`) execPowershell(`[Environment]::SetEnvironmentVariable("${name}", "${val}", "User")`)
core.info(`${name}="${val} was set in the environment."`) info(`${name}="${val} was set in the environment."`)
return return
} }
case "linux": case "linux":
@ -39,7 +39,7 @@ function addEnvSystem(name: string, valGiven: string | undefined) {
// find profile path // find profile path
const profile_path = untildify(".profile") const profile_path = untildify(".profile")
appendFileSync(profile_path, `\nexport ${name}="${val}"\n`) appendFileSync(profile_path, `\nexport ${name}="${val}"\n`)
core.info(`${name}="${val} was added to "${profile_path}"`) info(`${name}="${val} was added to "${profile_path}"`)
return return
} }
default: { default: {
@ -48,3 +48,46 @@ function addEnvSystem(name: string, valGiven: string | undefined) {
} }
process.env[name] = val process.env[name] = val
} }
/** An add path function that works locally or inside GitHub Actions */
export function addPath(path: string) {
process.env.PATH = `${path}${delimiter}${process.env.PATH}`
try {
if (isGitHubCI()) {
ghAddPath(path)
} else {
addPathSystem(path)
}
} catch (err) {
try {
error(err as Error)
return addPathSystem(path)
} catch (err2) {
error(err2 as Error)
}
error(`Failed to add ${path} to the percistent PATH. You should add it manually.`)
}
}
function addPathSystem(path: string) {
switch (process.platform) {
case "win32": {
// We do not use `execa.sync(`setx PATH "${path};%PATH%"`)` because of its character limit and also because %PATH% is different for user and system
execPowershell(
`$USER_PATH=([Environment]::GetEnvironmentVariable("PATH", "User")); [Environment]::SetEnvironmentVariable("PATH", "${path};$USER_PATH", "User")`
)
info(`${path} was added to the PATH.`)
return
}
case "linux":
case "darwin": {
const profile_path = untildify(".profile")
appendFileSync(profile_path, `\nexport PATH=${path}:$PATH\n`)
info(`${path} was added to "${profile_path}"`)
return
}
default: {
return
}
}
}

View File

@ -1,51 +0,0 @@
import { addPath as ghAddPath } from "@actions/core"
import { delimiter } from "path"
import * as core from "@actions/core"
import { isGitHubCI } from "../env/isci"
import { untildify_user as untildify } from "./untildify"
import { appendFileSync } from "fs"
import { error } from "../io/io"
import { execPowershell } from "../exec/powershell"
/** An add path function that works locally or inside GitHub Actions */
export function addPath(path: string) {
process.env.PATH = `${path}${delimiter}${process.env.PATH}`
try {
if (isGitHubCI()) {
ghAddPath(path)
} else {
addPathSystem(path)
}
} catch (err) {
try {
error(err as Error)
return addPathSystem(path)
} catch (err2) {
error(err2 as Error)
}
error(`Failed to add ${path} to the percistent PATH. You should add it manually.`)
}
}
function addPathSystem(path: string) {
switch (process.platform) {
case "win32": {
// We do not use `execa.sync(`setx PATH "${path};%PATH%"`)` because of its character limit and also because %PATH% is different for user and system
execPowershell(
`$USER_PATH=([Environment]::GetEnvironmentVariable("PATH", "User")); [Environment]::SetEnvironmentVariable("PATH", "${path};$USER_PATH", "User")`
)
core.info(`${path} was added to the PATH.`)
return
}
case "linux":
case "darwin": {
const profile_path = untildify(".profile")
appendFileSync(profile_path, `\nexport PATH=${path}:$PATH\n`)
core.info(`${path} was added to "${profile_path}"`)
return
}
default: {
return
}
}
}

View File

@ -1,6 +1,6 @@
import { find, downloadTool, cacheDir } from "@actions/tool-cache" import { find, downloadTool, cacheDir } from "@actions/tool-cache"
import { info } from "@actions/core" import { info } from "@actions/core"
import { addPath } from "../path/addPath" import { addPath } from "../env/addEnv"
import { join } from "path" import { join } from "path"
import { existsSync } from "fs" import { existsSync } from "fs"
import { tmpdir } from "os" import { tmpdir } from "os"

View File

@ -1,5 +1,5 @@
/* eslint-disable require-atomic-updates */ /* eslint-disable require-atomic-updates */
import { addPath } from "../path/addPath" import { addPath } from "../env/addEnv"
import which from "which" import which from "which"
import { setupChocolatey } from "../../chocolatey/chocolatey" import { setupChocolatey } from "../../chocolatey/chocolatey"
import { InstallationInfo } from "./setupBin" import { InstallationInfo } from "./setupBin"

View File

@ -3,7 +3,7 @@ import { getExecOutput } from "@actions/exec"
import execa from "execa" import execa from "execa"
import which from "which" import which from "which"
import { info } from "@actions/core" import { info } from "@actions/core"
import { addPath } from "../path/addPath" import { addPath } from "../env/addEnv"
import { setupPython } from "../../python/python" import { setupPython } from "../../python/python"
import { isBinUptoDate } from "./version" import { isBinUptoDate } from "./version"
import { join } from "path" import { join } from "path"