-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.py
157 lines (126 loc) · 4.18 KB
/
compile.py
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
import re
import os
import json
import argparse
import functools
import subprocess
import dukpy
import dukpy.babel
def await_changes(filename, events=()):
dir, name = os.path.split(filename)
dir = (dir or '.') + '/'
cmdline = ['inotifywait', '-m']
for e in events:
cmdline.extend(['-e', e])
cmdline.append(dir)
p = subprocess.Popen(
cmdline,
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
universal_newlines=True)
with p:
try:
for line in p.stdout:
n = line.split()[2]
if n == name:
yield
except KeyboardInterrupt:
return
compilers = {}
def compiler(src_ext, dest_ext):
def decorator(fn):
compilers[src_ext] = (dest_ext, fn)
return fn
return decorator
def transform_file(input_filename, output_filename, fn, exn_fn):
prev_source = None
def transformer():
nonlocal prev_source
with open(input_filename) as fp:
source = fp.read()
if source == prev_source:
return
prev_source = source
print("Transform %s to %s" % (input_filename, output_filename))
try:
result = fn(source)
except dukpy.JSRuntimeError as exn:
if exn_fn:
exn_fn(exn)
else:
print(exn)
else:
with open(output_filename, 'w') as fp:
fp.write(result)
return transformer
@compiler('.es6', '.js')
def babel_compile(source):
return dukpy.babel.babel_compile(source)['code']
@compiler('.es6x', '.js')
def babel_jsx_compile(source):
r = dukpy.babel.babel_compile(source, presets=['es2015', 'stage-0', 'react'])
return r['code']
def send_message_to_vim(servername, message):
cmdline = ['vim', '--servername', servername, '--remote-send',
':<C-U>echom %s<CR>' % json.dumps(message)]
subprocess.check_call(cmdline)
def pass_exception_to_vim(servername, filename):
def handle_exception(exn):
print(exn)
mo = re.search(r'^SyntaxError: .*: (.*) \((\d+):(\d+)\)\n', str(exn))
if mo:
text = mo.group(1)
lnum = mo.group(2)
col = mo.group(3)
quickfix_entry = {
"filename": filename,
"lnum": int(lnum),
"col": int(col) + 1,
"text": text,
}
cmdline = ['vim', '--servername', servername, '--remote-expr',
'[setqflist([%s]), feedkeys(":\<C-U>cc 1\<CR>")]' %
json.dumps(quickfix_entry)]
subprocess.check_call(cmdline)
else:
first_line = str(exn).splitlines()[0]
send_message_to_vim(servername, first_line)
return handle_exception
def send_messages_to_vim(servername, fn):
@functools.wraps(fn)
def wrapped(*args, **kwargs):
send_message_to_vim(servername, 'Compiling...')
res = fn(*args, **kwargs)
send_message_to_vim(servername, 'OK')
return res
return wrapped
#@compiler('.js', '.es6')
#def es5to6(source):
# jsi = dukpy.JSInterpreter()
# jsi.loader.register_path('./js_modules')
# return jsi.evaljs(
# 'var convert = require("5to6"); convert(dukpy.jscode)',
# jscode=source)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-w', '--watch', action='store_true')
parser.add_argument('--vim-servername')
parser.add_argument('filename')
args = parser.parse_args()
base, ext = os.path.splitext(args.filename)
try:
dest_ext, fn = compilers[ext]
except KeyError:
parser.error("Filename must end with one of: %s" % sorted(compilers))
output_filename = base + dest_ext
exn_fn = None
if args.vim_servername:
fn = send_messages_to_vim(args.vim_servername, fn)
exn_fn = pass_exception_to_vim(args.vim_servername, args.filename)
transformer = transform_file(args.filename, output_filename, fn, exn_fn)
transformer()
if args.watch:
for _ in await_changes(args.filename, ['close_write', 'moved_to']):
transformer()
if __name__ == "__main__":
main()