Improve cache for tools

This commit is contained in:
Shivam Mathur
2026-01-21 10:29:36 +05:30
parent 185f9de395
commit 341bc9e176
2 changed files with 54 additions and 29 deletions

View File

@@ -200,14 +200,21 @@ 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"
$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){
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 {
@@ -215,15 +222,26 @@ Function Add-Tool() {
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 { }
} 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 -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"
}

View File

@@ -177,15 +177,26 @@ 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"
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"
tool_version=$(get_tool_version "$tool" "$ver_param")
@@ -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[*]}"