From 341bc9e17688b11a423b2bc10339d3f0fd6d3c55 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Wed, 21 Jan 2026 10:29:36 +0530 Subject: [PATCH] Improve cache for tools --- src/scripts/tools/add_tools.ps1 | 56 +++++++++++++++++++++------------ src/scripts/tools/add_tools.sh | 27 ++++++++++------ 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/src/scripts/tools/add_tools.ps1 b/src/scripts/tools/add_tools.ps1 index 5dcb053b..bcf3cb9e 100644 --- a/src/scripts/tools/add_tools.ps1 +++ b/src/scripts/tools/add_tools.ps1 @@ -200,30 +200,48 @@ Function Add-Tool() { [Parameter(Position = 2, Mandatory = $false)] $ver_param ) - if (Test-Path $bin_dir\$tool) { - Copy-Item $bin_dir\$tool -Destination $bin_dir\$tool.old -Force - } + $urls = $urls -split ',' $tool_path = "$bin_dir\$tool" - foreach ($url in $urls){ - if (($url | Split-Path -Extension) -eq ".exe") { - $tool_path = "$tool_path.exe" - } - try { - $status_code = (Invoke-WebRequest -Passthru -Uri $url -OutFile $tool_path).StatusCode - } catch { - if($url -match '.*github.com.*releases.*latest.*') { - try { - $url = $url.replace("releases/latest/download", "releases/download/" + ([regex]::match((Get-File -Url ($url.split('/release')[0] + "/releases")).Content, "([0-9]+\.[0-9]+\.[0-9]+)/" + ($url.Substring($url.LastIndexOf("/") + 1))).Groups[0].Value).split('/')[0]) - $status_code = (Invoke-WebRequest -Passthru -Uri $url -OutFile $tool_path).StatusCode - } catch { } + $is_exe = ((($urls[0] | Split-Path -Extension).ToLowerInvariant()) -eq '.exe') + if ($is_exe) { $tool_path = "$tool_path.exe" } + $tool_ext = if ($is_exe) { '.exe' } else { '' } + $url_stream = [System.IO.MemoryStream]::New([System.Text.Encoding]::UTF8.GetBytes($urls[0])) + $cache_key = (Get-FileHash -InputStream $url_stream -Algorithm SHA256).Hash.Substring(0, 16) + $cache_path = "$env:TEMP\$tool-$cache_key$tool_ext" + $status_code = 200 + if (Test-Path $cache_path -PathType Leaf) { + Copy-Item $cache_path -Destination $tool_path -Force + } else { + $backup_path = "$tool_path.bak" + if (Test-Path $tool_path) { Copy-Item $tool_path -Destination $backup_path -Force } + foreach ($url in $urls){ + try { + $status_code = (Invoke-WebRequest -Passthru -Uri $url -OutFile $tool_path).StatusCode + } catch { + if($url -match '.*github.com.*releases.*latest.*') { + try { + $url = $url.replace("releases/latest/download", "releases/download/" + ([regex]::match((Get-File -Url ($url.split('/release')[0] + "/releases")).Content, "([0-9]+\.[0-9]+\.[0-9]+)/" + ($url.Substring($url.LastIndexOf("/") + 1))).Groups[0].Value).split('/')[0]) + $status_code = (Invoke-WebRequest -Passthru -Uri $url -OutFile $tool_path).StatusCode + } catch { + $status_code = 0 + } + } else { + $status_code = 0 + } + } + if($status_code -eq 200 -and (Test-Path $tool_path)) { + Copy-Item $tool_path -Destination $cache_path -Force + break } } - if($status_code -eq 200 -and (Test-Path $tool_path)) { - break + if ($status_code -ne 200 -and (Test-Path $backup_path)) { + Copy-Item $backup_path -Destination $tool_path -Force } + Remove-Item $backup_path -Force -ErrorAction SilentlyContinue } - if (((Get-ChildItem -Path $bin_dir/* | Where-Object Name -Match "^$tool(.exe|.phar)*$").Count -gt 0)) { + $escaped_tool = [regex]::Escape($tool) + if (((Get-ChildItem -Path $bin_dir/* | Where-Object Name -Match "^$escaped_tool(\.exe|\.phar)?$").Count -gt 0)) { $bat_content = @() $bat_content += "@ECHO off" $bat_content += "setlocal DISABLEDELAYEDEXPANSION" @@ -237,8 +255,6 @@ Function Add-Tool() { } 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" } diff --git a/src/scripts/tools/add_tools.sh b/src/scripts/tools/add_tools.sh index 70626c7d..9e2c398d 100644 --- a/src/scripts/tools/add_tools.sh +++ b/src/scripts/tools/add_tools.sh @@ -177,14 +177,25 @@ add_tool() { sudo mkdir -p "$tool_path_dir" fi add_path "$tool_path_dir" - if [ -e "$tool_path" ]; then - sudo cp -aL "$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]}") + cache_key=$(get_sha256 "${url[0]}" | head -c 16) + cache_path="/tmp/${tool}-${cache_key}" + status_code="200" + if [ -f "$cache_path" ]; then + sudo cp -a "$cache_path" "$tool_path" + else + [ -f "$tool_path" ] && sudo cp -a "$tool_path" "$tool_path.bak" + 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 + sudo cp -a "$tool_path" "$cache_path" + elif [ -f "$tool_path.bak" ]; then + sudo mv "$tool_path.bak" "$tool_path" + fi + sudo rm -f "$tool_path.bak" fi if [ "$status_code" = "200" ]; then add_tools_helper "$tool" @@ -193,8 +204,6 @@ add_tool() { else if [ "$tool" = "composer" ]; then export fail_fast=true - elif [ -e /tmp/"$tool" ]; then - sudo cp -a /tmp/"$tool" "$tool_path" fi if [ "$status_code" = "404" ]; then add_log "$cross" "$tool" "Failed to download $tool from ${url[*]}"