Repository navigation
Expand file tree
/
Copy pathcsrun.ps1
More file actions
394 lines (385 loc) · 24.6 KB
/
Copy pathcsrun.ps1
File metadata and controls
394 lines (385 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#requires -Version 7.0
<#
.SYNOPSIS
Runs a complete trusted C# console program or genuine xUnit v3 tests.
.DESCRIPTION
Accepts a schemaVersion 1 JSON manifest, or direct options normalized to the
same manifest. Requires an installed .NET 10+ SDK and the requested runtime.
The framework defaults to the latest supported GA LTS channel. Explicit
modern .NET targets are checked against the official release index, cached for
up to 12 hours. Legacy .NET Framework targets (net471, net472, net48, net481)
are supported natively on Windows only.
Scriptblock callers can inspect $LASTEXITCODE without leaving their session;
pwsh -File returns that status to the calling process.
Executes code and package build assets with your permissions; this is not a sandbox.
.PARAMETER ManifestJson
Canonical JSON manifest with schemaVersion, mode, source and optional
framework, dependsOn, sdk and frameworkReferences fields. Exclusive with direct options.
.PARAMETER Mode
console (default) or test.
.PARAMETER Framework
Target framework; defaults to the latest supported LTS channel. Accepts .NET 10+
TFMs (for example net10.0 or net10.0-windows) and Windows-only .NET Framework
TFMs (net471, net472, net48, net481).
.PARAMETER DependsOn
Explicit PackageId@ExactVersion pins. No floating versions or discovery.
.PARAMETER Source
Complete C# source text, exclusive with File and Clipboard.
.PARAMETER File
UTF-8 C# source file, exclusive with Source and Clipboard.
.PARAMETER Clipboard
Read complete source from the clipboard. Also the default if no source is given.
.EXAMPLE
./csrun.ps1 -Source 'System.Console.WriteLine("hello");'
.EXAMPLE
./csrun.ps1 -ManifestJson (Get-Content ./samples/datespan.json -Raw)
.EXAMPLE
./csrun.ps1 -Mode test -File ./tests/fixtures/OutputLinesTest.cs -DependsOn Codebelt.Extensions.Xunit@13.0.0,xunit.v3@4.0.3
.EXAMPLE
./csrun.ps1 -Clipboard -Framework net11.0
#>
[CmdletBinding()]
param(
[string] $ManifestJson,
[string] $Mode = 'console',
[string] $Framework,
[string[]] $DependsOn = @(),
[string] $Source,
[string] $File,
[switch] $Clipboard
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# Native failures are propagated explicitly, including under caller profiles.
$PSNativeCommandUseErrorActionPreference = $false
function Get-PackagePins($Specs) {
$pins = [ordered]@{}
foreach ($spec in $Specs) {
if ($spec -isnot [string] -or $spec -cnotmatch '^(?<id>[A-Za-z0-9_][A-Za-z0-9_.-]{0,99})@(?<version>[0-9]+\.[0-9]+\.[0-9]+(?:\.[0-9]+)?(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?)$') {
throw "Invalid package pin '$spec'; use PackageId@ExactVersion (for example xunit.v3@4.0.3)."
}
$id = $Matches.id
$version = $Matches.version
if ($pins.Contains($id) -and $pins[$id] -cne $version) { throw "Conflicting package versions for '$id'." }
$pins[$id] = $version
}
return ,$pins
}
function Get-ReleaseIndex {
$cache = Join-Path ([IO.Path]::GetTempPath()) 'csrun-dotnet-releases-index.json'
$json = $null
if ((Test-Path -LiteralPath $cache) -and (Get-Item -LiteralPath $cache).LastWriteTimeUtc -gt [DateTime]::UtcNow.AddHours(-12) -and (Get-Item -LiteralPath $cache).LastWriteTimeUtc -le [DateTime]::UtcNow) {
try {
$json = Get-Content -LiteralPath $cache -Raw
$cachedIndex = ConvertFrom-Json -InputObject $json -AsHashtable -ErrorAction Stop
if ($cachedIndex -isnot [System.Collections.IDictionary] -or $cachedIndex['releases-index'] -isnot [array] -or $cachedIndex['releases-index'].Count -eq 0) { $json = $null }
} catch { $json = $null }
}
if (-not $json) {
try {
$json = (Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/dotnet/core/main/release-notes/releases-index.json' -TimeoutSec 20 -ErrorAction Stop).Content
$downloadedIndex = ConvertFrom-Json -InputObject $json -AsHashtable -ErrorAction Stop
if ($downloadedIndex -isnot [System.Collections.IDictionary] -or $downloadedIndex['releases-index'] -isnot [array] -or $downloadedIndex['releases-index'].Count -eq 0) { throw 'Invalid release index.' }
try { [IO.File]::WriteAllText($cache, $json) } catch { }
} catch { throw "Unable to obtain the official .NET release index and no valid recent cache is available: $($_.Exception.Message)" }
}
$index = ConvertFrom-Json -InputObject $json -AsHashtable
if (-not $index.Contains('releases-index') -or $index['releases-index'] -isnot [array] -or $index['releases-index'].Count -eq 0) { throw 'Invalid .NET release index; cannot validate the framework.' }
return ,$index['releases-index']
}
function Resolve-Framework($Framework, $Index, [bool] $WindowsPlatform = $IsWindows) {
if ($Framework -is [string] -and $Framework -cmatch '^net4(7[12]|8|81)$') {
if (-not $WindowsPlatform) { throw "Framework '$Framework' targets .NET Framework and is only supported natively on Windows." }
return $Framework
}
$channels = @($Index | Where-Object { $_.product -eq '.NET' -and $_['channel-version'] -match '^\d+\.\d+$' -and [version]$_['channel-version'] -ge [version]'10.0' } | Sort-Object { [version]$_['channel-version'] } -Descending)
if ($null -eq $Framework) {
$lts = @($channels | Where-Object { $_['release-type'] -eq 'lts' -and $_['support-phase'] -in @('active', 'maintenance') -and $_['latest-release'] -match '^\d+\.\d+\.\d+$' }) | Select-Object -First 1
if (-not $lts) { throw 'The release index contains no supported GA LTS channel.' }
return Resolve-Framework "net$($lts['channel-version'])" $Index $WindowsPlatform
}
if ($Framework -isnot [string] -or $Framework -cnotmatch '^net(?<channel>[1-9][0-9]*\.[0-9]+)(?:-(?:windows|linux|macos|android|ios|maccatalyst|tvos|browser)(?:[0-9]+(?:\.[0-9]+){0,3})?)?$') { throw 'Invalid framework; supply a .NET target framework such as net10.0, net10.0-windows, or net48 (Windows only).' }
$channel = $Matches.channel
$entry = @($channels | Where-Object { $_['channel-version'] -ceq $channel })
if ($entry.Count -ne 1) { throw "Framework '$Framework' has no unique channel in the official .NET release index." }
$entry = $entry[0]
if ($entry['release-type'] -notin @('lts','sts')) { throw "Framework '$Framework' has an unrecognized release type." }
if ($entry['support-phase'] -in @('active','maintenance') -and $entry['latest-release'] -match '^\d+\.\d+\.\d+$') { return $Framework }
if ($entry['support-phase'] -in @('preview','go-live')) {
$latestPreview = @($channels | Where-Object { $_['support-phase'] -in @('preview','go-live') -and $_['release-type'] -in @('lts','sts') } | Select-Object -First 1)
if ($latestPreview.Count -eq 1 -and $latestPreview[0]['channel-version'] -ceq $channel) { return $Framework }
}
throw "Framework '$Framework' is not a supported GA channel or the latest preview/go-live channel."
}
function Get-ValidatedManifest($InputManifest) {
if ($InputManifest -isnot [System.Collections.IDictionary]) { throw 'Manifest must be a JSON object.' }
$allowed = @('schemaVersion', 'mode', 'framework', 'source', 'dependsOn', 'sdk', 'frameworkReferences')
foreach ($key in $InputManifest.Keys) {
if ($key -cnotin $allowed) { throw "Unknown manifest field '$key'." }
}
foreach ($key in @('schemaVersion', 'mode', 'source')) {
if (-not $InputManifest.Contains($key)) { throw "Missing required manifest field '$key'." }
}
if (($InputManifest.schemaVersion -isnot [long] -and $InputManifest.schemaVersion -isnot [int]) -or $InputManifest.schemaVersion -ne 1) {
throw 'Unsupported schemaVersion; expected integer 1.'
}
if ($InputManifest.mode -isnot [string] -or $InputManifest.mode -notin @('console', 'test')) { throw 'Unsupported mode; use console or test.' }
$framework = $null
if ($InputManifest.Contains('framework')) {
if ($InputManifest.framework -isnot [string]) { throw 'Invalid framework; supply a string or omit the field to select the LTS default.' }
$framework = $InputManifest.framework
}
if ($InputManifest.source -isnot [string] -or [string]::IsNullOrWhiteSpace($InputManifest.source)) { throw 'source must be a nonempty complete C# program or test source.' }
$sdk = 'Microsoft.NET.Sdk'
if ($InputManifest.Contains('sdk')) {
if ($InputManifest.sdk -cnotin @('Microsoft.NET.Sdk', 'Microsoft.NET.Sdk.Web')) { throw 'Unsupported sdk; use Microsoft.NET.Sdk or Microsoft.NET.Sdk.Web.' }
$sdk = $InputManifest.sdk
}
$specs = @()
if ($InputManifest.Contains('dependsOn')) {
if ($InputManifest.dependsOn -isnot [array]) { throw 'dependsOn must be an array of package pins.' }
$specs = $InputManifest.dependsOn
}
$pins = Get-PackagePins $specs
$refs = @()
if ($InputManifest.Contains('frameworkReferences')) {
if ($InputManifest.frameworkReferences -isnot [array]) { throw 'frameworkReferences must be an array.' }
foreach ($ref in $InputManifest.frameworkReferences) {
if ($ref -isnot [string] -or $ref -cnotmatch '^[A-Za-z][A-Za-z0-9_.]*$') { throw 'Invalid frameworkReferences entry.' }
}
$refs = @($InputManifest.frameworkReferences | Select-Object -Unique)
}
$executionMode = $InputManifest.mode.ToLowerInvariant()
if ($executionMode -eq 'test') {
foreach ($id in @($pins.Keys)) {
if ($id -like 'xunit*' -and $id -notin @('xunit.v3', 'xunit.analyzers')) {
throw "Incompatible test infrastructure package '$id'; use xunit.v3@4.0.3."
}
}
if ($pins.Contains('xunit.v3')) {
# The supported compatibility band uses the same MTP v2 integration.
if ($pins['xunit.v3'] -notmatch '^4\.0\.(\d+)$' -or [long]$Matches[1] -lt 3) { throw 'Incompatible xunit.v3 pin; supported baseline is 4.0.3 (stable 4.0 patch versions >= 3).'}
} else { $pins['xunit.v3'] = '4.0.3' }
} elseif ($refs.Count -gt 0 -and ($sdk -cne 'Microsoft.NET.Sdk.Web' -or @($refs | Where-Object { $_ -cne 'Microsoft.AspNetCore.App' }).Count -gt 0)) {
throw 'Console frameworkReferences require sdk Microsoft.NET.Sdk.Web and Microsoft.AspNetCore.App; use test mode for other explicit framework references.'
}
return @{ Mode = $executionMode; Framework = $framework; Source = $InputManifest.source; Sdk = $sdk; Pins = $pins; References = $refs }
}
function Get-ConsoleSource($Manifest, [switch] $StripDirectives) {
$directives = [ordered]@{ 'property TargetFramework' = $Manifest.Framework; 'sdk' = $Manifest.Sdk }
$seen = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
foreach ($id in $Manifest.Pins.Keys) { $directives["package $id"] = $Manifest.Pins[$id] }
# Inspect only the directive preamble, never C# declarations or method bodies.
# Original source is appended byte-for-character, including existing directives.
$source = $Manifest.Source
$inBlockComment = $false
$removals = [System.Collections.Generic.List[hashtable]]::new()
$pos = 0
while ($pos -lt $source.Length) {
$eolStart = $source.IndexOfAny([char[]]@("`r", "`n"), $pos)
if ($eolStart -lt 0) {
$lineEnd = $source.Length
$nextPos = $source.Length
} else {
$lineEnd = $eolStart
$nextPos = if ($source[$eolStart] -eq "`r" -and ($eolStart + 1) -lt $source.Length -and $source[$eolStart + 1] -eq "`n") { $eolStart + 2 } else { $eolStart + 1 }
}
$line = $source.Substring($pos, $lineEnd - $pos)
$offset = 0
$commentEnd = 0
$hasPrefixComment = $false
# Skip leading comments only; do not scan strings or the C# program body.
while ($offset -lt $line.Length) {
if ($inBlockComment) {
$end = $line.IndexOf('*/', $offset, [StringComparison]::Ordinal)
if ($end -lt 0) { $offset = $line.Length; break }
$offset = $end + 2
$commentEnd = $offset
$inBlockComment = $false
$hasPrefixComment = $true
} else {
while ($offset -lt $line.Length -and [char]::IsWhiteSpace($line[$offset])) { $offset++ }
if ($offset -ge $line.Length) { break }
if (($offset + 1) -lt $line.Length -and $line[$offset] -eq '/' -and $line[$offset + 1] -eq '*') {
$inBlockComment = $true
$offset += 2
$hasPrefixComment = $true
} else {
break
}
}
}
$text = $line.Substring($offset).TrimEnd()
if ($text.Length -eq 0 -or $text.StartsWith('//')) { $pos = $nextPos; continue }
# C# preprocessor directives such as #nullable do not end the SDK's
# directive preamble. Leave them untouched, but validate following #: lines.
if ($text.StartsWith('#') -and -not $text.StartsWith('#:')) { $pos = $nextPos; continue }
if (-not $text.StartsWith('#:')) { break }
$key = $null
$value = $null
if ($text -cmatch '^#:property\s+TargetFramework\s*=\s*(\S+)\s*$') { $key = 'property TargetFramework'; $value = $Matches[1] }
elseif ($text -cmatch '^#:sdk\s+(\S+)\s*$') { $key = 'sdk'; $value = $Matches[1] }
elseif ($text -cmatch '^#:package\s+(\S+)\s*$') {
$pin = Get-PackagePins @($Matches[1])
$id = @($pin.Keys)[0]; $key = "package $id"; $value = $pin[$id]
} else { throw "Unsupported source directive '$text'; put build configuration in the manifest." }
if (-not $directives.Contains($key) -or $directives[$key] -cne $value) { throw "Source directive conflicts with manifest: $text" }
if ($seen.Contains($key)) { throw "Duplicate source directive '$text'; the SDK requires one occurrence per setting." }
# A matching directive in the original source supplies this setting.
$null = $seen.Add($key)
if ($hasPrefixComment) {
$removals.Add(@{ Start = $pos + $commentEnd; End = $lineEnd })
} else {
$removals.Add(@{ Start = $pos; End = $nextPos })
}
$pos = $nextPos
}
if ($StripDirectives) {
if ($removals.Count -eq 0) { return $source }
$builder = [System.Text.StringBuilder]::new($source.Length)
$cursor = 0
foreach ($span in $removals) {
$null = $builder.Append($source, $cursor, $span.Start - $cursor)
$cursor = $span.End
}
$null = $builder.Append($source, $cursor, $source.Length - $cursor)
return $builder.ToString()
}
$preamble = foreach ($key in $directives.Keys) {
if ($seen.Contains($key)) { continue }
if ($key -eq 'sdk') { "#:sdk $($directives[$key])" }
elseif ($key.StartsWith('package ')) { "#:$key@$($directives[$key])" }
else { "#:$key=$($directives[$key])" }
}
# NuGet must not substitute an approximate match for an unavailable pin.
return ($preamble -join "`n") + "`n#:property WarningsAsErrors=NU1603;NU1605`n" + $Manifest.Source
}
function Write-Project($Manifest, [string] $Directory, [bool] $IsNetFramework = $false, $SourceOverride = $null) {
$project = Join-Path $Directory 'Sample.csproj'
$settings = [System.Xml.XmlWriterSettings]::new()
$settings.Indent = $true
$writer = [System.Xml.XmlWriter]::Create($project, $settings)
try {
$writer.WriteStartElement('Project'); $writer.WriteAttributeString('Sdk', $Manifest.Sdk)
$writer.WriteStartElement('PropertyGroup')
$properties = [ordered]@{ OutputType = 'Exe'; TargetFramework = $Manifest.Framework; ImplicitUsings = 'enable'; Nullable = 'enable'; WarningsAsErrors = 'NU1603;NU1605' }
if ($IsNetFramework) { $properties['LangVersion'] = 'latest' }
if ($Manifest.Mode -eq 'test') { $properties['UseMicrosoftTestingPlatformRunner'] = 'true' }
foreach ($key in $properties.Keys) {
$writer.WriteElementString($key, $properties[$key])
}
$writer.WriteEndElement()
$writer.WriteStartElement('ItemGroup')
foreach ($id in $Manifest.Pins.Keys) {
$writer.WriteStartElement('PackageReference'); $writer.WriteAttributeString('Include', $id)
# NuGet bracket syntax enforces an exact version rather than a lower bound.
$writer.WriteAttributeString('Version', "[$($Manifest.Pins[$id])]"); $writer.WriteEndElement()
}
if ($IsNetFramework -and -not $Manifest.Pins.Contains('Microsoft.NETFramework.ReferenceAssemblies')) {
$writer.WriteStartElement('PackageReference')
$writer.WriteAttributeString('Include', 'Microsoft.NETFramework.ReferenceAssemblies')
$writer.WriteAttributeString('Version', '[1.0.3]')
$writer.WriteAttributeString('PrivateAssets', 'All')
$writer.WriteEndElement()
}
foreach ($ref in $Manifest.References) {
if ($Manifest.Sdk -eq 'Microsoft.NET.Sdk.Web' -and $ref -eq 'Microsoft.AspNetCore.App') { continue }
$writer.WriteStartElement('FrameworkReference'); $writer.WriteAttributeString('Include', $ref); $writer.WriteEndElement()
}
$writer.WriteEndElement(); $writer.WriteEndElement()
} finally { $writer.Dispose() }
$sourceText = if ($null -ne $SourceOverride) { $SourceOverride } else { $Manifest.Source }
[System.IO.File]::WriteAllText((Join-Path $Directory 'Sample.cs'), $sourceText, [System.Text.UTF8Encoding]::new($false))
return $project
}
$temporaryDirectory = $null
$exitCode = 1
try {
if ($PSBoundParameters.ContainsKey('ManifestJson')) {
if (@($PSBoundParameters.Keys | Where-Object { $_ -in @('Mode', 'Framework', 'DependsOn', 'Source', 'File', 'Clipboard') }).Count -gt 0) { throw 'ManifestJson is mutually exclusive with direct input/configuration arguments.' }
try { $inputManifest = ConvertFrom-Json -InputObject $ManifestJson -AsHashtable -ErrorAction Stop }
catch { throw "Invalid manifest JSON: $($_.Exception.Message)" }
} else {
$sourceCount = @($PSBoundParameters.Keys | Where-Object { $_ -in @('Source', 'File', 'Clipboard') }).Count
if ($sourceCount -gt 1) { throw 'Source, File and Clipboard are mutually exclusive.' }
if ($PSBoundParameters.ContainsKey('File')) { $Source = Get-Content -LiteralPath $File -Raw -Encoding utf8 }
elseif (-not $PSBoundParameters.ContainsKey('Source')) {
if (-not (Get-Command Get-Clipboard -ErrorAction SilentlyContinue)) { throw 'Clipboard unavailable; supply Source or File.' }
try { $Source = Get-Clipboard -Raw -ErrorAction Stop }
catch { throw 'Clipboard unavailable; supply Source or File.' }
}
$inputManifest = @{ schemaVersion = 1; mode = $Mode; dependsOn = $DependsOn; source = $Source }
if ($PSBoundParameters.ContainsKey('Framework')) { $inputManifest.framework = $Framework }
}
$manifest = Get-ValidatedManifest $inputManifest
$isNetFramework = ($manifest.Framework -is [string] -and $manifest.Framework -cmatch '^net4(7[12]|8|81)$')
$releaseIndex = if ($isNetFramework) { $null } else { Get-ReleaseIndex }
$manifest.Framework = Resolve-Framework $manifest.Framework $releaseIndex
$consoleSource = $null
if ($manifest.Mode -eq 'console') { $consoleSource = Get-ConsoleSource $manifest -StripDirectives:$isNetFramework }
$dotnet = Get-Command dotnet -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1
if ($null -eq $dotnet) { throw 'Missing .NET SDK; install a compatible .NET 10+ SDK and the requested runtime.' }
$sdkVersion = & $dotnet.Source --version
if ($LASTEXITCODE -ne 0 -or $sdkVersion -notmatch '^(\d+)\.' -or [int]$Matches[1] -lt 10) { throw 'No compatible .NET 10+ SDK selected. Check installed SDKs and the current directory global.json.' }
if (-not $isNetFramework) {
$null = $manifest.Framework -match '^net(?<channel>\d+\.\d+)'
$targetChannel = [version]$Matches.channel
$targetRelease = @($releaseIndex | Where-Object { $_.product -eq '.NET' -and $_['channel-version'] -eq $targetChannel.ToString() })[0]
$selectedVersion = [version](($sdkVersion -split '-')[0])
if ($selectedVersion.Major -lt $targetChannel.Major -or ($selectedVersion.Major -eq $targetChannel.Major -and $selectedVersion.Minor -lt $targetChannel.Minor)) { throw "Selected SDK $sdkVersion cannot build $($manifest.Framework); select a compatible SDK using global.json or install one." }
$runtimes = @(& $dotnet.Source --list-runtimes)
if ($LASTEXITCODE -ne 0) { throw 'Unable to list installed .NET runtimes.' }
$requiredRuntimes = @('Microsoft.NETCore.App')
if ($manifest.Sdk -eq 'Microsoft.NET.Sdk.Web' -or 'Microsoft.AspNetCore.App' -in $manifest.References) { $requiredRuntimes += 'Microsoft.AspNetCore.App' }
if (@($manifest.References | Where-Object { $_ -match '^Microsoft\.WindowsDesktop\.App(?:\.|$)' }).Count) { $requiredRuntimes += 'Microsoft.WindowsDesktop.App' }
$prereleasePattern = if ($targetRelease['support-phase'] -in @('preview', 'go-live')) { '(?:-[^ ]+)?' } else { '' }
foreach ($runtime in $requiredRuntimes) {
if (-not @($runtimes | Where-Object { $_ -match "^$([regex]::Escape($runtime)) $([regex]::Escape($targetChannel.ToString()))\.\d+$prereleasePattern " }).Count) { throw "Missing $runtime runtime for .NET $targetChannel required by $($manifest.Framework); install the runtime before running csrun." }
}
}
$dependencies = @($manifest.Pins.Keys | ForEach-Object { "$_@$($manifest.Pins[$_])" })
Write-Host "csrun: $($manifest.Mode) | $($manifest.Framework) | dependencies: $($dependencies -join ', ')"
if ($manifest.Mode -eq 'console' -and -not $isNetFramework) {
$OutputEncoding = [System.Text.UTF8Encoding]::new($false)
if (@(Get-ChildItem -LiteralPath (Get-Location).Path -File -Filter '*.*proj').Count -gt 0) {
# SDK 11 selects a current-directory project before considering '-'.
# --file forces native file-based execution without changing program cwd.
$temporaryDirectory = Join-Path ([System.IO.Path]::GetTempPath()) ('csrun-' + [guid]::NewGuid().ToString('N'))
$null = New-Item -ItemType Directory -Path $temporaryDirectory
$entryFile = Join-Path $temporaryDirectory 'Sample.cs'
[System.IO.File]::WriteAllText($entryFile, $consoleSource, [System.Text.UTF8Encoding]::new($false))
& $dotnet.Source run --file $entryFile --no-launch-profile
} else {
$consoleSource | & $dotnet.Source run - --no-launch-profile
}
$exitCode = $LASTEXITCODE
} elseif ($manifest.Mode -eq 'console') {
# File-based apps do not support .NET Framework TFMs directly; fall back to an isolated SDK-style project.
$temporaryDirectory = Join-Path ([System.IO.Path]::GetTempPath()) ('csrun-' + [guid]::NewGuid().ToString('N'))
$null = New-Item -ItemType Directory -Path $temporaryDirectory
$project = Write-Project $manifest $temporaryDirectory $true $consoleSource
& $dotnet.Source run --project $project --framework $manifest.Framework --no-launch-profile
$exitCode = $LASTEXITCODE
} else {
$temporaryDirectory = Join-Path ([System.IO.Path]::GetTempPath()) ('csrun-' + [guid]::NewGuid().ToString('N'))
$null = New-Item -ItemType Directory -Path $temporaryDirectory
$project = Write-Project $manifest $temporaryDirectory $isNetFramework
& $dotnet.Source run --project $project --framework $manifest.Framework --no-launch-profile -- --show-live-output on --progress off --no-banner
$exitCode = $LASTEXITCODE
}
} catch {
[Console]::Error.WriteLine("csrun: $($_.Exception.Message)")
$exitCode = 1
} finally {
if ($null -ne $temporaryDirectory -and (Test-Path -LiteralPath $temporaryDirectory)) {
try {
$resolvedDirectory = [System.IO.Path]::GetFullPath($temporaryDirectory)
$tempRoot = [System.IO.Path]::GetFullPath([System.IO.Path]::GetTempPath()).TrimEnd([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar)
if ([System.IO.Path]::GetDirectoryName($resolvedDirectory) -cne $tempRoot -or [System.IO.Path]::GetFileName($resolvedDirectory) -cnotmatch '^csrun-[a-f0-9]{32}$') { throw 'Unsafe temporary artifact cleanup path.' }
Remove-Item -LiteralPath $resolvedDirectory -Recurse -Force
}
catch { [Console]::Error.WriteLine("csrun: could not clean temporary artifacts '$temporaryDirectory': $($_.Exception.Message)"); if ($exitCode -eq 0) { $exitCode = 1 } }
}
}
$global:LASTEXITCODE = $exitCode
if ($MyInvocation.MyCommand -is [System.Management.Automation.ExternalScriptInfo]) { exit $exitCode }