From d189609ea953e0d2a3cb030801678a900f4e4af5 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Sun, 2 Jan 2022 16:43:37 +0530 Subject: [PATCH] Refactor tools functions to add_tools scripts --- src/scripts/common.sh | 120 --------------------- src/scripts/darwin.sh | 2 +- src/scripts/linux.sh | 2 +- src/scripts/tools/add_tools.ps1 | 186 +++++++++++++++++++++++++++++++- src/scripts/tools/add_tools.sh | 151 ++++++++++++++++++++++++-- src/scripts/win32.ps1 | 166 ---------------------------- 6 files changed, 328 insertions(+), 299 deletions(-) diff --git a/src/scripts/common.sh b/src/scripts/common.sh index 6aaf9af4..e8cf91be 100644 --- a/src/scripts/common.sh +++ b/src/scripts/common.sh @@ -6,11 +6,6 @@ export old_versions="5.[3-5]" export jit_versions="8.[0-9]" export nightly_versions="8.[2-9]" export xdebug3_versions="7.[2-4]|8.[0-9]" -export tool_path_dir="/usr/local/bin" -export composer_home="$HOME/.composer" -export composer_bin="$composer_home/vendor/bin" -export composer_json="$composer_home/composer.json" -export composer_lock="$composer_home/composer.lock" export latest="releases/latest/download" export github="https://github.com/shivammathur" export jsdeliver="https://cdn.jsdelivr.net/gh/shivammathur" @@ -254,121 +249,6 @@ add_unstable_extension() { add_pecl_extension "$extension" "$pecl_version" "$prefix" } -# Function to extract tool version. -get_tool_version() { - tool=$1 - param=$2 - alp="[a-zA-Z0-9]" - version_regex="[0-9]+((\.{1}$alp+)+)(\.{0})(-$alp+){0,1}" - if [ "$tool" = "composer" ]; then - composer_alias_version="$(grep -Ea "const\sBRANCH_ALIAS_VERSION" "$tool_path_dir/composer" | grep -Eo "$version_regex")" - if [[ -n "$composer_alias_version" ]]; then - composer_version="$composer_alias_version+$(grep -Ea "const\sVERSION" "$tool_path_dir/composer" | grep -Eo "$alp+" | tail -n 1)" - else - composer_version="$(grep -Ea "const\sVERSION" "$tool_path_dir/composer" | grep -Eo "$version_regex")" - fi - echo "$composer_version" | sudo tee /tmp/composer_version - else - $tool "$param" 2>/dev/null | sed -Ee "s/[Cc]omposer(.)?$version_regex//g" | grep -Eo "$version_regex" | head -n 1 - fi -} - -# Function to configure composer -configure_composer() { - tool_path=$1 - sudo ln -sf "$tool_path" "$tool_path.phar" - php -r "try {\$p=new Phar('$tool_path.phar', 0);exit(0);} catch(Exception \$e) {exit(1);}" - if [ $? -eq 1 ]; then - add_log "$cross" "composer" "Could not download composer" - exit 1 - fi - if ! [ -d "$composer_home" ]; then - sudo -u "$(id -un)" -g "$(id -gn)" mkdir -p -m=00755 "$composer_home" - else - sudo chown -R "$(id -un)":"$(id -gn)" "$composer_home" - fi - if ! [ -e "$composer_json" ]; then - echo '{}' | tee "$composer_json" >/dev/null - chmod 644 "$composer_json" - fi - cat "${dist:?}"/../src/configs/composer.env >> "$GITHUB_ENV" - echo "$composer_bin" >>"$GITHUB_PATH" - if [ -n "$COMPOSER_TOKEN" ]; then - composer -q config -g github-oauth.github.com "$COMPOSER_TOKEN" - fi -} - -# Function to setup a remote tool. -add_tool() { - url=$1 - 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 - if [ -e "$tool_path" ]; then - sudo cp -a "$tool_path" /tmp/"$tool" - fi - IFS="," read -r -a url <<<"$url" - status_code=$(get -v -e "$tool_path" "${url[@]}") - if [ "$status_code" != "200" ] && [[ "${url[0]}" =~ .*github.com.*releases.*latest.* ]]; then - url[0]="${url[0]//releases\/latest\/download/releases/download/$(get -s -n "" "$(echo "${url[0]}" | cut -d '/' -f '1-5')/releases" | grep -Eo -m 1 "([0-9]+\.[0-9]+\.[0-9]+)/$(echo "${url[0]}" | sed -e "s/.*\///")" | cut -d '/' -f 1)}" - status_code=$(get -v -e "$tool_path" "${url[0]}") - fi - if [ "$status_code" = "200" ]; then - add_tools_helper "$tool" - tool_version=$(get_tool_version "$tool" "$ver_param") - add_log "$tick" "$tool" "Added $tool $tool_version" - else - if [ "$tool" = "composer" ]; then - fail_fast=true - elif [ -e /tmp/"$tool" ]; then - sudo cp -a /tmp/"$tool" "$tool_path" - fi - add_log "$cross" "$tool" "Could not setup $tool" - fi -} - -# Function to setup a tool using composer. -add_composertool() { - tool=$1 - release=$2 - prefix=$3 - scope=$4 - if [[ "$tool" =~ prestissimo|composer-prefetcher ]]; then - composer_version=$(cat /tmp/composer_version) - if [ "$(echo "$composer_version" | cut -d'.' -f 1)" != "1" ]; then - echo "::warning:: Skipping $tool, as it does not support Composer $composer_version. Specify composer:v1 in tools to use $tool" - add_log "$cross" "$tool" "Skipped" - 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 - scoped_dir="$composer_bin/_tools/$tool-$(echo -n "$release" | shasum -a 256 | cut -d ' ' -f 1)" - if ! [ -e "$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" - fi - fi - log=$(grep "$prefix$tool" /tmp/composer.log) && - tool_version=$(get_tool_version 'echo' "$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 - sudo cp -p "$tool_path_dir/composer" "$composer_bin" - fi -} - # Function to get PHP version in semver format. php_semver() { php -v | grep -Eo -m 1 "[0-9]+\.[0-9]+\.[0-9]+((-?[a-zA-Z]+([0-9]+)?)?){2}" | head -n 1 diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index 03f51a6c..014d6f90 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -249,9 +249,9 @@ export HOMEBREW_NO_AUTO_UPDATE=1 export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 # shellcheck source=. +. "${scripts:?}"/common.sh . "${scripts:?}"/ext/source.sh . "${scripts:?}"/tools/add_tools.sh -. "${scripts:?}"/common.sh read_env self_hosted_setup setup_php diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index 639abddf..f0c0f5b9 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -230,10 +230,10 @@ scripts="${dist}"/../src/scripts . /etc/os-release # shellcheck source=. +. "${scripts:?}"/common.sh . "${scripts:?}"/ext/source.sh . "${scripts:?}"/tools/ppa.sh . "${scripts:?}"/tools/add_tools.sh -. "${scripts:?}"/common.sh read_env self_hosted_setup setup_php diff --git a/src/scripts/tools/add_tools.ps1 b/src/scripts/tools/add_tools.ps1 index 303ece4d..2b0c72fc 100644 --- a/src/scripts/tools/add_tools.ps1 +++ b/src/scripts/tools/add_tools.ps1 @@ -1,3 +1,58 @@ +# Variables +$composer_bin = "$env:APPDATA\Composer\vendor\bin" +$composer_json = "$env:APPDATA\Composer\composer.json" +$composer_lock = "$env:APPDATA\Composer\composer.lock" + +# Function to configure composer. +Function Edit-ComposerConfig() { + Param( + [Parameter(Position = 0, Mandatory = $true)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] + $tool_path + ) + Copy-Item $tool_path -Destination "$tool_path.phar" + php -r "try {`$p=new Phar('$tool_path.phar', 0);exit(0);} catch(Exception `$e) {exit(1);}" + if ($? -eq $False) { + Add-Log "$cross" "composer" "Could not download composer" + exit 1; + } + New-Item -ItemType Directory -Path $composer_bin -Force > $null 2>&1 + 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 + Write-Output $composer_bin | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 + if (Test-Path env:COMPOSER_TOKEN) { + composer -q config -g github-oauth.github.com $env:COMPOSER_TOKEN + } +} + +# Function to extract tool version. +Function Get-ToolVersion() { + Param ( + [Parameter(Position = 0, Mandatory = $true)] + $tool, + [Parameter(Position = 1, Mandatory = $true)] + $param + ) + $alp = "[a-zA-Z0-9]" + $version_regex = "[0-9]+((\.{1}$alp+)+)(\.{0})(-$alp+){0,1}" + if($tool -eq 'composer') { + $composer_branch_alias = Select-String -Pattern "const\sBRANCH_ALIAS_VERSION" -Path $bin_dir\composer -Raw | Select-String -Pattern $version_regex | ForEach-Object { $_.matches.Value } + if ($composer_branch_alias) { + $composer_version = $composer_branch_alias + '+' + (Select-String -Pattern "const\sVERSION" -Path $bin_dir\composer -Raw | Select-String -Pattern "[a-zA-Z0-9]+" -AllMatches | ForEach-Object { $_.matches[2].Value }) + } else { + $composer_version = Select-String -Pattern "const\sVERSION" -Path $bin_dir\composer -Raw | Select-String -Pattern $version_regex | ForEach-Object { $_.matches.Value } + } + Set-Variable -Name 'composer_version' -Value $composer_version -Scope Global + return "$composer_version" + } + return . $tool $param 2> $null | ForEach-Object { $_ -replace "composer $version_regex", '' } | Select-String -Pattern $version_regex | Select-Object -First 1 | ForEach-Object { $_.matches.Value } +} + +# Helper function to configure tools. Function Add-ToolsHelper() { Param ( [Parameter(Position = 0, Mandatory = $true)] @@ -26,4 +81,133 @@ Function Add-ToolsHelper() { } elseif($tool -eq "wp-cli") { Copy-Item $bin_dir\wp-cli.bat -Destination $bin_dir\wp.bat } -} \ No newline at end of file +} + +# Function to add tools. +Function Add-Tool() { + Param ( + [Parameter(Position = 0, Mandatory = $true)] + [ValidateNotNull()] + $url, + [Parameter(Position = 1, Mandatory = $true)] + [ValidateNotNull()] + $tool, + [Parameter(Position = 2, Mandatory = $true)] + [ValidateNotNull()] + $ver_param + ) + if (Test-Path $bin_dir\$tool) { + Copy-Item $bin_dir\$tool -Destination $bin_dir\$tool.old -Force + } + if($url.Count -gt 1) { + $url = $url[0] + } + $tool_path = "$bin_dir\$tool" + if (($url | Split-Path -Extension) -eq ".exe") { + $tool_path = "$tool_path.exe" + } + try { + Invoke-WebRequest -Uri $url -OutFile $tool_path + } catch { + if($url -match '.*github.com.*releases.*latest.*') { + try { + $url = $url.replace("releases/latest/download", "releases/download/" + ([regex]::match((Invoke-WebRequest -Uri ($url.split('/release')[0] + "/releases")).Content, "([0-9]+\.[0-9]+\.[0-9]+)/" + ($url.Substring($url.LastIndexOf("/") + 1))).Groups[0].Value).split('/')[0]) + Invoke-WebRequest -Uri $url -OutFile $tool_path + } catch { } + } + } + if (((Get-ChildItem -Path $bin_dir/* | Where-Object Name -Match "^$tool(.exe|.phar)*$").Count -gt 0)) { + $bat_content = @() + $bat_content += "@ECHO off" + $bat_content += "setlocal DISABLEDELAYEDEXPANSION" + $bat_content += "SET BIN_TARGET=%~dp0/" + $tool + $bat_content += "php %BIN_TARGET% %*" + Set-Content -Path $bin_dir\$tool.bat -Value $bat_content + Add-ToolsHelper $tool + Add-ToProfile $current_profile $tool "New-Alias $tool $bin_dir\$tool.bat" >$null 2>&1 + $tool_version = Get-ToolVersion $tool $ver_param + Add-Log $tick $tool "Added $tool $tool_version" + } else { + if($tool -eq "composer") { + $env:fail_fast = 'true' + } elseif (Test-Path $bin_dir\$tool.old) { + Copy-Item $bin_dir\$tool.old -Destination $bin_dir\$tool -Force + } + Add-Log $cross $tool "Could not add $tool" + } +} + +Function Add-ScopedComposertool() { + Param ( + [Parameter(Position = 0, Mandatory = $true)] + [string] + $tool, + [Parameter(Position = 1, Mandatory = $true)] + [string] + $release, + [Parameter(Position = 2, Mandatory = $true)] + [string] + $prefix + ) + $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 + } + return ((Test-Path $scoped_tool_dir\composer.json) -and (findstr $prefix$tool $scoped_tool_dir\composer.json)) +} + +# Function to setup a tool using composer. +Function Add-Composertool() { + Param ( + [Parameter(Position = 0, Mandatory = $true)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] + $tool, + [Parameter(Position = 1, Mandatory = $true)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] + $release, + [Parameter(Position = 2, Mandatory = $true)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] + $prefix, + [Parameter(Position = 3, Mandatory = $true)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] + $scope + ) + if($tool -match "prestissimo|composer-prefetcher" -and $composer_version.split('.')[0] -ne "1") { + Write-Output "::warning:: Skipping $tool, as it does not support Composer $composer_version. Specify composer:v1 in tools to use $tool" + 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 + if(Test-Path $composer_bin\composer) { + Copy-Item -Path "$bin_dir\composer" -Destination "$composer_bin\composer" -Force + } + Add-ToolsHelper $tool + if($json) { + $tool_version = Get-ToolVersion "Write-Output" "$log" + Add-Log $tick $tool "Added $tool $tool_version" + } else { + Add-Log $cross $tool "Could not setup $tool" + } +} diff --git a/src/scripts/tools/add_tools.sh b/src/scripts/tools/add_tools.sh index 749c34ca..3c2ec815 100644 --- a/src/scripts/tools/add_tools.sh +++ b/src/scripts/tools/add_tools.sh @@ -1,13 +1,65 @@ +# Variables +export tool_path_dir="/usr/local/bin" +export composer_home="$HOME/.composer" +export composer_bin="$composer_home/vendor/bin" +export composer_json="$composer_home/composer.json" +export composer_lock="$composer_home/composer.lock" + +# Function to extract tool version. +get_tool_version() { + tool=$1 + param=$2 + alp="[a-zA-Z0-9]" + version_regex="[0-9]+((\.{1}$alp+)+)(\.{0})(-$alp+){0,1}" + if [ "$tool" = "composer" ]; then + composer_alias_version="$(grep -Ea "const\sBRANCH_ALIAS_VERSION" "$tool_path_dir/composer" | grep -Eo "$version_regex")" + if [[ -n "$composer_alias_version" ]]; then + composer_version="$composer_alias_version+$(grep -Ea "const\sVERSION" "$tool_path_dir/composer" | grep -Eo "$alp+" | tail -n 1)" + else + composer_version="$(grep -Ea "const\sVERSION" "$tool_path_dir/composer" | grep -Eo "$version_regex")" + fi + echo "$composer_version" | sudo tee /tmp/composer_version + else + $tool "$param" 2>/dev/null | sed -Ee "s/[Cc]omposer(.)?$version_regex//g" | grep -Eo "$version_regex" | head -n 1 + fi +} + +# Function to configure composer +configure_composer() { + tool_path=$1 + sudo ln -sf "$tool_path" "$tool_path.phar" + php -r "try {\$p=new Phar('$tool_path.phar', 0);exit(0);} catch(Exception \$e) {exit(1);}" + if [ $? -eq 1 ]; then + add_log "${cross:?}" "composer" "Could not download composer" + exit 1 + fi + if ! [ -d "$composer_home" ]; then + sudo -u "$(id -un)" -g "$(id -gn)" mkdir -p -m=00755 "$composer_home" + else + sudo chown -R "$(id -un)":"$(id -gn)" "$composer_home" + fi + if ! [ -e "$composer_json" ]; then + echo '{}' | tee "$composer_json" >/dev/null + chmod 644 "$composer_json" + fi + cat "${dist:?}"/../src/configs/composer.env >> "$GITHUB_ENV" + echo "$composer_bin" >>"$GITHUB_PATH" + if [ -n "$COMPOSER_TOKEN" ]; then + composer -q config -g github-oauth.github.com "$COMPOSER_TOKEN" + fi +} + +# Helper function to configure tools. 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 "$codeception_bin"/codecept "$codeception_bin"/codeception elif [ "$tool" = "composer" ]; then - configure_composer "${tool_path:?}" + configure_composer "$tool_path" elif [ "$tool" = "cs2pr" ]; then - sudo sed -i 's/\r$//; s/exit(9)/exit(0)/' "${tool_path:?}" 2>/dev/null || - sudo sed -i '' 's/\r$//; s/exit(9)/exit(0)/' "${tool_path:?}" + sudo sed -i 's/\r$//; s/exit(9)/exit(0)/' "$tool_path" 2>/dev/null || + sudo sed -i '' 's/\r$//; s/exit(9)/exit(0)/' "$tool_path" elif [ "$tool" = "phan" ]; then add_extension fileinfo extension >/dev/null 2>&1 add_extension ast extension >/dev/null 2>&1 @@ -17,13 +69,92 @@ add_tools_helper() { add_extension xml extension >/dev/null 2>&1 elif [ "$tool" = "phpDocumentor" ]; then add_extension fileinfo extension >/dev/null 2>&1 - sudo ln -s "${tool_path:?}" "${tool_path_dir:?}"/phpdocumentor 2>/dev/null || true - sudo ln -s "${tool_path:?}" "${tool_path_dir:?}"/phpdoc + sudo ln -s "$tool_path" "$tool_path_dir"/phpdocumentor 2>/dev/null || true + sudo ln -s "$tool_path" "$tool_path_dir"/phpdoc elif [[ "$tool" =~ phpunit(-polyfills)?$ ]]; then - if [ -e "${tool_path_dir:?}"/phpunit ]; then - sudo cp "${tool_path_dir:?}"/phpunit "${composer_bin:?}" + if [ -e "$tool_path_dir"/phpunit ]; then + sudo cp "$tool_path_dir"/phpunit "$composer_bin" fi elif [[ "$tool" =~ (symfony|vapor|wp)-cli ]]; then - sudo ln -s "${tool_path:?}" "${tool_path_dir:?}"/"${tool%-*}" + sudo ln -s "$tool_path" "$tool_path_dir"/"${tool%-*}" fi -} \ No newline at end of file +} + +# Function to setup a remote tool. +add_tool() { + url=$1 + 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 + if [ -e "$tool_path" ]; then + sudo cp -a "$tool_path" /tmp/"$tool" + fi + IFS="," read -r -a url <<<"$url" + status_code=$(get -v -e "$tool_path" "${url[@]}") + if [ "$status_code" != "200" ] && [[ "${url[0]}" =~ .*github.com.*releases.*latest.* ]]; then + url[0]="${url[0]//releases\/latest\/download/releases/download/$(get -s -n "" "$(echo "${url[0]}" | cut -d '/' -f '1-5')/releases" | grep -Eo -m 1 "([0-9]+\.[0-9]+\.[0-9]+)/$(echo "${url[0]}" | sed -e "s/.*\///")" | cut -d '/' -f 1)}" + status_code=$(get -v -e "$tool_path" "${url[0]}") + fi + if [ "$status_code" = "200" ]; then + add_tools_helper "$tool" + tool_version=$(get_tool_version "$tool" "$ver_param") + add_log "${tick:?}" "$tool" "Added $tool $tool_version" + else + if [ "$tool" = "composer" ]; then + export fail_fast=true + elif [ -e /tmp/"$tool" ]; then + sudo cp -a /tmp/"$tool" "$tool_path" + fi + add_log "$cross" "$tool" "Could not setup $tool" + fi +} + +# Function to setup a tool using composer in a different scope. +add_scoped_composertool() { + tool=$1 + release=$2 + prefix=$3 + scoped_dir="$composer_bin/_tools/$tool-$(echo -n "$release" | shasum -a 256 | cut -d ' ' -f 1)" + if ! [ -e "$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" + fi +} + +# Function to setup a tool using composer. +add_composertool() { + tool=$1 + release=$2 + prefix=$3 + scope=$4 + if [[ "$tool" =~ prestissimo|composer-prefetcher ]]; then + composer_version=$(cat /tmp/composer_version) + if [ "$(echo "$composer_version" | cut -d'.' -f 1)" != "1" ]; then + echo "::warning:: Skipping $tool, as it does not support Composer $composer_version. Specify composer:v1 in tools to use $tool" + add_log "$cross" "$tool" "Skipped" + 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_log "$cross" "$tool" "Could not setup $tool" + add_tools_helper "$tool" + if [ -e "$composer_bin/composer" ]; then + sudo cp -p "$tool_path_dir/composer" "$composer_bin" + fi +} diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index 9e471dee..63410393 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -309,168 +309,6 @@ Function Disable-AllShared() { Add-Log $tick "none" "Disabled all shared extensions" } -# Function to configure composer. -Function Edit-ComposerConfig() { - Param( - [Parameter(Position = 0, Mandatory = $true)] - [ValidateNotNull()] - [ValidateLength(1, [int]::MaxValue)] - [string] - $tool_path - ) - Copy-Item $tool_path -Destination "$tool_path.phar" - php -r "try {`$p=new Phar('$tool_path.phar', 0);exit(0);} catch(Exception `$e) {exit(1);}" - if ($? -eq $False) { - Add-Log "$cross" "composer" "Could not download composer" - exit 1; - } - 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 - Write-Output $composer_bin | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 - if (Test-Path env:COMPOSER_TOKEN) { - composer -q config -g github-oauth.github.com $env:COMPOSER_TOKEN - } -} - -# Function to extract tool version. -Function Get-ToolVersion() { - Param ( - [Parameter(Position = 0, Mandatory = $true)] - $tool, - [Parameter(Position = 1, Mandatory = $true)] - $param - ) - $alp = "[a-zA-Z0-9]" - $version_regex = "[0-9]+((\.{1}$alp+)+)(\.{0})(-$alp+){0,1}" - if($tool -eq 'composer') { - $composer_branch_alias = Select-String -Pattern "const\sBRANCH_ALIAS_VERSION" -Path $bin_dir\composer -Raw | Select-String -Pattern $version_regex | ForEach-Object { $_.matches.Value } - if ($composer_branch_alias) { - $composer_version = $composer_branch_alias + '+' + (Select-String -Pattern "const\sVERSION" -Path $bin_dir\composer -Raw | Select-String -Pattern "[a-zA-Z0-9]+" -AllMatches | ForEach-Object { $_.matches[2].Value }) - } else { - $composer_version = Select-String -Pattern "const\sVERSION" -Path $bin_dir\composer -Raw | Select-String -Pattern $version_regex | ForEach-Object { $_.matches.Value } - } - Set-Variable -Name 'composer_version' -Value $composer_version -Scope Global - return "$composer_version" - } - return . $tool $param 2> $null | ForEach-Object { $_ -replace "composer $version_regex", '' } | Select-String -Pattern $version_regex | Select-Object -First 1 | ForEach-Object { $_.matches.Value } -} - -# Function to add tools. -Function Add-Tool() { - Param ( - [Parameter(Position = 0, Mandatory = $true)] - [ValidateNotNull()] - $url, - [Parameter(Position = 1, Mandatory = $true)] - [ValidateNotNull()] - $tool, - [Parameter(Position = 2, Mandatory = $true)] - [ValidateNotNull()] - $ver_param - ) - if (Test-Path $bin_dir\$tool) { - Copy-Item $bin_dir\$tool -Destination $bin_dir\$tool.old -Force - } - if($url.Count -gt 1) { - $url = $url[0] - } - $tool_path = "$bin_dir\$tool" - if (($url | Split-Path -Extension) -eq ".exe") { - $tool_path = "$tool_path.exe" - } - try { - Invoke-WebRequest -Uri $url -OutFile $tool_path - } catch { - if($url -match '.*github.com.*releases.*latest.*') { - try { - $url = $url.replace("releases/latest/download", "releases/download/" + ([regex]::match((Invoke-WebRequest -Uri ($url.split('/release')[0] + "/releases")).Content, "([0-9]+\.[0-9]+\.[0-9]+)/" + ($url.Substring($url.LastIndexOf("/") + 1))).Groups[0].Value).split('/')[0]) - Invoke-WebRequest -Uri $url -OutFile $tool_path - } catch { } - } - } - if (((Get-ChildItem -Path $bin_dir/* | Where-Object Name -Match "^$tool(.exe|.phar)*$").Count -gt 0)) { - $bat_content = @() - $bat_content += "@ECHO off" - $bat_content += "setlocal DISABLEDELAYEDEXPANSION" - $bat_content += "SET BIN_TARGET=%~dp0/" + $tool - $bat_content += "php %BIN_TARGET% %*" - Set-Content -Path $bin_dir\$tool.bat -Value $bat_content - Add-ToolsHelper $tool - Add-ToProfile $current_profile $tool "New-Alias $tool $bin_dir\$tool.bat" >$null 2>&1 - $tool_version = Get-ToolVersion $tool $ver_param - Add-Log $tick $tool "Added $tool $tool_version" - } else { - if($tool -eq "composer") { - $env:fail_fast = 'true' - } elseif (Test-Path $bin_dir\$tool.old) { - Copy-Item $bin_dir\$tool.old -Destination $bin_dir\$tool -Force - } - Add-Log $cross $tool "Could not add $tool" - } -} - -# Function to setup a tool using composer. -Function Add-Composertool() { - Param ( - [Parameter(Position = 0, Mandatory = $true)] - [ValidateNotNull()] - [ValidateLength(1, [int]::MaxValue)] - [string] - $tool, - [Parameter(Position = 1, Mandatory = $true)] - [ValidateNotNull()] - [ValidateLength(1, [int]::MaxValue)] - [string] - $release, - [Parameter(Position = 2, Mandatory = $true)] - [ValidateNotNull()] - [ValidateLength(1, [int]::MaxValue)] - [string] - $prefix, - [Parameter(Position = 3, Mandatory = $true)] - [ValidateNotNull()] - [ValidateLength(1, [int]::MaxValue)] - [string] - $scope - ) - if($tool -match "prestissimo|composer-prefetcher" -and $composer_version.split('.')[0] -ne "1") { - Write-Output "::warning:: Skipping $tool, as it does not support Composer $composer_version. Specify composer:v1 in tools to use $tool" - 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 { - $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 - } - $json = (Test-Path $scoped_tool_dir\composer.json) -and (findstr $prefix$tool $scoped_tool_dir\composer.json) - } - $log = findstr $prefix$tool $env:APPDATA\Composer\composer.log - if(Test-Path $composer_bin\composer) { - Copy-Item -Path "$bin_dir\composer" -Destination "$composer_bin\composer" -Force - } - Add-ToolsHelper $tool - if($json) { - $tool_version = Get-ToolVersion "Write-Output" "$log" - Add-Log $tick $tool "Added $tool $tool_version" - } else { - Add-Log $cross $tool "Could not setup $tool" - } -} - # Function to handle request to add PECL. Function Add-Pecl() { Add-Log $tick "PECL" "Use extensions input to setup PECL extensions on windows" @@ -484,9 +322,6 @@ $ext_dir = "$php_dir\ext" $bin_dir = $php_dir $github = 'https://github.com' $php_builder = "$github/shivammathur/php-builder-windows" -$composer_bin = "$env:APPDATA\Composer\vendor\bin" -$composer_json = "$env:APPDATA\Composer\composer.json" -$composer_lock = "$env:APPDATA\Composer\composer.lock" $current_profile = "$env:TEMP\setup-php.ps1" $ProgressPreference = 'SilentlyContinue' $jit_versions = '8.[0-9]' @@ -591,6 +426,5 @@ if($version -lt "5.5") { Enable-PhpExtension -Extension $enable_extensions -Path $php_dir Add-PhpCAInfo Copy-Item -Path $dist\..\src\configs\pm\*.json -Destination $env:RUNNER_TOOL_CACHE -New-Item -ItemType Directory -Path $composer_bin -Force > $null 2>&1 Write-Output "::set-output name=php-version::$($installed.FullVersion)" Add-Log $tick "PHP" "$status PHP $($installed.FullVersion)$extra_version"