|
| 1 | +param( |
| 2 | + [string]$ProjectRoot = $(Split-Path -Parent $PSScriptRoot), |
| 3 | + [string]$Configuration = "Debug" |
| 4 | +) |
| 5 | + |
| 6 | +$ErrorActionPreference = "Stop" |
| 7 | + |
| 8 | +$items = @( |
| 9 | + @{ Namespace = "Rougamo.Fody.Tests.Mono48"; Class = "MonoTests"; TargetType = "Rougamo.Fody.Tests.MonoTests"; SourceFile = "MonoTests.cs" }, |
| 10 | + @{ Namespace = "Rougamo.Fody.Tests.Mono48"; Class = "BasicTest"; TargetType = "Rougamo.Fody.Tests.BasicTest"; SourceFile = "BasicTest.cs" }, |
| 11 | + @{ Namespace = "Rougamo.Fody.Tests.Mono48"; Class = "IssueTest"; TargetType = "Rougamo.Fody.Tests.IssueTest"; SourceFile = "IssueTest.cs" }, |
| 12 | + @{ Namespace = "Rougamo.Fody.Tests.Mono48"; Class = "ConfiguredMoTests"; TargetType = "Rougamo.Fody.Tests.ConfiguredMoTests"; SourceFile = "ConfiguredMoTests.cs" }, |
| 13 | + @{ Namespace = "Rougamo.Fody.Tests.Mono48"; Class = "PatternTests"; TargetType = "Rougamo.Fody.Tests.PatternTests"; SourceFile = "PatternTests.cs" }, |
| 14 | + |
| 15 | + @{ Namespace = "Rougamo.Fody.Tests.Mono48.Signatures"; Class = "SignatureBasicTests"; TargetType = "Rougamo.Fody.Tests.Signatures.SignatureBasicTests"; SourceFile = "Signatures/SignatureBasicTests.cs" }, |
| 16 | + @{ Namespace = "Rougamo.Fody.Tests.Mono48.Signatures"; Class = "WildcardMatchTests"; TargetType = "Rougamo.Fody.Tests.Signatures.WildcardMatchTests"; SourceFile = "Signatures/WildcardMatchTests.cs" }, |
| 17 | + @{ Namespace = "Rougamo.Fody.Tests.Mono48.Signatures"; Class = "PropertyMatchTests"; TargetType = "Rougamo.Fody.Tests.Signatures.PropertyMatchTests"; SourceFile = "Signatures/PropertyMatchTests.cs" }, |
| 18 | + @{ Namespace = "Rougamo.Fody.Tests.Mono48.Signatures"; Class = "GetterMatchTests"; TargetType = "Rougamo.Fody.Tests.Signatures.GetterMatchTests"; SourceFile = "Signatures/PropertyMatchTests.cs" }, |
| 19 | + @{ Namespace = "Rougamo.Fody.Tests.Mono48.Signatures"; Class = "SetterMatchTests"; TargetType = "Rougamo.Fody.Tests.Signatures.SetterMatchTests"; SourceFile = "Signatures/PropertyMatchTests.cs" }, |
| 20 | + @{ Namespace = "Rougamo.Fody.Tests.Mono48.Signatures"; Class = "PatternParseTests"; TargetType = "Rougamo.Fody.Tests.Signatures.PatternParseTests"; SourceFile = "Signatures/PatternParseTests.cs" }, |
| 21 | + @{ Namespace = "Rougamo.Fody.Tests.Mono48.Signatures"; Class = "ExecutionMatchTests"; TargetType = "Rougamo.Fody.Tests.Signatures.ExecutionMatchTests"; SourceFile = "Signatures/ExecutionMatchTests.cs" }, |
| 22 | + @{ Namespace = "Rougamo.Fody.Tests.Mono48.Signatures"; Class = "MethodMatchTests"; TargetType = "Rougamo.Fody.Tests.Signatures.MethodMatchTests"; SourceFile = "Signatures/ExecutionMatchTests.cs" } |
| 23 | +) |
| 24 | + |
| 25 | +function Test-PreprocessorCondition { |
| 26 | + param( |
| 27 | + [string]$Expression, |
| 28 | + [bool]$IsDebug |
| 29 | + ) |
| 30 | + |
| 31 | + $exp = ($Expression -replace '\s+', '') |
| 32 | + switch ($exp) { |
| 33 | + "DEBUG" { return $IsDebug } |
| 34 | + "!DEBUG" { return -not $IsDebug } |
| 35 | + default { return $true } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function Get-ActiveContent { |
| 40 | + param( |
| 41 | + [string]$Content, |
| 42 | + [bool]$IsDebug |
| 43 | + ) |
| 44 | + |
| 45 | + $lines = $Content -split "`r?`n" |
| 46 | + $states = New-Object System.Collections.Generic.List[bool] |
| 47 | + $states.Add($true) |
| 48 | + $sb = New-Object System.Text.StringBuilder |
| 49 | + |
| 50 | + foreach ($line in $lines) { |
| 51 | + if ($line -match '^\s*#if\s+(.+)$') { |
| 52 | + $parent = $states[$states.Count - 1] |
| 53 | + $condition = Test-PreprocessorCondition -Expression $matches[1] -IsDebug $IsDebug |
| 54 | + $states.Add($parent -and $condition) |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + if ($line -match '^\s*#else\b') { |
| 59 | + if ($states.Count -gt 1) { |
| 60 | + $parent = $states[$states.Count - 2] |
| 61 | + $current = $states[$states.Count - 1] |
| 62 | + $states[$states.Count - 1] = $parent -and (-not $current) |
| 63 | + } |
| 64 | + continue |
| 65 | + } |
| 66 | + |
| 67 | + if ($line -match '^\s*#endif\b') { |
| 68 | + if ($states.Count -gt 1) { |
| 69 | + $states.RemoveAt($states.Count - 1) |
| 70 | + } |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + if ($states[$states.Count - 1]) { |
| 75 | + [void]$sb.AppendLine($line) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return $sb.ToString() |
| 80 | +} |
| 81 | + |
| 82 | +$isDebug = $Configuration -imatch 'debug' |
| 83 | + |
| 84 | +$factPattern = '(?ms)\[Fact(?:\([^\)]*\))?\]\s*public\s+(?:async\s+)?(?:Task|void)\s+([A-Za-z_][A-Za-z0-9_]*)\s*\(' |
| 85 | +$grouped = [ordered]@{} |
| 86 | + |
| 87 | +foreach ($item in $items) { |
| 88 | + $sourcePath = Join-Path $ProjectRoot $item.SourceFile |
| 89 | + $content = Get-Content -Path $sourcePath -Raw -Encoding UTF8 |
| 90 | + $content = Get-ActiveContent -Content $content -IsDebug $isDebug |
| 91 | + $names = [regex]::Matches($content, $factPattern) | ForEach-Object { $_.Groups[1].Value } | Select-Object -Unique |
| 92 | + |
| 93 | + if (-not $grouped.Contains($item.Namespace)) { |
| 94 | + $grouped[$item.Namespace] = @() |
| 95 | + } |
| 96 | + $grouped[$item.Namespace] += [pscustomobject]@{ |
| 97 | + Class = $item.Class |
| 98 | + TargetType = $item.TargetType |
| 99 | + Methods = $names |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +$sb = New-Object System.Text.StringBuilder |
| 104 | +[void]$sb.AppendLine("#if NET48 && MONO_COMPAT") |
| 105 | +[void]$sb.AppendLine("using Xunit;") |
| 106 | +[void]$sb.AppendLine("") |
| 107 | +[void]$sb.AppendLine("// <auto-generated>") |
| 108 | +[void]$sb.AppendLine("// Generated by Mono48/Generate-Mono48Mirror.ps1") |
| 109 | +[void]$sb.AppendLine("// </auto-generated>") |
| 110 | +[void]$sb.AppendLine("") |
| 111 | + |
| 112 | +foreach ($ns in $grouped.Keys) { |
| 113 | + [void]$sb.AppendLine("namespace $ns") |
| 114 | + [void]$sb.AppendLine("{") |
| 115 | + foreach ($entry in $grouped[$ns]) { |
| 116 | + [void]$sb.AppendLine(" [Collection(""Mono48"")]") |
| 117 | + [void]$sb.AppendLine(" public class $($entry.Class)") |
| 118 | + [void]$sb.AppendLine(" {") |
| 119 | + foreach ($method in $entry.Methods) { |
| 120 | + $factFullName = "$($entry.TargetType).$method" |
| 121 | + [void]$sb.AppendLine(" [Fact] public void $method() => MonoRunnerBridge.RunMonoFact(""$factFullName"");") |
| 122 | + } |
| 123 | + [void]$sb.AppendLine(" }") |
| 124 | + [void]$sb.AppendLine("") |
| 125 | + } |
| 126 | + [void]$sb.AppendLine("}") |
| 127 | + [void]$sb.AppendLine("") |
| 128 | +} |
| 129 | + |
| 130 | +[void]$sb.AppendLine("#endif") |
| 131 | + |
| 132 | +$outputPath = Join-Path $PSScriptRoot "Mono48MirrorTests.g.cs" |
| 133 | +[System.IO.File]::WriteAllText($outputPath, $sb.ToString(), (New-Object System.Text.UTF8Encoding($false))) |
| 134 | +Write-Host "Generated $outputPath" |
0 commit comments