Files
xianyan/tools/setup_pubspec.ps1
2026-06-02 02:31:31 +08:00

138 lines
4.6 KiB
PowerShell

# ============================================================
# 闲言APP — pubspec.yaml 平台模板生成脚本
# 创建时间: 2026-06-02
# 更新时间: 2026-06-02
# 作用: 根据平台选择模板生成 pubspec.yaml
# 上次更新: 初始版本
# 用法:
# .\tools\setup_pubspec.ps1 -Platform ohos # 鸿蒙端
# .\tools\setup_pubspec.ps1 -Platform macos # MacBook Pro端
# .\tools\setup_pubspec.ps1 # 自动检测平台
# ============================================================
param(
[ValidateSet("ohos", "macos", "auto")]
[string]$Platform = "auto"
)
$ErrorActionPreference = "Stop"
$ProjectRoot = $PSScriptRoot
if ($ProjectRoot) {
$ProjectRoot = Split-Path -Parent $ProjectRoot
}
if (-not $ProjectRoot) {
$ProjectRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
if ($ProjectRoot) {
$ProjectRoot = Split-Path -Parent $ProjectRoot
}
}
if (-not $ProjectRoot) {
$ProjectRoot = (Get-Location).Path
}
$OhosTemplate = Join-Path $ProjectRoot "pubspec.ohos.yaml"
$MacosTemplate = Join-Path $ProjectRoot "pubspec.macos.yaml"
$OutputFile = Join-Path $ProjectRoot "pubspec.yaml"
function Write-Status {
param([string]$Message)
Write-Host "[setup_pubspec] $Message" -ForegroundColor Cyan
}
function Write-Warning {
param([string]$Message)
Write-Host "[setup_pubspec] WARNING: $Message" -ForegroundColor Yellow
}
function Write-Error {
param([string]$Message)
Write-Host "[setup_pubspec] ERROR: $Message" -ForegroundColor Red
}
# --- 自动检测平台 ---
if ($Platform -eq "auto") {
Write-Status "Auto-detecting platform..."
$flutterPath = Get-Command flutter -ErrorAction SilentlyContinue
if ($flutterPath) {
$flutterVersion = & flutter --version 2>&1 | Select-String -Pattern "ohos|HarmonyOS" -Quiet
if ($flutterVersion) {
$Platform = "ohos"
Write-Status "Detected flutter-ohos SDK -> ohos"
} else {
$Platform = "macos"
Write-Status "Detected official Flutter SDK -> macos"
}
} else {
# 检查 packages 目录是否存在
$packagesDir = Join-Path $ProjectRoot "packages"
if (Test-Path $packagesDir) {
$Platform = "ohos"
Write-Status "Found packages/ directory -> ohos"
} else {
$Platform = "macos"
Write-Status "No packages/ directory -> macos"
}
}
}
# --- 选择模板 ---
$TemplateFile = switch ($Platform) {
"ohos" { $OhosTemplate }
"macos" { $MacosTemplate }
}
if (-not (Test-Path $TemplateFile)) {
Write-Error "Template file not found: $TemplateFile"
Write-Error "Expected: pubspec.ohos.yaml or pubspec.macos.yaml in project root"
exit 1
}
# --- 备份现有 pubspec.yaml ---
if (Test-Path $OutputFile) {
$BackupFile = Join-Path $ProjectRoot "pubspec.yaml.bak"
Copy-Item $OutputFile $BackupFile -Force
Write-Status "Backed up existing pubspec.yaml -> pubspec.yaml.bak"
}
# --- 复制模板 ---
Copy-Item $TemplateFile $OutputFile -Force
$PlatformLabel = switch ($Platform) {
"ohos" { "鸿蒙端 (HarmonyOS)" }
"macos" { "MacBook Pro端 (iOS/macOS)" }
}
Write-Status "========================================"
Write-Status "Generated pubspec.yaml for: $PlatformLabel"
Write-Status "Template: $(Split-Path $TemplateFile -Leaf)"
Write-Status "Output: pubspec.yaml"
Write-Status "========================================"
# --- 验证关键差异 ---
if ($Platform -eq "ohos") {
$hasPathRef = Select-String -Path $OutputFile -Pattern "path: packages/" -Quiet
if (-not $hasPathRef) {
Write-Warning "ohos template has no 'path: packages/' references - check template!"
}
Write-Status "Next steps:"
Write-Status " 1. flutter pub get"
Write-Status " 2. flutter build hap (or your ohos build command)"
} else {
$hasPathRef = Select-String -Path $OutputFile -Pattern "path: packages/" -Quiet
if ($hasPathRef) {
Write-Warning "macos template still has 'path: packages/' references - check template!"
}
Write-Status "Next steps:"
Write-Status " 1. flutter pub get"
Write-Status " 2. Apply pub cache patches (see iOS_macOS_Developer_Guide.md section 2.6)"
Write-Status " 3. flutter build ios --no-codesign"
Write-Status " 4. flutter build macos"
}
Write-Status ""
Write-Status "NOTE: pubspec.yaml is in .gitignore and will NOT be committed."
Write-Status "When adding a new dependency, update BOTH pubspec.ohos.yaml AND pubspec.macos.yaml"
Write-Status "Then update iOS_macOS_Developer_Guide.md to notify the other platform developer."