This repository was archived by the owner on Sep 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli
More file actions
executable file
·173 lines (159 loc) · 4.94 KB
/
Copy pathcli
File metadata and controls
executable file
·173 lines (159 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env bash
#
# Helper script to launch Brokk CLI from either a specific JAR or the most-recent
# built artifact. By default it scans app/build/libs for the latest brokk-*.jar.
set -euo pipefail
# Determine the directory this script lives in, resolving any symlinks.
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
LIB_DIR="$SCRIPT_DIR/app/build/libs"
CLI_JAR=""
CLASSPATH=""
# Launch BprCli.
# We use PGC because it's more space-efficient than the default G1,
# and in cli mode we're not latency sensitive.
# Remote debugger is ENABLED by default. Disable via --no-debug or BROKK_DEBUG=false.
# Supported CLI flags:
# --debug[=PORT] enable JDWP on PORT (default 5005)
# --debug-port=PORT same as --debug=PORT
# --debug-suspend wait for debugger to attach before starting
# --no-debug disable JDWP (overrides env/default)
# Env vars:
# BROKK_CLI_JAR=<path> path to a specific Brokk CLI jar
# BROKK_DEBUG=true|false|PORT enable/disable debug; PORT overrides port (true/1/yes/on; false/0/no/off)
# BROKK_DEBUG_PORT=PORT port to listen on (default 5005)
# BROKK_DEBUG_SUSPEND=y|n whether to suspend on start (default n)
declare -a JVM_ARGS=()
declare -a APP_ARGS=()
DEBUG_ENABLED=false
DEBUG_PORT="${BROKK_DEBUG_PORT:-5005}"
DEBUG_SUSPEND="${BROKK_DEBUG_SUSPEND:-n}"
# Environment override for debug behavior
if [[ -n "${BROKK_DEBUG:-}" ]]; then
brokk_debug_lc="$(printf '%s' "${BROKK_DEBUG}" | tr '[:upper:]' '[:lower:]')"
case "${brokk_debug_lc}" in
false|0|no|off)
DEBUG_ENABLED=false
;;
true|1|yes|on)
DEBUG_ENABLED=true
;;
*)
if [[ "${BROKK_DEBUG}" =~ ^[0-9]+$ ]]; then
DEBUG_ENABLED=true
DEBUG_PORT="${BROKK_DEBUG}"
else
# Unknown value; keep default enabled
DEBUG_ENABLED=true
fi
;;
esac
fi
while [[ $# -gt 0 ]]; do
case "$1" in
--cli-jar)
if [[ $# -lt 2 ]]; then
echo "Error: --cli-jar requires a path argument." >&2
exit 1
fi
CLI_JAR="$2"
shift 2
continue
;;
--cli-jar=*)
CLI_JAR="${1#--cli-jar=}"
;;
--debug)
DEBUG_ENABLED=true
;;
--debug=*)
DEBUG_ENABLED=true
DEBUG_PORT="${1#--debug=}"
;;
--debug-port=*)
DEBUG_ENABLED=true
DEBUG_PORT="${1#--debug-port=}"
;;
--debug-suspend)
DEBUG_ENABLED=true
DEBUG_SUSPEND="y"
;;
--no-debug)
DEBUG_ENABLED=false
;;
-X*|-D*)
JVM_ARGS+=("$1")
;;
*)
APP_ARGS+=("$1")
;;
esac
shift
done
if [[ -z "$CLI_JAR" && -n "${BROKK_CLI_JAR:-}" ]]; then
CLI_JAR="$BROKK_CLI_JAR"
fi
if [[ -n "$CLI_JAR" ]]; then
# Expand ~ in the path.
CLI_JAR="${CLI_JAR/#\~/$HOME}"
# Resolve relative paths against current working directory.
if [[ "$CLI_JAR" != /* ]]; then
CLI_JAR="$PWD/$CLI_JAR"
fi
if [[ ! -f "$CLI_JAR" ]]; then
echo "Error: specified BROKK_CLI_JAR not found: $CLI_JAR" >&2
exit 1
fi
CLASSPATH="$CLI_JAR"
else
# Ensure at least one brokk*.jar exists (matches brokk.jar and brokk-*.jar)
if ! ls "$LIB_DIR"/brokk*.jar &>/dev/null; then
echo "Error: No brokk*.jar found in $LIB_DIR" >&2
exit 1
fi
# Use the most recently modified brokk*.jar as the classpath
CLASSPATH="$(ls -t "$LIB_DIR"/brokk*.jar | head -n 1)"
fi
# Determine which BprCli class exists in the selected JAR
if jar -tf "$CLASSPATH" | grep -q '^ai/brokk/cli/BprCli\.class$'; then
CLASS_NAME="ai.brokk.cli.BprCli"
elif jar -tf "$CLASSPATH" | grep -q '^io/github/jbellis/brokk/cli/BprCli\.class$'; then
CLASS_NAME="io.github.jbellis.brokk.cli.BprCli"
else
echo "Error: BprCli class not found in $CLASSPATH" >&2
exit 1
fi
# Build the final JVM argument list, adding a default -Xmx1G unless the caller
# already supplied an explicit -Xmx option.
declare -a EFFECTIVE_JVM_ARGS=("-ea" "-XX:+UseParallelGC")
# Always use test logging configuration
EFFECTIVE_JVM_ARGS+=("-Dlog4j.configurationFile=$SCRIPT_DIR/app/src/test/resources/log4j2-test.xml")
xmx_specified=false
for arg in "${JVM_ARGS[@]-}"; do
if [[ $arg == -Xmx* ]]; then
xmx_specified=true
break
fi
done
if ! $xmx_specified; then
EFFECTIVE_JVM_ARGS+=("-Xmx1G")
fi
# Append any user-supplied JVM args (which may include their own -Xmx)
EFFECTIVE_JVM_ARGS+=(${JVM_ARGS[@]+"${JVM_ARGS[@]}"})
# Add JDWP agent if debugging is enabled and not already specified by the user
jdwp_specified=false
for arg in "${JVM_ARGS[@]-}"; do
if [[ $arg == -agentlib:jdwp* || $arg == -Xrunjdwp* ]]; then
jdwp_specified=true
break
fi
done
if $DEBUG_ENABLED && ! $jdwp_specified; then
EFFECTIVE_JVM_ARGS+=("-agentlib:jdwp=transport=dt_socket,server=y,suspend=$DEBUG_SUSPEND,address=*:$DEBUG_PORT")
echo "Debugging enabled on port $DEBUG_PORT (suspend=$DEBUG_SUSPEND)" >&2
fi
# Launch BprCli with the determined class name
exec java \
"${EFFECTIVE_JVM_ARGS[@]}" \
-cp "$CLASSPATH" \
"$CLASS_NAME" \
"${APP_ARGS[@]-}"