#!/usr/bin/env python2 # A very simple preprocessor for Screeps js scripts import os import sys IN = "src" OUT = "build" _cache = {} def getFile(name): path = os.path.join(IN, name.replace("/", os.path.sep)) if path in _cache: return _cache[path] if os.path.isfile(path): fh = open(path, "rb") data = fh.read() fh.close() _cache[path] = data return data return None if __name__ == "__main__": mainjs = getFile("main.js") _out = [] for line in mainjs.split('\n'): if line.startswith("#include"): fn = line.split("<")[1].split(">")[0] data = getFile(fn) if data is None: raise ImportError("Unable to include {fn}, no such file.".format(fn=fn)) _out.append("// {fn}".format(fn=fn)) _out.append(data) _out.append("// END {fn}".format(fn=fn)) else: _out.append(line) if not os.path.exists(OUT): os.mkdir(OUT) ofn = os.path.join(OUT, "main.js") ofh = open(ofn, "wb") ofh.write('\n'.join(_out)) ofh.close()