Improve tool cache to not cache persistent urls

This commit is contained in:
Shivam Mathur
2026-03-15 11:36:39 +05:30
parent 6a1d559c57
commit a5c2146f3f
2 changed files with 31 additions and 4 deletions

View File

@@ -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 to extract tool version.
Function Get-ToolVersion() { Function Get-ToolVersion() {
Param ( Param (
@@ -208,8 +220,9 @@ Function Add-Tool() {
$url_stream = [System.IO.MemoryStream]::New([System.Text.Encoding]::UTF8.GetBytes($urls[0])) $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_key = (Get-FileHash -InputStream $url_stream -Algorithm SHA256).Hash.Substring(0, 16)
$cache_path = "$env:TEMP\$tool-$cache_key$tool_ext" $cache_path = "$env:TEMP\$tool-$cache_key$tool_ext"
$use_cache = -not (Test-MutableToolUrl $urls[0])
$status_code = 200 $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 Copy-Item $cache_path -Destination $tool_path -Force
} else { } else {
$backup_path = "$tool_path.bak" $backup_path = "$tool_path.bak"
@@ -230,7 +243,9 @@ Function Add-Tool() {
} }
} }
if($status_code -eq 200 -and (Test-Path $tool_path)) { 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 break
} }
} }

View File

@@ -113,6 +113,16 @@ set_composer_env() {
fi 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. # Helper function to configure tools.
add_tools_helper() { add_tools_helper() {
tool=$1 tool=$1
@@ -184,8 +194,10 @@ add_tool() {
IFS="," read -r -a url <<<"$url" IFS="," read -r -a url <<<"$url"
cache_key=$(get_sha256 "${url[0]}" | head -c 16) cache_key=$(get_sha256 "${url[0]}" | head -c 16)
cache_path="$tool_cache_path_dir/${tool}-${cache_key}" cache_path="$tool_cache_path_dir/${tool}-${cache_key}"
use_cache=true
is_mutable_tool_url "${url[0]}" && use_cache=false
status_code="200" status_code="200"
if [ -f "$cache_path" ]; then if [ "$use_cache" = "true" ] && [ -f "$cache_path" ]; then
sudo cp -a "$cache_path" "$tool_path" sudo cp -a "$cache_path" "$tool_path"
else else
[ -f "$tool_path" ] && sudo cp -a "$tool_path" "$tool_path.bak" [ -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)}" 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]}") status_code=$(get -v -e "$tool_path" "${url[0]}")
fi fi
if [ "$status_code" = "200" ]; then if [ "$status_code" = "200" ] && [ "$use_cache" = "true" ]; then
sudo cp -a "$tool_path" "$cache_path" sudo cp -a "$tool_path" "$cache_path"
elif [ -f "$tool_path.bak" ]; then elif [ -f "$tool_path.bak" ]; then
sudo mv "$tool_path.bak" "$tool_path" sudo mv "$tool_path.bak" "$tool_path"