From a5c2146f3f9b2e377ae27dae141f779eeb21e65c Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Sun, 15 Mar 2026 11:36:39 +0530 Subject: [PATCH] Improve tool cache to not cache persistent urls --- src/scripts/tools/add_tools.ps1 | 19 +++++++++++++++++-- src/scripts/tools/add_tools.sh | 16 ++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/scripts/tools/add_tools.ps1 b/src/scripts/tools/add_tools.ps1 index bcf3cb9e..b65487c1 100644 --- a/src/scripts/tools/add_tools.ps1 +++ b/src/scripts/tools/add_tools.ps1 @@ -115,6 +115,18 @@ Function Set-ComposerEnv() { } } +# Function to identify latest-like URLs that should bypass the persistent cache. +Function Test-MutableToolUrl() { + Param( + [Parameter(Position = 0, Mandatory = $true)] + [string] + $Url + ) + $mutableUrlRegex = '(^|[/?#._=-])(latest|stable|preview|snapshot|nightly|master)([/?#._=-]|$)|/releases/latest/download/' + $versionLikeRegex = '(^|[^0-9])[0-9]+\.[0-9]+([.-][0-9A-Za-z]+)*' + return ($Url -match $mutableUrlRegex) -or (($Url -match '\.phar([?#].*)?$') -and -not ($Url -match $versionLikeRegex)) +} + # Function to extract tool version. Function Get-ToolVersion() { Param ( @@ -208,8 +220,9 @@ Function Add-Tool() { $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" + $use_cache = -not (Test-MutableToolUrl $urls[0]) $status_code = 200 - if (Test-Path $cache_path -PathType Leaf) { + if ($use_cache -and (Test-Path $cache_path -PathType Leaf)) { Copy-Item $cache_path -Destination $tool_path -Force } else { $backup_path = "$tool_path.bak" @@ -230,7 +243,9 @@ Function Add-Tool() { } } if($status_code -eq 200 -and (Test-Path $tool_path)) { - Copy-Item $tool_path -Destination $cache_path -Force + if ($use_cache) { + Copy-Item $tool_path -Destination $cache_path -Force + } break } } diff --git a/src/scripts/tools/add_tools.sh b/src/scripts/tools/add_tools.sh index d577894b..294a1513 100644 --- a/src/scripts/tools/add_tools.sh +++ b/src/scripts/tools/add_tools.sh @@ -113,6 +113,16 @@ set_composer_env() { fi } +# Function to identify latest-like URLs that should bypass the persistent cache. +is_mutable_tool_url() { + local tool_url=$1 + local mutable_url_regex='(^|[/?#._=-])(latest|stable|preview|snapshot|nightly|master)([/?#._=-]|$)|/releases/latest/download/' + local version_like_regex='(^|[^0-9])[0-9]+\.[0-9]+([.-][0-9A-Za-z]+)*' + [[ "$tool_url" =~ $mutable_url_regex ]] && return 0 + [[ "$tool_url" =~ \.phar([?#].*)?$ && ! "$tool_url" =~ $version_like_regex ]] && return 0 + return 1 +} + # Helper function to configure tools. add_tools_helper() { tool=$1 @@ -184,8 +194,10 @@ add_tool() { IFS="," read -r -a url <<<"$url" cache_key=$(get_sha256 "${url[0]}" | head -c 16) cache_path="$tool_cache_path_dir/${tool}-${cache_key}" + use_cache=true + is_mutable_tool_url "${url[0]}" && use_cache=false status_code="200" - if [ -f "$cache_path" ]; then + if [ "$use_cache" = "true" ] && [ -f "$cache_path" ]; then sudo cp -a "$cache_path" "$tool_path" else [ -f "$tool_path" ] && sudo cp -a "$tool_path" "$tool_path.bak" @@ -194,7 +206,7 @@ add_tool() { 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 + if [ "$status_code" = "200" ] && [ "$use_cache" = "true" ]; then sudo cp -a "$tool_path" "$cache_path" elif [ -f "$tool_path.bak" ]; then sudo mv "$tool_path.bak" "$tool_path"