fix: use powershell for addEnv and addPath

This commit is contained in:
Amin Yahyaabadi 2022-01-30 17:20:36 -08:00
parent 802f1bce3a
commit 614ed712da
5 changed files with 29 additions and 14 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,12 +1,12 @@
import { exportVariable } from "@actions/core"
import * as core from "@actions/core"
import execa from "execa"
import { isGitHubCI } from "./isci"
import { untildify_user as untildify } from "../path/untildify"
import { appendFileSync } from "fs"
import { join } from "path"
import { isRoot } from "./sudo"
import { error } from "../io/io"
import { execPowershell } from "../exec/powershell"
/** An add path function that works locally or inside GitHub Actions */
export function addEnv(name: string, val: string | undefined) {
@ -31,11 +31,8 @@ function addEnvSystem(name: string, valGiven: string | undefined) {
const val = valGiven ?? ""
switch (process.platform) {
case "win32": {
if (val.length <= 1024) {
execa.sync(`setx "${name}" "${val}"`)
} else {
execa.sync(`powershell -C "[Environment]::SetEnvironmentVariable('${name}', '${val}', 'User')"`)
}
// We do not use `execa.sync(`setx PATH "${path};%PATH%"`)` because of its character limit
execPowershell(`[Environment]::SetEnvironmentVariable('${name}', '${val}', 'User')`)
core.info(`${name}="${val} was set in the environment."`)
return
}

View File

@ -0,0 +1,19 @@
import execa from "execa"
import which from "which"
let powershell: string | undefined
export function execPowershell(command: string) {
if (powershell === undefined) {
const maybePwsh = which.sync("pwsh", { nothrow: true })
if (maybePwsh !== null) {
powershell = maybePwsh
}
const maybePowerShell = which.sync("powershell", { nothrow: true })
if (maybePowerShell !== null) {
powershell = maybePowerShell
}
}
execa.sync(`${powershell} -C "${command}"`)
}

View File

@ -1,11 +1,11 @@
import { addPath as ghAddPath } from "@actions/core"
import { delimiter } from "path"
import * as core from "@actions/core"
import execa from "execa"
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) {
@ -30,11 +30,10 @@ export function addPath(path: string) {
function addPathSystem(path: string) {
switch (process.platform) {
case "win32": {
if (`${path};${process.env.PATH}`.length <= 1024) {
execa.sync(`setx PATH "${path};%PATH%"`)
} else {
execa.sync(`powershell -C "[Environment]::SetEnvironmentVariable('PATH', \\"${path};$env:PATH\\", 'User')"`)
}
// 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
}