# SerpShot Python SDK 发布脚本 (PowerShell)

Write-Host "🚀 SerpShot Python SDK 发布脚本" -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan
Write-Host ""

# 检查是否在正确的目录
if (-not (Test-Path "pyproject.toml")) {
    Write-Host "❌ 错误: 请在项目根目录运行此脚本" -ForegroundColor Red
    exit 1
}

# 检查 uv 是否安装
try {
    $null = Get-Command uv -ErrorAction Stop
} catch {
    Write-Host "❌ 错误: 未找到 uv，请先安装 uv" -ForegroundColor Red
    Write-Host "   安装方法: pip install uv" -ForegroundColor Yellow
    exit 1
}

# 显示当前版本
Write-Host "📦 当前版本:" -ForegroundColor Green
$currentVersion = (Select-String -Path "pyproject.toml" -Pattern '^version = ').Line
Write-Host $currentVersion
Write-Host ""

# 询问是否更新版本
$updateVersion = Read-Host "是否要更新版本号? (y/n)"
if ($updateVersion -eq "y" -or $updateVersion -eq "Y") {
    $versionInput = Read-Host "输入新版本号 (例如: 0.1.1)"
    if (-not [string]::IsNullOrWhiteSpace($versionInput)) {
        # 更新 pyproject.toml
        (Get-Content pyproject.toml) -replace '^version = ".*"', "version = `"$versionInput`"" | Set-Content pyproject.toml
        # 更新 __init__.py
        (Get-Content serpshot/__init__.py) -replace '^__version__ = ".*"', "__version__ = `"$versionInput`"" | Set-Content serpshot/__init__.py
        Write-Host ""
        Write-Host "✅ 版本已更新为: $versionInput" -ForegroundColor Green
        Write-Host ""
    }
}

# 构建包
Write-Host "🔨 构建包..." -ForegroundColor Yellow
uv build --no-sources
Write-Host "✅ 构建完成" -ForegroundColor Green
Write-Host ""

# 列出构建产物
Write-Host "📦 构建产物:" -ForegroundColor Green
Get-ChildItem dist/ | Format-Table Name, Length, LastWriteTime
Write-Host ""

# 询问是否发布
$publish = Read-Host "是否发布到 PyPI? (y/n)"
if ($publish -eq "y" -or $publish -eq "Y") {
    if (-not $env:UV_PUBLISH_TOKEN) {
        Write-Host "⚠️  未设置 UV_PUBLISH_TOKEN 环境变量" -ForegroundColor Yellow
        $token = Read-Host "请输入 PyPI token (或按 Ctrl+C 取消)"
        $env:UV_PUBLISH_TOKEN = $token
    }
    
    Write-Host "📤 发布到 PyPI..." -ForegroundColor Yellow
    uv publish
    Write-Host ""
    Write-Host "✅ 发布成功！" -ForegroundColor Green
    Write-Host ""
    Write-Host "🎉 包已发布到 PyPI" -ForegroundColor Cyan
    Write-Host "   等待几分钟后，可以通过以下命令安装:" -ForegroundColor White
    Write-Host "   pip install serpshot" -ForegroundColor Gray
    Write-Host "   或" -ForegroundColor White
    Write-Host "   uv add serpshot" -ForegroundColor Gray
} else {
    Write-Host "⏭️  跳过发布" -ForegroundColor Yellow
    Write-Host "   构建产物在 dist/ 目录，可以稍后手动发布:" -ForegroundColor White
    Write-Host "   uv publish --token your-token" -ForegroundColor Gray
}

