-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfleet.sh
More file actions
156 lines (144 loc) · 4.95 KB
/
Copy pathfleet.sh
File metadata and controls
156 lines (144 loc) · 4.95 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
# fleet.sh — bash/zsh shell integration for the `fleet` CLI.
#
# Most commands forward straight to `python -m fleet`. The exception is
# `fleet open <task> [-F NAME]`, which has to change the parent shell's
# working directory — a child process can't do that. So this function
# calls `python -m fleet task path <task> [-F NAME]` to resolve the
# workspace path (Python knows about fleets, default vs override, etc.),
# then does the cd + `code` invocation in-shell.
#
# This is the bash/zsh equivalent of Fleet.psm1. PowerShell 7 runs on
# Linux/macOS too, so you can use either; this is here so you don't need
# pwsh just to get `fleet open`.
#
# One-time setup: source this file from your shell rc, replacing
# <install-dir> with the path where you cloned this repo:
#
# source <install-dir>/fleet.sh
#
# (e.g. source "$HOME/src/fleet/fleet.sh")
#
# It sets PYTHONPATH to the package's src/ for the duration of each call,
# so no `pip install` is needed. If you installed via `pip install -e .`
# the `fleet` entry point already exists and you only need the `open`
# helper below — but sourcing this file is harmless either way.
# Resolve the directory this script lives in (bash and zsh differ here).
if [ -n "${BASH_SOURCE:-}" ]; then
_fleet_self="${BASH_SOURCE[0]}"
elif [ -n "${ZSH_VERSION:-}" ]; then
_fleet_self="${(%):-%N}"
else
_fleet_self="$0"
fi
_FLEET_SRC="$(cd "$(dirname "$_fleet_self")/src" 2>/dev/null && pwd)"
unset _fleet_self
# Pick a Python launcher once. Prefers `python3` then `python`.
_fleet_python() {
if command -v python3 >/dev/null 2>&1; then
printf 'python3'
elif command -v python >/dev/null 2>&1; then
printf 'python'
else
return 1
fi
}
# Run `python -m fleet ...` with src/ prepended to PYTHONPATH, then
# restored. Component-aware so e.g. `.../src-experimental` doesn't
# false-match `.../src`.
_fleet_run() {
local py
py="$(_fleet_python)" || {
printf 'fleet: no Python interpreter found on PATH (tried python3, python). Install Python >= 3.10.\n' >&2
return 1
}
local old_pp="${PYTHONPATH:-}"
local sep=':'
# Prepend src/ unless it's already a discrete entry. The case glob
# wraps both sides in separators so `.../src` can't false-match
# `.../src-experimental`, and avoids touching IFS.
case "${sep}${old_pp}${sep}" in
*"${sep}${_FLEET_SRC}${sep}"*)
: ;; # already present (or _FLEET_SRC empty) — leave PYTHONPATH alone
*)
if [ -n "$_FLEET_SRC" ]; then
if [ -n "$old_pp" ]; then
export PYTHONPATH="${_FLEET_SRC}${sep}${old_pp}"
else
export PYTHONPATH="$_FLEET_SRC"
fi
fi
;;
esac
"$py" -m fleet "$@"
local rc=$?
if [ -n "$old_pp" ]; then
export PYTHONPATH="$old_pp"
else
unset PYTHONPATH
fi
return $rc
}
fleet() {
if [ "$#" -eq 0 ]; then
_fleet_run --help
return
fi
# `open` and `task open` need to mutate the parent shell — handle locally.
if [ "$1" = "open" ]; then
shift
_fleet_open "$@"
return
fi
if [ "$1" = "task" ] && [ "${2:-}" = "open" ]; then
shift 2
_fleet_open "$@"
return
fi
_fleet_run "$@"
}
_fleet_open() {
if [ "$#" -eq 0 ]; then
printf 'Usage: fleet open <task-name> [-F <fleet>]\n' >&2
return 2
fi
# Pass the entire remainder through to `task path` so -F / --fleet,
# quoted args, etc. are handled by argparse exactly once. `task path`
# writes only the workspace path to stdout; diagnostics go to stderr.
local workspace
workspace="$(_fleet_run task path "$@")"
local rc=$?
if [ "$rc" -ne 0 ]; then
return "$rc"
fi
# Trim to the first non-empty line.
workspace="$(printf '%s\n' "$workspace" | sed -n '/[^[:space:]]/{p;q;}')"
if [ -z "$workspace" ]; then
printf 'fleet task path produced no output\n' >&2
return 1
fi
if [ ! -d "$workspace" ]; then
printf 'Resolved workspace does not exist: %s\n' "$workspace" >&2
return 1
fi
cd "$workspace" || return 1
if command -v code >/dev/null 2>&1; then
code "$workspace"
else
printf 'VS Code (code) not on PATH; skipping launch.\n' >&2
fi
}
# Source the shell completion glue. The script lives in the package so the
# Python engine and shell glue stay in lockstep. zsh and bash get separate
# files because they speak slightly different completion dialects.
if [ -n "${ZSH_VERSION:-}" ]; then
_fleet_completion_script="${_FLEET_SRC}/fleet/completions/fleet.zsh"
elif [ -n "${BASH_VERSION:-}" ]; then
_fleet_completion_script="${_FLEET_SRC}/fleet/completions/fleet.bash"
else
_fleet_completion_script=""
fi
if [ -n "$_fleet_completion_script" ] && [ -f "$_fleet_completion_script" ]; then
# shellcheck disable=SC1090
. "$_fleet_completion_script"
fi
unset _fleet_completion_script