-
Notifications
You must be signed in to change notification settings - Fork 34
/
janet-netrepl
89 lines (75 loc) · 2.19 KB
/
janet-netrepl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env janet
(import spork/netrepl)
(import spork/argparse)
(defn- main
[&]
(def ap
(argparse/argparse
"Start a networked REPL server"
:default
{:kind :accumulate
:help "Files to load into the environment"}
"host"
{:short "H"
:help (string "The server host to serve the repl on. Default is " netrepl/default-host)
:default netrepl/default-host
:kind :option}
"port"
{:short "P"
:help (string "The server port to serve the repl on. Default is " netrepl/default-port)
:default netrepl/default-port
:kind :option}
"client"
{:short "c"
:kind :flag
:help "Spawn a netrepl client instead of a server"}
"client-name"
{:short "n"
:kind :option
:help "Set the name of the connecting client"}
"library"
{:short "l"
:kind :accumulate
:help "Load libraries in the repl as with the janet -l flag"}
"dofile"
{:short "d"
:kind :option
:help "Pass in a file to evaluate with dofile for each new environment table"}
"message"
{:short "m"
:kind :option
:help "Specify a short message to show clients on connection"}
"message-file"
{:short "M"
:kind :option
:help "Specify a file to load as a welcome message for new connections"}
"single-env"
{:short "s"
:kind :flag
:help "Share a single environment between multiple connections"}))
# Break on help text
(unless ap (break))
(def host (ap "host"))
(def port (ap "port"))
(def dof (ap "dofile"))
(def msg (ap "message"))
(def msg-file (ap "message-file"))
(def files (get ap :default []))
(def libraries (get ap "library" []))
(defn make-msg
[&]
(if msg (string msg "\n")
(if msg-file (slurp msg-file))))
(defn env-make
[&]
(let [e (make-env)]
(each l libraries (merge-module e (require l)))
(each f files (merge-module e (dofile f)))
(put e :pretty-format "%.20Q")
(when dof (dofile dof :env e))
e))
(if (ap "client")
(netrepl/client host port (ap "client-name"))
(if (ap "single-env")
(netrepl/server-single host port env-make nil make-msg)
(netrepl/server host port env-make nil make-msg))))