Skip to content

Commit 771228c

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

File tree

33 files changed

+244
-228
lines changed

33 files changed

+244
-228
lines changed

dist/actions/setup-cpp.js

Lines changed: 30 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/actions/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js

Lines changed: 30 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js

Lines changed: 30 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 10 additions & 9 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 37 additions & 36 deletions
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
}

0 commit comments

Comments
 (0)