Skip to content

Commit 1837079

Browse files
committed
fix: parallelize updating rc files
1 parent a4e6fc4 commit 1837079

File tree

9 files changed

+135
-96
lines changed

9 files changed

+135
-96
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: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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, readFile, 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"
@@ -121,12 +121,12 @@ async function addEnvSystem(name: string, valGiven: string | undefined, options:
121121
}
122122
case "linux":
123123
case "darwin": {
124-
await setupCppInProfile()
124+
await sourceCpprc()
125125
if (options.shouldAddOnlyIfNotDefined) {
126-
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`)
127127
info(`if not defined ${name} then ${name}="${val}" was added to "${cpprc_path}`)
128128
} else {
129-
appendFileSync(cpprc_path, `\nexport ${name}="${val}"\n`)
129+
await appendFile(cpprc_path, `\nexport ${name}="${val}"\n`)
130130
info(`${name}="${val}" was added to "${cpprc_path}`)
131131
}
132132
return
@@ -150,8 +150,8 @@ async function addPathSystem(path: string) {
150150
}
151151
case "linux":
152152
case "darwin": {
153-
await setupCppInProfile()
154-
appendFileSync(cpprc_path, `\nexport PATH="${path}:$PATH"\n`)
153+
await sourceCpprc()
154+
await appendFile(cpprc_path, `\nexport PATH="${path}:$PATH"\n`)
155155
info(`"${path}" was added to "${cpprc_path}"`)
156156
return
157157
}
@@ -165,62 +165,70 @@ async function addPathSystem(path: string) {
165165
let setupCppInProfile_called = false
166166

167167
/// handles adding conditions to source .cpprc file from .bashrc and .profile
168-
export async function setupCppInProfile() {
168+
export async function sourceCpprc() {
169169
if (setupCppInProfile_called) {
170170
return
171171
}
172172

173+
const source_cpprc_string =
174+
`\n# source .cpprc if SOURCE_CPPRC is not set to 0\nif [[ "$SOURCE_CPPRC" != 0 && -f "${cpprc_path}" ]]; then source "${cpprc_path}"; fi\n`
175+
176+
try {
177+
await Promise.all([
178+
addCpprcHeader(),
179+
sourceCpprcInProfile(source_cpprc_string),
180+
sourceCpprcInBashrc(source_cpprc_string),
181+
])
182+
} catch (err) {
183+
warning(`Failed to add ${source_cpprc_string} to .profile or .bashrc. You should add it manually: ${err}`)
184+
}
185+
186+
setupCppInProfile_called = true
187+
}
188+
189+
async function addCpprcHeader() {
173190
// a variable that prevents source_cpprc from being called from .bashrc and .profile
174-
const source_cpprc_str = "# Automatically Generated by setup-cpp\nexport SOURCE_CPPRC=0"
191+
const cpprc_header = "# Automatically Generated by setup-cpp\nexport SOURCE_CPPRC=0"
175192

176193
if (await pathExists(cpprc_path)) {
177-
const cpprc_content = readFileSync(cpprc_path, "utf8")
178-
if (!cpprc_content.includes(source_cpprc_str)) {
194+
const cpprc_content = await readFile(cpprc_path, "utf8")
195+
if (!cpprc_content.includes(cpprc_header)) {
179196
// already executed setupCppInProfile
180-
appendFileSync(cpprc_path, `\n${source_cpprc_str}\n`)
181-
info(`Added ${source_cpprc_str} to ${cpprc_path}`)
197+
await appendFile(cpprc_path, `\n${cpprc_header}\n`)
198+
info(`Added ${cpprc_header} to ${cpprc_path}`)
182199
}
183200
}
201+
}
184202

185-
// source cpprc in bashrc/profile
186-
187-
const source_cpprc_string =
188-
`\n# source .cpprc if SOURCE_CPPRC is not set to 0\nif [[ "$SOURCE_CPPRC" != 0 && -f "${cpprc_path}" ]]; then source "${cpprc_path}"; fi\n`
189-
190-
try {
191-
// source cpprc in .profile
192-
const profile_path = untildifyUser("~/.profile")
193-
if (await pathExists(profile_path)) {
194-
const profileContent = readFileSync(profile_path, "utf-8")
195-
if (!profileContent.includes(source_cpprc_string)) {
196-
appendFileSync(profile_path, source_cpprc_string)
197-
info(`${source_cpprc_string} was added to ${profile_path}`)
198-
}
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}`)
199210
}
211+
}
212+
}
200213

201-
// source cpprc in .bashrc too
202-
const bashrc_path = untildifyUser("~/.bashrc")
203-
if (await pathExists(bashrc_path)) {
204-
const bashrcContent = readFileSync(bashrc_path, "utf-8")
205-
if (!bashrcContent.includes(source_cpprc_string)) {
206-
appendFileSync(bashrc_path, source_cpprc_string)
207-
info(`${source_cpprc_string} was added to ${bashrc_path}`)
208-
}
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}`)
209221
}
210-
} catch (err) {
211-
warning(`Failed to add ${source_cpprc_string} to .profile or .bashrc. You should add it manually: ${err}`)
212222
}
213-
214-
setupCppInProfile_called = true
215223
}
216224

217225
export async function finalizeCpprc() {
218226
if (await pathExists(cpprc_path)) {
219-
const entries = readFileSync(cpprc_path, "utf-8").split("\n")
227+
const entries = (await readFile(cpprc_path, "utf-8")).split("\n")
220228

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

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

225233
await grantUserWriteAccess(cpprc_path)
226234
}

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)