Skip to content

Commit d20e347

Browse files
committed
feat: de-duplicate cpprc
1 parent ea23ed9 commit d20e347

File tree

6 files changed

+25
-9
lines changed

6 files changed

+25
-9
lines changed

dist/node12/setup_cpp.js

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/node12/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/node16/setup_cpp.js

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/node16/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/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import { setupVcpkg } from "./vcpkg/vcpkg"
3939
import { join } from "patha"
4040
import { setupVCVarsall } from "./vcvarsall/vcvarsall"
4141
import { setupKcov } from "./kcov/kcov"
42-
import { addEnv } from "./utils/env/addEnv"
42+
import { addEnv, finalizeCpprc } from "./utils/env/addEnv"
4343
import { setupSevenZip } from "./sevenzip/sevenzip"
4444
import { setupGraphviz } from "./graphviz/graphviz"
4545
import { setupNala } from "./nala/nala"
@@ -270,6 +270,8 @@ export async function main(args: string[]): Promise<number> {
270270
info(`took ${timeFormatter.format(time1, time2) || "0 seconds"}`)
271271
}
272272

273+
finalizeCpprc()
274+
273275
if (successMessages.length === 0 && errorMessages.length === 0) {
274276
warning("setup_cpp was called without any arguments. Nothing to do.")
275277
return 0

src/utils/env/addEnv.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { exportVariable, addPath as ghAddPath, info, setFailed } from "@actions/core"
22
import ciDetect from "@npmcli/ci-detect"
33
import { untildifyUser } from "untildify-user"
4-
import { appendFileSync, existsSync, readFileSync } from "fs"
4+
import { appendFileSync, existsSync, readFileSync, writeFileSync } from "fs"
55
import { error, warning } from "ci-log"
66
import { execPowershell } from "exec-powershell"
77
import { delimiter } from "path"
@@ -119,7 +119,7 @@ export function setupCppInProfile() {
119119
}
120120

121121
// a variable that prevents source_cpprc from being called from .bashrc and .profile
122-
const source_cpprc_str = "export SOURCE_CPPRC=0"
122+
const source_cpprc_str = "# Automatically Generated by setup-cpp\nexport SOURCE_CPPRC=0"
123123

124124
if (existsSync(cpprc_path)) {
125125
const cpprc_content = readFileSync(cpprc_path, "utf8")
@@ -132,8 +132,6 @@ export function setupCppInProfile() {
132132
appendFileSync(cpprc_path, `\n${source_cpprc_str}\n`)
133133
info(`Added ${source_cpprc_str} to ${cpprc_path}`)
134134

135-
giveUserAccess(cpprc_path)
136-
137135
// source cpprc in bashrc/profile
138136

139137
const source_cpprc_string = `\n# source .cpprc if SOURCE_CPPRC is not set to 0\nif [[ "$SOURCE_CPPRC" != 0 && -f "${cpprc_path}" ]]; then source "${cpprc_path}"; fi\n`
@@ -154,3 +152,19 @@ export function setupCppInProfile() {
154152

155153
setupCppInProfile_called = true
156154
}
155+
156+
export function finalizeCpprc() {
157+
if (existsSync(cpprc_path)) {
158+
const entries = readFileSync(cpprc_path, "utf-8").split("\n")
159+
160+
const unique_entries = [...new Set(entries.reverse())].reverse() // remove duplicates, keeping the latest entry
161+
162+
writeFileSync(cpprc_path, unique_entries.join("\n"))
163+
164+
try {
165+
giveUserAccess(cpprc_path)
166+
} catch {
167+
// ignore
168+
}
169+
}
170+
}

0 commit comments

Comments
 (0)