-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcore_logic.sh
More file actions
170 lines (144 loc) · 5.33 KB
/
core_logic.sh
File metadata and controls
170 lines (144 loc) · 5.33 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
# Source this file in your .bashrc or .zshrc
# Usage: source $SWITCH_FILE
# Auto-detect the repo root based on this script's location
if [ -n "$BASH_SOURCE" ]; then
_SCRIPT_PATH="${BASH_SOURCE[0]}"
elif [ -n "$ZSH_VERSION" ]; then
_SCRIPT_PATH="${(%):-%x}"
else
_SCRIPT_PATH="$0"
fi
# Use pwd -P to resolve symlinks to physical path
FLUTTER_REPO_ROOT="$(cd "$(dirname "$_SCRIPT_PATH")" >/dev/null 2>&1 && pwd -P)"
_fswitch_get_worktree_data() {
if command -v git &> /dev/null && [ -d "$FLUTTER_REPO_ROOT" ]; then
git -C "$FLUTTER_REPO_ROOT" worktree list 2>/dev/null | grep -v "(bare)"
fi
}
_fswitch_resolve() {
local target=$1
if [[ -z "$FLUTTER_REPO_ROOT" ]]; then
return
fi
local resolved=""
while read -r wt_path hash branch_info rest; do
local rel_path="${wt_path#$FLUTTER_REPO_ROOT/}"
local is_root=0
if [[ "$rel_path" == "$wt_path" && "$wt_path" == "$FLUTTER_REPO_ROOT" ]]; then
rel_path="."
is_root=1
fi
if [[ "$rel_path" == ".bare" ]]; then
continue
fi
local branch_name="${branch_info#\[}"
branch_name="${branch_name%\]}"
if [[ "$target" == "$rel_path" ]] || [[ "$target" == "$branch_name" ]]; then
resolved="$rel_path"
break
fi
done < <(_fswitch_get_worktree_data)
echo "$resolved"
}
fswitch() {
local target=$1
if [[ -z "$FLUTTER_REPO_ROOT" ]]; then
echo "❌ Error: FLUTTER_REPO_ROOT is not set. Could not detect repo root."
return 1
fi
# Resolve target to directory (relative path from root, or ".")
local dir_name=$(_fswitch_resolve "$target")
if [[ -z "$dir_name" ]]; then
echo "❌ Invalid target: '$target'"
echo " Available contexts:"
_fswitch_get_worktree_data | while read -r wt_path hash branch_info rest; do
local d="${wt_path#$FLUTTER_REPO_ROOT/}"
if [[ "$d" == "$wt_path" && "$wt_path" == "$FLUTTER_REPO_ROOT" ]]; then
d="."
fi
local b="${branch_info#\[}"
b="${b%\]}"
echo " - $d ($b)"
done
return 1
else
# Target resolved, check bin
local full_bin_path
local et_bin_path
# Handle absolute paths (e.g. symlink resolution mismatches or external worktrees)
if [[ "$dir_name" == /* ]]; then
full_bin_path="$dir_name/bin"
et_bin_path="$dir_name/engine/src/flutter/bin"
elif [[ "$dir_name" == "." ]]; then
full_bin_path="$FLUTTER_REPO_ROOT/bin"
et_bin_path="$FLUTTER_REPO_ROOT/engine/src/flutter/bin"
else
full_bin_path="$FLUTTER_REPO_ROOT/$dir_name/bin"
et_bin_path="$FLUTTER_REPO_ROOT/$dir_name/engine/src/flutter/bin"
fi
if [[ ! -d "$full_bin_path" ]]; then
echo "❌ Error: Flutter bin directory not found at:"
echo " $full_bin_path"
return 1
else
# 2. Clean PATH
# We remove any path containing the FLUTTER_REPO_ROOT to avoid conflicts
# This prevents having both 'master' and 'stable' in PATH at the same time
# Use -F to ensure fixed string matching (no regex)
local new_path=$(echo "$PATH" | tr ':' '\n' | grep -vF "$FLUTTER_REPO_ROOT" | tr '\n' ':' | sed 's/:$//')
# 3. Update PATH
# Prepend the new target's bin directory (and et path if it exists)
if [[ -d "$et_bin_path" ]]; then
export PATH="$full_bin_path:$et_bin_path:$new_path"
else
export PATH="$full_bin_path:$new_path"
fi
# 4. Verify
echo "✅ Switched to Flutter $dir_name"
echo " Flutter: $(which flutter)"
echo " Dart: $(which dart)"
fi
fi
}
_fswitch_completion() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local targets=()
while read -r wt_path hash branch_info rest; do
local dir_name="${wt_path#$FLUTTER_REPO_ROOT/}"
if [[ "$dir_name" == "$wt_path" && "$wt_path" == "$FLUTTER_REPO_ROOT" ]]; then
dir_name="${wt_path##*/}"
fi
local branch_name="${branch_info#\[}"
branch_name="${branch_name%\]}"
targets+=("$dir_name")
if [ -n "$branch_name" ]; then
targets+=("$branch_name")
fi
done < <(_fswitch_get_worktree_data)
# Deduplicate and generate completion
local unique_targets=$(echo "${targets[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')
COMPREPLY=( $(compgen -W "${unique_targets}" -- ${cur}) )
}
complete -F _fswitch_completion fswitch
fcd() {
local flutter_path
flutter_path=$(command -v flutter)
if [[ -z "$flutter_path" ]]; then
echo "❌ Flutter command not found. Run 'fswitch <target>' first."
return 1
fi
local bin_dir
bin_dir=$(dirname "$flutter_path")
# Check if we are in 'bin' and go up one level
if [[ "$(basename "$bin_dir")" == "bin" ]]; then
cd "$(dirname "$bin_dir")"
else
cd "$bin_dir"
fi
}
alias froot=fcd
# Optional: Default to master on load if no flutter is found
if ! command -v flutter &> /dev/null; then
fswitch master
echo "ℹ️ Flutter environment loaded. Use 'fswitch stable' or 'fswitch master' to activate."
fi