Created
November 6, 2013 18:29
-
-
Save mvasilkov/7341556 to your computer and use it in GitHub Desktop.
Helper tool for Box2D-HTML5 conversion. Fucking kill Google, one Closure Library at a time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sys | |
def replacer_pro(match): | |
print match.group(1) | |
parts = match.group(1).split('.') | |
init_code = '' | |
cur = '' | |
for part in parts: | |
if cur: | |
cur += '.' + part | |
else: | |
cur = part | |
init_code += """if (typeof NAME === 'undefined') | |
NAME = {}""".replace('NAME', cur) + '\n' | |
if init_code: | |
init_code = '\n' + init_code | |
return '// -removed- ' + match.group(0) + init_code | |
def replacer_req(match): | |
print match.group(1) | |
name = match.group(1) | |
init_code = """if (typeof NAME === 'undefined') | |
throw Error('I can haz NAME?')""".replace('NAME', name) + '\n' | |
init_code = '\n' + init_code | |
return '// -removed- ' + match.group(0) + init_code | |
def wrap(filename): | |
print '- %s' % filename | |
with open(filename) as f: | |
text = f.read() | |
text = re.sub(r"^goog\.provide\('(.+?)'\);?", | |
replacer_pro, text, flags=re.M) | |
text = re.sub(r"^goog\.require\('(.+?)'\);?", | |
replacer_req, text, flags=re.M) | |
with open(filename, 'wb') as f: | |
f.write(text) | |
if __name__ == '__main__': | |
for a in sys.argv[1:]: | |
wrap(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment