Skip to content

Commit d0c86b0

Browse files
committed
feat!: add guard and source RC options for os-env
1 parent 77e6430 commit d0c86b0

33 files changed

+244
-228
lines changed

dist/actions/setup-cpp.js

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

dist/actions/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/legacy/setup-cpp.js

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

dist/legacy/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/modern/setup-cpp.js

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

dist/modern/setup-cpp.js.map

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

packages/os-env/src/add-env.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { exportVariable as ghExportVariable } from "@actions/core"
33
import { GITHUB_ACTIONS } from "ci-info"
44
import { error, info } from "ci-log"
55
import { execPowershell } from "exec-powershell"
6-
import { untildifyUser } from "untildify-user"
7-
import { sourceRC } from "./rc-file.js"
6+
import { defaultRcPath, sourceRCInRc } from "./rc-file.js"
87
import { escapeString } from "./utils.js"
98
const { appendFile } = promises
109

@@ -17,6 +16,8 @@ export type AddEnvOptions = {
1716
* The path to the RC file that the env variables should be added to.
1817
*/
1918
rcPath: string
19+
/** Provide a name (your tool) to add a variable guard for sourcing your rc file */
20+
guard?: string
2021
}
2122
/**
2223
* Add an environment variable.
@@ -32,7 +33,7 @@ export async function addEnv(
3233
const options = {
3334
shouldEscapeSpace: false,
3435
shouldAddOnlyIfNotDefined: false,
35-
rcPath: untildifyUser(".bashrc"),
36+
rcPath: defaultRcPath,
3637
...givenOptions,
3738
}
3839

@@ -76,7 +77,7 @@ async function addEnvSystem(name: string, valGiven: string | undefined, options:
7677
}
7778
case "linux":
7879
case "darwin": {
79-
await sourceRC(options.rcPath)
80+
await sourceRCInRc(options)
8081
if (options.shouldAddOnlyIfNotDefined) {
8182
await appendFile(options.rcPath, `\nif [ -z "\${${name}}" ]; then export ${name}="${val}"; fi\n`)
8283
info(`if not defined ${name} then ${name}="${val}" was added to "${options.rcPath}`)

packages/os-env/src/add-path.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import { addPath as ghAddPath } from "@actions/core"
44
import { GITHUB_ACTIONS } from "ci-info"
55
import { error, info } from "ci-log"
66
import { execPowershell } from "exec-powershell"
7-
import { untildifyUser } from "untildify-user"
8-
import { sourceRC } from "./rc-file.js"
7+
import { defaultRcPath, sourceRCInRc } from "./rc-file.js"
98
const { appendFile } = promises
109

1110
type AddPathOptions = {
1211
/**
1312
* The path to the RC file that the PATH variables should be added to.
1413
*/
1514
rcPath: string
15+
/** Provide a name (your tool) to add a variable guard for sourcing your rc file */
16+
guard?: string
1617
}
1718
/**
1819
* Add a path to the PATH environment variable.
@@ -21,7 +22,7 @@ type AddPathOptions = {
2122
*/
2223

2324
export async function addPath(path: string, givenOptions: Partial<AddPathOptions> = {}) {
24-
const options = { rcPath: untildifyUser(".bashrc"), ...givenOptions }
25+
const options = { rcPath: defaultRcPath, ...givenOptions }
2526

2627
if (isIgnoredPath(path)) {
2728
return
@@ -34,17 +35,17 @@ export async function addPath(path: string, givenOptions: Partial<AddPathOptions
3435
ghAddPath(path)
3536
} catch (err) {
3637
error(err as Error)
37-
await addPathSystem(path, options.rcPath)
38+
await addPathSystem(path, options)
3839
}
3940
} else {
40-
await addPathSystem(path, options.rcPath)
41+
await addPathSystem(path, options)
4142
}
4243
} catch (err) {
4344
error(`${err}\nFailed to add ${path} to the percistent PATH. You should add it manually.`)
4445
}
4546
}
4647

47-
async function addPathSystem(path: string, rcPath: string) {
48+
async function addPathSystem(path: string, options: AddPathOptions) {
4849
switch (process.platform) {
4950
case "win32": {
5051
// We do not use `execaSync(`setx PATH "${path};%PATH%"`)` because of its character limit and also because %PATH% is different for user and system
@@ -56,9 +57,9 @@ async function addPathSystem(path: string, rcPath: string) {
5657
}
5758
case "linux":
5859
case "darwin": {
59-
await sourceRC(rcPath)
60-
await appendFile(rcPath, `\nexport PATH="${path}:$PATH"\n`)
61-
info(`"${path}" was added to "${rcPath}"`)
60+
await sourceRCInRc(options)
61+
await appendFile(options.rcPath, `\nexport PATH="${path}:$PATH"\n`)
62+
info(`"${path}" was added to "${options.rcPath}"`)
6263
return
6364
}
6465
default: {

packages/os-env/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { addEnv } from "./add-env.js"
22
export { addPath } from "./add-path.js"
3-
export { finalizeRC, sourceRC } from "./rc-file.js"
3+
export { finalizeRC, sourceRCInRc as sourceRC } from "./rc-file.js"

packages/os-env/src/rc-file.ts

+37-36
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,26 @@ import { pathExists } from "path-exists"
66
import { untildifyUser } from "untildify-user"
77
const { appendFile, readFile, writeFile } = promises
88

9-
async function sourceRC_raw(rcPath: string) {
10-
const sourceRcString =
11-
`\n# source .cpprc if SOURCE_CPPRC is not set to 0\nif [[ "$SOURCE_CPPRC" != 0 && -f "${rcPath}" ]]; then source "${rcPath}"; fi\n`
9+
export const defaultRcPath = untildifyUser("~/.bashrc")
10+
11+
export type RcOptions = {
12+
/** The path to the RC file that the env variables should be added to. */
13+
rcPath: string
14+
15+
/** Provide a name (your tool) to add a variable guard for sourcing your rc file */
16+
guard?: string
17+
}
18+
19+
async function sourceRCInRc_(options: RcOptions) {
20+
const sourceRcString = options.guard === undefined
21+
? `source "${options.rcPath}"`
22+
: `# ${options.guard}\nif [[ "$SOURCE_${options.guard.toUpperCase()}RC" != 0 && -f "${options.rcPath}" ]]; then source "${options.rcPath}"; fi`
1223

1324
try {
1425
await Promise.all([
15-
addRCHeader(rcPath),
16-
sourceRcInProfile(sourceRcString),
17-
sourceRCInBashrc(sourceRcString),
26+
addRCHeader(options),
27+
addSourceToTargetRc(sourceRcString, untildifyUser("~/.bashrc")),
28+
addSourceToTargetRc(sourceRcString, untildifyUser("~/.profile")),
1829
])
1930
} catch (err) {
2031
warning(`Failed to add ${sourceRcString} to .profile or .bashrc. You should add it manually: ${err}`)
@@ -24,52 +35,42 @@ async function sourceRC_raw(rcPath: string) {
2435
/**
2536
* handles adding conditions to source rc file from .bashrc and .profile
2637
*/
27-
export const sourceRC = memoize(sourceRC_raw, { isPromise: true })
38+
export const sourceRCInRc = memoize(sourceRCInRc_, { isPromise: true })
2839

29-
async function addRCHeader(rcPath: string) {
30-
// a variable that prevents source_cpprc from being called from .bashrc and .profile
31-
const rcHeader = "# Automatically Generated by os-env\nexport SOURCE_CPPRC=0"
40+
async function addRCHeader(options: RcOptions) {
41+
// a variable that prevents source rc from being called from .bashrc and .profile
42+
const rcHeader = options.guard === undefined
43+
? "# Automatically Generated by os-env"
44+
: `# Automatically Generated by os-env ${options.guard}\nexport SOURCE_${options.guard.toUpperCase()}RC=0`
3245

33-
if (await pathExists(rcPath)) {
34-
const rcContent = await readFile(rcPath, "utf8")
46+
if (await pathExists(options.rcPath)) {
47+
const rcContent = await readFile(options.rcPath, "utf8")
3548
if (!rcContent.includes(rcHeader)) {
3649
// already executed setupCppInProfile
37-
await appendFile(rcPath, `\n${rcHeader}\n`)
38-
info(`Added ${rcHeader} to ${rcPath}`)
50+
await appendFile(options.rcPath, `\n${rcHeader}\n`)
51+
info(`Added ${rcHeader} to ${options.rcPath}`)
3952
}
4053
}
4154
}
4255

43-
async function sourceRCInBashrc(sourceRcString: string) {
44-
const bashrcPath = untildifyUser("~/.bashrc")
45-
if (await pathExists(bashrcPath)) {
46-
const bashrcContent = await readFile(bashrcPath, "utf-8")
56+
async function addSourceToTargetRc(sourceRcString: string, targetRcPath: string) {
57+
if (await pathExists(targetRcPath)) {
58+
const bashrcContent = await readFile(targetRcPath, "utf-8")
4759
if (!bashrcContent.includes(sourceRcString)) {
48-
await appendFile(bashrcPath, sourceRcString)
49-
info(`${sourceRcString} was added to ${bashrcPath}`)
50-
}
51-
}
52-
}
53-
54-
async function sourceRcInProfile(sourceRcString: string) {
55-
const profilePath = untildifyUser("~/.profile")
56-
if (await pathExists(profilePath)) {
57-
const profileContent = await readFile(profilePath, "utf-8")
58-
if (!profileContent.includes(sourceRcString)) {
59-
await appendFile(profilePath, sourceRcString)
60-
info(`${sourceRcString} was added to ${profilePath}`)
60+
await appendFile(targetRcPath, sourceRcString)
61+
info(`${sourceRcString} was added to ${targetRcPath}`)
6162
}
6263
}
6364
}
6465

65-
export async function finalizeRC(rcPath: string) {
66-
if (await pathExists(rcPath)) {
67-
const entries = (await readFile(rcPath, "utf-8")).split("\n")
66+
export async function finalizeRC(rcOptions: RcOptions) {
67+
if (await pathExists(rcOptions.rcPath)) {
68+
const entries = (await readFile(rcOptions.rcPath, "utf-8")).split("\n")
6869

6970
const uniqueEntries = [...new Set(entries.reverse())].reverse() // remove duplicates, keeping the latest entry
7071

71-
await writeFile(rcPath, uniqueEntries.join("\n"))
72+
await writeFile(rcOptions.rcPath, uniqueEntries.join("\n"))
7273

73-
await grantUserWriteAccess(rcPath)
74+
await grantUserWriteAccess(rcOptions.rcPath)
7475
}
7576
}

src/brew/brew.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { readFile } from "fs/promises"
66
import { addPath } from "os-env"
77
import { dirname } from "patha"
88
import which from "which"
9-
import { rcPath } from "../cli-options"
9+
import { rcOptions } from "../cli-options"
1010

1111
/* eslint-disable require-atomic-updates */
1212
let binDir: string | undefined
@@ -48,7 +48,7 @@ export async function setupBrew(_version: string, _setupDir: string, _arch: stri
4848
})
4949

5050
binDir = getBrewPath()
51-
await addPath(binDir, { rcPath })
51+
await addPath(binDir, rcOptions)
5252

5353
return { binDir }
5454
}

src/chocolatey/chocolatey.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { addPath } from "os-env"
33
import { pathExists } from "path-exists"
44
import { dirname } from "patha"
55
import which from "which"
6-
import { rcPath } from "../cli-options"
6+
import { rcOptions } from "../cli-options"
77
import type { InstallationInfo } from "../utils/setup/setupBin"
88

99
/* eslint-disable require-atomic-updates */
@@ -55,7 +55,7 @@ export async function setupChocolatey(
5555
)
5656

5757
const chocoPath = `${process.env.ALLUSERSPROFILE}\\chocolatey\\bin`
58-
await addPath(chocoPath, { rcPath })
58+
await addPath(chocoPath, rcOptions)
5959

6060
const maybeChoco = which.sync("choco", { nothrow: true })
6161
if (maybeChoco !== null) {

src/cli-options.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,7 @@ export function getSuccessMessage(tool: string, installationInfo: InstallationIn
7373
return msg
7474
}
7575

76-
export const rcPath = untildifyUser("~/.cpprc")
76+
export const rcOptions = {
77+
rcPath: untildifyUser(".cpprc"),
78+
guard: "cpp",
79+
}

src/compilers.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { endGroup, notice, startGroup } from "@actions/core"
33
import { error, info } from "ci-log"
44
import { addEnv } from "os-env"
55
import semverValid from "semver/functions/valid"
6-
import { getSuccessMessage } from "./cli-options"
6+
import { getSuccessMessage, rcOptions } from "./cli-options"
77
import { setupGcc, setupMingw } from "./gcc/gcc"
88
import { activateGcovGCC, activateGcovLLVM } from "./gcovr/gcovr"
99
import { setupLLVM } from "./llvm/llvm"
@@ -69,7 +69,7 @@ export async function installCompiler(
6969

7070
if (hasLLVM) {
7171
// remove back the added CPPFLAGS of LLVM that include the LLVM headers
72-
await addEnv("CPPFLAGS", "")
72+
await addEnv("CPPFLAGS", "", rcOptions)
7373
}
7474

7575
await activateGcovGCC(gccVersion)
@@ -92,7 +92,7 @@ export async function installCompiler(
9292

9393
if (hasLLVM) {
9494
// remove the CPPFLAGS of LLVM that include the LLVM headers
95-
await addEnv("CPPFLAGS", "")
95+
await addEnv("CPPFLAGS", "", rcOptions)
9696
}
9797

9898
successMessages.push(getSuccessMessage("msvc", installationInfo))
@@ -101,7 +101,7 @@ export async function installCompiler(
101101
case "appleclang":
102102
case "applellvm": {
103103
notice("Assuming apple-clang is already installed")
104-
await Promise.all([addEnv("CC", "clang"), addEnv("CXX", "clang++")])
104+
await Promise.all([addEnv("CC", "clang", rcOptions), addEnv("CXX", "clang++", rcOptions)])
105105
successMessages.push(getSuccessMessage("apple-clang", undefined))
106106
break
107107
}

src/cppcheck/cppcheck.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { addPath } from "os-env"
2-
import { rcPath } from "../cli-options"
2+
import { rcOptions } from "../cli-options"
33
import { hasDnf } from "../utils/env/hasDnf"
44
import { isArch } from "../utils/env/isArch"
55
import { isUbuntu } from "../utils/env/isUbuntu"
@@ -38,6 +38,6 @@ export async function setupCppcheck(version: string | undefined, _setupDir: stri
3838

3939
async function activateWinCppcheck() {
4040
const binDir = "C:/Program Files/Cppcheck"
41-
await addPath(binDir, { rcPath })
41+
await addPath(binDir, rcOptions)
4242
return binDir
4343
}

src/doxygen/doxygen.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { getVersion } from "../versions/versions"
1212

1313
import { pathExists } from "path-exists"
1414
import retry from "retry-as-promised"
15-
import { rcPath } from "../cli-options"
15+
import { rcOptions } from "../cli-options"
1616
import { hasDnf } from "../utils/env/hasDnf"
1717
import { isArch } from "../utils/env/isArch"
1818
import { isUbuntu } from "../utils/env/isUbuntu"
@@ -139,7 +139,7 @@ async function activateWinDoxygen() {
139139
// eslint-disable-next-line no-await-in-loop
140140
if (await pathExists(join(binDir, "doxygen.exe"))) {
141141
// eslint-disable-next-line no-await-in-loop
142-
await addPath(binDir, { rcPath })
142+
await addPath(binDir, rcOptions)
143143
return binDir
144144
}
145145
}

0 commit comments

Comments
 (0)