From 829cb4ef40a23fccd9c856f570449a28d24b42ba Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Mon, 2 Nov 2020 16:33:14 +0530 Subject: [PATCH] Fetch release with tool when latest one does not --- src/scripts/darwin.sh | 4 ++++ src/scripts/linux.sh | 4 ++++ src/scripts/win32.ps1 | 47 +++++++++++++++++++++++++------------------ 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index 36c42773..80d67130 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -192,6 +192,10 @@ add_tool() { status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "${urls[1]}") else status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url") + if [ "$status_code" != "200" ] && [[ "$url" =~ .*github.com.*releases.*latest.* ]]; then + url=$(echo $url | sed -e "s|releases/latest/download|releases/download/$(curl "${curl_opts[@]}" "$(echo "$url" | cut -d '/' -f '1-5')/releases" | grep -Eo -m 1 "([0-9]+\.[0-9]+\.[0-9]+)/$(echo "$url" | sed -e "s/.*\///")" | cut -d '/' -f 1)|") + status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url") + fi fi if [ "$status_code" = "200" ]; then sudo chmod a+x "$tool_path" diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index 75e83bab..071dce37 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -295,6 +295,10 @@ add_tool() { status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "${urls[1]}") else status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url") + if [ "$status_code" != "200" ] && [[ "$url" =~ .*github.com.*releases.*latest.* ]]; then + url=$(echo $url | sed -e "s|releases/latest/download|releases/download/$(curl "${curl_opts[@]}" "$(echo "$url" | cut -d '/' -f '1-5')/releases" | grep -Eo -m 1 "([0-9]+\.[0-9]+\.[0-9]+)/$(echo "$url" | sed -e "s/.*\///")" | cut -d '/' -f 1)|") + status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url") + fi fi if [ "$status_code" = "200" ]; then sudo chmod a+x "$tool_path" diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index 2ccf30dc..b730e5cc 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -255,28 +255,35 @@ Function Add-Tool() { } else { try { Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $bin_dir\$tool - $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-ToProfile $current_profile $tool "New-Alias $tool $bin_dir\$tool.bat" >$null 2>&1 - } catch { } - } - if($tool -eq "phan") { - Add-Extension fileinfo >$null 2>&1 - Add-Extension ast >$null 2>&1 - } elseif($tool -eq "phive") { - Add-Extension xml >$null 2>&1 - } elseif($tool -eq "cs2pr") { - (Get-Content $bin_dir/cs2pr).replace('exit(9)', 'exit(0)') | Set-Content $bin_dir/cs2pr - } elseif($tool -eq "composer") { - Edit-ComposerConfig $bin_dir\$tool - } elseif($tool -eq "wp-cli") { - Copy-Item $bin_dir\wp-cli.bat -Destination $bin_dir\wp.bat + } catch { + if($url -match '.*github.com.*releases.*latest.*') { + try { + $url = $url.replace("releases/latest/download", "releases/download/" + ([regex]::match((Invoke-WebRequest -UseBasicParsing -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 -UseBasicParsing -Uri $url -OutFile $bin_dir\$tool + } 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-ToProfile $current_profile $tool "New-Alias $tool $bin_dir\$tool.bat" >$null 2>&1 + if($tool -eq "phan") { + Add-Extension fileinfo >$null 2>&1 + Add-Extension ast >$null 2>&1 + } elseif($tool -eq "phive") { + Add-Extension xml >$null 2>&1 + } elseif($tool -eq "cs2pr") { + (Get-Content $bin_dir/cs2pr).replace('exit(9)', 'exit(0)') | Set-Content $bin_dir/cs2pr + } elseif($tool -eq "composer") { + Edit-ComposerConfig $bin_dir\$tool + } elseif($tool -eq "wp-cli") { + Copy-Item $bin_dir\wp-cli.bat -Destination $bin_dir\wp.bat + } $tool_version = Get-ToolVersion $tool $ver_param Add-Log $tick $tool "Added $tool $tool_version" } else {