From 2b729b11304b697ad9cbc19d7419ce91eac8678a Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Tue, 18 Jan 2022 04:41:51 +0530 Subject: [PATCH] Refactor setting environment variables --- src/scripts/tools/add_tools.ps1 | 4 ++-- src/scripts/tools/add_tools.sh | 4 ++-- src/scripts/unix.sh | 25 +++++++++++++++++++++++++ src/scripts/win32.ps1 | 28 ++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/scripts/tools/add_tools.ps1 b/src/scripts/tools/add_tools.ps1 index 81dce152..f5c3476b 100644 --- a/src/scripts/tools/add_tools.ps1 +++ b/src/scripts/tools/add_tools.ps1 @@ -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 + '"}}') } } diff --git a/src/scripts/tools/add_tools.sh b/src/scripts/tools/add_tools.sh index 1ff9a413..322b8ff0 100644 --- a/src/scripts/tools/add_tools.sh +++ b/src/scripts/tools/add_tools.sh @@ -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 } diff --git a/src/scripts/unix.sh b/src/scripts/unix.sh index 88306a6b..bdf596f7 100644 --- a/src/scripts/unix.sh +++ b/src/scripts/unix.sh @@ -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 diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index da53fa6a..8dcd10a8 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -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")) {