Giter VIP home page Giter VIP logo

babashka.nrepl's Introduction

babashka.nrepl

The nREPL server from babashka as a library, so it can be used from other sci-based CLIs.

Sci is available here: https://github.com/borkdude/sci

Usage

To start a server, call babashka.nrepl.server/start-server!. To stop a server call babashka.nrepl.server/stop-server!

Starting a Server

Before you start a server you will need to create a sci context:

(require '[sci.core :as sci])
(require '[sci.addons :as addons])

(def opts (-> {:namespaces {'foo.bar {'x 1}}} addons/future))
(def sci-ctx (sci/init opts))

A sci context is derived from options as documented in sci.core/eval-string and contains the runtime state of a sci session. This context is re-used over successive REPL invocations.

To start an nREPL server in your project, call babashka.nrepl.server/start-server!. The call takes two arguments, your initial sci context and some options including the IP address to bind to, the port number and optional debug and quiet flags. E.g.:

(babashka.nrepl.server/start-server! sci-ctx {:host "127.0.0.1" :port 23456})
;; Started nREPL server at 127.0.0.1:23456

If :debug is set to true, the nREPL server will print to stdout all the messages it is receiving over the nREPL channel.

If :debug-send is set to true, the server will also print the messages it is sending.

if :quiet is set to true, the nREPL server will not print out the message "starting nREPL server at...". If not specified then :quiet defaults to false, and the message will be printed.

If :port is not specified, it defaults to 1667.

If :host is not specified, it defaults to 0.0.0.0 (bind to every interface).

Options can contain a :describe map which will be merged in with the response for the :describe op.

If no options hashmap is specified at all, all the defaults will be used. Thus the following is a valid way to launch an nREPL server.

(babashka.nrepl.server/start-server! sci-ctx)
;; Started nREPL server at 0.0.0.0:1667

Stopping a Server

Pass the result you received from start-server! to stop-server! to shut down the server.

(->
  (babashka.nrepl.server/start-server! sci-ctx)
  (babashka.nrepl.server/stop-server!))
Started nREPL server at 0.0.0.0:1667
nil

Parsing an nREPL options string

Use babashka.nrepl.server/parse-opt to parse strings like:

(babashka.nrepl.server/parse-opt "localhost:1667")
;;=> {:host "localhost", :port 1667}
(babashka.nrepl.server/parse-opt "1667")
;;=> {:host nil, :port 1667}

You can pass the return value of parse-opt to start-server!:

(babashka.nrepl.server/start-server!
    sci-ctx
    (babashka.nrepl.server/parse-opt "localhost:23456"))

Tips and Tricks

Blocking after launching

Often you will want to launch the server and then block execution until the server is shutdown (at which point the code will continue executing), or ctrl-C is pressed (at which point the process will exit). This can be achieved as follows:

(babashka.nrepl.server/start-server! sci-ctx {:host "127.0.0.1"
                                              :port 1667})
@(promise)

Complaints about clojure.main/repl-requires

Connecting to the nREPL from CIDER gives:

;; nREPL:
clojure.lang.ExceptionInfo: Could not resolve symbol: clojure.main/repl-requires [at line 1, column 42]

This is because some nREPL clients use clojure.main/repl-requires to find a list of automatic requires to run at the beginning of the nREPL. Simply supply a value in you sci bound namespace for this value:

(-> sci-ctx
    (assoc-in [:namespaces 'clojure.main 'repl-requires]
        '[[clojure.repl :refer [dir doc]]])
    sci.addons/future
    sci.core/init
    babashka.nrepl.server/start-server!)

Authors

The main body of work was done by Michiel Borkent (@borkdude). Addition rework and some added functionality was done by Crispin Wellington (@retrogradeorbit).

License

The project code is Copyright © 2019-2020 Michiel Borkent

It is distributed under the Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)

babashka.nrepl's People

Contributors

retrogradeorbit avatar borkdude avatar plexus avatar llacom avatar tzzh avatar

Stargazers

Roman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.