Improve tools setup

Log full semver of tools installed with composer

Refactor adding paths to PATH
This commit is contained in:
Shivam Mathur 2022-01-11 06:04:19 +05:30
parent b7cdb2373c
commit f3010adf3e
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
5 changed files with 90 additions and 55 deletions

View File

@ -23,7 +23,7 @@ Function Edit-ComposerConfig() {
Set-Content -Path $composer_json -Value "{}"
}
Get-Content -Path $dist\..\src\configs\composer.env | Add-Content -Path $env:GITHUB_ENV
Write-Output $composer_bin | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8
Add-Path $composer_bin
if (Test-Path env:COMPOSER_TOKEN) {
composer -q config -g github-oauth.github.com $env:COMPOSER_TOKEN
}
@ -60,7 +60,7 @@ Function Add-ToolsHelper() {
$tool
)
if($tool -eq "codeception") {
Copy-Item $codeception_bin\codecept.bat -Destination $codeception_bin\codeception.bat
Copy-Item $env:codeception_bin\codecept.bat -Destination $env:codeception_bin\codeception.bat
} elseif($tool -eq "composer") {
Edit-ComposerConfig $bin_dir\$tool
} elseif($tool -eq "cs2pr") {
@ -77,7 +77,7 @@ Function Add-ToolsHelper() {
Add-ToProfile $current_profile "symfony" "New-Alias symfony $bin_dir\symfony-cli.exe"
Add-ToProfile $current_profile "symfony_cli" "New-Alias symfony-cli $bin_dir\symfony-cli.exe"
} elseif($tool -match "vapor-cli") {
Copy-Item $vapor_cli_bin\vapor.bat -Destination $vapor_cli_bin\vapor-cli.bat
Copy-Item $env:vapor_cli_bin\vapor.bat -Destination $env:vapor_cli_bin\vapor-cli.bat
} elseif($tool -eq "wp-cli") {
Copy-Item $bin_dir\wp-cli.bat -Destination $bin_dir\wp.bat
}
@ -137,7 +137,7 @@ Function Add-Tool() {
}
}
Function Add-ScopedComposertool() {
Function Add-ComposertoolHelper() {
Param (
[Parameter(Position = 0, Mandatory = $true)]
[string]
@ -147,18 +147,30 @@ Function Add-ScopedComposertool() {
$release,
[Parameter(Position = 2, Mandatory = $true)]
[string]
$prefix
$prefix,
[Parameter(Position = 3, Mandatory = $true)]
[string]
$scope
)
$release_stream = [System.IO.MemoryStream]::New([System.Text.Encoding]::ASCII.GetBytes($release))
$scoped_tool_dir_suffix = (Get-FileHash -InputStream $release_stream -Algorithm sha256).Hash
$scoped_tool_dir = "$composer_bin\_tools\$tool-$scoped_tool_dir_suffix"
if(-not(Test-Path $scoped_tool_dir)) {
New-Item -ItemType Directory -Force -Path $scoped_tool_dir > $null 2>&1
(composer global require $prefix$release -d $scoped_tool_dir.replace('\', '/') 2>&1 | Tee-Object -FilePath $env:APPDATA\Composer\composer.log) >$null 2>&1
Add-Content $scoped_tool_dir\vendor\bin -Path $env:GITHUB_PATH -Encoding utf8
New-Variable -Name ($tool.replace('-', '_') + '_bin') -Value $scoped_tool_dir\vendor\bin
if($scope -eq 'global') {
if(Test-Path $composer_lock) {
Remove-Item -Path $composer_lock -Force
}
composer global require $prefix$release >$null 2>&1
return composer global show $prefix$tool 2>&1 | findstr '^versions'
} else {
$release_stream = [System.IO.MemoryStream]::New([System.Text.Encoding]::ASCII.GetBytes($release))
$scoped_dir_suffix = (Get-FileHash -InputStream $release_stream -Algorithm sha256).Hash
$scoped_dir = "$composer_bin\_tools\$tool-$scoped_dir_suffix"
$unix_scoped_dir = $scoped_dir.replace('\', '/')
if(-not(Test-Path $scoped_dir)) {
New-Item -ItemType Directory -Force -Path $scoped_dir > $null 2>&1
composer require $prefix$release -d $unix_scoped_dir >$null 2>&1
}
[System.Environment]::SetEnvironmentVariable(($tool.replace('-', '_') + '_bin'), "$scoped_dir\vendor\bin")
Add-Path $scoped_dir\vendor\bin
return composer show $prefix$tool -d $unix_scoped_dir 2>&1 | findstr '^versions'
}
return ((Test-Path $scoped_tool_dir\composer.json) -and (findstr $prefix$tool $scoped_tool_dir\composer.json))
}
# Function to setup a tool using composer.
@ -190,21 +202,12 @@ Function Add-Composertool() {
Add-Log $cross $tool "Skipped"
Return
}
if($scope -eq 'global') {
if(Test-Path $composer_lock) {
Remove-Item -Path $composer_lock -Force
}
(composer global require $prefix$release 2>&1 | Tee-Object -FilePath $env:APPDATA\Composer\composer.log) >$null 2>&1
$json = findstr $prefix$tool $env:APPDATA\Composer\composer.json
} else {
$json = Add-ScopedComposertool -tool $tool -release $release -prefix $prefix
}
$log = findstr $prefix$tool $env:APPDATA\Composer\composer.log
$log = Add-ComposertoolHelper -tool $tool -release $release -prefix $prefix -scope $scope
if(Test-Path $composer_bin\composer) {
Copy-Item -Path "$bin_dir\composer" -Destination "$composer_bin\composer" -Force
}
Add-ToolsHelper $tool
if($json) {
if($log) {
$tool_version = Get-ToolVersion "Write-Output" "$log"
Add-Log $tick $tool "Added $tool $tool_version"
} else {

View File

@ -43,7 +43,7 @@ configure_composer() {
chmod 644 "$composer_json"
fi
cat "${dist:?}"/../src/configs/composer.env >> "$GITHUB_ENV"
echo "$composer_bin" >>"$GITHUB_PATH"
add_path "$composer_bin"
if [ -n "$COMPOSER_TOKEN" ]; then
composer -q config -g github-oauth.github.com "$COMPOSER_TOKEN"
fi
@ -53,8 +53,7 @@ configure_composer() {
add_tools_helper() {
tool=$1
if [ "$tool" = "codeception" ]; then
codeception_bin=$(grep codeception_bin "${GITHUB_ENV:?}" | cut -d '=' -f 2)
sudo ln -s "$codeception_bin"/codecept "$codeception_bin"/codeception
sudo ln -s "$scoped_dir"/vendor/bin/codecept "$scoped_dir"/vendor/bin/codeception
elif [ "$tool" = "composer" ]; then
configure_composer "$tool_path"
elif [ "$tool" = "cs2pr" ]; then
@ -75,6 +74,8 @@ add_tools_helper() {
if [ -e "$tool_path_dir"/phpunit ]; then
sudo cp "$tool_path_dir"/phpunit "$composer_bin"
fi
elif [[ "$tool" =~ vapor-cli ]]; then
sudo ln -s "$scoped_dir"/vendor/bin/vapor "$scoped_dir"/vendor/bin/vapor-cli
elif [[ "$tool" =~ (symfony|vapor|wp)-cli ]]; then
sudo ln -s "$tool_path" "$tool_path_dir"/"${tool%-*}"
fi
@ -86,10 +87,7 @@ add_tool() {
tool=$2
ver_param=$3
tool_path="$tool_path_dir/$tool"
if ! [[ "$PATH" =~ $tool_path_dir ]]; then
export PATH=$PATH:"$tool_path_dir"
echo "export PATH=\$PATH:$tool_path_dir" | sudo tee -a "$GITHUB_ENV" >/dev/null
fi
add_path "$tool_path_dir"
if [ -e "$tool_path" ]; then
sudo cp -aL "$tool_path" /tmp/"$tool"
fi
@ -114,17 +112,23 @@ add_tool() {
}
# Function to setup a tool using composer in a different scope.
add_scoped_composertool() {
add_composertool_helper() {
tool=$1
release=$2
prefix=$3
scope=$4
if [ "$scope" = "global" ]; then
sudo rm -f "$composer_lock" >/dev/null 2>&1 || true
composer global require "$prefix$release" >/dev/null 2>&1
composer global show "$prefix$tool" 2>&1 | grep -E ^versions | sudo tee /tmp/composer.log >/dev/null 2>&1
else
scoped_dir="$composer_bin/_tools/$tool-$(echo -n "$release" | shasum -a 256 | cut -d ' ' -f 1)"
if ! [ -e "$scoped_dir" ]; then
if ! [ -d "$scoped_dir" ]; then
mkdir -p "$scoped_dir"
composer require "$prefix$release" -d "$scoped_dir" 2>&1 | tee /tmp/composer.log >/dev/null 2>&1
export PATH=$PATH:"$scoped_dir"/vendor/bin
echo "${tool}"_bin="$scoped_dir"/vendor/bin | sudo tee -a "$GITHUB_ENV" >/dev/null
echo "$scoped_dir"/vendor/bin >>"$GITHUB_PATH"
composer require "$prefix$release" -d "$scoped_dir" >/dev/null 2>&1
composer show "$prefix$tool" -d "$scoped_dir" 2>&1 | grep -E ^versions | sudo tee /tmp/composer.log >/dev/null 2>&1
fi
add_path "$scoped_dir"/vendor/bin
fi
}
@ -142,16 +146,9 @@ add_composertool() {
return
fi
fi
(
if [ "$scope" = "global" ]; then
sudo rm -f "$composer_lock" >/dev/null 2>&1 || true
composer global require "$prefix$release" 2>&1 | tee /tmp/composer.log >/dev/null 2>&1
else
add_scoped_composertool "$tool" "$release" "$prefix"
fi
log=$(grep "$prefix$tool" /tmp/composer.log) &&
tool_version=$(get_tool_version 'echo' "$log") &&
add_log "$tick" "$tool" "Added $tool $tool_version"
add_composertool_helper "$tool" "$release" "$prefix" "$scope"
tool_version=$(get_tool_version cat /tmp/composer.log)
([ -s /tmp/composer.log ] && add_log "$tick" "$tool" "Added $tool $tool_version"
) || add_log "$cross" "$tool" "Could not setup $tool"
add_tools_helper "$tool"
if [ -e "$composer_bin/composer" ]; then

View File

@ -11,7 +11,7 @@ Function Add-Grpc_php_plugin() {
$msys_location = Add-Msys2
$logs = . $msys_location\usr\bin\bash -l -c "pacman -S --noconfirm mingw-w64-x86_64-grpc" >$null 2>&1
$grpc_version = Get-ToolVersion 'Write-Output' "$logs"
Write-Output "$msys_location\mingw64\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8
Add-Path $msys_location\mingw64\bin
Write-Output "::set-output name=grpc_php_plugin_path::$msys_location\mingw64\bin\grpc_php_plugin.exe"
Add-ToProfile $current_profile 'grpc_php_plugin' "New-Alias grpc_php_plugin $msys_location\mingw64\bin\grpc_php_plugin.exe"
Add-Log $tick "grpc_php_plugin" "Added grpc_php_plugin $grpc_version"

View File

@ -60,6 +60,34 @@ get() {
fi
}
# Function to get shell profile.
get_shell_profile() {
case "$SHELL" in
*bash*)
echo "${HOME}/.bashrc"
;;
*zsh*)
echo "${HOME}/.zshrc"
;;
*)
echo "${HOME}/.profile"
;;
esac
}
# Function to add a path to the PATH variable.
add_path() {
path_to_add=$1
[ ! -d "$path_to_add" ] && [[ ":$PATH:" == *":$path_to_add:"* ]] && return
if [[ -n "$GITHUB_PATH" ]]; then
echo "$path_to_add" | tee -a "$GITHUB_PATH" >/dev/null 2>&1
else
profile=$(get_shell_profile)
([ -e "$profile" ] && grep -q ":$path_to_add\"" "$profile" 2>/dev/null) || echo "export PATH=\"\${PATH:+\${PATH}:}\"$path_to_add" | sudo tee -a "$profile" >/dev/null 2>&1
fi
export PATH="${PATH:+${PATH}:}$path_to_add"
}
# Function to download and run scripts from GitHub releases with jsdeliver fallback.
run_script() {
repo=$1

View File

@ -64,10 +64,17 @@ Function Add-Path {
param(
[string]$PathItem
)
if(-not(Test-Path $PathItem) -or "$env:PATH;".contains("$PathItem;")) {
return
}
if ($env:GITHUB_PATH) {
Add-Content $PathItem -Path $env:GITHUB_PATH -Encoding utf8
} else {
$newPath = (Get-ItemProperty -Path 'hkcu:\Environment' -Name PATH).Path.replace("$PathItem;", '')
$newPath = $PathItem + ';' + $newPath
Set-ItemProperty -Path 'hkcu:\Environment' -Name Path -Value $newPath
Get-PathFromRegistry
}
}
# Function to make sure printf is in PATH.