From 2b20c780f4983023c6cebc9eb51532b27c7a0aa8 Mon Sep 17 00:00:00 2001 From: Jay Zawrotny Date: Thu, 13 Dec 2018 12:57:41 -0500 Subject: [PATCH] Setup middleware to warn user and fail-safely if cognitect.rebl is not found. --- src/nrebl/middleware.clj | 50 ++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/src/nrebl/middleware.clj b/src/nrebl/middleware.clj index a7d3961..91fb346 100644 --- a/src/nrebl/middleware.clj +++ b/src/nrebl/middleware.clj @@ -1,13 +1,25 @@ (ns nrebl.middleware - (:require [nrepl + ;; TODO: Please look at my lein-2.8.1 branch for cleanup of imports and + ;; supporting legacy lein with dynamic imports. + (:require [clojure.tools.nrepl [middleware :refer [set-descriptor!]] [transport :as transport]] - [nrepl.middleware.interruptible-eval :as ev] - [cognitect.rebl.ui :as ui] - [cognitect.rebl :as rebl] [clojure.datafy :refer [datafy]]) (:import - nrepl.transport.Transport)) + clojure.tools.nrepl.transport.Transport)) + +(def rebl-exists? + (try + (require '[cognitect.rebl :as rebl]) + true + (catch java.io.FileNotFoundException _ + false))) + +;; Ensure cognitect.rebl namespace exists +(when-not rebl-exists? + (create-ns 'rebl) + (intern 'rebl 'submit identity) + (intern 'rebl 'ui identity)) (defn send-to-rebl! [{:keys [code] :as req} {:keys [value] :as resp}] (when-let [value (datafy value)] @@ -17,7 +29,7 @@ (defn- wrap-rebl-sender "Wraps a `Transport` with code which prints the value of messages sent to it using the provided function." - [{:keys [id op ^Transport transport] :as request} ] + [{:keys [id op ^Transport transport] :as request}] (reify Transport (recv [this] (.recv transport)) @@ -28,11 +40,16 @@ (send-to-rebl! request resp)) this))) -(defn wrap-nrebl [handler] - (fn [{:keys [id op transport] :as request}] - (if (= op "start-rebl-ui") - (rebl/ui) - (handler (assoc request :transport (wrap-rebl-sender request)))))) +(defn wrap-nrebl + [handler] + (if rebl-exists? + (fn [{:keys [id op transport] :as request}] + (cond + (= op "start-rebl-ui") (rebl/ui) + :else (handler (assoc request :transport (wrap-rebl-sender request))))) + (do + (println "WARNING: cognitect.rebl namespace was not found on classpath. nrebl.middlware could not be started.") + handler))) (set-descriptor! #'wrap-nrebl {:requires #{} @@ -49,17 +66,16 @@ (-> (nrepl/client conn 1000) ; message receive timeout required ;(nrepl/message {:op "inspect-nrebl" :code "[1 2 3 4 5 6 7 8 9 10 {:a :b :c :d :e #{5 6 7 8 9 10}}]"}) (nrepl/message {:op "eval" :code "(do {:a :b :c [1 2 3 4] :d #{5 6 7 8} :e (range 20)})"}) - nrepl/response-values - )) + nrepl/response-values)) + (with-open [conn (nrepl/connect :port 52756)] (-> (nrepl/client conn 1000) ; message receive timeout required (nrepl/message {:op "start-rebl-ui"}) - nrepl/response-values - )) + nrepl/response-values)) + (require '[nrepl.server :as ser]) (def nrep (ser/start-server :port 55804 - :handler (ser/default-handler #'wrap-nrebl))) - ) + :handler (ser/default-handler #'wrap-nrebl))))