|
| 1 | +# Requires -Version 7 |
| 2 | + |
| 3 | +param( |
| 4 | + [Parameter(Mandatory = $true)] |
| 5 | + [string] |
| 6 | + $Version |
| 7 | +) |
| 8 | + |
| 9 | +function Generate-SBom { |
| 10 | + param( |
| 11 | + [Parameter(Mandatory = $true)] |
| 12 | + [string] |
| 13 | + $Version, |
| 14 | + |
| 15 | + [Parameter(Mandatory = $true)] |
| 16 | + [string] |
| 17 | + $Path, |
| 18 | + |
| 19 | + [Parameter(Mandatory = $true)] |
| 20 | + [string] |
| 21 | + $Name, |
| 22 | + |
| 23 | + [switch] |
| 24 | + $Json, |
| 25 | + |
| 26 | + [switch] |
| 27 | + $PostProcess |
| 28 | + ) |
| 29 | + |
| 30 | + $targetDir = Join-Path -Path build -ChildPath cyclonedx |
| 31 | + if (-not(Test-Path $targetDir -PathType Container)) { |
| 32 | + New-Item -Path $targetDir -ItemType Directory |
| 33 | + } |
| 34 | + $targetDir = Resolve-Path -Path $targetDir |
| 35 | + $fileName = $JSon ? "$Name.cylconedx.json" : "$Name.cylconedx.xml" |
| 36 | + $fullFileName = Join-Path -Path $targetDir -ChildPath $fileName |
| 37 | + |
| 38 | + $Args = @( |
| 39 | + "./src/main/$Path/$Name.csproj" |
| 40 | + "-ipr" |
| 41 | + "-o" |
| 42 | + "$targetDir" |
| 43 | + "-sv" |
| 44 | + "$Version" |
| 45 | + "-st" |
| 46 | + "library" |
| 47 | + "-sn" |
| 48 | + "$Name" |
| 49 | + "--set-nuget-purl" |
| 50 | + "-imp" |
| 51 | + "./src/shared/cyclonedx-metadata.xml" |
| 52 | + "-fn" |
| 53 | + "$fileName" |
| 54 | + ) |
| 55 | + |
| 56 | + Write-Output "Generating $fullFileName" |
| 57 | + if ($Json) { |
| 58 | + dotnet-CycloneDX @Args -j |
| 59 | + } else { |
| 60 | + dotnet-CycloneDX @Args |
| 61 | + } |
| 62 | + |
| 63 | + if ($PostProcess) { |
| 64 | + Write-Output "Post-Processing $fullFileName" |
| 65 | + if ($Json) { |
| 66 | + $body = Get-Content $fullFileName -Raw | ConvertFrom-Json -Depth 32 |
| 67 | + $body.dependencies | |
| 68 | + ForEach-Object { |
| 69 | + if ($_.ref -eq "xmlunit-core@1.0.0") { |
| 70 | + $_.ref = "pkg:nuget/XMLUnit.Core@$Version" |
| 71 | + } |
| 72 | + if ($_.dependsOn -and $_.dependsOn.Contains("xmlunit-core@1.0.0")) { |
| 73 | + $_.dependsOn = $_.dependsOn.Replace("xmlunit-core@1.0.0", |
| 74 | + "pkg:nuget/XMLUnit.Core@$Version") |
| 75 | + } |
| 76 | + } |
| 77 | + $body.components | |
| 78 | + ForEach-Object { |
| 79 | + if ($_.name -eq "xmlunit-core") { |
| 80 | + $_.name = "XMLUnit.Core" |
| 81 | + $_."bom-ref" = "pkg:nuget/XMLUnit.Core@$Version" |
| 82 | + $_.version = "$Version" |
| 83 | + } |
| 84 | + } |
| 85 | + $body | ConvertTo-Json -Depth 32 | Set-Content $fullFileName |
| 86 | + } else { |
| 87 | + $body = [xml](Get-Content $fullFileName) |
| 88 | + $body.bom.dependencies.dependency | |
| 89 | + ForEach-Object { |
| 90 | + if ($_.HasAttribute("ref") -and $_.Attributes["ref"].Value -eq "xmlunit-core@1.0.0") { |
| 91 | + $_.Attributes["ref"].Value = "pkg:nuget/XMLUnit.Core@$Version" |
| 92 | + } |
| 93 | + if ($_.dependency) { |
| 94 | + $_.dependency | |
| 95 | + ForEach-Object { |
| 96 | + if ($_.HasAttribute("ref") -and $_.Attributes["ref"].Value -eq "xmlunit-core@1.0.0") { |
| 97 | + $_.Attributes["ref"].Value = "pkg:nuget/XMLUnit.Core@$Version" |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + $body.bom.components.component | |
| 103 | + ForEach-Object { |
| 104 | + if ($_.HasAttribute("bom-ref") -and $_.Attributes["bom-ref"].Value -eq "xmlunit-core@1.0.0") { |
| 105 | + $_.Attributes["bom-ref"].Value = "pkg:nuget/XMLUnit.Core@$Version" |
| 106 | + $_.name = "XMLUnit.Core" |
| 107 | + $_.version = "$Version" |
| 108 | + } |
| 109 | + } |
| 110 | + $body.Save($fullFileName) |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +function Generate-SBoms { |
| 116 | + param( |
| 117 | + [Parameter(Mandatory = $true)] |
| 118 | + [string] |
| 119 | + $Version, |
| 120 | + |
| 121 | + [Parameter(Mandatory = $true)] |
| 122 | + [string] |
| 123 | + $Path, |
| 124 | + |
| 125 | + [Parameter(Mandatory = $true)] |
| 126 | + [string] |
| 127 | + $Name, |
| 128 | + |
| 129 | + [switch] |
| 130 | + $PostProcess |
| 131 | + ) |
| 132 | + |
| 133 | + Generate-SBom -Version $Version -Path $Path -Name $Name -PostProcess:$PostProcess |
| 134 | + Generate-SBom -Version $Version -Path $Path -Name $Name -Json -PostProcess:$PostProcess |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +function Generate-NUnit2-Constraints-SBom { |
| 139 | + param( |
| 140 | + [Parameter(Mandatory = $true)] |
| 141 | + [string] |
| 142 | + $Version, |
| 143 | + |
| 144 | + [switch] |
| 145 | + $Json |
| 146 | + ) |
| 147 | + |
| 148 | + $Name = "XMLUnit.NUnit2.Constraints" |
| 149 | + $targetDir = Join-Path -Path build -ChildPath cyclonedx |
| 150 | + if (-not(Test-Path $targetDir -PathType Container)) { |
| 151 | + New-Item -Path $targetDir -ItemType Directory |
| 152 | + } |
| 153 | + $targetDir = Resolve-Path -Path $targetDir |
| 154 | + $fileName = $JSon ? "$Name.cylconedx.json" : "$Name.cylconedx.xml" |
| 155 | + $fullFileName = Join-Path -Path $targetDir -ChildPath $fileName |
| 156 | + |
| 157 | + $Args = @( |
| 158 | + "./src/main/net-constraints-nunit2/NetFramework/$Name.NetFramework.csproj" |
| 159 | + "-o" |
| 160 | + "$targetDir" |
| 161 | + "-sv" |
| 162 | + "$Version" |
| 163 | + "-st" |
| 164 | + "library" |
| 165 | + "-sn" |
| 166 | + "$Name" |
| 167 | + "--set-nuget-purl" |
| 168 | + "-imp" |
| 169 | + "./src/shared/cyclonedx-metadata.xml" |
| 170 | + "-fn" |
| 171 | + "$fileName" |
| 172 | + ) |
| 173 | + |
| 174 | + Write-Output "Generating $fullFileName" |
| 175 | + if ($Json) { |
| 176 | + dotnet-CycloneDX @Args -j |
| 177 | + } else { |
| 178 | + dotnet-CycloneDX @Args |
| 179 | + } |
| 180 | + |
| 181 | + Write-Output "Post-Processing $fullFileName" |
| 182 | + if ($Json) { |
| 183 | + $body = Get-Content $fullFileName -Raw | ConvertFrom-Json -Depth 32 |
| 184 | + $coreReference =@" |
| 185 | + { |
| 186 | + "ref": "pkg:nuget/XMLUnit.Core@$Version" |
| 187 | + } |
| 188 | +"@ |
| 189 | + $body.dependencies += (ConvertFrom-Json -InputObject $coreReference) |
| 190 | + $coreComponent =@" |
| 191 | + { |
| 192 | + "type": "library", |
| 193 | + "bom-ref": "pkg:nuget/XMLUnit.Core@$Version", |
| 194 | + "name": "XMLUnit.Core", |
| 195 | + "version": "$Version", |
| 196 | + "scope": "required" |
| 197 | + } |
| 198 | +"@ |
| 199 | + $body.components += (ConvertFrom-Json -InputObject $coreComponent) |
| 200 | + $body.dependencies | |
| 201 | + ForEach-Object { |
| 202 | + if ($_.ref -eq "pkg:nuget/XMLUnit.NUnit2.Constraints@$Version") { |
| 203 | + $_.dependsOn += "pkg:nuget/XMLUnit.Core@$Version" |
| 204 | + } |
| 205 | + } |
| 206 | + $body | ConvertTo-Json -Depth 32 | Set-Content $fullFileName |
| 207 | + } else { |
| 208 | + $body = [xml](Get-Content $fullFileName) |
| 209 | + $coreReference = $body.CreateElement("dependency", $body.bom.NamespaceURI) |
| 210 | + $coreReference.SetAttribute("ref", "pkg:nuget/XMLUnit.Core@$Version") |
| 211 | + $body.bom.dependencies.AppendChild($coreReference) |
| 212 | + $coreComponent = $body.CreateElement("component", $body.bom.NamespaceURI) |
| 213 | + $coreComponent.SetAttribute("type", "library") |
| 214 | + $coreComponent.SetAttribute("bom-ref", "pkg:nuget/XMLUnit.Core@$Version") |
| 215 | + $name = $body.CreateElement("name", $body.bom.NamespaceURI) |
| 216 | + $name.InnerText = "XMLUnit.Core" |
| 217 | + $coreComponent.AppendChild($name) |
| 218 | + $versionElement = $body.CreateElement("version", $body.bom.NamespaceURI) |
| 219 | + $versionElement.InnerText = "$Version" |
| 220 | + $coreComponent.AppendChild($versionElement) |
| 221 | + $scope = $body.CreateElement("scope", $body.bom.NamespaceURI) |
| 222 | + $scope.InnerText = "required" |
| 223 | + $coreComponent.AppendChild($scope) |
| 224 | + $body.bom.components.AppendChild($coreComponent) |
| 225 | + $body.bom.dependencies.dependency | |
| 226 | + ForEach-Object { |
| 227 | + if ($_.HasAttribute("ref") -and $_.Attributes["ref"].Value -eq "pkg:nuget/XMLUnit.NUnit2.Constraints@$Version") { |
| 228 | + $_.AppendChild($coreReference.Clone()) |
| 229 | + } |
| 230 | + } |
| 231 | + $body.Save($fullFileName) |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +Generate-SBoms -Path net-core -Name XMLUnit.Core -Version $Version |
| 236 | +Generate-SBoms -Path net-constraints-nunit3 -Name XMLUnit.NUnit3.Constraints -Version $Version -PostProcess |
| 237 | +Generate-SBoms -Path net-constraints-nunit4 -Name XMLUnit.NUnit4.Constraints -Version $Version -PostProcess |
| 238 | +Generate-SBoms -Path net-placeholders -Name XMLUnit.Placeholders -Version $Version -PostProcess |
| 239 | +Generate-NUnit2-Constraints-SBom -Version $Version |
| 240 | +Generate-NUnit2-Constraints-SBom -Version $Version -JSon |
0 commit comments