Merge pull request #43 from aminya/setup_cpp_rc [skip ci]

This commit is contained in:
Amin Yahyaabadi 2022-03-01 03:10:32 -08:00 committed by GitHub
commit 650820a2b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 124 additions and 95 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,13 +79,14 @@ 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`.
For the tools, instead of `true` that chooses the default version, you can pass a specific version.
NOTE: In the `compiler` entry, you can specify the version after `-` like `llvm-11.0.0`. For the tools, you can pass a specific version instead of `true` that chooses the default version
NOTE: you will not need `sudo` if you are already a root user (e.g., in a GitLab runner).
NOTE: On Unix systems, when `setup-cpp` is used locally or in other CI services like GitLab, the environment variables are added to `~/.cpprc`. You should run `source ~/.cpprc` to immediately activate the environment variables. This file is automatically sourced in the next shell restart from `~/.bashrc` or `~/.profile` if `SOURCE_CPPRC` is not set to `0`. To deactivate `.cpprc` in the next shell restart, rename/remove `~/.cpprc`.
NOTE: On Unix systems, you will not need `sudo` if you are already a root user (e.g., in a GitLab runner or Docker).
### With Nodejs
@ -100,7 +101,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 +110,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 +191,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 +267,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

@ -3,7 +3,7 @@ import execa from "execa"
import { existsSync } from "fs"
import { dirname } from "path"
import which from "which"
import { addPath } from "../utils/path/addPath"
import { addPath } from "../utils/env/addEnv"
import { InstallationInfo } from "../utils/setup/setupBin"
let binDir: string | undefined

View File

@ -1,4 +1,4 @@
import { addPath } from "../utils/path/addPath"
import { addPath } from "../utils/env/addEnv"
import { setupAptPack } from "../utils/setup/setupAptPack"
import { setupBrewPack } from "../utils/setup/setupBrewPack"
import { setupChocoPack } from "../utils/setup/setupChocoPack"

View File

@ -1,4 +1,4 @@
import { addPath } from "../utils/path/addPath"
import { addPath } from "../utils/env/addEnv"
import { setupAptPack } from "../utils/setup/setupAptPack"
import { InstallationInfo, PackageInfo, setupBin } from "../utils/setup/setupBin"
import { setupBrewPack } from "../utils/setup/setupBrewPack"

View File

@ -1,4 +1,4 @@
import { addPath } from "../utils/path/addPath"
import { addPath } from "../utils/env/addEnv"
import { existsSync } from "fs"
import { setupAptPack } from "../utils/setup/setupAptPack"
import { setupBrewPack } from "../utils/setup/setupBrewPack"

View File

@ -1,4 +1,4 @@
import { addPath } from "../utils/path/addPath"
import { addPath } from "../utils/env/addEnv"
import { setupAptPack } from "../utils/setup/setupAptPack"
import { InstallationInfo } from "../utils/setup/setupBin"
import { setupBrewPack } from "../utils/setup/setupBrewPack"

View File

@ -227,12 +227,12 @@ export async function main(args: string[]): Promise<number> {
if (!isGitHubCI()) {
switch (process.platform) {
case "win32": {
core.info("Run `RefreshEnv.cmd` or restart your shell to update the environment.")
warning("Run `RefreshEnv.cmd` or restart your shell to update the environment.")
break
}
case "linux":
case "darwin": {
core.info("Run `source ~/.profile` or restart your shell to update the environment.")
warning("Run `source ~/.cpprc` or restart your shell to update the environment.")
break
}
default: {

View File

@ -1,4 +1,4 @@
import { addPath } from "../utils/path/addPath"
import { addPath } from "../utils/env/addEnv"
import { setupChocoPack } from "../utils/setup/setupChocoPack"
// eslint-disable-next-line @typescript-eslint/no-unused-vars

View File

@ -1,5 +1,5 @@
import * as core from "@actions/core"
import { addPath } from "../utils/path/addPath"
import { addPath } from "../utils/env/addEnv"
import { setupAptPack } from "../utils/setup/setupAptPack"
import { setupBrewPack } from "../utils/setup/setupBrewPack"
import { setupChocoPack } from "../utils/setup/setupChocoPack"

View File

@ -1,12 +1,10 @@
import { exportVariable } from "@actions/core"
import * as core from "@actions/core"
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 { join } from "path"
import { isRoot } from "./sudo"
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"
/** An add path function that works locally or inside GitHub Actions */
export function addEnv(name: string, val: string | undefined) {
@ -27,26 +25,42 @@ export function addEnv(name: string, val: string | undefined) {
}
}
/** An add path function that works locally or inside GitHub Actions */
export function addPath(path: string) {
process.env.PATH = `${path}${delimiter}${process.env.PATH}`
try {
if (isGitHubCI()) {
ghAddPath(path)
} else {
addPathSystem(path)
}
} catch (err) {
try {
error(err as Error)
return addPathSystem(path)
} catch (err2) {
error(err2 as Error)
}
error(`Failed to add ${path} to the percistent PATH. You should add it manually.`)
}
}
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")`)
core.info(`${name}="${val} was set in the environment."`)
info(`${name}="${val} was set in the environment."`)
return
}
case "linux":
case "darwin": {
// find profile path
let profile_path = untildify(".profile")
if (isRoot() && typeof process.env.SUDO_USER === "string") {
// use the user profile even if root
profile_path = join("/home/", process.env.SUDO_USER, ".profile")
}
appendFileSync(profile_path, `\nexport ${name}="${val}"\n`)
core.info(`${name}="${val} was added to "${profile_path}"`)
setupCppInProfile()
appendFileSync(cpprc_path, `\nexport ${name}="${val}"\n`)
info(`${name}="${val} was added to "${cpprc_path}"`)
return
}
default: {
@ -55,3 +69,67 @@ function addEnvSystem(name: string, valGiven: string | undefined) {
}
process.env[name] = val
}
function addPathSystem(path: string) {
switch (process.platform) {
case "win32": {
// We do not use `execa.sync(`setx PATH "${path};%PATH%"`)` because of its character limit and also because %PATH% is different for user and system
execPowershell(
`$USER_PATH=([Environment]::GetEnvironmentVariable("PATH", "User")); [Environment]::SetEnvironmentVariable("PATH", "${path};$USER_PATH", "User")`
)
info(`${path} was added to the PATH.`)
return
}
case "linux":
case "darwin": {
setupCppInProfile()
appendFileSync(cpprc_path, `\nexport PATH=${path}:$PATH\n`)
info(`${path} was added to "${cpprc_path}"`)
return
}
default: {
return
}
}
}
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 not set to 0\nif [[ "$SOURCE_CPPRC" != 0 && -f "${cpprc_path}" ]]; 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
}

View File

@ -1,51 +0,0 @@
import { addPath as ghAddPath } from "@actions/core"
import { delimiter } from "path"
import * as core from "@actions/core"
import { isGitHubCI } from "../env/isci"
import { untildify_user as untildify } from "./untildify"
import { appendFileSync } from "fs"
import { error } from "../io/io"
import { execPowershell } from "../exec/powershell"
/** An add path function that works locally or inside GitHub Actions */
export function addPath(path: string) {
process.env.PATH = `${path}${delimiter}${process.env.PATH}`
try {
if (isGitHubCI()) {
ghAddPath(path)
} else {
addPathSystem(path)
}
} catch (err) {
try {
error(err as Error)
return addPathSystem(path)
} catch (err2) {
error(err2 as Error)
}
error(`Failed to add ${path} to the percistent PATH. You should add it manually.`)
}
}
function addPathSystem(path: string) {
switch (process.platform) {
case "win32": {
// We do not use `execa.sync(`setx PATH "${path};%PATH%"`)` because of its character limit and also because %PATH% is different for user and system
execPowershell(
`$USER_PATH=([Environment]::GetEnvironmentVariable("PATH", "User")); [Environment]::SetEnvironmentVariable("PATH", "${path};$USER_PATH", "User")`
)
core.info(`${path} was added to the PATH.`)
return
}
case "linux":
case "darwin": {
const profile_path = untildify(".profile")
appendFileSync(profile_path, `\nexport PATH=${path}:$PATH\n`)
core.info(`${path} was added to "${profile_path}"`)
return
}
default: {
return
}
}
}

View File

@ -1,6 +1,6 @@
import { find, downloadTool, cacheDir } from "@actions/tool-cache"
import { info } from "@actions/core"
import { addPath } from "../path/addPath"
import { addPath } from "../env/addEnv"
import { join } from "path"
import { existsSync } from "fs"
import { tmpdir } from "os"

View File

@ -1,5 +1,5 @@
/* eslint-disable require-atomic-updates */
import { addPath } from "../path/addPath"
import { addPath } from "../env/addEnv"
import which from "which"
import { setupChocolatey } from "../../chocolatey/chocolatey"
import { InstallationInfo } from "./setupBin"

View File

@ -3,7 +3,7 @@ import { getExecOutput } from "@actions/exec"
import execa from "execa"
import which from "which"
import { info } from "@actions/core"
import { addPath } from "../path/addPath"
import { addPath } from "../env/addEnv"
import { setupPython } from "../../python/python"
import { isBinUptoDate } from "./version"
import { join } from "path"