Skip to content

Instantly share code, notes, and snippets.

@danner
Created April 2, 2015 18:09
Show Gist options
  • Save danner/c684fdb06ff12aec963a to your computer and use it in GitHub Desktop.
Save danner/c684fdb06ff12aec963a to your computer and use it in GitHub Desktop.
Python script to convert angular code to allow minification using inline annotation
import re
import sys
from tempfile import mkstemp
from shutil import move
from os import remove
def convert(source_file_path):
fh, target_file_path = mkstemp()
module_regex = re.compile(r"\.module\(.*, function\((?P<args>.*)\)")
with open(target_file_path, 'w') as target_file:
with open(source_file_path, 'r') as source_file:
#locate the controller line
got_conversion = False
for line in source_file:
groups = re.search(module_regex, line)
if groups and groups.groupdict()['args']:
got_conversion = True
new_line = line[:line.index(', function')+2]
new_line += "[\n '%s'," % "', '".join(groups.groupdict()['args'].split(', '))
new_line += "\n %s" % line[line.index(', function')+2:]
target_file.write(new_line)
else:
if got_conversion:
#now we're looking for the last line.
if line == "});\n":
target_file.write("}]);")
else:
target_file.write(line)
else:
target_file.write(line)
#comment these lines out to do a trial run.
remove(source_file_path)
move(target_file_path, source_file_path)
if __name__ == "__main__":
#usage: find ./app -name "*.js" -not -path "*/bower_components/*" -not -path "*/node_modules/*" | python convert_angular_injection.py
for filename in sys.stdin:
convert(filename.strip())
@danner
Copy link
Author

danner commented Apr 2, 2015

This is obviously based on your style guidelines, we luckily had very consistent formatting, it was just wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment