Skip to content

Commit 8b8fd86

Browse files
authored
Merge pull request #179 from aminya/locale [skip test]
2 parents 381ebd4 + 471b553 commit 8b8fd86

File tree

6 files changed

+81
-41
lines changed

6 files changed

+81
-41
lines changed

dist/node12/setup-cpp.js

+22-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/node12/setup-cpp.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/node16/setup-cpp.js

+13-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/node16/setup-cpp.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/env/addEnv.ts

+40-7
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,45 @@ import { giveUserAccess } from "user-access"
1010
import escapeQuote from "escape-quotes"
1111
import { pathExists } from "path-exists"
1212

13+
type AddEnvOptions = {
14+
/** If true, the value will be escaped with quotes and spaces will be escaped with backslash */
15+
shouldEscapeSpace?: boolean
16+
/** If true, the variable will be only added if it is not defined */
17+
shouldAddOnlyIfNotDefined?: boolean
18+
}
19+
20+
const defaultAddEnvOptions: AddEnvOptions = {
21+
shouldEscapeSpace: false,
22+
shouldAddOnlyIfNotDefined: false,
23+
}
24+
1325
/**
1426
* Add an environment variable.
1527
*
1628
* This function is cross-platforms and works in all the local or CI systems.
1729
*/
18-
export async function addEnv(name: string, valGiven: string | undefined, shouldEscapeSpace: boolean = false) {
19-
const val = escapeString(valGiven ?? "", shouldEscapeSpace)
30+
export async function addEnv(
31+
name: string,
32+
valGiven: string | undefined,
33+
options: AddEnvOptions = defaultAddEnvOptions
34+
) {
35+
const val = escapeString(valGiven ?? "", options.shouldEscapeSpace)
2036
try {
2137
if (GITHUB_ACTIONS) {
2238
try {
39+
if (options.shouldAddOnlyIfNotDefined) {
40+
if (process.env[name] !== undefined) {
41+
info(`Environment variable ${name} is already defined. Skipping.`)
42+
return
43+
}
44+
}
2345
exportVariable(name, val)
2446
} catch (err) {
2547
error(err as Error)
26-
await addEnvSystem(name, val)
48+
await addEnvSystem(name, val, options)
2749
}
2850
} else {
29-
await addEnvSystem(name, val)
51+
await addEnvSystem(name, val, options)
3052
}
3153
} catch (err) {
3254
error(err as Error)
@@ -65,10 +87,16 @@ export async function addPath(path: string) {
6587

6688
export const cpprc_path = untildifyUser(".cpprc")
6789

68-
async function addEnvSystem(name: string, valGiven: string | undefined) {
90+
async function addEnvSystem(name: string, valGiven: string | undefined, options: AddEnvOptions) {
6991
const val = valGiven ?? ""
7092
switch (process.platform) {
7193
case "win32": {
94+
if (options.shouldAddOnlyIfNotDefined) {
95+
if (process.env[name] !== undefined) {
96+
info(`Environment variable ${name} is already defined. Skipping.`)
97+
return
98+
}
99+
}
72100
// We do not use `execaSync(`setx PATH "${path};%PATH%"`)` because of its character limit
73101
await execPowershell(`[Environment]::SetEnvironmentVariable('${name}', '${val}', "User")`)
74102
info(`${name}='${val}' was set in the environment.`)
@@ -77,8 +105,13 @@ async function addEnvSystem(name: string, valGiven: string | undefined) {
77105
case "linux":
78106
case "darwin": {
79107
await setupCppInProfile()
80-
appendFileSync(cpprc_path, `\nexport ${name}="${val}"\n`)
81-
info(`${name}="${val}" was added to "${cpprc_path}`)
108+
if (options.shouldAddOnlyIfNotDefined) {
109+
appendFileSync(cpprc_path, `\nif [ -z "\${${name}}" ]; then export ${name}="${val}"; fi\n`)
110+
info(`if not defined ${name} then ${name}="${val}" was added to "${cpprc_path}`)
111+
} else {
112+
appendFileSync(cpprc_path, `\nexport ${name}="${val}"\n`)
113+
info(`${name}="${val}" was added to "${cpprc_path}`)
114+
}
82115
return
83116
}
84117
default: {

src/utils/setup/setupAptPack.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ async function initApt(apt: string) {
113113
]
114114
if (apt === "nala") {
115115
// enable utf8 otherwise it fails because of the usage of ASCII encoding
116-
promises.push(addEnv("LANG", "C.UTF-8"), addEnv("LC_ALL", "C.UTF-8"))
116+
promises.push(
117+
addEnv("LANG", "C.UTF-8", { shouldAddOnlyIfNotDefined: true }),
118+
addEnv("LC_ALL", "C.UTF-8", { shouldAddOnlyIfNotDefined: true })
119+
)
117120
}
118121
await Promise.all(promises)
119122
}

0 commit comments

Comments
 (0)