2017-03-07 23:10:32 +08:00
|
|
|
#!/usr/bin/env python
|
2014-06-07 03:12:18 +08:00
|
|
|
import argparse
|
2010-02-01 22:16:01 +08:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2017-07-21 20:32:50 +08:00
|
|
|
from pathlib import Path
|
2010-02-01 22:16:01 +08:00
|
|
|
|
2015-07-17 05:49:02 +08:00
|
|
|
try:
|
|
|
|
import closure
|
|
|
|
except ImportError:
|
|
|
|
closure_compiler = None
|
|
|
|
else:
|
2017-07-21 20:32:50 +08:00
|
|
|
closure_compiler = closure.get_jar_filename()
|
2015-07-17 05:49:02 +08:00
|
|
|
|
2019-11-04 16:59:34 +08:00
|
|
|
js_path = Path(__file__).parents[1] / 'static' / 'admin' / 'js'
|
2010-02-01 22:16:01 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2010-02-01 22:16:01 +08:00
|
|
|
def main():
|
|
|
|
description = """With no file paths given this script will automatically
|
2010-04-13 18:29:44 +08:00
|
|
|
compress all jQuery-based files of the admin app. Requires the Google Closure
|
|
|
|
Compiler library and Java version 6 or later."""
|
2014-06-07 03:12:18 +08:00
|
|
|
parser = argparse.ArgumentParser(description=description)
|
|
|
|
parser.add_argument('file', nargs='*')
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"-c", dest="compiler", default="~/bin/compiler.jar",
|
|
|
|
help="path to Closure Compiler jar file",
|
|
|
|
)
|
|
|
|
parser.add_argument("-v", "--verbose", action="store_true", dest="verbose")
|
|
|
|
parser.add_argument("-q", "--quiet", action="store_false", dest="verbose")
|
2014-06-07 03:12:18 +08:00
|
|
|
options = parser.parse_args()
|
2010-02-01 22:16:01 +08:00
|
|
|
|
2017-11-06 23:23:29 +08:00
|
|
|
compiler = Path(closure_compiler or options.compiler).expanduser()
|
2017-07-21 20:32:50 +08:00
|
|
|
if not compiler.exists():
|
2014-09-04 20:15:09 +08:00
|
|
|
sys.exit(
|
|
|
|
"Google Closure compiler jar file %s not found. Please use the -c "
|
|
|
|
"option to specify the path." % compiler
|
|
|
|
)
|
2010-02-01 22:16:01 +08:00
|
|
|
|
2014-06-07 03:12:18 +08:00
|
|
|
if not options.file:
|
2010-02-01 22:16:01 +08:00
|
|
|
if options.verbose:
|
|
|
|
sys.stdout.write("No filenames given; defaulting to admin scripts\n")
|
2016-03-29 06:33:29 +08:00
|
|
|
files = [
|
2017-07-21 20:32:50 +08:00
|
|
|
js_path / f
|
|
|
|
for f in ["actions.js", "collapse.js", "inlines.js", "prepopulate.js"]
|
2016-03-29 06:33:29 +08:00
|
|
|
]
|
2014-06-07 03:12:18 +08:00
|
|
|
else:
|
2017-07-21 20:32:50 +08:00
|
|
|
files = [Path(f) for f in options.file]
|
|
|
|
|
|
|
|
for file_path in files:
|
|
|
|
to_compress = file_path.expanduser()
|
|
|
|
if to_compress.exists():
|
|
|
|
to_compress_min = to_compress.with_suffix('.min.js')
|
2019-08-28 16:19:30 +08:00
|
|
|
cmd = [
|
|
|
|
'java',
|
|
|
|
'-jar', str(compiler),
|
|
|
|
'--rewrite_polyfills=false',
|
|
|
|
'--js', str(to_compress),
|
|
|
|
'--js_output_file', str(to_compress_min),
|
|
|
|
]
|
2010-02-01 22:16:01 +08:00
|
|
|
if options.verbose:
|
2019-08-28 16:19:30 +08:00
|
|
|
sys.stdout.write("Running: %s\n" % ' '.join(cmd))
|
|
|
|
subprocess.run(cmd)
|
2010-02-01 22:16:01 +08:00
|
|
|
else:
|
|
|
|
sys.stdout.write("File %s not found. Sure it exists?\n" % to_compress)
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2010-02-01 22:16:01 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|