param([ValidateSet('Debug', 'Release')][string]$Configuration = 'Release', [switch]$SkipTests) $ErrorActionPreference = 'Stop' if (![System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)) { throw 'Unity Attach 发布仅支持 Windows x64。' } $vswhere = Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio/Installer/vswhere.exe' if (!(Test-Path -LiteralPath $vswhere)) { throw '需要 Visual Studio C++ x64 构建工具。' } $visualStudio = & $vswhere -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath if (!$visualStudio) { throw '需要 Visual Studio C++ x64 构建工具。' } $vcvars = Join-Path $visualStudio 'VC/Auxiliary/Build/vcvars64.bat' $nativeSource = Join-Path $PSScriptRoot 'vendor/Native/Bootstrap.cpp' $nativeOutput = Join-Path $PSScriptRoot 'vendor/obj/native' New-Item -ItemType Directory -Force -Path $nativeOutput | Out-Null $command = 'call "{0}" >nul && cl /nologo /Brepro /LD /EHsc /MT /O2 "{1}" /Fo"{2}" /link /Brepro /OUT:"{3}" /IMPLIB:"{4}"' -f $vcvars,$nativeSource,(Join-Path $nativeOutput 'Bootstrap.obj'),(Join-Path $nativeOutput 'DotCraft.Unity.Native.dll'),(Join-Path $nativeOutput 'DotCraft.Unity.Native.lib') & cmd.exe /d /c $command if ($LASTEXITCODE -ne 0) { throw 'Unity Attach native bootstrap 构建失败。' } if (!$SkipTests) { & dotnet run --project (Join-Path $PSScriptRoot 'tests/Agc.Unity.Attach.Tests.csproj') -c $Configuration if ($LASTEXITCODE -ne 0) { throw 'Unity Attach helper 测试失败。' } } $output = Join-Path $PSScriptRoot 'publish/win-x64' & dotnet publish (Join-Path $PSScriptRoot 'Agc.Unity.Attach.csproj') -c $Configuration -r win-x64 --self-contained true -p:PublishSingleFile=true -p:PublishTrimmed=false -o $output if ($LASTEXITCODE -ne 0) { throw 'Unity Attach helper 发布失败。' } $licenses = Join-Path $output 'licenses' New-Item -ItemType Directory -Force -Path $licenses | Out-Null Copy-Item -LiteralPath (Join-Path $PSScriptRoot 'vendor/LICENSE') -Destination (Join-Path $licenses 'DotCraft-Apache-2.0.txt') Copy-Item -LiteralPath (Join-Path $PSScriptRoot 'licenses/Roslyn-MIT.txt') -Destination $licenses Copy-Item -LiteralPath (Join-Path $PSScriptRoot 'NOTICE'),(Join-Path $PSScriptRoot 'THIRD-PARTY-NOTICES.txt') -Destination $output Copy-Item -LiteralPath (Join-Path $PSScriptRoot 'vendor/upstream.json') -Destination $licenses $assets = Get-Content -LiteralPath (Join-Path $PSScriptRoot 'obj/project.assets.json') -Raw | ConvertFrom-Json $runtime = @($assets.project.frameworks.PSObject.Properties | ForEach-Object { $_.Value.downloadDependencies } | Where-Object name -EQ 'Microsoft.NETCore.App.Runtime.win-x64' | Select-Object -Unique name, version) if ($runtime.Count -ne 1) { throw '无法定位自包含 .NET runtime 许可。' } $runtimeVersion = $runtime[0].version.Trim('[',']').Split(',')[0].Trim() $runtimeDirectory = $null foreach ($folder in $assets.packageFolders.PSObject.Properties.Name) { $candidate = Join-Path $folder ('microsoft.netcore.app.runtime.win-x64/' + $runtimeVersion) if (Test-Path -LiteralPath $candidate) { $runtimeDirectory = $candidate; break } } if (!$runtimeDirectory) { throw '无法定位自包含 .NET runtime 包。' } foreach ($name in @('LICENSE.TXT', 'THIRD-PARTY-NOTICES.TXT')) { $source = Join-Path $runtimeDirectory $name if (!(Test-Path -LiteralPath $source)) { throw "缺少 .NET runtime 许可文件:$name" } Copy-Item -LiteralPath $source -Destination (Join-Path $licenses ('dotnet-' + $name)) } foreach ($package in @('microsoft.codeanalysis.common', 'microsoft.codeanalysis.csharp')) { $notice = $null foreach ($folder in $assets.packageFolders.PSObject.Properties.Name) { $candidate = Join-Path $folder ($package + '/5.3.0/ThirdPartyNotices.rtf') if (Test-Path -LiteralPath $candidate) { $notice = $candidate; break } } if (!$notice) { throw "缺少 Roslyn 第三方声明:$package" } Copy-Item -LiteralPath $notice -Destination (Join-Path $licenses ($package + '-ThirdPartyNotices.rtf')) } $oldDebugFile = Join-Path $output 'DotCraft.Unity.Attach.pdb' if (Test-Path -LiteralPath $oldDebugFile) { Remove-Item -LiteralPath $oldDebugFile } if (!(Test-Path -LiteralPath (Join-Path $output 'Agc.Unity.Attach.exe'))) { throw '缺少 self-contained helper。' } Write-Output "Unity Attach helper 已发布:$output"