|
| 1 | +param( |
| 2 | + [string]$JarPath = "custom_spider.jar", |
| 3 | + [string]$SourceDir = "goProxy" |
| 4 | +) |
| 5 | + |
| 6 | +# Ensure 7z is available |
| 7 | +$7zCmd = Get-Command "7z" -ErrorAction SilentlyContinue |
| 8 | +if (-not $7zCmd) { |
| 9 | + Write-Error "Please ensure 7-Zip is installed and added to your system PATH." |
| 10 | + exit 1 |
| 11 | +} |
| 12 | + |
| 13 | +# Check if target file exists |
| 14 | +if (-not (Test-Path $JarPath)) { |
| 15 | + Write-Error "Target file not found: $JarPath" |
| 16 | + exit 1 |
| 17 | +} |
| 18 | + |
| 19 | +$ArmPath = Join-Path $SourceDir "goProxy-arm" |
| 20 | +$Arm64Path = Join-Path $SourceDir "goProxy-arm64" |
| 21 | + |
| 22 | +if (-not (Test-Path $ArmPath) -or -not (Test-Path $Arm64Path)) { |
| 23 | + Write-Error "Compiled goProxy files not found. Please run build_goproxy.ps1 first." |
| 24 | + exit 1 |
| 25 | +} |
| 26 | + |
| 27 | +Write-Host "Updating assets directory in $JarPath..." -ForegroundColor Cyan |
| 28 | + |
| 29 | +$TempAssetsDir = "temp_assets_for_jar\assets" |
| 30 | +New-Item -ItemType Directory -Force -Path $TempAssetsDir | Out-Null |
| 31 | +Copy-Item -Path $ArmPath -Destination $TempAssetsDir -Force |
| 32 | +Copy-Item -Path $Arm64Path -Destination $TempAssetsDir -Force |
| 33 | + |
| 34 | +try { |
| 35 | + # 为了避免在 jar 中创建 temp_assets_for_jar 目录,我们需要进入到该目录后再执行打包 |
| 36 | + # 使用 Push-Location 和 Pop-Location 来切换工作目录 |
| 37 | + Push-Location "temp_assets_for_jar" |
| 38 | + |
| 39 | + # 构造相对路径的 jar 文件路径,因为我们改变了工作目录 |
| 40 | + $AbsoluteJarPath = Resolve-Path "..\$JarPath" |
| 41 | + $updateCmd = "7z u `"$AbsoluteJarPath`" `"assets\*`"" |
| 42 | + |
| 43 | + Invoke-Expression $updateCmd | Out-Null |
| 44 | + |
| 45 | + if ($LASTEXITCODE -ne 0) { |
| 46 | + throw "7z command failed with exit code: $LASTEXITCODE" |
| 47 | + } |
| 48 | + |
| 49 | + Pop-Location |
| 50 | + Write-Host "Successfully updated files to assets directory in $JarPath." -ForegroundColor Green |
| 51 | +} catch { |
| 52 | + if ((Get-Location).Path -like "*temp_assets_for_jar*") { |
| 53 | + Pop-Location |
| 54 | + } |
| 55 | + Write-Error "Failed to update jar file: $_" |
| 56 | + Remove-Item -Path "temp_assets_for_jar" -Recurse -Force -ErrorAction SilentlyContinue |
| 57 | + exit 1 |
| 58 | +} finally { |
| 59 | + Remove-Item -Path "temp_assets_for_jar" -Recurse -Force -ErrorAction SilentlyContinue |
| 60 | +} |
| 61 | + |
| 62 | +Write-Host "Calculating MD5 for $JarPath..." -ForegroundColor Cyan |
| 63 | +try { |
| 64 | + $md5Hash = (Get-FileHash -Path $JarPath -Algorithm MD5).Hash.ToLower() |
| 65 | + $Md5FilePath = "$JarPath.md5" |
| 66 | + $md5Hash | Out-File -FilePath $Md5FilePath -Encoding ascii |
| 67 | + Write-Host "MD5 Calculation complete: $md5Hash" -ForegroundColor Green |
| 68 | + Write-Host "MD5 written to file: $Md5FilePath" -ForegroundColor Green |
| 69 | +} catch { |
| 70 | + Write-Error "Failed to calculate MD5: $_" |
| 71 | + exit 1 |
| 72 | +} |
| 73 | + |
| 74 | +Write-Host "All operations completed successfully!" -ForegroundColor Green |
0 commit comments