2
0

CodeCheck.ps1 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/build.ps1 -ci -norestore /t:InstallDotNet
  42. }
  43. #
  44. # Duplicate .csproj files can cause issues with a shared build output folder
  45. #
  46. $projectFileNames = New-Object 'System.Collections.Generic.HashSet[string]'
  47. # Ignore duplicates in submodules. These should be isolated from the rest of the build.
  48. # Ignore duplicates in the .ref folder. This is expected.
  49. Get-ChildItem -Recurse "$repoRoot/src/*.*proj" `
  50. | ? { $_.FullName -notmatch 'submodules' } `
  51. | ? { (Split-Path -Leaf (Split-Path -Parent $_)) -ne 'ref' } `
  52. | % {
  53. $fileName = [io.path]::GetFileNameWithoutExtension($_)
  54. if (-not ($projectFileNames.Add($fileName))) {
  55. LogError -code 'BUILD003' -filepath $_ `
  56. "Multiple project files named '$fileName' exist. Project files should have a unique name to avoid conflicts in build output."
  57. }
  58. }
  59. #
  60. # Versions.props and Version.Details.xml
  61. #
  62. Write-Host "Checking that Versions.props and Version.Details.xml match"
  63. [xml] $versionProps = Get-Content "$repoRoot/eng/Versions.props"
  64. [xml] $versionDetails = Get-Content "$repoRoot/eng/Version.Details.xml"
  65. $globalJson = Get-Content $repoRoot/global.json | ConvertFrom-Json
  66. $versionVars = New-Object 'System.Collections.Generic.HashSet[string]'
  67. foreach ($vars in $versionProps.SelectNodes("//PropertyGroup[`@Label=`"Automated`"]/*")) {
  68. $versionVars.Add($vars.Name) | Out-Null
  69. }
  70. foreach ($dep in $versionDetails.SelectNodes('//Dependency')) {
  71. Write-Verbose "Found $dep"
  72. $expectedVersion = $dep.Version
  73. if ($dep.Name -in $globalJson.'msbuild-sdks'.PSObject.Properties.Name) {
  74. $actualVersion = $globalJson.'msbuild-sdks'.($dep.Name)
  75. if ($expectedVersion -ne $actualVersion) {
  76. LogError `
  77. "MSBuild SDK version '$($dep.Name)' in global.json does not match the value in Version.Details.xml. Expected '$expectedVersion', actual '$actualVersion'" `
  78. -filepath "$repoRoot\global.json"
  79. }
  80. }
  81. else {
  82. $varName = $dep.Name -replace '\.',''
  83. $varName = $varName -replace '\-',''
  84. $varName = "${varName}PackageVersion"
  85. $versionVar = $versionProps.SelectSingleNode("//PropertyGroup[`@Label=`"Automated`"]/$varName")
  86. $actualVersion = $versionVar.InnerText
  87. $versionVars.Remove($varName) | Out-Null
  88. if (-not $versionVar) {
  89. LogError "Missing version variable '$varName' in the 'Automated' property group in $repoRoot/eng/Versions.props"
  90. continue
  91. }
  92. if ($expectedVersion -ne $actualVersion) {
  93. LogError `
  94. "Version variable '$varName' does not match the value in Version.Details.xml. Expected '$expectedVersion', actual '$actualVersion'" `
  95. -filepath "$repoRoot\eng\Versions.props"
  96. }
  97. }
  98. }
  99. foreach ($unexpectedVar in $versionVars) {
  100. LogError `
  101. "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." `
  102. -filepath "$repoRoot\eng\Versions.props"
  103. }
  104. #
  105. # Solutions
  106. #
  107. Write-Host "Checking that solutions are up to date"
  108. Get-ChildItem "$repoRoot/*.sln" -Recurse `
  109. | ? {
  110. # These .sln files are used by the templating engine.
  111. ($_.Name -ne "RazorComponentsWeb-CSharp.sln")
  112. } `
  113. | % {
  114. Write-Host " Checking $(Split-Path -Leaf $_)"
  115. $slnDir = Split-Path -Parent $_
  116. $sln = $_
  117. & dotnet sln $_ list `
  118. | ? { $_ -like '*proj' } `
  119. | % {
  120. $proj = Join-Path $slnDir $_
  121. if (-not (Test-Path $proj)) {
  122. LogError "Missing project. Solution references a project which does not exist: $proj. [$sln] "
  123. }
  124. }
  125. }
  126. #
  127. # Generated code check
  128. #
  129. Write-Host "Re-running code generation"
  130. Write-Host "Re-generating project lists"
  131. Invoke-Block {
  132. & $PSScriptRoot\GenerateProjectList.ps1 -ci:$ci
  133. }
  134. Write-Host "Re-generating references assemblies"
  135. Invoke-Block {
  136. & $PSScriptRoot\GenerateReferenceAssemblies.ps1 -ci:$ci
  137. }
  138. Write-Host "Re-generating package baselines"
  139. $dotnet = 'dotnet'
  140. if ($ci) {
  141. $dotnet = "$repoRoot/.dotnet/x64/dotnet.exe"
  142. }
  143. Invoke-Block {
  144. & $dotnet run -p "$repoRoot/eng/tools/BaselineGenerator/"
  145. }
  146. Write-Host "Re-generating Browser.JS files"
  147. Invoke-Block {
  148. & $dotnet build "$repoRoot\src\Components\Browser.JS\Microsoft.AspNetCore.Components.Browser.JS.npmproj"
  149. }
  150. Write-Host "Run git diff to check for pending changes"
  151. # Redirect stderr to stdout because PowerShell does not consistently handle output to stderr
  152. $changedFiles = & cmd /c 'git --no-pager diff --ignore-space-at-eol --name-only 2>nul'
  153. if ($changedFiles) {
  154. foreach ($file in $changedFiles) {
  155. $filePath = Resolve-Path "${repoRoot}/${file}"
  156. 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
  157. & git --no-pager diff --ignore-space-at-eol $filePath
  158. }
  159. }
  160. }
  161. finally {
  162. Write-Host ""
  163. Write-Host "Summary:"
  164. Write-Host ""
  165. Write-Host " $($errors.Length) error(s)"
  166. Write-Host ""
  167. foreach ($err in $errors) {
  168. Write-Host -f Red $err
  169. }
  170. if ($errors) {
  171. exit 1
  172. }
  173. }