feat: add environment variables to .cpprc when setup_cpp used locally

This commit is contained in:
Amin Yahyaabadi 2022-03-01 02:43:50 -08:00
parent 1af7af9c53
commit f052b759d1
9 changed files with 86 additions and 43 deletions

View File

@ -6,6 +6,7 @@
"CPATH",
"Cppcheck",
"CPPFLAGS",
"cpprc",
"dyld",
"eabi",
"execa",

View File

@ -61,7 +61,7 @@ An example that installs llvm, cmake, ninja, ccache, and vcpkg:
curl -LJO "https://github.com/aminya/setup-cpp/releases/download/v0.9.4/setup_cpp_windows.exe"
./setup_cpp_windows --compiler llvm --cmake true --ninja true --ccache true --vcpkg true
RefreshEnv.cmd # reload the environment
RefreshEnv.cmd # activate cpp enviroment variables
```
```ps1
@ -70,7 +70,7 @@ wget "https://github.com/aminya/setup-cpp/releases/download/v0.9.4/setup_cpp_lin
chmod +x setup_cpp_linux
sudo ./setup_cpp_linux --compiler llvm --cmake true --ninja true --ccache true --vcpkg true
source ~/.profile # reload the environment
source ~/.cpprc # activate cpp enviroment variables
```
```ps1
@ -79,7 +79,7 @@ wget "https://github.com/aminya/setup-cpp/releases/download/v0.9.4/setup_cpp_mac
chmod +x setup_cpp_mac
sudo ./setup_cpp_mac --compiler llvm --cmake true --ninja true --ccache true --vcpkg true
source ~/.profile # reload the environment
source ~/.cpprc # activate cpp enviroment variables
```
NOTE: In the `compiler` entry, you can specify the version after `-` like `llvm-11.0.0`.
@ -100,7 +100,7 @@ Open the shell as admin, download via `curl`, then install
curl -LJO "https://github.com/aminya/setup-cpp/releases/download/v0.9.4/setup_cpp.js"
node ./setup_cpp.js --compiler llvm --cmake true --ninja true --ccache true --vcpkg true
RefreshEnv.cmd # reload the environment
RefreshEnv.cmd # activate cpp enviroment variables
```
On Linux or Mac:
@ -109,7 +109,7 @@ On Linux or Mac:
wget "https://github.com/aminya/setup-cpp/releases/download/v0.9.4/setup_cpp.js"
sudo node ./setup_cpp.js --compiler llvm --cmake true --ninja true --ccache true --vcpkg true
source ~/.profile # reload the environment
source ~/.cpprc # activate cpp enviroment variables
```
## Inside GitHub Actions
@ -190,8 +190,8 @@ RUN chmod +x ./setup_cpp_linux
# install llvm, cmake, ninja, and ccache
RUN ./setup_cpp_linux --compiler llvm --cmake true --ninja true --ccache true
# reload the environment
RUN source ~/.profile
# activate cpp enviroment variables
RUN source ~/.cpprc
ENTRYPOINT [ "/bin/sh" ]
```
@ -266,7 +266,7 @@ stages:
curl -LJO "https://github.com/aminya/setup-cpp/releases/download/v0.9.4/setup_cpp_linux"
chmod +x setup_cpp_linux
./setup_cpp_linux --compiler $compiler --cmake true --ninja true --ccache true --vcpkg true
source ~/.profile
source ~/.cpprc
.test: &test |
# Build and Test

View File

@ -11,6 +11,6 @@ RUN chmod +x ./setup_cpp_linux
RUN ./setup_cpp_linux --compiler llvm --cmake true --ninja true --ccache true --vcpkg true
# reload the environment
CMD source ~/.profile
CMD source ~/.cpprc
ENTRYPOINT [ "/bin/sh" ]

View File

@ -11,6 +11,6 @@ WORKDIR "/"
RUN node ./setup_cpp.js --compiler llvm --cmake true --ninja true --cppcheck true --ccache true --vcpkg true --doxygen true --gcovr true
# reload the environment and print the versions
CMD source ~/.profile && clang --version && cmake --version && ninja --version && ccache --version && cppcheck --version && vcpkg --version && doxygen --version && dot --version && gcovr --version
CMD source ~/.cpprc && clang --version && cmake --version && ninja --version && ccache --version && cppcheck --version && vcpkg --version && doxygen --version && dot --version && gcovr --version
ENTRYPOINT [ "/bin/sh" ]

View File

@ -19,6 +19,6 @@ WORKDIR "/"
RUN node ./setup_cpp.js --compiler llvm --cmake true --ninja true --cppcheck true --ccache true --vcpkg true --doxygen true --gcovr true
# reload the environment and print the versions
CMD source ~/.profile && clang --version && cmake --version && ninja --version && ccache --version && cppcheck --version && vcpkg --version && doxygen --version && dot --version && gcovr --version
CMD source ~/.cpprc && clang --version && cmake --version && ninja --version && ccache --version && cppcheck --version && vcpkg --version && doxygen --version && dot --version && gcovr --version
ENTRYPOINT [ "/bin/sh" ]

2
dist/setup_cpp.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -232,7 +232,7 @@ export async function main(args: string[]): Promise<number> {
}
case "linux":
case "darwin": {
core.info("Run `source ~/.profile` or restart your shell to update the environment.")
core.info("Run `source ~/.cpprc` or restart your shell to update the environment.")
break
}
default: {

View File

@ -1,8 +1,8 @@
import { exportVariable, addPath as ghAddPath, info } from "@actions/core"
import { isGitHubCI } from "./isci"
import { untildify_user as untildify } from "../path/untildify"
import { appendFileSync } from "fs"
import { error } from "../io/io"
import { appendFileSync, existsSync, readFileSync } from "fs"
import { error, warning } from "../io/io"
import { execPowershell } from "../exec/powershell"
import { delimiter } from "path"
@ -25,30 +25,6 @@ export function addEnv(name: string, val: string | undefined) {
}
}
function addEnvSystem(name: string, valGiven: string | undefined) {
const val = valGiven ?? ""
switch (process.platform) {
case "win32": {
// We do not use `execa.sync(`setx PATH "${path};%PATH%"`)` because of its character limit
execPowershell(`[Environment]::SetEnvironmentVariable("${name}", "${val}", "User")`)
info(`${name}="${val} was set in the environment."`)
return
}
case "linux":
case "darwin": {
// find profile path
const profile_path = untildify(".profile")
appendFileSync(profile_path, `\nexport ${name}="${val}"\n`)
info(`${name}="${val} was added to "${profile_path}"`)
return
}
default: {
// fall through shell path modification
}
}
process.env[name] = val
}
/** An add path function that works locally or inside GitHub Actions */
export function addPath(path: string) {
process.env.PATH = `${path}${delimiter}${process.env.PATH}`
@ -69,6 +45,31 @@ export function addPath(path: string) {
}
}
const cpprc_path = untildify(".cpprc")
function addEnvSystem(name: string, valGiven: string | undefined) {
const val = valGiven ?? ""
switch (process.platform) {
case "win32": {
// We do not use `execa.sync(`setx PATH "${path};%PATH%"`)` because of its character limit
execPowershell(`[Environment]::SetEnvironmentVariable("${name}", "${val}", "User")`)
info(`${name}="${val} was set in the environment."`)
return
}
case "linux":
case "darwin": {
setupCppInProfile()
appendFileSync(cpprc_path, `\nexport ${name}="${val}"\n`)
info(`${name}="${val} was added to "${cpprc_path}"`)
return
}
default: {
// fall through shell path modification
}
}
process.env[name] = val
}
function addPathSystem(path: string) {
switch (process.platform) {
case "win32": {
@ -81,9 +82,9 @@ function addPathSystem(path: string) {
}
case "linux":
case "darwin": {
const profile_path = untildify(".profile")
appendFileSync(profile_path, `\nexport PATH=${path}:$PATH\n`)
info(`${path} was added to "${profile_path}"`)
setupCppInProfile()
appendFileSync(cpprc_path, `\nexport PATH=${path}:$PATH\n`)
info(`${path} was added to "${cpprc_path}"`)
return
}
default: {
@ -91,3 +92,44 @@ function addPathSystem(path: string) {
}
}
}
let setupCppInProfile_called = false
/// handles adding conditions to source .cpprc file from .bashrc and .profile
function setupCppInProfile() {
if (setupCppInProfile_called) {
return
}
// a variable that prevents source_cpprc from being called from .bashrc and .profile
const source_cpprc_str = "export SOURCE_CPPRC=0"
if (existsSync(cpprc_path)) {
const cpprc_content = readFileSync(cpprc_path, "utf8")
if (cpprc_content.includes(source_cpprc_str)) {
// already executed setupCppInProfile
return
}
}
appendFileSync(cpprc_path, `\n${source_cpprc_str}\n`)
info(`Added ${source_cpprc_str} to ${cpprc_path}`)
const source_cpprc_string = `\n# source .cpprc if SOURCE_CPPRC is set to 1\nif [ "$SOURCE_CPPRC" = 1 ]; then source "${cpprc_path}"; fi\n`
try {
// source cpprc in .profile
const profile_path = untildify(".profile")
appendFileSync(profile_path, source_cpprc_string)
info(`${source_cpprc_string} was added to ${profile_path}`)
// source cpprc in .bashrc too
const bashrc_path = untildify(".bashrc")
appendFileSync(bashrc_path, source_cpprc_string)
info(`${source_cpprc_string} was added to ${bashrc_path}`)
} catch (err) {
warning(`Failed to add ${source_cpprc_string} to .profile or .bashrc. You should add it manually: ${err}`)
}
setupCppInProfile_called = true
}