_tmp/VRCOSC.Modules_dotnet_update.ps1

#!/usr/bin/env pwsh
# Build, commit, and optionally publish VRCOSC Modules using Bluscream-BuildTools
param(
    [switch]$Publish
)

$ErrorActionPreference = "Stop"

# Install and import Bluscream-BuildTools module
Write-Host "📦 Installing Bluscream-BuildTools module..." -ForegroundColor Cyan
if (-not (Get-Module -ListAvailable -Name Bluscream-BuildTools)) {
    Install-Module -Name Bluscream-BuildTools -Scope CurrentUser -Force -Repository PSGallery
    if ($LASTEXITCODE -ne 0) {
        throw "Failed to install Bluscream-BuildTools module from PowerShell Gallery"
    }
}
Import-Module Bluscream-BuildTools -Force
if (-not (Get-Module Bluscream-BuildTools)) {
    throw "Failed to import Bluscream-BuildTools module"
}
Write-Host "✓ Bluscream-BuildTools module loaded" -ForegroundColor Green

# Configuration
$repoUrl = "https://github.com/Bluscream/VRCOSC-Modules"
$ProjectDir = "$PSScriptRoot\VRCOSC.Modules"
$ProjectFile = "$ProjectDir\Bluscream.Modules.csproj"
$AssemblyInfoPath = "$ProjectDir\AssemblyInfo.cs"
$SourceDll = "$ProjectDir\bin\Release\net8.0-windows10.0.26100.0\Bluscream.Modules.dll"
$ReleaseDll = "$PSScriptRoot\VRCOSC.Modules.dll"

Write-Host "╔════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ VRCOSC Modules - Build & Release Script ║" -ForegroundColor Cyan
Write-Host "╚════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""

# Step 1: Version Management using Bluscream-BuildTools
Write-Host "🔢 Managing version with Bluscream-BuildTools..." -ForegroundColor Green

if (-not (Get-Command Set-ProjectVersion -ErrorAction SilentlyContinue)) {
    throw "Set-ProjectVersion command not found in Bluscream-BuildTools module"
}

$versionInfo = Set-ProjectVersion -ProjectPath $ProjectDir -VersionType AssemblyInfo -AutoIncrement
if (-not $versionInfo -or -not $versionInfo.NewVersion) {
    throw "Failed to update version using Set-ProjectVersion"
}

$ReleaseTag = $versionInfo.NewVersion
Write-Host "✓ Version updated to $ReleaseTag using Bluscream-BuildTools" -ForegroundColor Green

Write-Host ""

# Step 2: Complete Build Workflow using Bluscream-BuildTools
Write-Host "📦 Starting complete build workflow..." -ForegroundColor Green

if (-not (Get-Command Start-BuildWorkflow -ErrorAction SilentlyContinue)) {
    throw "Start-BuildWorkflow command not found in Bluscream-BuildTools module"
}

# Execute complete build workflow
$buildWorkflow = Start-BuildWorkflow -ProjectPath $ProjectFile -Configuration "Release" -Architecture "win-x64" -Framework "net8.0-windows10.0.26100.0" -AssemblyName "Bluscream.Modules" -OutputDirectory "./dist/" -CreateArchive -ArchiveName "VRCOSC.Modules-v$ReleaseTag" -CleanOutput

if (-not $buildWorkflow -or -not $buildWorkflow.Success) {
    throw "Build workflow failed"
}

Write-Host "✓ Complete build workflow succeeded" -ForegroundColor Green

# Copy the main DLL to the release location for backward compatibility
$mainDll = $buildWorkflow.CopiedFiles | Where-Object { $_ -like "*.dll" } | Select-Object -First 1
if ($mainDll) {
    Copy-Item $mainDll $ReleaseDll -Force
    Write-Host "✓ Copied main DLL to release location: $ReleaseDll" -ForegroundColor Green
}

# Step 4: Git operations using Bluscream-BuildTools
Write-Host "📝 Committing changes..." -ForegroundColor Green

if (-not (Get-Command Invoke-GitCommit -ErrorAction SilentlyContinue)) {
    throw "Invoke-GitCommit command not found in Bluscream-BuildTools module"
}

$commitResult = Invoke-GitCommit -Message "Update VRCOSC modules v$ReleaseTag" -Push
if (-not $commitResult -or -not $commitResult.Success) {
    throw "Git operations failed: $($commitResult.ErrorMessage)"
}

Write-Host "✓ Committed and pushed using Bluscream-BuildTools" -ForegroundColor Green

# Step 5: Create GitHub release (only if -Publish flag is used)
if ($Publish) {
    Write-Host "🚀 Creating GitHub release..." -ForegroundColor Green
    
    if (-not (Get-Command New-GitHubRelease -ErrorAction SilentlyContinue)) {
        throw "New-GitHubRelease command not found in Bluscream-BuildTools module"
    }
    
    # Prepare release assets from build workflow
    $releaseAssets = @($ReleaseDll)  # Main DLL for backward compatibility
    
    # Add archive if it exists
    $archivePath = $buildWorkflow.ArchivePath
    if ($archivePath -and (Test-Path $archivePath)) {
        $releaseAssets += $archivePath
    }
    
    # Add all built files as individual assets
    foreach ($file in $buildWorkflow.CopiedFiles) {
        if ($file -ne $ReleaseDll) {
            # Don't duplicate the main DLL
            $releaseAssets += $file
        }
    }
    
    $releaseResult = New-GitHubRelease -Repository $repoUrl -Tag $ReleaseTag -Title "VRCOSC Modules v$ReleaseTag" -Body "VRCOSC Modules v$ReleaseTag`n`nChanges:`n- Update VRCOSC modules v$ReleaseTag`n`nFiles included:`n$($releaseAssets | ForEach-Object { "- $(Split-Path $_ -Leaf)" } | Out-String)" -Prerelease -AssetPath $releaseAssets
    if (-not $releaseResult -or -not $releaseResult.Success) {
        throw "Release creation failed: $($releaseResult.ErrorMessage)"
    }
    
    Write-Host "✓ Release created using Bluscream-BuildTools: $($releaseResult.ReleaseUrl)" -ForegroundColor Green
}
else {
    Write-Host "⏭️ Skipping release (use -Publish to create release)" -ForegroundColor Yellow
}

# Step 6: Build in Debug mode
Write-Host "🔧 Building in Debug mode..." -ForegroundColor Green

$buildResult = Invoke-DotnetBuild -ProjectPath $ProjectFile -Configuration Debug -OutputPath "$ProjectDir\bin\Debug"
if (-not $buildResult -or -not $buildResult.Success) {
    throw "Debug build failed: $($buildResult.ErrorMessage)"
}

Write-Host "✓ Debug build succeeded using Bluscream-BuildTools" -ForegroundColor Green

# Summary
Write-Host ""
Write-Host "╔════════════════════════════════════════════════════════════╗" -ForegroundColor Green
Write-Host "║ ✓ ALL DONE! ║" -ForegroundColor Green
Write-Host "╚════════════════════════════════════════════════════════════╝" -ForegroundColor Green
Write-Host ""

if ($Publish) {
    Write-Host "📦 Release: $repoUrl/releases/tag/$ReleaseTag" -ForegroundColor Magenta
}
Write-Host "📍 Release DLL: $ReleaseDll" -ForegroundColor Cyan
Write-Host "📍 Local VRCOSC: %APPDATA%\VRCOSC\packages\local\Bluscream.Modules.dll (Debug)" -ForegroundColor Cyan