# Script per build e deploy ottimizzato per Windows Service # Eseguire da PowerShell come Amministratore param( [string]$OutputPath = "C:\Temp\Publish\Data_Coupler", [string]$ServicePath = "C:\Services\DataCoupler", [switch]$InstallService = $false, [switch]$CleanBuild = $false ) Write-Host "=== Data Coupler Build & Deploy Script ===" -ForegroundColor Cyan Write-Host "Output Path: $OutputPath" -ForegroundColor Yellow Write-Host "Service Path: $ServicePath" -ForegroundColor Yellow try { # Naviga alla directory del progetto $projectRoot = Split-Path $PSScriptRoot -Parent Set-Location $projectRoot Write-Host "Directory progetto: $projectRoot" -ForegroundColor Green # Clean build se richiesto if ($CleanBuild) { Write-Host "`nPulizia build precedenti..." -ForegroundColor Yellow dotnet clean Data_Coupler.sln } # Build del progetto in modalità Release Write-Host "`nBuild del progetto..." -ForegroundColor Green $buildResult = dotnet publish Data_Coupler/Data_Coupler.csproj ` --configuration Release ` --output $OutputPath ` --self-contained true ` --runtime win-x64 ` --verbosity minimal ` -p:PublishSingleFile=false ` -p:PublishReadyToRun=true if ($LASTEXITCODE -ne 0) { throw "Errore durante il build del progetto" } Write-Host "Build completato con successo!" -ForegroundColor Green # Verifica che l'eseguibile sia stato creato $exePath = Join-Path $OutputPath "Data_Coupler.exe" if (-not (Test-Path $exePath)) { throw "File eseguibile non trovato: $exePath" } Write-Host "Eseguibile creato: $exePath" -ForegroundColor Green # Crea la directory del servizio se non esiste if (-not (Test-Path $ServicePath)) { Write-Host "`nCreazione directory servizio: $ServicePath" -ForegroundColor Yellow New-Item -ItemType Directory -Path $ServicePath -Force } # Copia i file nella directory del servizio Write-Host "`nCopia file nella directory del servizio..." -ForegroundColor Yellow Copy-Item -Path "$OutputPath\*" -Destination $ServicePath -Recurse -Force Write-Host "File copiati in $ServicePath" -ForegroundColor Green # Installa il servizio se richiesto if ($InstallService) { Write-Host "`nInstallazione del servizio Windows..." -ForegroundColor Cyan $serviceExePath = Join-Path $ServicePath "Data_Coupler.exe" $installScript = Join-Path $projectRoot "Scripts\install-service.ps1" if (Test-Path $installScript) { & $installScript -ServicePath $serviceExePath } else { Write-Warning "Script di installazione non trovato: $installScript" Write-Host "Installa manualmente il servizio con:" -ForegroundColor Yellow Write-Host "New-Service -Name 'DataCouplerService' -BinaryPathName '$serviceExePath' -StartupType Automatic" -ForegroundColor Cyan } } Write-Host "`n=== Deploy completato con successo! ===" -ForegroundColor Green Write-Host "Directory pubblicazione: $OutputPath" -ForegroundColor Cyan Write-Host "Directory servizio: $ServicePath" -ForegroundColor Cyan if ($InstallService) { Write-Host "URL applicazione: http://localhost:7550" -ForegroundColor Magenta } else { Write-Host "Per installare il servizio, esegui con -InstallService" -ForegroundColor Yellow } } catch { Write-Error "Errore durante il deploy: $($_.Exception.Message)" exit 1 } finally { # Torna alla directory originale Pop-Location -ErrorAction SilentlyContinue }