|
| 1 | +param( |
| 2 | + [string]$Version, |
| 3 | + [switch]$Sign, |
| 4 | + [switch]$Publish, |
| 5 | + [string]$NuGetApiKey |
| 6 | +) |
| 7 | + |
| 8 | +$ErrorActionPreference = 'Stop' |
| 9 | +$ProgressPreference = 'SilentlyContinue' |
| 10 | + |
| 11 | +# Get script root and project root |
| 12 | +$scriptroot = $PSScriptRoot |
| 13 | +if (-not $scriptroot) { |
| 14 | + $scriptroot = Split-Path -Path $MyInvocation.MyCommand.Path |
| 15 | +} |
| 16 | +$root = Split-Path -Path $scriptroot |
| 17 | +$csvProjectPath = Join-Path $root "project\Dataplat.Dbatools.Csv" |
| 18 | +$csvCsproj = Join-Path $csvProjectPath "Dataplat.Dbatools.Csv.csproj" |
| 19 | +$artifactsDir = Join-Path $root "artifacts" |
| 20 | +$csvArtifacts = Join-Path $artifactsDir "csv" |
| 21 | + |
| 22 | +Write-Host "=== Dataplat.Dbatools.Csv Build Script ===" -ForegroundColor Cyan |
| 23 | +Write-Host "" |
| 24 | + |
| 25 | +# Update or read version using XML parsing (safer than regex) |
| 26 | +[xml]$csproj = Get-Content $csvCsproj |
| 27 | +$propertyGroup = $csproj.Project.PropertyGroup | Where-Object { $_.Version } | Select-Object -First 1 |
| 28 | + |
| 29 | +if ($Version) { |
| 30 | + Write-Host "Updating version to: $Version" -ForegroundColor Yellow |
| 31 | + $propertyGroup.Version = $Version |
| 32 | + $csproj.Save($csvCsproj) |
| 33 | +} else { |
| 34 | + $Version = $propertyGroup.Version |
| 35 | + Write-Host "Building version: $Version" -ForegroundColor Yellow |
| 36 | +} |
| 37 | + |
| 38 | +# Clean and create artifacts directory |
| 39 | +if (Test-Path $csvArtifacts) { |
| 40 | + Remove-Item $csvArtifacts -Recurse -Force |
| 41 | +} |
| 42 | +$null = New-Item -ItemType Directory -Path $csvArtifacts -Force |
| 43 | + |
| 44 | +# Clean previous builds |
| 45 | +Write-Host "Cleaning previous builds..." -ForegroundColor Yellow |
| 46 | +Push-Location $csvProjectPath |
| 47 | +try { |
| 48 | + dotnet clean -c Release --nologo 2>$null |
| 49 | +} catch { } |
| 50 | +Pop-Location |
| 51 | + |
| 52 | +# Build first (without packing) so we can sign the DLLs |
| 53 | +Write-Host "Building project..." -ForegroundColor Yellow |
| 54 | +Push-Location $csvProjectPath |
| 55 | +try { |
| 56 | + dotnet build -c Release --nologo |
| 57 | + if ($LASTEXITCODE -ne 0) { |
| 58 | + throw "dotnet build failed with exit code $LASTEXITCODE" |
| 59 | + } |
| 60 | +} finally { |
| 61 | + Pop-Location |
| 62 | +} |
| 63 | + |
| 64 | +# Sign the DLLs BEFORE packing if requested |
| 65 | +if ($Sign) { |
| 66 | + if (Get-Command Invoke-DbatoolsTrustedSigning -ErrorAction SilentlyContinue) { |
| 67 | + Write-Host "" |
| 68 | + Write-Host "=== Signing with Azure Trusted Signing ===" -ForegroundColor Cyan |
| 69 | + |
| 70 | + # Find built DLLs in the bin/Release folders |
| 71 | + $binPath = Join-Path $csvProjectPath "bin\Release" |
| 72 | + $dllsToSign = Get-ChildItem -Path $binPath -Filter "*.dll" -Recurse | |
| 73 | + Where-Object { $_.Name -like "*Dbatools*" -or $_.Name -like "*Dataplat*" } |
| 74 | + |
| 75 | + if ($dllsToSign) { |
| 76 | + Write-Host "Signing $($dllsToSign.Count) DLL(s)..." -ForegroundColor Yellow |
| 77 | + |
| 78 | + foreach ($dll in $dllsToSign) { |
| 79 | + Write-Host " Signing: $($dll.Name)" -ForegroundColor Gray |
| 80 | + $result = $dll.FullName | Invoke-DbatoolsTrustedSigning |
| 81 | + if ($result.Status -ne 'Valid') { |
| 82 | + throw "Signing failed for $($dll.Name): Status '$($result.Status)'. Cannot continue with unsigned DLLs." |
| 83 | + } |
| 84 | + Write-Host " Signed (Thumbprint: $($result.Thumbprint))" -ForegroundColor Green |
| 85 | + } |
| 86 | + } else { |
| 87 | + Write-Host "No DLLs found to sign" -ForegroundColor Yellow |
| 88 | + } |
| 89 | + } else { |
| 90 | + Write-Warning "Invoke-DbatoolsTrustedSigning not found - skipping signing" |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +# Now pack (will include the signed DLLs) |
| 95 | +Write-Host "Packing NuGet package..." -ForegroundColor Yellow |
| 96 | +Push-Location $csvProjectPath |
| 97 | +try { |
| 98 | + dotnet pack -c Release --nologo --no-build -o $csvArtifacts |
| 99 | + if ($LASTEXITCODE -ne 0) { |
| 100 | + throw "dotnet pack failed with exit code $LASTEXITCODE" |
| 101 | + } |
| 102 | +} finally { |
| 103 | + Pop-Location |
| 104 | +} |
| 105 | + |
| 106 | +# Find the generated packages |
| 107 | +$nupkg = Get-ChildItem -Path $csvArtifacts -Filter "*.nupkg" | Select-Object -First 1 |
| 108 | +$snupkg = Get-ChildItem -Path $csvArtifacts -Filter "*.snupkg" | Select-Object -First 1 |
| 109 | + |
| 110 | +if (-not $nupkg) { |
| 111 | + throw "No .nupkg file found in $csvArtifacts" |
| 112 | +} |
| 113 | + |
| 114 | +Write-Host "Package created: $($nupkg.Name)" -ForegroundColor Green |
| 115 | +if ($snupkg) { |
| 116 | + Write-Host "Symbols package: $($snupkg.Name)" -ForegroundColor Green |
| 117 | +} |
| 118 | + |
| 119 | +# Publish to NuGet if requested |
| 120 | +if ($Publish) { |
| 121 | + if (-not $NuGetApiKey) { |
| 122 | + $NuGetApiKey = $env:NUGET_API_KEY |
| 123 | + } |
| 124 | + |
| 125 | + if (-not $NuGetApiKey) { |
| 126 | + Write-Warning "No NuGet API key provided. Set -NuGetApiKey or `$env:NUGET_API_KEY" |
| 127 | + } else { |
| 128 | + Write-Host "" |
| 129 | + Write-Host "=== Publishing to NuGet ===" -ForegroundColor Cyan |
| 130 | + |
| 131 | + dotnet nuget push $nupkg.FullName --api-key $NuGetApiKey --source https://api.nuget.org/v3/index.json --skip-duplicate |
| 132 | + |
| 133 | + if ($snupkg) { |
| 134 | + dotnet nuget push $snupkg.FullName --api-key $NuGetApiKey --source https://api.nuget.org/v3/index.json --skip-duplicate |
| 135 | + } |
| 136 | + |
| 137 | + if ($LASTEXITCODE -eq 0) { |
| 138 | + Write-Host "Published to NuGet!" -ForegroundColor Green |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +Write-Host "" |
| 144 | +Write-Host "=== Build Complete ===" -ForegroundColor Cyan |
| 145 | +Write-Host "Output: $csvArtifacts" -ForegroundColor White |
| 146 | +Get-ChildItem $csvArtifacts | ForEach-Object { |
| 147 | + Write-Host " $($_.Name)" -ForegroundColor Gray |
| 148 | +} |
0 commit comments