& { #requires -Version 7.0 $ErrorActionPreference = 'Stop' $PSNativeCommandUseErrorActionPreference = $false $manifest = @' { "dependsOn": [ "Cuemon.Core@11.0.0", "Cuemon.Threading@11.0.0" ], "framework": "net10.0", "mode": "console", "schemaVersion": 1, "source": "// Program.cs\nusing System;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing Cuemon.Threading;\n\nawait new AsyncFuncFactoryExample().DemonstrateAsync();\n\nnamespace Cuemon.Threading\n{\n public class AsyncFuncFactoryExample\n {\n public async Task DemonstrateAsync()\n {\n var factory = AsyncFuncFactory.Create(ct =>\n {\n return Task.FromResult(42);\n });\n var result = await factory.ExecuteMethodAsync(CancellationToken.None);\n Console.WriteLine($\"Result: {result}\");\n\n var factoryWithArg = AsyncFuncFactory.Create(async (int a, int b, CancellationToken ct) =>\n {\n await Task.Delay(10, ct);\n return a + b;\n }, 3, 4);\n var sum = await factoryWithArg.ExecuteMethodAsync(CancellationToken.None);\n Console.WriteLine($\"Sum: {sum}\");\n }\n }\n}" } '@ $OutputEncoding = [Text.UTF8Encoding]::new($false) [Console]::OutputEncoding = $OutputEncoding $verifyTests = $false function Assert-CsrunTestOutput([string] $Output) { $lines = @($Output -split '\r?\n') $headers = @($lines | Where-Object { $_ -match '^Test run summary: Passed! - .+\)$' }) if ($headers.Count -ne 1) { throw 'Expected exactly one completed xUnit test summary.' } $headerIndex = [Array]::IndexOf($lines, $headers[0]) $summaryLines = @($lines | Select-Object -Skip ($headerIndex + 1) -First 4) $counts = @{} foreach ($line in $summaryLines) { if ($line -match '^\s*(total|failed|succeeded|skipped):\s*(\d+)\s*$') { $counts[$Matches[1]] = [int]$Matches[2] } } if ($counts.Count -ne 4) { throw 'The xUnit test summary counts are incomplete or ambiguous.' } $failed = $counts.failed $passed = $counts.succeeded $skipped = $counts.skipped $total = $counts.total if ($total -lt 1 -or $passed -lt 1 -or $failed -ne 0 -or $skipped -ne 0 -or $passed -ne $total) { throw 'Tests must execute at least once, all pass, and none fail or skip.' } } # Downloads and executes the latest stable csrun release with your permissions. # Source and release history: https://github.com/codebeltnet/csrun $engineDirectory = Join-Path ([IO.Path]::GetTempPath()) ('csrun-' + [guid]::NewGuid().ToString('N')) $null = New-Item -ItemType Directory -Path $engineDirectory $enginePath = Join-Path $engineDirectory 'csrun.ps1' $process = $null try { $release = Invoke-RestMethod 'https://api.github.com/repos/codebeltnet/csrun/releases/latest' $assets = @($release.assets | Where-Object name -CEQ 'csrun.ps1') if ($assets.Count -ne 1 -or $assets[0].digest -notmatch '^sha256:[0-9a-f]{64}$') { throw "csrun release $($release.tag_name) must publish one csrun.ps1 asset with a SHA-256 digest." } $asset = $assets[0] Invoke-WebRequest $asset.browser_download_url -OutFile $enginePath $actualHash = (Get-FileHash -LiteralPath $enginePath -Algorithm SHA256).Hash if ("sha256:$actualHash" -ne $asset.digest) { throw "csrun asset checksum mismatch: $actualHash" } $startInfo = [Diagnostics.ProcessStartInfo]::new('pwsh') foreach ($argument in @('-NoProfile','-NonInteractive','-File')) { $startInfo.ArgumentList.Add($argument) } $startInfo.ArgumentList.Add($enginePath) $startInfo.ArgumentList.Add('-ManifestJson') $startInfo.ArgumentList.Add($manifest) $startInfo.RedirectStandardOutput = $true $startInfo.RedirectStandardError = $true $startInfo.UseShellExecute = $false $process = [Diagnostics.Process]::new() $process.StartInfo = $startInfo if (-not $process.Start()) { throw 'Could not start csrun.' } $stdoutTask = $process.StandardOutput.ReadToEndAsync() $stderrTask = $process.StandardError.ReadToEndAsync() $process.WaitForExit() $output = $stdoutTask.GetAwaiter().GetResult() $diagnostics = $stderrTask.GetAwaiter().GetResult() Write-Output $output if ($diagnostics) { [Console]::Error.Write($diagnostics) } } catch { [Console]::Error.WriteLine("csrun diagnostics retained in: $engineDirectory") throw } finally { $resolvedTemp = [IO.Path]::TrimEndingDirectorySeparator([IO.Path]::GetFullPath([IO.Path]::GetTempPath())) $resolvedEngineDirectory = [IO.Path]::GetFullPath($engineDirectory) if ([IO.Path]::GetDirectoryName($resolvedEngineDirectory) -ne $resolvedTemp -or [IO.Path]::GetFileName($resolvedEngineDirectory) -cnotmatch '^csrun-[a-f0-9]{32}$') { throw 'Unsafe cleanup path.' } if ($process -and $process.ExitCode -eq 0) { Remove-Item -LiteralPath $resolvedEngineDirectory -Recurse -Force } } if ($process.ExitCode -ne 0) { throw "csrun execution failed with exit code $($process.ExitCode). $diagnostics" } if ($verifyTests) { Assert-CsrunTestOutput $output } }