Skip to content

Commit

Permalink
Merge pull request #179 from aminya/locale [skip test]
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored Jul 6, 2023
2 parents 381ebd4 + 471b553 commit 8b8fd86
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 41 deletions.
42 changes: 22 additions & 20 deletions dist/node12/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/node12/setup-cpp.js.map

Large diffs are not rendered by default.

24 changes: 13 additions & 11 deletions dist/node16/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/node16/setup-cpp.js.map

Large diffs are not rendered by default.

47 changes: 40 additions & 7 deletions src/utils/env/addEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,45 @@ import { giveUserAccess } from "user-access"
import escapeQuote from "escape-quotes"
import { pathExists } from "path-exists"

type AddEnvOptions = {
/** If true, the value will be escaped with quotes and spaces will be escaped with backslash */
shouldEscapeSpace?: boolean
/** If true, the variable will be only added if it is not defined */
shouldAddOnlyIfNotDefined?: boolean
}

const defaultAddEnvOptions: AddEnvOptions = {
shouldEscapeSpace: false,
shouldAddOnlyIfNotDefined: false,
}

/**
* Add an environment variable.
*
* This function is cross-platforms and works in all the local or CI systems.
*/
export async function addEnv(name: string, valGiven: string | undefined, shouldEscapeSpace: boolean = false) {
const val = escapeString(valGiven ?? "", shouldEscapeSpace)
export async function addEnv(
name: string,
valGiven: string | undefined,
options: AddEnvOptions = defaultAddEnvOptions
) {
const val = escapeString(valGiven ?? "", options.shouldEscapeSpace)
try {
if (GITHUB_ACTIONS) {
try {
if (options.shouldAddOnlyIfNotDefined) {
if (process.env[name] !== undefined) {
info(`Environment variable ${name} is already defined. Skipping.`)
return
}
}
exportVariable(name, val)
} catch (err) {
error(err as Error)
await addEnvSystem(name, val)
await addEnvSystem(name, val, options)
}
} else {
await addEnvSystem(name, val)
await addEnvSystem(name, val, options)
}
} catch (err) {
error(err as Error)
Expand Down Expand Up @@ -65,10 +87,16 @@ export async function addPath(path: string) {

export const cpprc_path = untildifyUser(".cpprc")

async function addEnvSystem(name: string, valGiven: string | undefined) {
async function addEnvSystem(name: string, valGiven: string | undefined, options: AddEnvOptions) {
const val = valGiven ?? ""
switch (process.platform) {
case "win32": {
if (options.shouldAddOnlyIfNotDefined) {
if (process.env[name] !== undefined) {
info(`Environment variable ${name} is already defined. Skipping.`)
return
}
}
// We do not use `execaSync(`setx PATH "${path};%PATH%"`)` because of its character limit
await execPowershell(`[Environment]::SetEnvironmentVariable('${name}', '${val}', "User")`)
info(`${name}='${val}' was set in the environment.`)
Expand All @@ -77,8 +105,13 @@ async function addEnvSystem(name: string, valGiven: string | undefined) {
case "linux":
case "darwin": {
await setupCppInProfile()
appendFileSync(cpprc_path, `\nexport ${name}="${val}"\n`)
info(`${name}="${val}" was added to "${cpprc_path}`)
if (options.shouldAddOnlyIfNotDefined) {
appendFileSync(cpprc_path, `\nif [ -z "\${${name}}" ]; then export ${name}="${val}"; fi\n`)
info(`if not defined ${name} then ${name}="${val}" was added to "${cpprc_path}`)
} else {
appendFileSync(cpprc_path, `\nexport ${name}="${val}"\n`)
info(`${name}="${val}" was added to "${cpprc_path}`)
}
return
}
default: {
Expand Down
5 changes: 4 additions & 1 deletion src/utils/setup/setupAptPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ async function initApt(apt: string) {
]
if (apt === "nala") {
// enable utf8 otherwise it fails because of the usage of ASCII encoding
promises.push(addEnv("LANG", "C.UTF-8"), addEnv("LC_ALL", "C.UTF-8"))
promises.push(
addEnv("LANG", "C.UTF-8", { shouldAddOnlyIfNotDefined: true }),
addEnv("LC_ALL", "C.UTF-8", { shouldAddOnlyIfNotDefined: true })
)
}
await Promise.all(promises)
}
Expand Down

0 comments on commit 8b8fd86

Please sign in to comment.