Skip to content

Instantly share code, notes, and snippets.

@joshkh
Created March 13, 2017 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshkh/dacf13b76ef1f847a2b2d15b1bb37db2 to your computer and use it in GitHub Desktop.
Save joshkh/dacf13b76ef1f847a2b2d15b1bb37db2 to your computer and use it in GitHub Desktop.
intermine.dg.repl
(ns intermine.dg.repl
"
/*
* Copyright (C) 2016 Intermine
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. See the LICENSE file for more
* information or http://www.gnu.org/copyleft/lesser.html.
*
*/
Some handy functions for working in a REPL
"
(:use
clojure.pprint
clojure.stacktrace
clojure.inspector)
(:require
[intermine.dg.formats :as f]
[intermine.dg.factor-graphs]
[intermine.dg.server :as s]
[intermine.dg.mixtures.core]
[intermine.dg.ui.css :as css]
[figwheel-sidecar.repl-api :as fig]
[taoensso.timbre :as timbre]
[manifold.stream :as ms]
[cljs.build.api :as cljs]
[clojure.tools.nrepl :as nrepl]
[robert.hooke :as robert-hooke]
[me.raynes.conch.low-level :as shh]
[me.raynes.conch :refer [programs with-programs let-programs] :as sh]
[taoensso.timbre :as timbre :refer [debug]]
[taoensso.nippy :as nippy]
[clj-time.core :as tc]
[clj-time.format :as tf]
[clojure.java.io :as jio]
[clj-time.core :as cljt]
[datascript.core :as d]
[clojure.string :as string]))
(timbre/merge-config! s/logging-config)
(defonce my-server (atom nil))
(def cljs-options
{
:source-paths ["src"]
:pro {
:output-to "public/js/main.js" ; default: target/cljsbuild-main.js
:output-dir "public/js"
:asset-path "js"
:optimizations :advanced
}
:dev {
:source-map true
:output-to "public/js/main.js" ; default: target/cljsbuild-main.js
:output-dir "public/js"
:asset-path "js"
:optimizations :none
:main "intermine.dg.ui.main"
:pretty-print true
:parallel-build true
}
})
(defn cljs-build []
(cljs/build "src" (:dev cljs-options)))
(defn cljs-build-pro []
(cljs/build "src" (:pro cljs-options)))
(defn cljs-watch []
(cljs/watch "src" (:compiler cljs-options)))
(defn update-fn!
([naymspace fn-name]
(let [
n (read-string (str "(" (slurp (str "./src/" (string/replace naymspace "." "/") ".cljc")) ")"))
]
(println n)
)))
(defn make-capture-responses!
"Returns a function that records all REPL request-response pairs in the given list session"
([session]
(fn [f & args]
(let [v (apply f args)]
(try
(robert.hooke/with-hooks-disabled clojure.tools.nrepl.misc/response-for
(do
(cond (and (:value v) (not (.startsWith (:code (first args)) "(cursive")))
(swap! session conj {:request (:code (first args)) :response (:value v) :dt (cljt/now)}))))
(catch Exception e))
v))))
(defn persist-session! [session]
(try
(with-open [out (jio/output-stream "repl.log")]
(.write out (nippy/freeze (filter taoensso.nippy.utils/freezable? session))))
(catch Exception e (println e))))
(defn get-session! []
(try
(with-open [in (jio/input-stream "repl.log")]
(let [bytes (byte-array (.available in))]
(do
(.read in bytes 0 (.available in))
(nippy/thaw bytes))))
(catch Exception e (println e))))
(defn reconfigure-repl-capture! []
(defonce session (atom []))
(defonce time-format (tf/formatters :basic-date-time-no-ms))
(robert-hooke/clear-hooks #'clojure.tools.nrepl.misc/response-for)
(persist-session! @session)
(reset! session [])
(robert-hooke/add-hook #'clojure.tools.nrepl.misc/response-for (make-capture-responses! session)))
(defn get-papers [path] (map (fn [f] (let [less (programs less)] (less (str path f)))) (string/split-lines ((programs ls) path))))
(defn get-doi [s] (first (re-find #"\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?![\"&\'<>])\S)+)\b" s)))
(defn doi-bib [doi] (let [d (shh/stream-to-string (shh/proc "curl" "-LH" "Accept: application/x-bibtex; charset=utf-8" (str "http://dx.doi.org/" doi)) :out)] (if (string/starts-with? d "@") d "")) )
(defn see [p] (println p) p)
(defn all-dois [path]
(apply str
(map
(fn [paper]
(-> paper
get-doi
doi-bib
see
(str "\n")
(string/replace #"\\t" "")
(string/replace #"\"" "")))
(get-papers path))))
(defn send! [m] (do (ms/put! @(:channel @my-server) (pr-str m))))
(defn assoc*! [p m] (do (ms/put! @(:channel @my-server) (pr-str {:verb :assoc-in :type :stuff :path p :m m}))))
(defn reload-style [] (do (println "reload css") (ms/put! @(:channel @my-server) (pr-str {:verb :new :type :style :style (css/make-main-gui-style)}))))
(defn stop! [] (cond @my-server (try (.close (:server @my-server)) (catch Exception e))) (reset! my-server nil))
(defn restart!
([]
(restart! {:host "127.0.0.1" :port 8081}))
([config] (stop!)
(future (reset! my-server (s/start config)))))
(defn -main [& args]
(let [args (or args ["127.0.0.1" "8081"])]
(println "args" args)
;(println "Building client...")
;(cljs-build)
(let [[host port] args
a {:host (str host) :port (Long/parseLong port)}]
(restart! a)
(println "running server on" (str "http://" host ":" port "/")))
(while true)))
; {:host "172.25.136.81" :port 8080}
(println "to run the server (restart!)")
(println "to stop it (stop!) ")
(println "server state is in my-server")
(println "Remember to (cljs-build) the client if you haven't already")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment