2021-10-20 08:27:44 +08:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
2021-10-29 02:44:06 +08:00
|
|
|
# This script is used to completely rebuild the cgmanifgest.json file,
|
|
|
|
# which is used to generate the notice file.
|
2021-10-20 08:27:44 +08:00
|
|
|
# Requires the module dotnet.project.assets from the PowerShell Gallery authored by @TravisEz13
|
|
|
|
|
2021-10-30 07:29:40 +08:00
|
|
|
param(
|
2023-07-18 02:14:31 +08:00
|
|
|
[switch] $Fix,
|
|
|
|
[switch] $IsStable
|
2021-10-30 07:29:40 +08:00
|
|
|
)
|
|
|
|
|
2021-10-29 02:44:06 +08:00
|
|
|
Import-Module dotnet.project.assets
|
2021-10-30 07:29:40 +08:00
|
|
|
Import-Module "$PSScriptRoot\..\.github\workflows\GHWorkflowHelper" -Force
|
2021-10-29 02:44:06 +08:00
|
|
|
. "$PSScriptRoot\..\tools\buildCommon\startNativeExecution.ps1"
|
|
|
|
|
2021-12-21 03:26:27 +08:00
|
|
|
$packageSourceName = 'findMissingNoticesNugetOrg'
|
|
|
|
if (!(Get-PackageSource -Name $packageSourceName -ErrorAction SilentlyContinue)) {
|
|
|
|
$null = Register-PackageSource -Name $packageSourceName -Location https://www.nuget.org/api/v2 -ProviderName NuGet
|
|
|
|
}
|
|
|
|
|
2021-10-29 02:44:06 +08:00
|
|
|
$existingRegistrationTable = @{}
|
2021-12-18 03:28:14 +08:00
|
|
|
$cgManifestPath = (Resolve-Path -Path $PSScriptRoot\..\tools\cgmanifest.json).ProviderPath
|
|
|
|
$existingRegistrationsJson = Get-Content $cgManifestPath | ConvertFrom-Json -AsHashtable
|
2021-10-29 02:44:06 +08:00
|
|
|
$existingRegistrationsJson.Registrations | ForEach-Object {
|
|
|
|
$registration = [Registration]$_
|
2021-10-30 07:29:40 +08:00
|
|
|
if ($registration.Component) {
|
2021-10-29 02:44:06 +08:00
|
|
|
$name = $registration.Component.Name()
|
2024-11-01 04:13:04 +08:00
|
|
|
if (!$existingRegistrationTable.ContainsKey($name)) {
|
|
|
|
$existingRegistrationTable.Add($name, $registration)
|
|
|
|
}
|
2021-10-29 02:44:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 08:27:44 +08:00
|
|
|
Class Registration {
|
|
|
|
[Component]$Component
|
|
|
|
[bool]$DevelopmentDependency
|
|
|
|
}
|
|
|
|
|
|
|
|
Class Component {
|
|
|
|
[ValidateSet("nuget")]
|
|
|
|
[String] $Type
|
|
|
|
[Nuget]$Nuget
|
|
|
|
|
|
|
|
[string]ToString() {
|
|
|
|
$message = "Type: $($this.Type)"
|
|
|
|
if ($this.Type -eq "nuget") {
|
|
|
|
$message += "; $($this.Nuget)"
|
|
|
|
}
|
|
|
|
return $message
|
|
|
|
}
|
|
|
|
|
|
|
|
[string]Name() {
|
|
|
|
switch ($this.Type) {
|
|
|
|
"nuget" {
|
|
|
|
return $($this.Nuget.Name)
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
throw "Unknown component type: $($this.Type)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw "How did we get here?!?"
|
|
|
|
}
|
|
|
|
|
|
|
|
[string]Version() {
|
|
|
|
switch ($this.Type) {
|
|
|
|
"nuget" {
|
|
|
|
return $($this.Nuget.Version)
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
throw "Unknown component type: $($this.Type)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw "How did we get here?!?"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Class Nuget {
|
|
|
|
[string]$Name
|
|
|
|
[string]$Version
|
|
|
|
|
|
|
|
[string]ToString() {
|
|
|
|
return "$($this.Name) - $($this.Version)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 01:25:39 +08:00
|
|
|
$winDesktopSdk = 'Microsoft.NET.Sdk.WindowsDesktop'
|
|
|
|
if (!$IsWindows) {
|
|
|
|
$winDesktopSdk = 'Microsoft.NET.Sdk'
|
|
|
|
Write-Warning "Always using $winDesktopSdk since this is not windows!!!"
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:58:35 +08:00
|
|
|
function ConvertTo-SemVer {
|
|
|
|
param(
|
|
|
|
[String] $Version
|
|
|
|
)
|
|
|
|
|
|
|
|
[System.Management.Automation.SemanticVersion]$desiredVersion = [System.Management.Automation.SemanticVersion]::Empty
|
|
|
|
|
|
|
|
try {
|
|
|
|
$desiredVersion = $Version
|
|
|
|
} catch {
|
|
|
|
<#
|
|
|
|
Json.More.Net broke the rules and published 2.0.1.2 as 2.0.1.
|
|
|
|
So, I'm making the logic work for that scenario by
|
|
|
|
thorwing away any part that doesn't match non-pre-release semver portion
|
|
|
|
#>
|
2024-11-01 04:13:04 +08:00
|
|
|
$null = $Version -match '^(\d+\.\d+\.\d+).*'
|
2024-08-10 02:58:35 +08:00
|
|
|
$desiredVersion = $matches[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return $desiredVersion
|
|
|
|
}
|
|
|
|
|
2021-10-20 08:27:44 +08:00
|
|
|
function New-NugetComponent {
|
|
|
|
param(
|
|
|
|
[string]$name,
|
2021-10-30 07:29:40 +08:00
|
|
|
[string]$version,
|
|
|
|
[switch]$DevelopmentDependency
|
2021-10-20 08:27:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
$nuget = [Nuget]@{
|
|
|
|
Name = $name
|
|
|
|
Version = $version
|
|
|
|
}
|
|
|
|
$Component = [Component]@{
|
|
|
|
Type = "nuget"
|
|
|
|
Nuget = $nuget
|
|
|
|
}
|
|
|
|
|
|
|
|
$registration = [Registration]@{
|
|
|
|
Component = $Component
|
2021-10-30 07:29:40 +08:00
|
|
|
DevelopmentDependency = $DevelopmentDependency
|
2021-10-20 08:27:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $registration
|
|
|
|
}
|
|
|
|
|
2021-11-02 01:25:39 +08:00
|
|
|
$nugetPublicVersionCache = [System.Collections.Generic.Dictionary[string, string]]::new()
|
|
|
|
function Get-NuGetPublicVersion {
|
|
|
|
param(
|
2021-12-21 03:26:27 +08:00
|
|
|
[parameter(Mandatory)]
|
2021-11-02 01:25:39 +08:00
|
|
|
[string]$Name,
|
2021-12-21 03:26:27 +08:00
|
|
|
[parameter(Mandatory)]
|
2021-11-02 01:25:39 +08:00
|
|
|
[string]$Version
|
|
|
|
)
|
|
|
|
|
|
|
|
if($nugetPublicVersionCache.ContainsKey($Name)) {
|
|
|
|
return $nugetPublicVersionCache[$Name]
|
|
|
|
}
|
|
|
|
|
2024-08-10 02:58:35 +08:00
|
|
|
[System.Management.Automation.SemanticVersion]$desiredVersion = ConvertTo-SemVer -Version $Version
|
2021-11-02 01:25:39 +08:00
|
|
|
|
|
|
|
$publicVersion = $null
|
2021-12-21 03:26:27 +08:00
|
|
|
$publicVersion = Find-Package -Name $Name -AllowPrereleaseVersions -source $packageSourceName -AllVersions -ErrorAction SilentlyContinue | ForEach-Object {
|
2024-08-10 02:58:35 +08:00
|
|
|
[System.Management.Automation.SemanticVersion]$packageVersion = ConvertTo-SemVer -Version $_.Version
|
2021-11-02 01:25:39 +08:00
|
|
|
$_ | Add-Member -Name SemVer -MemberType NoteProperty -Value $packageVersion -PassThru
|
2024-08-10 02:58:35 +08:00
|
|
|
} | Where-Object {
|
|
|
|
$_.SemVer -le $desiredVersion
|
|
|
|
} | Sort-Object -Property semver -Descending | Select-Object -First 1 -ExpandProperty Version
|
2021-11-02 01:25:39 +08:00
|
|
|
|
|
|
|
if(!$publicVersion) {
|
|
|
|
Write-Warning "No public version found for $Name, using $Version"
|
|
|
|
$publicVersion = $Version
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$nugetPublicVersionCache.ContainsKey($Name)) {
|
|
|
|
$nugetPublicVersionCache.Add($Name, $publicVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
return $publicVersion
|
2021-10-20 08:27:44 +08:00
|
|
|
}
|
|
|
|
|
2021-11-02 01:25:39 +08:00
|
|
|
function Get-CGRegistrations {
|
2021-10-29 02:44:06 +08:00
|
|
|
param(
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
[ValidateSet(
|
2023-09-26 00:28:14 +08:00
|
|
|
"linux-musl-x64",
|
2021-10-29 02:44:06 +08:00
|
|
|
"linux-arm",
|
|
|
|
"linux-arm64",
|
|
|
|
"linux-x64",
|
|
|
|
"osx-arm64",
|
|
|
|
"osx-x64",
|
|
|
|
"win-arm64",
|
2023-09-26 08:12:08 +08:00
|
|
|
"win-x64",
|
|
|
|
"win-x86",
|
2021-10-29 02:44:06 +08:00
|
|
|
"modules")]
|
|
|
|
[string]$Runtime,
|
|
|
|
|
|
|
|
[Parameter(Mandatory)]
|
|
|
|
[System.Collections.Generic.Dictionary[string, Registration]] $RegistrationTable
|
|
|
|
)
|
|
|
|
|
2021-10-30 07:29:40 +08:00
|
|
|
$registrationChanged = $false
|
2021-10-29 02:44:06 +08:00
|
|
|
|
2024-02-16 11:15:11 +08:00
|
|
|
$dotnetTargetName = 'net9.0'
|
|
|
|
$dotnetTargetNameWin7 = 'net9.0-windows8.0'
|
2021-10-29 02:44:06 +08:00
|
|
|
$unixProjectName = 'powershell-unix'
|
|
|
|
$windowsProjectName = 'powershell-win-core'
|
|
|
|
$actualRuntime = $Runtime
|
|
|
|
|
|
|
|
switch -regex ($Runtime) {
|
|
|
|
"alpine-.*" {
|
|
|
|
$folder = $unixProjectName
|
|
|
|
$target = "$dotnetTargetName|$Runtime"
|
|
|
|
}
|
|
|
|
"linux-.*" {
|
|
|
|
$folder = $unixProjectName
|
|
|
|
$target = "$dotnetTargetName|$Runtime"
|
|
|
|
}
|
|
|
|
"osx-.*" {
|
|
|
|
$folder = $unixProjectName
|
|
|
|
$target = "$dotnetTargetName|$Runtime"
|
|
|
|
}
|
2023-09-26 08:12:08 +08:00
|
|
|
"win-x*" {
|
2021-10-29 02:44:06 +08:00
|
|
|
$sdkToUse = $winDesktopSdk
|
|
|
|
$folder = $windowsProjectName
|
|
|
|
$target = "$dotnetTargetNameWin7|$Runtime"
|
|
|
|
}
|
|
|
|
"win-.*" {
|
|
|
|
$folder = $windowsProjectName
|
|
|
|
$target = "$dotnetTargetNameWin7|$Runtime"
|
|
|
|
}
|
|
|
|
"modules" {
|
|
|
|
$folder = "modules"
|
|
|
|
$actualRuntime = 'linux-x64'
|
|
|
|
$target = "$dotnetTargetName|$actualRuntime"
|
|
|
|
}
|
|
|
|
Default {
|
|
|
|
throw "Invalid runtime name: $Runtime"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Write-Verbose "Getting registrations for $folder - $actualRuntime ..." -Verbose
|
|
|
|
Get-PSDrive -Name $folder -ErrorAction Ignore | Remove-PSDrive
|
|
|
|
Push-Location $PSScriptRoot\..\src\$folder
|
|
|
|
try {
|
|
|
|
Start-NativeExecution -VerboseOutputOnError -sb {
|
|
|
|
dotnet restore --runtime $actualRuntime "/property:SDKToUse=$sdkToUse"
|
|
|
|
}
|
|
|
|
$null = New-PADrive -Path $PSScriptRoot\..\src\$folder\obj\project.assets.json -Name $folder
|
|
|
|
try {
|
2021-10-30 07:29:40 +08:00
|
|
|
$targets = Get-ChildItem -Path "${folder}:/targets/$target" -ErrorAction Stop | Where-Object { $_.Type -eq 'package' } | select-object -ExpandProperty name
|
2021-10-29 02:44:06 +08:00
|
|
|
} catch {
|
|
|
|
Get-ChildItem -Path "${folder}:/targets" | Out-String | Write-Verbose -Verbose
|
|
|
|
throw
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
Pop-Location
|
|
|
|
Get-PSDrive -Name $folder -ErrorAction Ignore | Remove-PSDrive
|
|
|
|
}
|
|
|
|
|
|
|
|
$targets | ForEach-Object {
|
|
|
|
$target = $_
|
|
|
|
$parts = ($target -split '\|')
|
|
|
|
$name = $parts[0]
|
|
|
|
$targetVersion = $parts[1]
|
2021-11-02 01:25:39 +08:00
|
|
|
$publicVersion = Get-NuGetPublicVersion -Name $name -Version $targetVersion
|
2021-10-29 02:44:06 +08:00
|
|
|
|
|
|
|
# Add the registration to the cgmanifest if the TPN does not contain the name of the target OR
|
|
|
|
# the exisitng CG contains the registration, because if the existing CG contains the registration,
|
|
|
|
# that might be the only reason it is in the TPN.
|
2021-10-30 07:29:40 +08:00
|
|
|
if (!$RegistrationTable.ContainsKey($target)) {
|
|
|
|
$DevelopmentDependency = $false
|
2021-11-02 01:25:39 +08:00
|
|
|
if (!$existingRegistrationTable.ContainsKey($name) -or $existingRegistrationTable.$name.Component.Version() -ne $publicVersion) {
|
2021-10-30 07:29:40 +08:00
|
|
|
$registrationChanged = $true
|
|
|
|
}
|
|
|
|
if ($existingRegistrationTable.ContainsKey($name) -and $existingRegistrationTable.$name.DevelopmentDependency) {
|
|
|
|
$DevelopmentDependency = $true
|
|
|
|
}
|
|
|
|
|
2021-11-02 01:25:39 +08:00
|
|
|
$registration = New-NugetComponent -Name $name -Version $publicVersion -DevelopmentDependency:$DevelopmentDependency
|
2021-10-29 02:44:06 +08:00
|
|
|
$RegistrationTable.Add($target, $registration)
|
2021-10-20 08:27:44 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-30 07:29:40 +08:00
|
|
|
|
|
|
|
return $registrationChanged
|
2021-10-20 08:27:44 +08:00
|
|
|
}
|
|
|
|
|
2021-10-29 02:44:06 +08:00
|
|
|
$registrations = [System.Collections.Generic.Dictionary[string, Registration]]::new()
|
|
|
|
$lastCount = 0
|
2021-10-30 07:29:40 +08:00
|
|
|
$registrationChanged = $false
|
2023-09-26 08:12:08 +08:00
|
|
|
foreach ($runtime in "win-x64", "linux-x64", "osx-x64", "linux-musl-x64", "linux-arm", "linux-arm64", "osx-arm64", "win-arm64", "win-x86") {
|
2021-10-30 07:29:40 +08:00
|
|
|
$registrationChanged = (Get-CGRegistrations -Runtime $runtime -RegistrationTable $registrations) -or $registrationChanged
|
2021-10-29 02:44:06 +08:00
|
|
|
$count = $registrations.Count
|
|
|
|
$newCount = $count - $lastCount
|
|
|
|
$lastCount = $count
|
|
|
|
Write-Verbose "$newCount new registrations, $count total..." -Verbose
|
2021-10-20 08:27:44 +08:00
|
|
|
}
|
|
|
|
|
2021-10-29 02:44:06 +08:00
|
|
|
$newRegistrations = $registrations.Keys | Sort-Object | ForEach-Object { $registrations[$_] }
|
|
|
|
|
2023-07-18 02:14:31 +08:00
|
|
|
if ($IsStable) {
|
|
|
|
foreach ($registion in $newRegistrations) {
|
|
|
|
$name = $registion.Component.Name()
|
|
|
|
$version = $registion.Component.Version()
|
|
|
|
$developmentDependency = $registion.DevelopmentDependency
|
|
|
|
if ($version -match '-' -and !$developmentDependency) {
|
|
|
|
throw "Version $version of $name is preview. This is not allowed."
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 02:44:06 +08:00
|
|
|
$count = $newRegistrations.Count
|
2022-09-07 03:21:53 +08:00
|
|
|
$newJson = @{
|
2023-02-23 08:12:16 +08:00
|
|
|
Registrations = $newRegistrations
|
2022-09-07 03:21:53 +08:00
|
|
|
'$schema' = "https://json.schemastore.org/component-detection-manifest.json"
|
|
|
|
} | ConvertTo-Json -depth 99
|
|
|
|
|
2021-10-30 07:29:40 +08:00
|
|
|
if ($Fix -and $registrationChanged) {
|
|
|
|
$newJson | Set-Content $cgManifestPath
|
|
|
|
Set-GWVariable -Name CGMANIFEST_PATH -Value $cgManifestPath
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$Fix -and $registrationChanged) {
|
|
|
|
$temp = Get-GWTempPath
|
|
|
|
|
|
|
|
$tempJson = Join-Path -Path $temp -ChildPath "cgmanifest$((Get-Date).ToString('yyyMMddHHmm')).json"
|
|
|
|
$newJson | Set-Content $tempJson -Encoding utf8NoBOM
|
|
|
|
Set-GWVariable -Name CGMANIFEST_PATH -Value $tempJson
|
|
|
|
throw "cgmanifest is out of date. run ./tools/findMissingNotices.ps1 -Fix. Generated cgmanifest is here: $tempJson"
|
|
|
|
}
|
|
|
|
|
2021-10-29 02:44:06 +08:00
|
|
|
Write-Verbose "$count registrations created!" -Verbose
|