feat: de-duplicate cpprc

This commit is contained in:
Amin Yahyaabadi 2022-11-20 23:14:33 -08:00
parent ea23ed92ca
commit d20e347c91
6 changed files with 25 additions and 9 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -39,7 +39,7 @@ import { setupVcpkg } from "./vcpkg/vcpkg"
import { join } from "patha"
import { setupVCVarsall } from "./vcvarsall/vcvarsall"
import { setupKcov } from "./kcov/kcov"
import { addEnv } from "./utils/env/addEnv"
import { addEnv, finalizeCpprc } from "./utils/env/addEnv"
import { setupSevenZip } from "./sevenzip/sevenzip"
import { setupGraphviz } from "./graphviz/graphviz"
import { setupNala } from "./nala/nala"
@ -270,6 +270,8 @@ export async function main(args: string[]): Promise<number> {
info(`took ${timeFormatter.format(time1, time2) || "0 seconds"}`)
}
finalizeCpprc()
if (successMessages.length === 0 && errorMessages.length === 0) {
warning("setup_cpp was called without any arguments. Nothing to do.")
return 0

View File

@ -1,7 +1,7 @@
import { exportVariable, addPath as ghAddPath, info, setFailed } from "@actions/core"
import ciDetect from "@npmcli/ci-detect"
import { untildifyUser } from "untildify-user"
import { appendFileSync, existsSync, readFileSync } from "fs"
import { appendFileSync, existsSync, readFileSync, writeFileSync } from "fs"
import { error, warning } from "ci-log"
import { execPowershell } from "exec-powershell"
import { delimiter } from "path"
@ -119,7 +119,7 @@ export function setupCppInProfile() {
}
// a variable that prevents source_cpprc from being called from .bashrc and .profile
const source_cpprc_str = "export SOURCE_CPPRC=0"
const source_cpprc_str = "# Automatically Generated by setup-cpp\nexport SOURCE_CPPRC=0"
if (existsSync(cpprc_path)) {
const cpprc_content = readFileSync(cpprc_path, "utf8")
@ -132,8 +132,6 @@ export function setupCppInProfile() {
appendFileSync(cpprc_path, `\n${source_cpprc_str}\n`)
info(`Added ${source_cpprc_str} to ${cpprc_path}`)
giveUserAccess(cpprc_path)
// source cpprc in bashrc/profile
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() {
setupCppInProfile_called = true
}
export function finalizeCpprc() {
if (existsSync(cpprc_path)) {
const entries = readFileSync(cpprc_path, "utf-8").split("\n")
const unique_entries = [...new Set(entries.reverse())].reverse() // remove duplicates, keeping the latest entry
writeFileSync(cpprc_path, unique_entries.join("\n"))
try {
giveUserAccess(cpprc_path)
} catch {
// ignore
}
}
}