mirror of
https://github.com/shivammathur/setup-php.git
synced 2026-01-23 00:58:57 +07:00
Improve cache for tools
This commit is contained in:
@@ -200,30 +200,48 @@ Function Add-Tool() {
|
|||||||
[Parameter(Position = 2, Mandatory = $false)]
|
[Parameter(Position = 2, Mandatory = $false)]
|
||||||
$ver_param
|
$ver_param
|
||||||
)
|
)
|
||||||
if (Test-Path $bin_dir\$tool) {
|
$urls = $urls -split ','
|
||||||
Copy-Item $bin_dir\$tool -Destination $bin_dir\$tool.old -Force
|
|
||||||
}
|
|
||||||
$tool_path = "$bin_dir\$tool"
|
$tool_path = "$bin_dir\$tool"
|
||||||
foreach ($url in $urls){
|
$is_exe = ((($urls[0] | Split-Path -Extension).ToLowerInvariant()) -eq '.exe')
|
||||||
if (($url | Split-Path -Extension) -eq ".exe") {
|
if ($is_exe) { $tool_path = "$tool_path.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]))
|
||||||
try {
|
$cache_key = (Get-FileHash -InputStream $url_stream -Algorithm SHA256).Hash.Substring(0, 16)
|
||||||
$status_code = (Invoke-WebRequest -Passthru -Uri $url -OutFile $tool_path).StatusCode
|
$cache_path = "$env:TEMP\$tool-$cache_key$tool_ext"
|
||||||
} catch {
|
$status_code = 200
|
||||||
if($url -match '.*github.com.*releases.*latest.*') {
|
if (Test-Path $cache_path -PathType Leaf) {
|
||||||
try {
|
Copy-Item $cache_path -Destination $tool_path -Force
|
||||||
$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])
|
} else {
|
||||||
$status_code = (Invoke-WebRequest -Passthru -Uri $url -OutFile $tool_path).StatusCode
|
$backup_path = "$tool_path.bak"
|
||||||
} catch { }
|
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)) {
|
if ($status_code -ne 200 -and (Test-Path $backup_path)) {
|
||||||
break
|
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 = @()
|
||||||
$bat_content += "@ECHO off"
|
$bat_content += "@ECHO off"
|
||||||
$bat_content += "setlocal DISABLEDELAYEDEXPANSION"
|
$bat_content += "setlocal DISABLEDELAYEDEXPANSION"
|
||||||
@@ -237,8 +255,6 @@ Function Add-Tool() {
|
|||||||
} else {
|
} else {
|
||||||
if($tool -eq "composer") {
|
if($tool -eq "composer") {
|
||||||
$env:fail_fast = 'true'
|
$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"
|
Add-Log $cross $tool "Could not add $tool"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -177,14 +177,25 @@ add_tool() {
|
|||||||
sudo mkdir -p "$tool_path_dir"
|
sudo mkdir -p "$tool_path_dir"
|
||||||
fi
|
fi
|
||||||
add_path "$tool_path_dir"
|
add_path "$tool_path_dir"
|
||||||
if [ -e "$tool_path" ]; then
|
|
||||||
sudo cp -aL "$tool_path" /tmp/"$tool"
|
|
||||||
fi
|
|
||||||
IFS="," read -r -a url <<<"$url"
|
IFS="," read -r -a url <<<"$url"
|
||||||
status_code=$(get -v -e "$tool_path" "${url[@]}")
|
cache_key=$(get_sha256 "${url[0]}" | head -c 16)
|
||||||
if [ "$status_code" != "200" ] && [[ "${url[0]}" =~ .*github.com.*releases.*latest.* ]]; then
|
cache_path="/tmp/${tool}-${cache_key}"
|
||||||
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="200"
|
||||||
status_code=$(get -v -e "$tool_path" "${url[0]}")
|
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
|
fi
|
||||||
if [ "$status_code" = "200" ]; then
|
if [ "$status_code" = "200" ]; then
|
||||||
add_tools_helper "$tool"
|
add_tools_helper "$tool"
|
||||||
@@ -193,8 +204,6 @@ add_tool() {
|
|||||||
else
|
else
|
||||||
if [ "$tool" = "composer" ]; then
|
if [ "$tool" = "composer" ]; then
|
||||||
export fail_fast=true
|
export fail_fast=true
|
||||||
elif [ -e /tmp/"$tool" ]; then
|
|
||||||
sudo cp -a /tmp/"$tool" "$tool_path"
|
|
||||||
fi
|
fi
|
||||||
if [ "$status_code" = "404" ]; then
|
if [ "$status_code" = "404" ]; then
|
||||||
add_log "$cross" "$tool" "Failed to download $tool from ${url[*]}"
|
add_log "$cross" "$tool" "Failed to download $tool from ${url[*]}"
|
||||||
|
|||||||
Reference in New Issue
Block a user