Skip to content

Commit

Permalink
Add a REPL (#35)
Browse files Browse the repository at this point in the history
* Upgrade Geni to v0.0.42

* Upgrade Clojure to v1.11.1

* Fix dependencies for REPL

* Implement Datajure REPL
  • Loading branch information
skylee03 authored Dec 27, 2023
1 parent cc40224 commit c4c8a0f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
18 changes: 17 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,24 @@
"--add-opens=java.base/java.util.concurrent=ALL-UNNAMED"
"--add-opens=java.base/sun.nio.ch=ALL-UNNAMED"]
:profiles {:uberjar {:aot :all
:jvm-opts ["-Dclojure.compiler.direct-linking=true"]}
:plugins [[arctype/log4j2-plugins-cache "1.0.0"]]
:middleware [leiningen.log4j2-plugins-cache/middleware]
:manifest {"Multi-Release" true}
:dependencies [[log4j/log4j "1.2.17"]
[org.apache.logging.log4j/log4j-core "2.21.0"]
;; Arrow
[org.apache.arrow/arrow-memory-core "4.0.0"]
[org.apache.arrow/arrow-vector "4.0.0"
:exclusions [commons-codec com.fasterxml.jackson.core/jackson-databind]]
;; Spark
[org.apache.spark/spark-avro_2.12 "3.3.3"]
[org.apache.spark/spark-core_2.12 "3.3.3"]
[org.apache.spark/spark-hive_2.12 "3.3.3"]
[org.apache.spark/spark-mllib_2.12 "3.3.3"]
[org.apache.spark/spark-sql_2.12 "3.3.3"]
[org.apache.spark/spark-streaming_2.12 "3.3.3"]]}
:test {:dependencies [[org.apache.logging.log4j/log4j-core "2.21.0"]]}
:repl {:dependencies [[org.apache.logging.log4j/log4j-core "2.21.0"]]}
:provided {:dependencies [[com.fasterxml.jackson.core/jackson-core "2.15.3"]
[com.fasterxml.jackson.core/jackson-annotations "2.15.3"]
[org.apache.spark/spark-core_2.12 "3.3.3" :exclusions [org.apache.logging.log4j/log4j-slf4j-impl]]
Expand Down
41 changes: 40 additions & 1 deletion src/datajure/dsl.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns datajure.dsl
(:refer-clojure :exclude [print]))
(:refer-clojure :exclude [print])
(:require [datajure.repl :as repl])
(:gen-class))

(require '[tech.v3.dataset :as ds]
'[tablecloth.api :as tc]
Expand Down Expand Up @@ -130,3 +132,40 @@
"Choose `back` as the backend implementation."
[back]
(reset! backend back))

(def init-eval
"The initial form evaluated when the REPL starts up."
'(do
(require
'[tech.v3.dataset :as ds]
'[tablecloth.api :as tc]
'[clojask.dataframe :as ck]
'[zero-one.geni.core :as g]
'[datajure.dsl :as dtj])))

(defn- custom-stream [script-path]
(try
(-> (str (slurp script-path) "\nexit\n")
(.getBytes "UTF-8")
(java.io.ByteArrayInputStream.))
(catch Exception _
(println (str "Cannot find file " script-path "!"))
(System/exit 1))))

(defn -main
"The Datajure CLI entrypoint.
It does the following:
- Prints a welcome note.
- Launches an nREPL server, which writes to `.nrepl-port` for a
text editor to connect to.
- Starts a REPL(-y).
"
[& args]
(println repl/welcome-note)
(let [script-path (if (empty? args) nil (first args))]
(repl/launch-repl (merge {:port (+ 65001 (rand-int 500))
:custom-eval init-eval}
(when script-path
{:input-stream (custom-stream script-path)}))))
(System/exit 0))
31 changes: 31 additions & 0 deletions src/datajure/repl.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(ns datajure.repl
(:require
[clojure.string]
[clojure.java.io :as io]
[nrepl.server]
[reply.main]))

(defn- client [opts]
(let [port (:port opts)
host (or (:host opts) "127.0.0.1")
default-opts {:color true :history-file ".nrepl-history"}
opts (assoc (merge default-opts opts) :attach (str host ":" port))]
(reply.main/launch-nrepl opts)))

(defn datajure-prompt
"Custom Datajure REPL prompt."
[ns-]
(str "datajure-repl (" ns- ")> "))

(defn launch-repl
"Starts an nREPL server and steps into a REPL-y."
[opts]
(let [port (:port opts)
server (nrepl.server/start-server :port port)]
(doto (io/file ".nrepl-port") .deleteOnExit (spit port))
(client (merge {:custom-prompt datajure-prompt} opts))
(nrepl.server/stop-server server)))

(def welcome-note
"A REPL welcome note."
(str "Datajure " "1.0.1"))

0 comments on commit c4c8a0f

Please sign in to comment.