-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcore_logic.ps1
More file actions
209 lines (180 loc) · 7.34 KB
/
core_logic.ps1
File metadata and controls
209 lines (180 loc) · 7.34 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
# Source this file in your PowerShell Profile
# Usage: . "$SwitchFile"
# Reset root to ensure no stale values
$global:FlutterRepoRoot = $null
if ($PSScriptRoot) {
$global:FlutterRepoRoot = $PSScriptRoot
}
else {
$global:FlutterRepoRoot = Get-Location | Select-Object -ExpandProperty Path
}
function Get-FlutterWorktrees {
if (Test-Path "$global:FlutterRepoRoot" -PathType Container) {
$gitArgs = @("-C", "$global:FlutterRepoRoot", "worktree", "list")
try {
$output = & git $gitArgs 2>$null
if ($LASTEXITCODE -eq 0) {
$output | Where-Object { $_ -notmatch "\(bare\)" } | ForEach-Object {
if ($_ -match '^(?<path>.*?)\s+[0-9a-f]+\s+(?<extra>.*)$') {
$fullPath = $matches['path']
$extra = $matches['extra']
# Calculate DirName (Relative path or Leaf name)
$dirName = ""
# Normalize for comparison (Git output usually has forward slashes or matches system)
# We use simple string operations.
# Assuming $global:FlutterRepoRoot is normalized to system separators by PSScriptRoot logic usually?
# But git output might use forward slashes even on Windows.
$normFull = $fullPath -replace '[\\/]', [System.IO.Path]::DirectorySeparatorChar
$normRoot = $global:FlutterRepoRoot -replace '[\\/]', [System.IO.Path]::DirectorySeparatorChar
if ($normFull -eq $normRoot) {
$dirName = Split-Path -Leaf $normFull
}
elseif ($normFull.StartsWith($normRoot, [System.StringComparison]::OrdinalIgnoreCase)) {
# Remove root prefix and leading slash
$dirName = $normFull.Substring($normRoot.Length).TrimStart([System.IO.Path]::DirectorySeparatorChar)
}
else {
# Fallback
$dirName = Split-Path -Leaf $normFull
}
$branch = ""
if ($extra -match '\[(?<br>.*?)\]') {
$branch = $matches['br']
}
[PSCustomObject]@{
Path = $normFull
DirName = $dirName
Branch = $branch
}
}
}
}
}
catch {
Write-Warning "Could not execute 'git worktree list'. Ensure git is installed and in your PATH."
}
}
}
function fswitch {
param(
[Parameter(Mandatory = $true, Position = 0)]
[ArgumentCompleter({
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
$worktrees = Get-FlutterWorktrees
$targets = @()
if ($worktrees) {
$targets += $worktrees.DirName
$targets += $worktrees.Branch
}
$targets | Where-Object { $_ -like "$wordToComplete*" } | Sort-Object -Unique | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
})]
[string]$Target
)
$worktrees = Get-FlutterWorktrees
$resolvedWt = $null
if ($worktrees) {
foreach ($wt in $worktrees) {
if ($wt.DirName -eq ".bare") {
continue
}
if ($Target -eq $wt.DirName -or ($wt.Branch -and $Target -eq $wt.Branch)) {
$resolvedWt = $wt
break
}
}
}
if ($null -eq $resolvedWt) {
Write-Error "❌ Invalid target: '$Target'"
Write-Host " Available contexts:"
if ($worktrees) {
foreach ($wt in $worktrees) {
$bInfo = if ($wt.Branch) { $wt.Branch } else { "detached" }
Write-Host " - $($wt.DirName) ($bInfo)"
}
}
else {
Write-Host " (No worktrees found. Check if git is installed and '$global:FlutterRepoRoot' is a valid repo.)"
}
return
}
# 1. Clean Path
$sepChar = [System.IO.Path]::PathSeparator
if ($null -ne $env:PATH) {
$currentPath = $env:PATH.Split($sepChar, [System.StringSplitOptions]::RemoveEmptyEntries)
}
else {
$currentPath = @()
}
$cleanPath = @($currentPath) | Where-Object {
$path = $_
if ([string]::IsNullOrWhiteSpace($path)) {
return $false
}
if (-not [string]::IsNullOrEmpty($global:FlutterRepoRoot)) {
$normPath = $path.TrimEnd([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar)
$normRoot = $global:FlutterRepoRoot.TrimEnd([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar)
if ($normPath.StartsWith($normRoot)) {
return $false
}
}
return $true
}
# 2. Update Path
# Use the FULL PATH from the resolved worktree
$newBin = Join-Path $resolvedWt.Path "bin"
$etBin = Join-Path $resolvedWt.Path "engine\src\flutter\bin"
if (-not (Test-Path $newBin -PathType Container)) {
Write-Error "❌ Error: Flutter bin directory not found at '$newBin'"
return
}
if (-not $IsWindows) {
$flutterBin = Join-Path $newBin "flutter"
if (Test-Path $flutterBin) {
if (Get-Command chmod -ErrorAction SilentlyContinue) {
chmod +x "$flutterBin" 2>$null
}
}
}
$pathsToAdd = @($newBin)
if (Test-Path $etBin -PathType Container) {
$pathsToAdd += $etBin
}
if ($cleanPath.Count -gt 0) {
$env:PATH = ($pathsToAdd -join $sepChar) + $sepChar + ($cleanPath -join $sepChar)
}
else {
$env:PATH = ($pathsToAdd -join $sepChar)
}
# 3. Verify
Write-Host "✅ Switched to Flutter $($resolvedWt.DirName)" -ForegroundColor Green
$flutterPath = (Get-Command flutter -ErrorAction SilentlyContinue).Source
$dartPath = (Get-Command dart -ErrorAction SilentlyContinue).Source
Write-Host " Flutter: $flutterPath"
Write-Host " Dart: $dartPath"
}
function fcd {
$flutterPath = Get-Command flutter -ErrorAction SilentlyContinue
if ($null -eq $flutterPath) {
Write-Error "❌ Flutter command not found. Run 'fswitch <target>' first."
return
}
# flutterPath.Source will be something like ...\flutter_repo\master\bin\flutter.bat
$binDir = Split-Path $flutterPath.Source -Parent
# We expect 'bin' to be the parent. Go one level up.
if ((Split-Path $binDir -Leaf) -eq "bin") {
$rootDir = Split-Path $binDir -Parent
Set-Location $rootDir
}
else {
# Fallback if structure is weird, though fswitch enforces bin
Set-Location $binDir
}
}
Set-Alias -Name froot -Value fcd
# Optional: Default to a version on load if no flutter is found
if (-not (Get-Command flutter -ErrorAction SilentlyContinue)) {
# Switch to master
fswitch master
}