Refactor setting environment variables

This commit is contained in:
Shivam Mathur 2022-01-18 04:41:51 +05:30
parent 0374a8ae84
commit 2b729b1130
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
4 changed files with 57 additions and 4 deletions

View File

@ -22,10 +22,10 @@ Function Edit-ComposerConfig() {
if (-not(Test-Path $composer_json)) {
Set-Content -Path $composer_json -Value "{}"
}
Get-Content -Path $dist\..\src\configs\composer.env | Add-Content -Path $env:GITHUB_ENV
Add-EnvPATH $dist\..\src\configs\composer.env
Add-Path $composer_bin
if (Test-Path env:COMPOSER_TOKEN) {
composer -q config -g github-oauth.github.com $env:COMPOSER_TOKEN
Add-Env COMPOSER_AUTH ('{"github-oauth": {"github.com": "' + $env:COMPOSER_TOKEN + '"}}')
}
}

View File

@ -42,10 +42,10 @@ configure_composer() {
echo '{}' | tee "$composer_json" >/dev/null
chmod 644 "$composer_json"
fi
cat "${dist:?}"/../src/configs/composer.env >> "$GITHUB_ENV"
add_env_path "${dist:?}"/../src/configs/composer.env
add_path "$composer_bin"
if [ -n "$COMPOSER_TOKEN" ]; then
composer -q config -g github-oauth.github.com "$COMPOSER_TOKEN"
add_env COMPOSER_AUTH '{"github-oauth": {"github.com": "'"$COMPOSER_TOKEN"'"}}'
fi
}

View File

@ -87,6 +87,31 @@ add_path() {
export PATH="${PATH:+${PATH}:}$path_to_add"
}
# Function to add environment variables using a PATH.
add_env_path() {
env_path=$1
[ -e "$env_path" ] || return
if [[ -n "$GITHUB_ENV" ]]; then
cat "$env_path" >> "$GITHUB_ENV"
else
profile=$(get_shell_profile)
cat "$env_path" >> "$profile"
fi
}
# Function to add an environment variable.
add_env() {
env_name=$1
env_value=$2
if [[ -n "$GITHUB_ENV" ]]; then
echo "$env_name=$env_value" | tee -a "$GITHUB_ENV" >/dev/null 2>&1
else
profile=$(get_shell_profile)
echo "export $env_name=\"$env_value\"" | sudo tee -a "$profile" >/dev/null 2>&1
fi
export "$env_name"="$env_value"
}
# Function to download and run scripts from GitHub releases with jsdeliver fallback.
run_script() {
repo=$1

View File

@ -77,6 +77,34 @@ Function Add-Path {
}
}
# Function to add an environment variable.
Function Add-Env {
param(
[string]$EnvName,
[string]$EnvValue
)
if ($env:GITHUB_ENV) {
Add-Content "$EnvName=$EnvValue" -Path $env:GITHUB_ENV -Encoding utf8
} else {
Set-ItemProperty -Path 'hkcu:\Environment' -Name $EnvName -Value $EnvValue
}
}
# Function to add environment variables using a PATH.
Function Add-EnvPATH {
param(
[string]$EnvPATH
)
if(-not(Test-Path $EnvPATH)) {
return
}
$env_file = $current_profile
if ($env:GITHUB_ENV) {
$env_file = $env:GITHUB_ENV
}
Get-Content -Path $EnvPATH | Add-Content -Path $env_file -Encoding utf8
}
# Function to make sure printf is in PATH.
Function Add-Printf {
if (-not(Test-Path "C:\Program Files\Git\usr\bin\printf.exe")) {