Skip to content

Commit 17a5a83

Browse files
authored
Merge pull request #241 from aminya/bashrc-update [skip ci]
2 parents 072c438 + 1837079 commit 17a5a83

File tree

9 files changed

+142
-93
lines changed

9 files changed

+142
-93
lines changed

dist/actions/setup-cpp.js

Lines changed: 17 additions & 17 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: 17 additions & 17 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: 17 additions & 17 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.

src/utils/compat/fs/types.d.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
declare module "fs/promises" {
2+
import * as fs from "fs"
3+
export default fs.promises
4+
5+
export const {
6+
access,
7+
appendFile,
8+
chmod,
9+
chown,
10+
copyFile,
11+
lchmod,
12+
lchown,
13+
link,
14+
lstat,
15+
mkdir,
16+
mkdtemp,
17+
open,
18+
readdir,
19+
readFile,
20+
readlink,
21+
realpath,
22+
rename,
23+
rmdir,
24+
stat,
25+
symlink,
26+
truncate,
27+
unlink,
28+
utimes,
29+
writeFile,
30+
} = fs.promises
31+
}

src/utils/env/addEnv.ts

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import { error, warning } from "ci-log"
55
import escapeSpace from "escape-path-with-spaces"
66
import escapeQuote from "escape-quotes"
77
import { execPowershell } from "exec-powershell"
8-
import { appendFileSync, readFileSync, writeFileSync } from "fs"
8+
import { appendFile, readFile, writeFile } from "fs/promises"
99
import { delimiter } from "path"
1010
import { pathExists } from "path-exists"
1111
import { untildifyUser } from "untildify-user"
1212

1313
type AddEnvOptions = {
1414
/** If true, the value will be escaped with quotes and spaces will be escaped with backslash */
15-
shouldEscapeSpace?: boolean
15+
shouldEscapeSpace: boolean
1616
/** If true, the variable will be only added if it is not defined */
17-
shouldAddOnlyIfNotDefined?: boolean
17+
shouldAddOnlyIfNotDefined: boolean
1818
}
1919

2020
const defaultAddEnvOptions: AddEnvOptions = {
@@ -30,8 +30,10 @@ const defaultAddEnvOptions: AddEnvOptions = {
3030
export async function addEnv(
3131
name: string,
3232
valGiven: string | undefined,
33-
options: AddEnvOptions = defaultAddEnvOptions,
33+
givenOptions: Partial<AddEnvOptions> = defaultAddEnvOptions,
3434
) {
35+
const options = { ...defaultAddEnvOptions, ...givenOptions }
36+
3537
const val = escapeString(valGiven ?? "", options.shouldEscapeSpace)
3638
try {
3739
if (GITHUB_ACTIONS) {
@@ -119,12 +121,12 @@ async function addEnvSystem(name: string, valGiven: string | undefined, options:
119121
}
120122
case "linux":
121123
case "darwin": {
122-
await setupCppInProfile()
124+
await sourceCpprc()
123125
if (options.shouldAddOnlyIfNotDefined) {
124-
appendFileSync(cpprc_path, `\nif [ -z "\${${name}}" ]; then export ${name}="${val}"; fi\n`)
126+
await appendFile(cpprc_path, `\nif [ -z "\${${name}}" ]; then export ${name}="${val}"; fi\n`)
125127
info(`if not defined ${name} then ${name}="${val}" was added to "${cpprc_path}`)
126128
} else {
127-
appendFileSync(cpprc_path, `\nexport ${name}="${val}"\n`)
129+
await appendFile(cpprc_path, `\nexport ${name}="${val}"\n`)
128130
info(`${name}="${val}" was added to "${cpprc_path}`)
129131
}
130132
return
@@ -148,8 +150,8 @@ async function addPathSystem(path: string) {
148150
}
149151
case "linux":
150152
case "darwin": {
151-
await setupCppInProfile()
152-
appendFileSync(cpprc_path, `\nexport PATH="${path}:$PATH"\n`)
153+
await sourceCpprc()
154+
await appendFile(cpprc_path, `\nexport PATH="${path}:$PATH"\n`)
153155
info(`"${path}" was added to "${cpprc_path}"`)
154156
return
155157
}
@@ -163,54 +165,70 @@ async function addPathSystem(path: string) {
163165
let setupCppInProfile_called = false
164166

165167
/// handles adding conditions to source .cpprc file from .bashrc and .profile
166-
export async function setupCppInProfile() {
168+
export async function sourceCpprc() {
167169
if (setupCppInProfile_called) {
168170
return
169171
}
170172

171-
// a variable that prevents source_cpprc from being called from .bashrc and .profile
172-
const source_cpprc_str = "# Automatically Generated by setup-cpp\nexport SOURCE_CPPRC=0"
173-
174-
if (await pathExists(cpprc_path)) {
175-
const cpprc_content = readFileSync(cpprc_path, "utf8")
176-
if (cpprc_content.includes(source_cpprc_str)) {
177-
// already executed setupCppInProfile
178-
return
179-
}
180-
}
181-
182-
appendFileSync(cpprc_path, `\n${source_cpprc_str}\n`)
183-
info(`Added ${source_cpprc_str} to ${cpprc_path}`)
184-
185-
// source cpprc in bashrc/profile
186-
187173
const source_cpprc_string =
188174
`\n# source .cpprc if SOURCE_CPPRC is not set to 0\nif [[ "$SOURCE_CPPRC" != 0 && -f "${cpprc_path}" ]]; then source "${cpprc_path}"; fi\n`
189175

190176
try {
191-
// source cpprc in .profile
192-
const profile_path = untildifyUser("~/.profile")
193-
appendFileSync(profile_path, source_cpprc_string)
194-
info(`${source_cpprc_string} was added to ${profile_path}`)
195-
196-
// source cpprc in .bashrc too
197-
const bashrc_path = untildifyUser("~/.bashrc")
198-
appendFileSync(bashrc_path, source_cpprc_string)
199-
info(`${source_cpprc_string} was added to ${bashrc_path}`)
177+
await Promise.all([
178+
addCpprcHeader(),
179+
sourceCpprcInProfile(source_cpprc_string),
180+
sourceCpprcInBashrc(source_cpprc_string),
181+
])
200182
} catch (err) {
201183
warning(`Failed to add ${source_cpprc_string} to .profile or .bashrc. You should add it manually: ${err}`)
202184
}
203185

204186
setupCppInProfile_called = true
205187
}
206188

189+
async function addCpprcHeader() {
190+
// a variable that prevents source_cpprc from being called from .bashrc and .profile
191+
const cpprc_header = "# Automatically Generated by setup-cpp\nexport SOURCE_CPPRC=0"
192+
193+
if (await pathExists(cpprc_path)) {
194+
const cpprc_content = await readFile(cpprc_path, "utf8")
195+
if (!cpprc_content.includes(cpprc_header)) {
196+
// already executed setupCppInProfile
197+
await appendFile(cpprc_path, `\n${cpprc_header}\n`)
198+
info(`Added ${cpprc_header} to ${cpprc_path}`)
199+
}
200+
}
201+
}
202+
203+
async function sourceCpprcInBashrc(source_cpprc_string: string) {
204+
const bashrc_path = untildifyUser("~/.bashrc")
205+
if (await pathExists(bashrc_path)) {
206+
const bashrcContent = await readFile(bashrc_path, "utf-8")
207+
if (!bashrcContent.includes(source_cpprc_string)) {
208+
await appendFile(bashrc_path, source_cpprc_string)
209+
info(`${source_cpprc_string} was added to ${bashrc_path}`)
210+
}
211+
}
212+
}
213+
214+
async function sourceCpprcInProfile(source_cpprc_string: string) {
215+
const profile_path = untildifyUser("~/.profile")
216+
if (await pathExists(profile_path)) {
217+
const profileContent = await readFile(profile_path, "utf-8")
218+
if (!profileContent.includes(source_cpprc_string)) {
219+
await appendFile(profile_path, source_cpprc_string)
220+
info(`${source_cpprc_string} was added to ${profile_path}`)
221+
}
222+
}
223+
}
224+
207225
export async function finalizeCpprc() {
208226
if (await pathExists(cpprc_path)) {
209-
const entries = readFileSync(cpprc_path, "utf-8").split("\n")
227+
const entries = (await readFile(cpprc_path, "utf-8")).split("\n")
210228

211229
const unique_entries = [...new Set(entries.reverse())].reverse() // remove duplicates, keeping the latest entry
212230

213-
writeFileSync(cpprc_path, unique_entries.join("\n"))
231+
await writeFile(cpprc_path, unique_entries.join("\n"))
214232

215233
await grantUserWriteAccess(cpprc_path)
216234
}

src/utils/setup/setupAptPack.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { execRoot, execRootSync } from "admina"
22
import { GITHUB_ACTIONS } from "ci-info"
33
import { promises as fsPromises } from "fs"
44
import { pathExists } from "path-exists"
5-
import { addEnv, cpprc_path, setupCppInProfile } from "../env/addEnv"
5+
import { addEnv, cpprc_path, sourceCpprc } from "../env/addEnv"
66
import { InstallationInfo } from "./setupBin"
77
const { appendFile } = fsPromises
88
import { info, warning } from "ci-log"
@@ -223,7 +223,7 @@ export async function updateAptAlternatives(name: string, path: string, priority
223223
if (GITHUB_ACTIONS) {
224224
return execRoot("update-alternatives", ["--install", `/usr/bin/${name}`, name, path, priority.toString()])
225225
} else {
226-
await setupCppInProfile()
226+
await sourceCpprc()
227227
return appendFile(
228228
cpprc_path,
229229
`\nif [ $UID -eq 0 ]; then update-alternatives --install /usr/bin/${name} ${name} ${path} ${priority}; fi\n`,

0 commit comments

Comments
 (0)