/ scripts / build-and-install.ps1
build-and-install.ps1
  1  <#
  2  .SYNOPSIS
  3      APC Master Builder with Enhanced Error Detection and Testing
  4  #>
  5  [CmdletBinding()]
  6  param(
  7      [Parameter(Mandatory=$true)][string]$PluginName,
  8      [switch]$NoInstall,
  9      [switch]$SkipTests,
 10      [switch]$Strict
 11  )
 12  
 13  $ErrorActionPreference = "Stop"
 14  
 15  # Import required modules
 16  . "$PSScriptRoot\state-management.ps1"
 17  . "$PSScriptRoot\error-detection.ps1"
 18  . "$PSScriptRoot\terminal-monitoring.ps1"
 19  . "$PSScriptRoot\pluginval-integration.ps1"
 20  
 21  $RootPath = (Get-Item "$PSScriptRoot\..").FullName
 22  $BuildDir = "$RootPath\build"
 23  $StatusJson = Join-Path $RootPath "plugins\$PluginName\status.json"
 24  $UseVisage = $false
 25  
 26  if (Test-Path $StatusJson) {
 27      try {
 28          $state = Get-Content $StatusJson -Raw | ConvertFrom-Json
 29          if ($state.ui_framework -eq "visage") {
 30              $UseVisage = $true
 31          }
 32      } catch {
 33          Write-Warning "Could not read status.json; proceeding without framework hints."
 34      }
 35  }
 36  
 37  Write-Host "--- APC BUILDER: $PluginName ---" -ForegroundColor Cyan
 38  if ($UseVisage) {
 39      Write-Host "Framework: visage" -ForegroundColor DarkGray
 40  }
 41  
 42  # Validate prerequisites
 43  $state = Get-PluginState -PluginPath "plugins/$PluginName"
 44  if ($state.current_phase -ne "code_complete" -and -not $SkipTests) {
 45      Write-Warning "Plugin implementation not marked as complete. Use -SkipTests to override."
 46  }
 47  
 48  # 1. Configure with error monitoring
 49  Write-Host "Configuring build..." -ForegroundColor Yellow
 50  $visageFlag = if ($UseVisage) { "-DAPC_ENABLE_VISAGE:BOOL=ON" } else { "" }
 51  $configureCommand = "cmake -S `"$RootPath`" -B `"$BuildDir`" -G `"Visual Studio 17 2022`" -A x64 --fresh $visageFlag"
 52  $configResult = Invoke-MonitoredCommand -Command $configureCommand -ShowOutput -ThrowOnError
 53  
 54  if ($configResult.Errors.Count -gt 0) {
 55      Write-Host "Configuration warnings detected" -ForegroundColor Yellow
 56      $knownIssue = Find-KnownIssue -Errors $configResult.Errors
 57      if ($knownIssue) {
 58          Write-Host "Known configuration issue detected: $($knownIssue.Title)" -ForegroundColor Cyan
 59          Apply-KnownSolution -Issue $knownIssue
 60          # Retry configuration
 61          $configResult = Invoke-MonitoredCommand -Command $configureCommand -ShowOutput -ThrowOnError
 62      }
 63  }
 64  
 65  # 2. Build VST3 with error monitoring
 66  Write-Host "Compiling VST3..." -ForegroundColor Yellow
 67  $buildVst3Command = "cmake --build `"$BuildDir`" --config Release --target `"$($PluginName)_VST3`""
 68  $vst3Result = Invoke-MonitoredCommand -Command $buildVst3Command -ShowOutput -ThrowOnError
 69  
 70  if ($vst3Result.Errors.Count -gt 0) {
 71      Write-Host "VST3 build errors detected" -ForegroundColor Red
 72  
 73      # Check for known issues
 74      $knownIssue = Find-KnownIssue -Errors $vst3Result.Errors
 75      if ($knownIssue) {
 76          Write-Host "Known issue detected: $($knownIssue.Title)" -ForegroundColor Cyan
 77          Apply-KnownSolution -Issue $knownIssue
 78          # Retry build
 79          $vst3Result = Invoke-MonitoredCommand -Command $buildVst3Command -ShowOutput -ThrowOnError
 80      }
 81  
 82      # If still failing, auto-capture new issue
 83      if ($vst3Result.Errors.Count -gt 0) {
 84          New-IssueFromError -Errors $vst3Result.Errors -BuildOutput $vst3Result.Output
 85          throw "VST3 build failed - issue logged for investigation"
 86      }
 87  }
 88  
 89  # 3. Build Standalone with error monitoring
 90  Write-Host "Compiling Standalone..." -ForegroundColor Yellow
 91  $buildStandaloneCommand = "cmake --build `"$BuildDir`" --config Release --target `"$($PluginName)_Standalone`""
 92  $standaloneResult = Invoke-MonitoredCommand -Command $buildStandaloneCommand -ShowOutput -ThrowOnError
 93  
 94  if ($standaloneResult.Errors.Count -gt 0) {
 95      Write-Host "Standalone build errors detected" -ForegroundColor Red
 96  
 97      # Check for known issues
 98      $knownIssue = Find-KnownIssue -Errors $standaloneResult.Errors
 99      if ($knownIssue) {
100          Write-Host "Known issue detected: $($knownIssue.Title)" -ForegroundColor Cyan
101          Apply-KnownSolution -Issue $knownIssue
102          # Retry build
103          $standaloneResult = Invoke-MonitoredCommand -Command $buildStandaloneCommand -ShowOutput -ThrowOnError
104      }
105  
106      # If still failing, auto-capture new issue
107      if ($standaloneResult.Errors.Count -gt 0) {
108          New-IssueFromError -Errors $standaloneResult.Errors -BuildOutput $standaloneResult.Output
109          throw "Standalone build failed - issue logged for investigation"
110      }
111  }
112  
113  # 4. Run PluginVal tests
114  if (-not $SkipTests) {
115      Write-Host "Running PluginVal validation..." -ForegroundColor Yellow
116  
117      # Find the built VST3 plugin
118      $vst3Path = Get-ChildItem -Path "$BuildDir" -Recurse -Filter "$PluginName.vst3" | Select-Object -First 1
119      if ($vst3Path) {
120          $pluginvalResult = Test-WithPluginVal -PluginPath $vst3Path.FullName -PluginName $PluginName -Strict:$Strict
121  
122          if (-not $pluginvalResult.Passed -and -not $pluginvalResult.Skipped) {
123              Write-Host "PluginVal tests failed" -ForegroundColor Red
124              if ($Strict) {
125                  throw "PluginVal validation failed in strict mode"
126              } else {
127                  Write-Warning "PluginVal tests failed - proceeding with installation anyway"
128              }
129          }
130      } else {
131          Write-Warning "VST3 plugin not found for PluginVal testing"
132      }
133  }
134  
135  # 5. Install VST3
136  if (-not $NoInstall) {
137      Write-Host "Installing VST3..." -ForegroundColor Yellow
138      $Vst = Get-ChildItem -Path "$BuildDir" -Recurse -Filter "$($PluginName).vst3" | Select-Object -First 1
139      if ($Vst) {
140          $Dest = "C:\Program Files\Common Files\VST3\$($Vst.Name)"
141          try {
142              if (Test-Path $Dest) { Remove-Item -Path $Dest -Recurse -Force -ErrorAction SilentlyContinue }
143              Copy-Item -Path $Vst.FullName -Destination $Dest -Recurse -Force
144              Write-Host "INSTALLED VST3 to: $Dest" -ForegroundColor Green
145          } catch {
146              Write-Warning "Access Denied. Run as Admin to install VST3."
147          }
148      }
149  
150      # Locate Standalone
151      $Exe = Get-ChildItem -Path "$BuildDir" -Recurse -Filter "$($PluginName).exe" | Select-Object -First 1
152      if ($Exe) {
153          Write-Host "STANDALONE built at: $($Exe.FullName)" -ForegroundColor Green
154  
155          # Add icon to standalone executable
156          $IconPath = "$RootPath\plugins\$PluginName\Assets\icon.ico"
157          if (Test-Path $IconPath) {
158              Write-Host "Adding icon to standalone executable..." -ForegroundColor Yellow
159              try {
160                  & "$PSScriptRoot\add-icon-to-exe.ps1" -ExePath $Exe.FullName -IconPath $IconPath
161                  Write-Host "[OK] Icon embedded in executable" -ForegroundColor Green
162              } catch {
163                  Write-Warning "Failed to add icon to executable: $_"
164              }
165          } else {
166              Write-Host "No icon found at $IconPath - skipping icon embedding" -ForegroundColor Gray
167          }
168  
169          Write-Host "Tip: You can run this to bypass VST3 caching issues." -ForegroundColor Cyan
170      }
171  }
172  
173  # 6. Update build status
174  Update-PluginState -PluginPath "plugins/$PluginName" -Updates @{
175      "validation.build_completed" = $true
176      "validation.build_timestamp" = (Get-Date -Format "yyyy-MM-ddTHH:mm:ssZ")
177      "validation.build_errors" = ($vst3Result.Errors + $standaloneResult.Errors).Count
178  }
179  
180  Write-Host "Build process complete!" -ForegroundColor Green