diff --git a/pipelines/azure_pipelines.yml b/pipelines/azure_pipelines.yml index 098419ee9..8e9912632 100644 --- a/pipelines/azure_pipelines.yml +++ b/pipelines/azure_pipelines.yml @@ -57,6 +57,11 @@ extends: inputs: version: 8.x includePreviewVersions: true + - task: PowerShell@2 + displayName: Validate OData Package Versions + inputs: + targetType: 'inline' + script: 'powershell.exe -executionpolicy remotesigned -File $(Build.SourcesDirectory)\tool\ValidateODataPackageVersions.ps1' - task: DotNetCoreCLI@2 displayName: build Microsoft.AspNetCore.OData inputs: diff --git a/tool/ValidateODataPackageVersions.ps1 b/tool/ValidateODataPackageVersions.ps1 new file mode 100644 index 000000000..a0e6a35ce --- /dev/null +++ b/tool/ValidateODataPackageVersions.ps1 @@ -0,0 +1,97 @@ +<# +.SYNOPSIS +Validates the package versions in the .csproj file against the lower bound of the ODataLibPackageDependency version range in the builder.versions.settings.targets. + +.PARAMETER builderVersionTargetsPath +Specifies the path to the builder.versions.settings.targets file. + +.PARAMETER csprojPath +Specifies the path to the .csproj file. +#> + +Param( + [string] + $builderVersionTargetsPath, + [string] + $csprojPath +) + +# Constants +$ODATA_LIB_PACKAGE_DEPENDENCY_KEY = "ODataLibPackageDependency" +$ODATA_MODEL_BUILDER_PACKAGE_DEPENDENCY_KEY = "ODataModelBuilderPackageDependency" +$BUILDER_VERSION_TARGETS_FILENAME = "builder.versions.settings.targets" +$CSPROJ_FILENAME = "Microsoft.AspNetCore.OData.csproj" + +# Path to your builder.versions.settings.targets file +if ($builderVersionTargetsPath -eq "") { + $builderVersionTargetsPath = "$PSScriptRoot\$BUILDER_VERSION_TARGETS_FILENAME" +} + +# Path to your .csproj file +if ($csprojPath -eq "") { + $csprojPath = Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -ChildPath "src/Microsoft.AspNetCore.OData/$CSPROJ_FILENAME" +} + +# Function to extract the OData package dependency version range from the targets file +function Get-ODataPackageDependencyVersion { + param ( + [xml]$targets, + [string]$dependencyName + ) + $dependencyVersionRange = [string]$targets.Project.PropertyGroup.$dependencyName + $dependencyVersionRange = $dependencyVersionRange.Trim() + $dependencyVersionRange = $dependencyVersionRange -replace "\s", "" + + # Extract the lower bound version from the version range + $lowerBoundVersion = $dependencyVersionRange -replace "\[([^\]]+),.*", '$1' + return $lowerBoundVersion +} + +# Dictionary to store the OData package dependencies +$odataPackageDependenciesDict = [System.Collections.Generic.Dictionary[String,String]]::new() + +# Load the targets file +[xml]$targets = Get-Content $builderVersionTargetsPath + +# Extract the ODataLibPackageDependency version range +$lowerBoundVersion = Get-ODataPackageDependencyVersion -targets $targets -dependencyName $ODATA_LIB_PACKAGE_DEPENDENCY_KEY +$odataPackageDependenciesDict.Add($ODATA_LIB_PACKAGE_DEPENDENCY_KEY, $lowerBoundVersion) + +# Extract the ODataModelBuilderPackageDependency version range +$lowerBoundVersion = Get-ODataPackageDependencyVersion -targets $targets -dependencyName $ODATA_MODEL_BUILDER_PACKAGE_DEPENDENCY_KEY +$odataPackageDependenciesDict.Add($ODATA_MODEL_BUILDER_PACKAGE_DEPENDENCY_KEY, $lowerBoundVersion) + +# Extract the lower bound version from the version range +$lowerBoundVersion = $odataPackageDependenciesDict[$ODATA_LIB_PACKAGE_DEPENDENCY_KEY] + +# Load the .csproj file +[xml]$csproj = Get-Content $csprojPath + +# Extract the PackageReference versions +$PackageReference = $csproj.Project.ItemGroup.PackageReference + +# Dictionary to store the PackageReferences +$csprojPackageReferencesDict = @{ + $ODATA_LIB_PACKAGE_DEPENDENCY_KEY = "Microsoft.OData.Core|Microsoft.OData.Client|Microsoft.OData.Edm|Microsoft.Spatial" + $ODATA_MODEL_BUILDER_PACKAGE_DEPENDENCY_KEY = "Microsoft.OData.ModelBuilder" +} + + +foreach ($key in $odataPackageDependenciesDict.Keys) { + $lowerBoundVersion = $odataPackageDependenciesDict[$key] + $packageReferences = $PackageReference | Where-Object { $_.Include -match $csprojPackageReferencesDict[$key] } + $packageVersions = $packageReferences.Version + + Write-Host "Validating the package versions of '$key' in '$CSPROJ_FILENAME' against the lower bound of the '$key' version range in '$BUILDER_VERSION_TARGETS_FILENAME'." + + # Validate the versions + foreach ($version in $packageVersions) { + if ($version -ne $lowerBoundVersion) { + $exception = New-Object System.Exception( + "Error.VersionMismatch: '$key' version '$version' in '$csprojPath' do not match the lower bound '$lowerBoundVersion' of '$key' in '$builderVersionTargetsPath'.") + throw $exception + } + } + + Write-Host "Validation successful: Package versions match the lower bound of the $key version range." +} diff --git a/tool/builder.versions.settings.targets b/tool/builder.versions.settings.targets index 74387234c..b6ff151c5 100644 --- a/tool/builder.versions.settings.targets +++ b/tool/builder.versions.settings.targets @@ -3,13 +3,13 @@ 9 1 - 2 + 3 - [8.2.2, 9.0.0) + [8.2.3, 9.0.0) [2.0.0, 3.0.0)