|
67 | 67 | (when process |
68 | 68 | ;; Add shutdown hook |
69 | 69 | (try |
70 | | - (.addShutdownHook |
| 70 | + (.addShutdownHook |
71 | 71 | (Runtime/getRuntime) |
72 | 72 | (Thread. #(destroy-nrepl-process process))) |
73 | 73 | (log/debug "Added shutdown hook for nREPL process cleanup") |
74 | 74 | (catch Exception e |
75 | | - (log/warn e "Failed to add shutdown hook for process cleanup"))) |
| 75 | + (log/warn e "Failed to add shutdown hook for process cleanup"))) |
76 | 76 | process)) |
77 | 77 |
|
78 | 78 | (defn wait-for-port |
|
199 | 199 | (do |
200 | 200 | (log/debug "Port already provided without start command, skipping auto-start") |
201 | 201 | false) |
202 | | - |
| 202 | + |
203 | 203 | ;; CLI condition: start-nrepl-cmd AND project-dir provided (regardless of port) |
204 | 204 | (and start-nrepl-cmd project-dir) |
205 | 205 | (do |
206 | 206 | (log/info "Auto-start condition met: CLI args provided") |
207 | 207 | true) |
208 | | - |
| 208 | + |
209 | 209 | ;; Config file condition |
210 | 210 | :else |
211 | 211 | (if-let [config (load-config-if-exists project-dir)] |
|
216 | 216 | false) |
217 | 217 | false)))) |
218 | 218 |
|
| 219 | +(defn add-project-dir [nrepl-args] |
| 220 | + (if (:start-nrepl-cmd nrepl-args) |
| 221 | + (assoc nrepl-args :project-dir (System/getProperty "user.dir")) |
| 222 | + nrepl-args)) |
| 223 | + |
219 | 224 | (defn maybe-start-nrepl-process |
220 | 225 | "Main wrapper that conditionally starts an nREPL process. |
221 | 226 | Returns updated nrepl-args with discovered :port if process was started, |
222 | 227 | otherwise returns nrepl-args unchanged." |
223 | 228 | [nrepl-args] |
224 | | - (if (should-start-nrepl? nrepl-args) |
225 | | - (let [;; Load config and merge with CLI args (CLI takes precedence) |
226 | | - config (load-config-if-exists (:project-dir nrepl-args)) |
227 | | - merged-args (merge config nrepl-args) |
228 | | - {:keys [start-nrepl-cmd parse-nrepl-port port] |
229 | | - :or {parse-nrepl-port true}} merged-args] |
| 229 | + (let [nrepl-args' (add-project-dir nrepl-args)] |
| 230 | + (if (should-start-nrepl? nrepl-args') |
| 231 | + (let [;; Load config and merge with CLI args (CLI takes precedence) |
| 232 | + config (load-config-if-exists (:project-dir nrepl-args')) |
| 233 | + merged-args (merge config nrepl-args') |
| 234 | + {:keys [start-nrepl-cmd parse-nrepl-port port] |
| 235 | + :or {parse-nrepl-port true}} merged-args] |
230 | 236 | ;; Validate: if parse-nrepl-port is false, port must be provided |
231 | | - (when (and (false? parse-nrepl-port) (not port)) |
232 | | - (throw |
233 | | - (ex-info "When :parse-nrepl-port is false, :port must be provided" |
234 | | - {:start-nrepl-cmd start-nrepl-cmd |
235 | | - :parse-nrepl-port parse-nrepl-port}))) |
236 | | - (log/info "Starting nREPL process automatically") |
237 | | - (let [{:keys [port process]} (start-nrepl-process merged-args)] |
238 | | - (if port |
239 | | - (do |
240 | | - (log/info (str "Using discovered port: " port)) |
241 | | - (assoc nrepl-args :port port :nrepl-process process)) |
| 237 | + (when (and (false? parse-nrepl-port) (not port)) |
| 238 | + (throw |
| 239 | + (ex-info "When :parse-nrepl-port is false, :port must be provided" |
| 240 | + {:start-nrepl-cmd start-nrepl-cmd |
| 241 | + :parse-nrepl-port parse-nrepl-port}))) |
| 242 | + (log/info "Starting nREPL process automatically") |
| 243 | + (let [{:keys [port process]} (start-nrepl-process merged-args)] |
| 244 | + (if port |
| 245 | + (do |
| 246 | + (log/info (str "Using discovered port: " port)) |
| 247 | + (assoc nrepl-args :port port :nrepl-process process)) |
242 | 248 | ;; If parse-nrepl-port was false, we still started the process |
243 | 249 | ;; but didn't get a port - this is an error for our use case |
244 | | - (throw (ex-info "nREPL process started but no port available" |
245 | | - {:start-nrepl-cmd start-nrepl-cmd}))))) |
246 | | - (do |
247 | | - (log/debug "Auto-start conditions not met, using existing behavior") |
248 | | - nrepl-args))) |
| 250 | + (throw (ex-info "nREPL process started but no port available" |
| 251 | + {:start-nrepl-cmd start-nrepl-cmd}))))) |
| 252 | + (do |
| 253 | + (log/debug "Auto-start conditions not met, using existing behavior") |
| 254 | + nrepl-args)))) |
0 commit comments