|
1 | 1 | # Copyright (c) Microsoft Corporation. |
2 | 2 | # Licensed under the MIT License. |
3 | 3 |
|
| 4 | +# This script is used to completely rebuild the cgmanifgest.json file, |
| 5 | +# which is used to generate the notice file. |
4 | 6 | # Requires the module dotnet.project.assets from the PowerShell Gallery authored by @TravisEz13 |
5 | 7 |
|
6 | | -import-module dotnet.project.assets |
| 8 | +Import-Module dotnet.project.assets |
| 9 | +. "$PSScriptRoot\..\tools\buildCommon\startNativeExecution.ps1" |
| 10 | + |
| 11 | +$existingRegistrationTable = @{} |
| 12 | +$existingRegistrationsJson = Get-Content $PSScriptRoot\..\cgmanifest.json | ConvertFrom-Json -AsHashtable |
| 13 | +$existingRegistrationsJson.Registrations | ForEach-Object { |
| 14 | + $registration = [Registration]$_ |
| 15 | + if($registration.Component) { |
| 16 | + $name = $registration.Component.Name() |
| 17 | + $existingRegistrationTable.Add($name, $registration) |
| 18 | + } |
| 19 | +} |
| 20 | + |
7 | 21 | Class Registration { |
8 | 22 | [Component]$Component |
9 | 23 | [bool]$DevelopmentDependency |
@@ -79,58 +93,128 @@ function New-NugetComponent { |
79 | 93 | return $registration |
80 | 94 | } |
81 | 95 |
|
82 | | -$existingRegistrationTable = @{} |
83 | | -$newRegistrations = @() |
84 | | -$existingRegistrationsJson = Get-Content $PSScriptRoot\..\cgmanifest.json | ConvertFrom-Json -AsHashtable |
85 | | -$existingRegistrationsJson.Registrations | ForEach-Object { |
86 | | - $registration = [Registration]$_ |
87 | | - $existingRegistrationTable.Add($registration.Component.Name(), $registration) |
88 | | - $newRegistrations += $registration |
| 96 | +$winDesktopSdk = 'Microsoft.NET.Sdk.WindowsDesktop' |
| 97 | +if (!$IsWindows) { |
| 98 | + $winDesktopSdk = 'Microsoft.NET.Sdk' |
| 99 | + Write-Warning "Always using $winDesktopSdk since this is not windows!!!" |
89 | 100 | } |
90 | 101 |
|
91 | | -Get-PSDrive -Name pwsh-win-core -ErrorAction Ignore | Remove-PSDrive |
92 | | -Push-Location $PSScriptRoot\..\src\powershell-win-core |
93 | | -$null = dotnet restore |
94 | | -$null = New-PADrive -Path $PSScriptRoot\..\src\powershell-win-core\obj\project.assets.json -Name pwsh-win-core |
95 | | -$targets = Get-ChildItem -Path 'pwsh-win-core:/targets/net6.0-windows7.0|win7-x64' | Where-Object { |
96 | | - $_.Type -eq 'package' -and |
97 | | - $_.Name -notlike 'DotNetAnalyzers.DocumentationAnalyzers*' -and |
98 | | - $_.Name -notlike 'StyleCop*' -and |
99 | | - $_.Name -notlike 'Microsoft.CodeAnalysis.Analyzers*' -and |
100 | | - $_.Name -notlike 'Microsoft.CodeAnalysis.NetAnalyzers*' |
101 | | -} | select-object -ExpandProperty name |
102 | | -Pop-Location |
103 | | -Get-PSDrive -Name pwsh-win-core | Remove-PSDrive |
104 | | - |
105 | | -$updateRegistrations = @() |
106 | | -$targets | ForEach-Object { |
107 | | - $target = $_ |
108 | | - $parts = ($target -split '\|') |
109 | | - $name = $parts[0] |
110 | | - $targetVersion = $parts[1] |
111 | | - $pattern = [regex]::Escape($name) + " " |
112 | | - $tpnMatch = Select-String -Path $PSScriptRoot\..\ThirdPartyNotices.txt -Pattern $pattern |
113 | | - if (!$tpnMatch) { |
114 | | - if ($existingRegistrationTable.ContainsKey($name)) { |
115 | | - $registrationVersion = $existingRegistrationTable.$name.Component.Version() |
116 | | - if ($registrationVersion -ne $targetVersion) { |
117 | | - $registration = New-NugetComponent -Name $name -Version $targetVersion |
118 | | - $updateRegistrations += $registration |
119 | | - } else { |
120 | | - Write-Verbose "$target already registered: $registrationVersion" -Verbose |
121 | | - } |
122 | | - } else { |
| 102 | +Function Get-CGRegistrations { |
| 103 | + param( |
| 104 | + [Parameter(Mandatory)] |
| 105 | + [ValidateSet( |
| 106 | + "alpine-x64", |
| 107 | + "linux-arm", |
| 108 | + "linux-arm64", |
| 109 | + "linux-x64", |
| 110 | + "osx-arm64", |
| 111 | + "osx-x64", |
| 112 | + "win-arm", |
| 113 | + "win-arm64", |
| 114 | + "win7-x64", |
| 115 | + "win7-x86", |
| 116 | + "modules")] |
| 117 | + [string]$Runtime, |
| 118 | + |
| 119 | + [Parameter(Mandatory)] |
| 120 | + [System.Collections.Generic.Dictionary[string, Registration]] $RegistrationTable |
| 121 | + ) |
| 122 | + |
| 123 | + $newRegistrations = $Registrations |
| 124 | + |
| 125 | + $dotnetTargetName = 'net6.0' |
| 126 | + $dotnetTargetNameWin7 = 'net6.0-windows7.0' |
| 127 | + $unixProjectName = 'powershell-unix' |
| 128 | + $windowsProjectName = 'powershell-win-core' |
| 129 | + $actualRuntime = $Runtime |
| 130 | + |
| 131 | + switch -regex ($Runtime) { |
| 132 | + "alpine-.*" { |
| 133 | + $folder = $unixProjectName |
| 134 | + $target = "$dotnetTargetName|$Runtime" |
| 135 | + } |
| 136 | + "linux-.*" { |
| 137 | + $folder = $unixProjectName |
| 138 | + $target = "$dotnetTargetName|$Runtime" |
| 139 | + } |
| 140 | + "osx-.*" { |
| 141 | + $folder = $unixProjectName |
| 142 | + $target = "$dotnetTargetName|$Runtime" |
| 143 | + } |
| 144 | + "win7-.*" { |
| 145 | + $sdkToUse = $winDesktopSdk |
| 146 | + $folder = $windowsProjectName |
| 147 | + $target = "$dotnetTargetNameWin7|$Runtime" |
| 148 | + } |
| 149 | + "win-.*" { |
| 150 | + $folder = $windowsProjectName |
| 151 | + $target = "$dotnetTargetNameWin7|$Runtime" |
| 152 | + } |
| 153 | + "modules" { |
| 154 | + $folder = "modules" |
| 155 | + $actualRuntime = 'linux-x64' |
| 156 | + $target = "$dotnetTargetName|$actualRuntime" |
| 157 | + } |
| 158 | + Default { |
| 159 | + throw "Invalid runtime name: $Runtime" |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + Write-Verbose "Getting registrations for $folder - $actualRuntime ..." -Verbose |
| 164 | + Get-PSDrive -Name $folder -ErrorAction Ignore | Remove-PSDrive |
| 165 | + Push-Location $PSScriptRoot\..\src\$folder |
| 166 | + try { |
| 167 | + Start-NativeExecution -VerboseOutputOnError -sb { |
| 168 | + dotnet restore --runtime $actualRuntime "/property:SDKToUse=$sdkToUse" |
| 169 | + } |
| 170 | + $null = New-PADrive -Path $PSScriptRoot\..\src\$folder\obj\project.assets.json -Name $folder |
| 171 | + try { |
| 172 | + $targets = Get-ChildItem -Path "${folder}:/targets/$target" -ErrorAction Stop | Where-Object { |
| 173 | + $_.Type -eq 'package' -and |
| 174 | + $_.Name -notlike 'DotNetAnalyzers.DocumentationAnalyzers*' -and |
| 175 | + $_.Name -notlike 'StyleCop*' -and |
| 176 | + $_.Name -notlike 'Microsoft.CodeAnalysis.Analyzers*' -and |
| 177 | + $_.Name -notlike 'Microsoft.CodeAnalysis.NetAnalyzers*' |
| 178 | + } | select-object -ExpandProperty name |
| 179 | + } catch { |
| 180 | + Get-ChildItem -Path "${folder}:/targets" | Out-String | Write-Verbose -Verbose |
| 181 | + throw |
| 182 | + } |
| 183 | + } finally { |
| 184 | + Pop-Location |
| 185 | + Get-PSDrive -Name $folder -ErrorAction Ignore | Remove-PSDrive |
| 186 | + } |
| 187 | + |
| 188 | + $targets | ForEach-Object { |
| 189 | + $target = $_ |
| 190 | + $parts = ($target -split '\|') |
| 191 | + $name = $parts[0] |
| 192 | + $targetVersion = $parts[1] |
| 193 | + $pattern = [regex]::Escape($name) + " " |
| 194 | + $tpnMatch = Select-String -Path $PSScriptRoot\..\ThirdPartyNotices.txt -Pattern $pattern |
| 195 | + |
| 196 | + # Add the registration to the cgmanifest if the TPN does not contain the name of the target OR |
| 197 | + # the exisitng CG contains the registration, because if the existing CG contains the registration, |
| 198 | + # that might be the only reason it is in the TPN. |
| 199 | + if ((!$tpnMatch -or $existingRegistrationTable.ContainsKey($name)) -and !$RegistrationTable.ContainsKey($target)) { |
123 | 200 | $registration = New-NugetComponent -Name $name -Version $targetVersion |
124 | | - $newRegistrations += $registration |
| 201 | + $RegistrationTable.Add($target, $registration) |
125 | 202 | } |
126 | 203 | } |
127 | 204 | } |
128 | 205 |
|
129 | | -if ($updateRegistrations.count -gt 0) { |
130 | | - #TODO delete existing and add new registration |
131 | | - throw "updating registrations is not implemented" |
| 206 | +$registrations = [System.Collections.Generic.Dictionary[string, Registration]]::new() |
| 207 | +$lastCount = 0 |
| 208 | +foreach ($runtime in "win7-x64", "linux-x64", "osx-x64", "alpine-x64", "win-arm", "linux-arm", "linux-arm64", "osx-arm64", "win-arm64", "win7-x86") { |
| 209 | + Get-CGRegistrations -Runtime $runtime -RegistrationTable $registrations |
| 210 | + $count = $registrations.Count |
| 211 | + $newCount = $count - $lastCount |
| 212 | + $lastCount = $count |
| 213 | + Write-Verbose "$newCount new registrations, $count total..." -Verbose |
132 | 214 | } |
133 | 215 |
|
134 | | -$newCount = $newRegistrations.count - $existingRegistrationTable.count |
| 216 | +$newRegistrations = $registrations.Keys | Sort-Object | ForEach-Object { $registrations[$_] } |
| 217 | + |
| 218 | +$count = $newRegistrations.Count |
135 | 219 | @{Registrations = $newRegistrations } | ConvertTo-Json -depth 99 | Set-Content $PSScriptRoot\..\cgmanifest.json |
136 | | -Write-Verbose "$newCount registrations added" -Verbose |
| 220 | +Write-Verbose "$count registrations created!" -Verbose |
0 commit comments