-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.coffee
273 lines (230 loc) · 5.94 KB
/
main.coffee
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
logIO = true
logIO = false
inBrowser = typeof window != "undefined"
haveNode = typeof process != "undefined"
haveNodekit = inBrowser && haveNode
if not inBrowser && 1 # testmacromatic
bye = () ->
quitR3()
setTimeout bye, 1000
if inBrowser
editor = ace.edit("editor");
editor.getSession().setUseSoftTabs(false);
if haveNode
spawn = require('child_process').spawn
fs = require 'fs'
if haveNodekit
console.error "IGNORE rendersandbox when in nodekit, not implemented"
gui = require 'nw.gui'
win = gui.Window.get()
document.title = "WereCon"
win.show();
quitR3 = null
win.on 'close', () ->
quitR3()
this.close true
dir = null
workdir = null
child = null
g_id = 0
ls = null
nextTick = null
G = (name) ->
this.log = []
this.start = new Date()
this.id = ++ g_id
this.name = name
this
g = null
main = () ->
switch process.platform
when "linux"
dir = if haveNodekit then "#{process.cwd()}/coffee" else __dirname
d = "#{dir}/.."
workdir = if d.match /tmp/ then "#{process.execPath}/.." else d
fs.chmodSync "#{d}/r3", 0755
fs.chmodSync "#{d}/r3-child", 0755
ls = spawn "#{d}/r3", ["-cs","#{d}/partner.r3"],
{detached: true, stdio: 'pipe'}
when "win32"
#pipe does not work on wine
dir = if haveNodekit then "#{process.cwd()}\\coffee" else __dirname
d = "#{dir}\\.."
workdir = d
ls = spawn "#{d}\\r3.exe", ["-cs","#{d}\\partner.r3"], {stdio: 'pipe'}
plog "@#{process.platform} dir #{dir}"
plog "exe #{process.execPath}"
plog "work #{workdir}"
if inBrowser
document.title = "#{document.title} #{workdir}"
send "init", o: {workdir: {f: workdir}, datadir: {f: "#{dir}/.."}}
task "listen", () ->
buf = ""
quitR3 = callout () ->
if child
child.stdin.end()
child.kill()
send "quit"
ls.stdin.end()
ls.stdout.on "data", callout (data) ->
buf += data
while m = buf.match /(.*?)\n([^]*)/m
line = m[1]
buf = m[2]
if line.match /^\*\* /
r3log "error: #{line}#{buf}"
clearTimeout nextTick
#process.exit()
else if line.match /^~ /
if a = line.match /^~ (\S*) (.*)/
cmd = a[1]
args = a[2]
args = JSON.parse args if args
handle cmd, args
else
a = line.match /^~ (.*)/
cmd = a[1]
handle cmd
else
r3log line
ls.stderr.on "data", callout (data) ->
plog "stderr: " + data
ls.on "exit", callout (code) ->
plog "child done res: " + code
send = (s, v) ->
if v != undefined
v = JSON.stringify v
ls.stdin.write "#{s} #{v}\n"
else
ls.stdin.write "#{s}\n"
plog = (o) ->
if o != undefined then log o
return if g.log.length == 1 && g.log[0] == "..."
d = new Date()
header = "task: #{g.id}, #{g.name} @#{d}"
console.log header
console.log g.log
logBrowser "#{header}\n#{JSON.stringify g.log}\n"
g.log = ["..."]
o
logBrowser = (s) ->
if inBrowser
s = $('<div/>').text(s).html()
$("#log").append s
$("#log").prop "scrollTop", $("#log").prop "scrollHeight"
log = (o) ->
g.log.push o
o
r3log = (o) ->
console.log "R3: #{o}"
o = "R3: #{o}"
#o = "R3: #{o} @#{new Date()}"
logBrowser "#{o}\n"
callout = (f, go = g) ->
f.g = go
(args...) ->
if g
throw new Exception "task runs in task!"
g = f.g
try f args... catch e
header = "task: #{g.id}, #{g.name}"
console.log "callout failed [#{header}]:"
console.log e.stack
plog(e)
g = null
return
g = null
task = (name,f) ->
process.nextTick callout f, new G(name)
handle = (cmd, args) ->
#log "#{cmd} -:- #{JSON.stringify args}"
sendEvent = (e,cmd) -> # cmd from parent
contents = ([
{s: e.s},
if e.s != "editor" then {s: $("##{e.s}").val()}
else
sel = editor.session.getTextRange editor.getSelectionRange()
curs = editor.selection.getCursor()
o:
content: s: editor.getValue()
selection: s: sel
cursor: o: curs
] for e in args[2] )
res = [
[args[0], args[1]], contents
]
send cmd, res
switch cmd
when "set-html" then $("##{args[0].s}").html args[1].s
when "set-val"
if args[0].s != "editor" then $("##{args[0].s}").val args[1].s
else
if args[1].s
editor.setValue args[1].s
editor.gotoLine 0, 0, true # hack: clears focus
else
if args[1].o.content
editor.setValue args[1].o.content.s
if args[1].o.cursor
curs = args[1].o.cursor.o
editor.gotoLine curs.row + 1, curs.column, true
else
editor.gotoLine 0, 0, true # hack: clears focus
editor.focus()
when "focus"
if args.s != "editor" then $("##{args.s}").focus()
else editor.focus()
when "append-html"
s = args[1].s
l = $ "##{args[0].s}"
l.append s
l.prop "scrollTop", l.prop "scrollHeight"
when "append-text"
l = $ "##{args[0].s}"
s = $('<div/>').text(args[1].s).html()
l.append s
l.prop "scrollTop", l.prop "scrollHeight"
when "on-click"
$("##{args[0].s}").on 'click', callout (e) -> sendEvent e, "clicked"
when "on-text"
$("##{args[0].s}").on 'keyup', callout (e) ->
if e.keyCode == 13
sendEvent e, "text"
when "call"
[path, args] = args
child = spawn path.s, (a.s for a in args),
{detached: true, stdio: 'pipe'}
task "call", () ->
child.on "exit", callout (code) ->
send "call.exit", code
#plog "exit"
child.on "close", callout (code) ->
send "call.close", code
#plog "close"
child.stdout.on "data", callout (data) ->
data = "" + data
send "call.data", {s: data}
#plog "" + data
child.stderr.on "data", callout (data) ->
data = "" + data
send "call.error", {s: data}
#plog data
when "call-send"
child.stdin.write "#{args.s}\n"
when "call-kill"
child.kill()
when "editor-set" #deprecated, use set-val
editor.setValue args.s
else
send "error", ["unknown", {s: cmd}]
plog "Error: unknown #{cmd}"
task "main", () ->
if inBrowser
Zepto main
else
do main
task ">r3", () ->
window.r3 = {}
window.r3.send = callout (cmd, args) ->
#plog ">r3: #{cmd} #{JSON.stringify args}"
send cmd, args