[CmdletBinding()] param( [string]$BackendSource = "D:\Work\Inogen_new\Inogen backend", [string]$OutputRoot = (Join-Path $PSScriptRoot "..\artifacts"), [string]$SigningPrivateKey = $env:INOGEN_RELEASE_SIGNING_KEY, [string]$ApiBaseUrl, [string]$PreviousPackage, [switch]$SkipNpmInstall, [switch]$IncludeInternalTransfer ) $ErrorActionPreference = "Stop" $frontendSource = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path $backendSourcePath = (Resolve-Path -LiteralPath $BackendSource).Path $outputRootPath = [IO.Path]::GetFullPath($OutputRoot) $releaseName = "Inogen-{0}" -f (Get-Date -Format "yyyyMMdd-HHmmss") if ($IncludeInternalTransfer) { $releaseName += "-Internal" } $releasePath = Join-Path $outputRootPath $releaseName $backendOutput = Join-Path $releasePath "backend" $frontendOutput = Join-Path $releasePath "frontend" $setupOutput = Join-Path $releasePath "tools\Inogen.Setup" $transferOutput = Join-Path $releasePath "tools\Inogen.DataTransfer" $updatePath = "$releasePath-Update" if ([string]::IsNullOrWhiteSpace($SigningPrivateKey)) { throw "Release signing key is not configured. Generate one once with: dotnet run --project '\tools\Inogen.ReleaseProtector' -- generate-key '\inogen-release-private.pem'. Then set INOGEN_RELEASE_SIGNING_KEY to that absolute file path. Keep the private key outside the package and source tree." } if (-not [IO.Path]::IsPathRooted($SigningPrivateKey) -or -not (Test-Path -LiteralPath $SigningPrivateKey -PathType Leaf)) { throw "SigningPrivateKey must be the absolute path of an existing private PEM file." } $signingKeyPath = (Resolve-Path -LiteralPath $SigningPrivateKey).Path if ([string]::IsNullOrWhiteSpace($ApiBaseUrl)) { throw "Set -ApiBaseUrl to the HTTPS API address ending in /api/v1 for this deployment." } $apiUri = $null if (-not [Uri]::TryCreate($ApiBaseUrl.Trim(), [UriKind]::Absolute, [ref]$apiUri) -or $apiUri.Scheme -ne [Uri]::UriSchemeHttps -or $apiUri.AbsolutePath.TrimEnd('/') -ne '/api/v1' -or -not [string]::IsNullOrEmpty($apiUri.Query) -or -not [string]::IsNullOrEmpty($apiUri.Fragment) -or -not [string]::IsNullOrEmpty($apiUri.UserInfo)) { throw "ApiBaseUrl must be an absolute HTTPS URL ending in /api/v1 without query, fragment, or credentials." } New-Item -ItemType Directory -Force -Path $backendOutput, $frontendOutput, $setupOutput | Out-Null if ($IncludeInternalTransfer) { New-Item -ItemType Directory -Force -Path $transferOutput | Out-Null } function Assert-NativeCommandSucceeded([string]$operation) { if ($LASTEXITCODE -ne 0) { throw "$operation failed with exit code $LASTEXITCODE." } } function Protect-Assemblies([string]$targetPath, [string[]]$moduleNames) { $obfuscatedPath = Join-Path $targetPath "obfuscated" New-Item -ItemType Directory -Force -Path $obfuscatedPath | Out-Null $moduleElements = $moduleNames | ForEach-Object { $modulePath = [System.Security.SecurityElement]::Escape((Join-Path $targetPath $_)) $rules = @(" ") if ($_ -eq "Inogen.Infrastructure.dll") { $rules += " " $rules += " " $rules += " " } " $([Environment]::NewLine)$($rules -join [Environment]::NewLine)$([Environment]::NewLine) " } $configPath = Join-Path $targetPath "obfuscar.xml" $originalHashes = @{} foreach ($module in $moduleNames) { $originalHashes[$module] = (Get-FileHash -LiteralPath (Join-Path $targetPath $module) -Algorithm SHA256).Hash } $config = @( "", "", " ", " ", " ", " ", " ", " ", " ", " ", ($moduleElements -join [Environment]::NewLine), "" ) -join [Environment]::NewLine [IO.File]::WriteAllText($configPath, $config, [Text.UTF8Encoding]::new($false)) Push-Location $backendSourcePath try { dotnet tool run obfuscar.console -- $configPath Assert-NativeCommandSucceeded "Assembly obfuscation" } finally { Pop-Location } foreach ($module in $moduleNames) { $protectedModule = Join-Path $obfuscatedPath $module if (-not (Test-Path -LiteralPath $protectedModule -PathType Leaf)) { throw "Obfuscator did not produce $module." } if ((Get-FileHash -LiteralPath $protectedModule -Algorithm SHA256).Hash -eq $originalHashes[$module]) { throw "Obfuscator left $module unchanged." } Copy-Item -LiteralPath $protectedModule -Destination (Join-Path $targetPath $module) -Force } Remove-Item -LiteralPath $obfuscatedPath -Recurse -Force Remove-Item -LiteralPath $configPath -Force } function Write-SignedIntegrityManifest([string]$targetPath, [string[]]$fileNames) { $protectorProject = Join-Path $backendSourcePath "tools\Inogen.ReleaseProtector\Inogen.ReleaseProtector.csproj" dotnet run --project $protectorProject --configuration Release -- sign $signingKeyPath $targetPath @fileNames Assert-NativeCommandSucceeded "Release integrity signing" } Push-Location $frontendSource try { if (-not $SkipNpmInstall) { npm ci Assert-NativeCommandSucceeded "npm ci" } npm run typecheck Assert-NativeCommandSucceeded "Frontend typecheck" npm run build Assert-NativeCommandSucceeded "Frontend build" Copy-Item -Path (Join-Path $frontendSource "build\client\*") -Destination $frontendOutput -Recurse -Force $viteManifestDirectory = Join-Path $frontendOutput '.vite' if (Test-Path -LiteralPath $viteManifestDirectory) { Remove-Item -LiteralPath $viteManifestDirectory -Recurse -Force } $runtimeConfigPath = Join-Path $frontendOutput "config\runtime-config.js" $apiBaseUrlJson = ConvertTo-Json -InputObject $apiUri.AbsoluteUri.TrimEnd('/') -Compress [IO.File]::WriteAllText($runtimeConfigPath, "window.INOGEN_CONFIG = { apiBaseUrl: $apiBaseUrlJson };`n", [Text.UTF8Encoding]::new($false)) } finally { Pop-Location } dotnet publish (Join-Path $backendSourcePath "src\Inogen.Api\Inogen.Api.csproj") ` --configuration Release --output $backendOutput --no-self-contained Assert-NativeCommandSucceeded "Backend publish" $developmentSettings = Join-Path $backendOutput "appsettings.Development.json" if (Test-Path -LiteralPath $developmentSettings) { Remove-Item -LiteralPath $developmentSettings -Force } dotnet publish (Join-Path $backendSourcePath "tools\Inogen.Setup\Inogen.Setup.csproj") ` --configuration Release --output $setupOutput --no-self-contained Assert-NativeCommandSucceeded "Customer setup tool publish" if ($IncludeInternalTransfer) { dotnet publish (Join-Path $backendSourcePath "tools\Inogen.DataTransfer\Inogen.DataTransfer.csproj") ` --configuration Release --output $transferOutput --no-self-contained Assert-NativeCommandSucceeded "Internal transfer tool publish" } Push-Location $backendSourcePath try { dotnet tool restore Assert-NativeCommandSucceeded "Obfuscator restore" } finally { Pop-Location } $backendModules = @("Inogen.Api.dll", "Inogen.Application.dll", "Inogen.Domain.dll", "Inogen.Infrastructure.dll") $integrityModules = @("Inogen.Api.dll", "Inogen.Application.dll", "Inogen.Domain.dll", "Inogen.Infrastructure.dll") Protect-Assemblies $backendOutput $backendModules if (-not $IncludeInternalTransfer) { foreach ($module in @("Inogen.Application.dll", "Inogen.Domain.dll", "Inogen.Infrastructure.dll")) { Copy-Item -LiteralPath (Join-Path $backendOutput $module) -Destination (Join-Path $setupOutput $module) -Force } Protect-Assemblies $setupOutput @('Inogen.Setup.dll') } $publishedPaths = @($backendOutput, $setupOutput) if ($IncludeInternalTransfer) { $publishedPaths += $transferOutput } Get-ChildItem -LiteralPath $publishedPaths -Filter "*.pdb" -File | Remove-Item -Force Get-ChildItem -LiteralPath $publishedPaths -Filter "Mapping.txt" -File | Remove-Item -Force Write-SignedIntegrityManifest $backendOutput $integrityModules Copy-Item -LiteralPath (Join-Path $PSScriptRoot "appsettings.Production.template.json") ` -Destination (Join-Path $backendOutput "appsettings.Production.json") -Force Copy-Item -LiteralPath (Join-Path $PSScriptRoot "INSTALL.pdf") -Destination $releasePath -Force Copy-Item -LiteralPath (Join-Path $PSScriptRoot "initialize-admin.ps1") -Destination $releasePath -Force Copy-Item -LiteralPath (Join-Path $PSScriptRoot "migrate-target.ps1") -Destination $releasePath -Force Copy-Item -LiteralPath (Join-Path $PSScriptRoot "set-iis-runtime-connection.ps1") -Destination $releasePath -Force if ($IncludeInternalTransfer) { Copy-Item -LiteralPath (Join-Path $PSScriptRoot "INSTALL-INTERNAL.pdf") -Destination $releasePath -Force Copy-Item -LiteralPath (Join-Path $PSScriptRoot "select-internal-transfer.ps1") -Destination $releasePath -Force Copy-Item -LiteralPath (Join-Path $PSScriptRoot "prepare-transfer-connections.ps1") -Destination $releasePath -Force } Copy-Item -LiteralPath (Join-Path $PSScriptRoot "configure-windows-tls.ps1") -Destination $releasePath -Force Copy-Item -LiteralPath (Join-Path $PSScriptRoot "verify-public-security.ps1") -Destination $releasePath -Force Copy-Item -LiteralPath (Join-Path $PSScriptRoot "verify-runtime-sql-permissions.ps1") -Destination $releasePath -Force $assetPath = Join-Path $backendOutput "data\deployment\assets" New-Item -ItemType Directory -Force -Path $assetPath, (Join-Path $backendOutput "data\files"), ` (Join-Path $backendOutput "data\activation") | Out-Null foreach ($asset in @("product-logo.svg", "abniro.png", "besparan.png", "khwarazmi.png", "faravash1.png", "faravash2.png", "kimia.png")) { Copy-Item -LiteralPath (Join-Path $frontendSource "public\$asset") -Destination $assetPath -Force } if (-not $IncludeInternalTransfer) { $previousRelease = $null if (-not [string]::IsNullOrWhiteSpace($PreviousPackage)) { $previousRelease = Get-Item -LiteralPath $PreviousPackage -ErrorAction Stop if (-not $previousRelease.PSIsContainer -or -not (Test-Path -LiteralPath (Join-Path $previousRelease.FullName 'backend\Inogen.Api.dll')) -or -not (Test-Path -LiteralPath (Join-Path $previousRelease.FullName 'frontend\index.html'))) { throw 'PreviousPackage must be an extracted customer release folder with backend and frontend.' } } New-Item -ItemType Directory -Force -Path $updatePath | Out-Null foreach ($section in @('backend', 'frontend', 'tools')) { $sourceRoot = Join-Path $releasePath $section Get-ChildItem -LiteralPath $sourceRoot -Recurse -File | ForEach-Object { $relative = $_.FullName.Substring($sourceRoot.Length).TrimStart('\') if ($section -eq 'backend' -and ($relative -match '^data\\' -or $relative -match '^appsettings(\..+)?\.json$')) { return } if ($section -eq 'frontend' -and $relative -eq 'config\runtime-config.js') { return } $oldFile = if ($previousRelease) { Join-Path (Join-Path $previousRelease.FullName $section) $relative } else { $null } $requiredIntegrityModule = $section -eq 'backend' -and $relative -in $integrityModules if (-not $requiredIntegrityModule -and $oldFile -and (Test-Path -LiteralPath $oldFile) -and (Get-FileHash -LiteralPath $oldFile -Algorithm SHA256).Hash -eq (Get-FileHash -LiteralPath $_.FullName -Algorithm SHA256).Hash) { return } $destination = Join-Path (Join-Path $updatePath $section) $relative New-Item -ItemType Directory -Force -Path (Split-Path $destination -Parent) | Out-Null Copy-Item -LiteralPath $_.FullName -Destination $destination -Force } } Copy-Item -LiteralPath (Join-Path $PSScriptRoot 'UPDATE.pdf') -Destination $updatePath -Force [IO.File]::WriteAllText((Join-Path $updatePath 'VERSION.txt'), "Previous package: $(if ($previousRelease) { $previousRelease.Name } else { 'none (full application files)' })`r`nNew package: $releaseName`r`n", [Text.UTF8Encoding]::new($false)) $updateZip = "$updatePath.zip" Compress-Archive -LiteralPath $updatePath -DestinationPath $updateZip -CompressionLevel Optimal Write-Host "Update folder: $updatePath" Write-Host "Update archive: $updateZip" } $zipPath = "$releasePath.zip" Compress-Archive -LiteralPath $releasePath -DestinationPath $zipPath -CompressionLevel Optimal Write-Host "Release folder: $releasePath" Write-Host "Release archive: $zipPath"