mirror of https://github.com/aminya/setup-cpp
fix: use powershell for addEnv and addPath
This commit is contained in:
parent
802f1bce3a
commit
614ed712da
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,12 +1,12 @@
|
||||||
import { exportVariable } from "@actions/core"
|
import { exportVariable } from "@actions/core"
|
||||||
import * as core from "@actions/core"
|
import * as core from "@actions/core"
|
||||||
import execa from "execa"
|
|
||||||
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 { join } from "path"
|
import { join } from "path"
|
||||||
import { isRoot } from "./sudo"
|
import { isRoot } from "./sudo"
|
||||||
import { error } from "../io/io"
|
import { error } from "../io/io"
|
||||||
|
import { execPowershell } from "../exec/powershell"
|
||||||
|
|
||||||
/** 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,11 +31,8 @@ function addEnvSystem(name: string, valGiven: string | undefined) {
|
||||||
const val = valGiven ?? ""
|
const val = valGiven ?? ""
|
||||||
switch (process.platform) {
|
switch (process.platform) {
|
||||||
case "win32": {
|
case "win32": {
|
||||||
if (val.length <= 1024) {
|
// We do not use `execa.sync(`setx PATH "${path};%PATH%"`)` because of its character limit
|
||||||
execa.sync(`setx "${name}" "${val}"`)
|
execPowershell(`[Environment]::SetEnvironmentVariable('${name}', '${val}', 'User')`)
|
||||||
} else {
|
|
||||||
execa.sync(`powershell -C "[Environment]::SetEnvironmentVariable('${name}', '${val}', 'User')"`)
|
|
||||||
}
|
|
||||||
core.info(`${name}="${val} was set in the environment."`)
|
core.info(`${name}="${val} was set in the environment."`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -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}"`)
|
||||||
|
}
|
|
@ -1,11 +1,11 @@
|
||||||
import { addPath as ghAddPath } from "@actions/core"
|
import { addPath as ghAddPath } from "@actions/core"
|
||||||
import { delimiter } from "path"
|
import { delimiter } from "path"
|
||||||
import * as core from "@actions/core"
|
import * as core from "@actions/core"
|
||||||
import execa from "execa"
|
|
||||||
import { isGitHubCI } from "../env/isci"
|
import { isGitHubCI } from "../env/isci"
|
||||||
import { untildify_user as untildify } from "./untildify"
|
import { untildify_user as untildify } from "./untildify"
|
||||||
import { appendFileSync } from "fs"
|
import { appendFileSync } from "fs"
|
||||||
import { error } from "../io/io"
|
import { error } from "../io/io"
|
||||||
|
import { execPowershell } from "../exec/powershell"
|
||||||
|
|
||||||
/** 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,11 +30,10 @@ export function addPath(path: string) {
|
||||||
function addPathSystem(path: string) {
|
function addPathSystem(path: string) {
|
||||||
switch (process.platform) {
|
switch (process.platform) {
|
||||||
case "win32": {
|
case "win32": {
|
||||||
if (`${path};${process.env.PATH}`.length <= 1024) {
|
// 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
|
||||||
execa.sync(`setx PATH "${path};%PATH%"`)
|
execPowershell(
|
||||||
} else {
|
`$USER_PATH=[Environment]::GetEnvironmentVariable('PATH', 'User'); [Environment]::SetEnvironmentVariable('PATH', \\"${path};$USER_PATH\\", 'User')`
|
||||||
execa.sync(`powershell -C "[Environment]::SetEnvironmentVariable('PATH', \\"${path};$env:PATH\\", 'User')"`)
|
)
|
||||||
}
|
|
||||||
core.info(`${path} was added to the PATH.`)
|
core.info(`${path} was added to the PATH.`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue