CodeCheck.ps1 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #requires -version 5
  2. <#
  3. .SYNOPSIS
  4. This script runs a quick check for common errors, such as checking that Visual Studio solutions are up to date or that generated code has been committed to source.
  5. #>
  6. param(
  7. [switch]$ci
  8. )
  9. $ErrorActionPreference = 'Stop'
  10. Set-StrictMode -Version 1
  11. Import-Module -Scope Local -Force "$PSScriptRoot/common.psm1"
  12. $repoRoot = Resolve-Path "$PSScriptRoot/../.."
  13. [string[]] $errors = @()
  14. function LogError {
  15. param(
  16. [Parameter(Mandatory = $true, Position = 0)]
  17. [string]$message,
  18. [string]$FilePath,
  19. [string]$Code
  20. )
  21. if ($env:TF_BUILD) {
  22. $prefix = "##vso[task.logissue type=error"
  23. if ($FilePath) {
  24. $prefix = "${prefix};sourcepath=$FilePath"
  25. }
  26. if ($Code) {
  27. $prefix = "${prefix};code=$Code"
  28. }
  29. Write-Host "${prefix}]${message}"
  30. }
  31. $fullMessage = "error ${Code}: $message"
  32. if ($FilePath) {
  33. $fullMessage += " [$FilePath]"
  34. }
  35. Write-Host -f Red $fullMessage
  36. $script:errors += $fullMessage
  37. }
  38. try {
  39. if ($ci) {
  40. # Install dotnet.exe
  41. & $repoRoot/restore.cmd -ci -NoBuildNodeJS
  42. }
  43. . "$repoRoot/activate.ps1"
  44. #
  45. # Duplicate .csproj files can cause issues with a shared build output folder
  46. #
  47. $projectFileNames = New-Object 'System.Collections.Generic.HashSet[string]'
  48. # Ignore duplicates in submodules. These should be isolated from the rest of the build.
  49. # Ignore duplicates in the .ref folder. This is expected.
  50. Get-ChildItem -Recurse "$repoRoot/src/*.*proj" `
  51. | ? { $_.FullName -notmatch 'submodules' -and $_.FullName -notmatch 'node_modules' } `
  52. | ? { (Split-Path -Leaf (Split-Path -Parent $_)) -ne 'ref' } `
  53. | % {
  54. $fileName = [io.path]::GetFileNameWithoutExtension($_)
  55. if (-not ($projectFileNames.Add($fileName))) {
  56. LogError -code 'BUILD003' -filepath $_ `
  57. "Multiple project files named '$fileName' exist. Project files should have a unique name to avoid conflicts in build output."
  58. }
  59. }
  60. #
  61. # Versions.props and Version.Details.xml
  62. #
  63. Write-Host "Checking that Versions.props and Version.Details.xml match"
  64. [xml] $versionProps = Get-Content "$repoRoot/eng/Versions.props"
  65. [xml] $versionDetails = Get-Content "$repoRoot/eng/Version.Details.xml"
  66. $globalJson = Get-Content $repoRoot/global.json | ConvertFrom-Json
  67. $versionVars = New-Object 'System.Collections.Generic.HashSet[string]'
  68. foreach ($vars in $versionProps.SelectNodes("//PropertyGroup[`@Label=`"Automated`"]/*")) {
  69. $versionVars.Add($vars.Name) | Out-Null
  70. }
  71. foreach ($dep in $versionDetails.SelectNodes('//Dependency')) {
  72. Write-Verbose "Found $dep"
  73. $expectedVersion = $dep.Version
  74. if ($dep.Name -in $globalJson.'msbuild-sdks'.PSObject.Properties.Name) {
  75. $actualVersion = $globalJson.'msbuild-sdks'.($dep.Name)
  76. if ($expectedVersion -ne $actualVersion) {
  77. LogError `
  78. "MSBuild SDK version '$($dep.Name)' in global.json does not match the value in Version.Details.xml. Expected '$expectedVersion', actual '$actualVersion'" `
  79. -filepath "$repoRoot\global.json"
  80. }
  81. }
  82. else {
  83. $varName = $dep.Name -replace '\.',''
  84. $varName = $varName -replace '\-',''
  85. $varName = "${varName}PackageVersion"
  86. $versionVar = $versionProps.SelectSingleNode("//PropertyGroup[`@Label=`"Automated`"]/$varName")
  87. $actualVersion = $versionVar.InnerText
  88. $versionVars.Remove($varName) | Out-Null
  89. if (-not $versionVar) {
  90. LogError "Missing version variable '$varName' in the 'Automated' property group in $repoRoot/eng/Versions.props"
  91. continue
  92. }
  93. if ($expectedVersion -ne $actualVersion) {
  94. LogError `
  95. "Version variable '$varName' does not match the value in Version.Details.xml. Expected '$expectedVersion', actual '$actualVersion'" `
  96. -filepath "$repoRoot\eng\Versions.props"
  97. }
  98. }
  99. }
  100. foreach ($unexpectedVar in $versionVars) {
  101. LogError `
  102. "Version variable '$unexpectedVar' does not have a matching entry in Version.Details.xml. See https://github.com/aspnet/AspNetCore/blob/master/docs/ReferenceResolution.md for instructions on how to add a new dependency." `
  103. -filepath "$repoRoot\eng\Versions.props"
  104. }
  105. Write-Host "Checking that solutions are up to date"
  106. Get-ChildItem "$repoRoot/*.sln" -Recurse `
  107. | ? {
  108. # These .sln files are used by the templating engine.
  109. ($_.Name -ne "BlazorServerWeb_CSharp.sln")
  110. } `
  111. | % {
  112. Write-Host " Checking $(Split-Path -Leaf $_)"
  113. $slnDir = Split-Path -Parent $_
  114. $sln = $_
  115. & dotnet sln $_ list `
  116. | ? { $_ -like '*proj' } `
  117. | % {
  118. $proj = Join-Path $slnDir $_
  119. if (-not (Test-Path $proj)) {
  120. LogError "Missing project. Solution references a project which does not exist: $proj. [$sln] "
  121. }
  122. }
  123. }
  124. #
  125. # Generated code check
  126. #
  127. Write-Host "Re-running code generation"
  128. Write-Host "Re-generating project lists"
  129. Invoke-Block {
  130. & $PSScriptRoot\GenerateProjectList.ps1 -ci:$ci
  131. }
  132. Write-Host "Re-generating references assemblies"
  133. Invoke-Block {
  134. & $PSScriptRoot\GenerateReferenceAssemblies.ps1 -ci:$ci
  135. }
  136. # Temporarily disable package baseline generation while we stage for publishing
  137. # Write-Host "Re-generating package baselines"
  138. # Invoke-Block {
  139. # & dotnet run -p "$repoRoot/eng/tools/BaselineGenerator/"
  140. # }
  141. Write-Host "Run git diff to check for pending changes"
  142. # Redirect stderr to stdout because PowerShell does not consistently handle output to stderr
  143. $changedFiles = & cmd /c 'git --no-pager diff --ignore-space-at-eol --name-only 2>nul'
  144. # Temporary: Disable check for blazor js file
  145. $changedFilesExclusion = "src/Components/Web.JS/dist/Release/blazor.server.js"
  146. if ($changedFiles) {
  147. foreach ($file in $changedFiles) {
  148. if ($file -eq $changedFilesExclusion) {continue}
  149. $filePath = Resolve-Path "${repoRoot}/${file}"
  150. LogError "Generated code is not up to date in $file. You might need to regenerate the reference assemblies or project list (see docs/ReferenceAssemblies.md and docs/ReferenceResolution.md)" -filepath $filePath
  151. & git --no-pager diff --ignore-space-at-eol $filePath
  152. }
  153. }
  154. }
  155. finally {
  156. Write-Host ""
  157. Write-Host "Summary:"
  158. Write-Host ""
  159. Write-Host " $($errors.Length) error(s)"
  160. Write-Host ""
  161. foreach ($err in $errors) {
  162. Write-Host -f Red $err
  163. }
  164. if ($errors) {
  165. exit 1
  166. }
  167. }