-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.ps1
More file actions
151 lines (125 loc) · 4.94 KB
/
Copy pathrun.ps1
File metadata and controls
151 lines (125 loc) · 4.94 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
<#
.SYNOPSIS
Run the CLI or GUI project for development.
.DESCRIPTION
Runs the requested project using `dotnet run`. Accepts `-Cli` or `-Gui`, or a positional `cli|gui` argument. Any remaining arguments are forwarded to the application.
.EXAMPLE
.\run.ps1 -Cli -- -v
.\run.ps1 gui
#>
[CmdletBinding()]
Param(
[Alias('c')][switch]$Cli,
[Alias('g')][switch]$Gui,
[Alias('h')][switch]$Help,
[Parameter(Position = 0)]
[ValidateSet('cli','gui')]
[string]$Target,
[Parameter(ValueFromRemainingArguments=$true)]
[string[]]$Args,
[string]$Configuration = 'Debug'
)
$ErrorActionPreference = 'Stop'
# Ensure the console uses UTF-8 to avoid garbled Unicode output from `dotnet run` (e.g., box-drawing characters).
try {
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
} catch {
Write-Host "⚠️ Could not set console encoding to UTF-8: $_" -ForegroundColor Yellow
}
function Show-Usage {
@"
Usage: .\run.ps1 [OPTION] [-- <app-args>]
Run the project. When no target is selected the script defaults to the CLI project.
Options:
-Cli, -c Run the CLI project (default)
-Gui, -g Run the GUI project
cli|gui Positional equivalent to the switches above
-Help, -h Show this help message
Examples:
.\run.ps1 -Cli -- -v # run CLI with '-v'
.\run.ps1 gui # run GUI
"@
}
if ($Help) {
Show-Usage
exit 0
}
if (-not $Cli -and -not $Gui -and $null -ne $Target) {
switch ($Target) {
'cli' { $Cli = $true }
'gui' { $Gui = $true }
}
}
# If nothing selected, default to CLI
if (-not $Cli -and -not $Gui) {
Write-Host "ℹ️ No target selected; defaulting to CLI." -ForegroundColor Yellow
$Cli = $true
}
$projectRoot = $PSScriptRoot
$cliProject = Join-Path $projectRoot 'src/SshAgentEcho.Cli/SshAgentEcho.Cli.csproj'
$guiProject = Join-Path $projectRoot 'src/SshAgentEcho.Gui/SshAgentEcho.Gui.csproj'
function Run-Project {
param(
[string]$ProjectPath,
[bool]$Wait = $true
)
if ($Wait) {
Write-Host "🔧 Building project: $ProjectPath"
& dotnet build $ProjectPath -c $Configuration | Out-Host
# Try to locate the built DLL and run it via 'dotnet <dll>' for deterministic execution.
$projectDir = Split-Path $ProjectPath -Parent
$projName = [System.IO.Path]::GetFileNameWithoutExtension($ProjectPath)
$buildDir = Join-Path $projectDir "bin\$Configuration"
$dll = $null
if (Test-Path $buildDir) {
$dll = Get-ChildItem -Path $buildDir -Recurse -Filter "$projName.dll" -File -ErrorAction SilentlyContinue | Select-Object -First 1
}
if ($dll) {
Write-Host "🔧 Running DLL with dotnet: $($dll.FullName)"
$exeArgs = @($dll.FullName)
if ($Args -and $Args.Length -gt 0) { $exeArgs += '--'; $exeArgs += $Args }
& dotnet @exeArgs | Out-Host
return $LASTEXITCODE
}
# Fallback to 'dotnet run' if DLL not found
Write-Host "🔧 Running project (fallback): $ProjectPath"
$argList = @('run','--project',$ProjectPath,'-c',$Configuration)
if ($Args -and $Args.Length -gt 0) { $argList += '--'; $argList += $Args }
& dotnet @argList | Out-Host
return $LASTEXITCODE
}
else {
# Background run: attempt to run the built DLL if available, otherwise start 'dotnet run'
$projectDir = Split-Path $ProjectPath -Parent
$projName = [System.IO.Path]::GetFileNameWithoutExtension($ProjectPath)
$buildDir = Join-Path $projectDir "bin\$Configuration"
$dll = $null
if (Test-Path $buildDir) {
$dll = Get-ChildItem -Path $buildDir -Recurse -Filter "$projName.dll" -File -ErrorAction SilentlyContinue | Select-Object -First 1
}
if ($dll) {
$argList = @($dll.FullName)
if ($Args -and $Args.Length -gt 0) { $argList += '--'; $argList += $Args }
Write-Host "🔧 Starting DLL in background: $($dll.FullName)"
Start-Process -FilePath dotnet -ArgumentList $argList -NoNewWindow -PassThru | Out-Null
return 0
}
$argList = @('run','--project',$ProjectPath,'-c',$Configuration)
if ($Args -and $Args.Length -gt 0) { $argList += '--'; $argList += $Args }
Write-Host "🔧 Starting project in background: $ProjectPath"
Start-Process -FilePath dotnet -ArgumentList $argList -NoNewWindow -PassThru | Out-Null
return 0
}
}
$exitCode = 0
if ($Cli) {
$rc = Run-Project -ProjectPath $cliProject -Wait $true
if ($rc -ne 0) { Write-Host "❌ CLI exited with code $rc" -ForegroundColor Red; $exitCode = $rc }
}
if ($Gui) {
$rc = Run-Project -ProjectPath $guiProject -Wait $true
if ($rc -ne 0) { Write-Host "❌ GUI exited with code $rc" -ForegroundColor Red; $exitCode = $rc }
}
exit $exitCode