-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodrequire.py
35 lines (29 loc) · 1.26 KB
/
modrequire.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
from sys import argv
import re
import os.path
pattern = re.compile('(?:require(?:\(|\ )(?:\"|\'))(.+)(?:\"|\')(?:\))?')
basedir = os.path.dirname(os.path.realpath(argv[1]))
requires = {}
def parsefile(path, outfile):
with open(path, 'r') as infile:
text = infile.read()
matches = pattern.findall(text)
return (text, matches)
def handlematches(matches, outfile):
for match in matches:
if not match in requires:
path = match.split('.')
path[-1] = path[-1] + '.lua'
text, reqs = parsefile(os.path.join(basedir, *path), outfile)
requires[match] = text
handlematches(reqs, outfile)
def createfunctions(reqs, outfile):
outfile.write('local __modrequire_functions={}\n')
for req in reqs:
outfile.write('__modrequire_functions["' + req + '"]=function()\n' + reqs[req] + '\nend\n')
with open(argv[2], 'w+') as outfile:
text, matches = parsefile(argv[1], outfile)
handlematches(matches, outfile)
createfunctions(requires, outfile)
outfile.write('local __modrequire_modules={} function require(req) if __modrequire_modules[req] == nil then __modrequire_modules[req] = __modrequire_functions[req]() end return __modrequire_modules[req] end\n')
outfile.write(text)