Chromium Code Reviews| Index: third_party/closure_compiler/js_binary.py |
| diff --git a/third_party/closure_compiler/js_binary.py b/third_party/closure_compiler/js_binary.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..af835feeafa2c3ace318b1e954f6132db56e8de2 |
| --- /dev/null |
| +++ b/third_party/closure_compiler/js_binary.py |
| @@ -0,0 +1,116 @@ |
| +# Copyright 2017 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| +"""Used by a js_binary action to compile javascript files. |
| + |
| +This script takes in a list of sources and dependencies and compiles them all |
| +together into a single compiled .js file. The dependencies are ordered in a |
| +post-order, left-to-right traversal order. If multiple instances of the same |
| +source file are read, only the first is kept. The script can also take in |
| +optional --flags argument which will add custom flags to the compiler. Any |
| +extern files can also be passed in using the --extern flag. |
| +""" |
| + |
| +from argparse import ArgumentParser |
| +import os |
| +import subprocess |
| +import sys |
| + |
| +EXIT_FAILURE = 1 |
| + |
| +def IsExecutable(path): |
| + return os.path.isfile(path) and os.access(path, os.X_OK) |
| + |
| + |
| +def FindCommand(command): |
| + filepath, _ = os.path.split(command) |
| + if filepath and IsExecutable(command): |
| + return command |
| + |
| + if sys.platform == 'win32': |
| + # On Windows, if the command does not have an extension, cmd.exe will |
| + # try all extensions from PATHEXT when resolving the full path. |
| + command, ext = os.path.splitext(command) |
| + exts = [ext] if ext else os.environ['PATHEXT'].split(os.path.pathsep) |
| + else: |
| + exts = [''] |
| + |
| + for path in os.environ['PATH'].split(os.path.pathsep): |
| + for ext in exts: |
| + path = os.path.join(path, command) + ext |
| + if IsExecutable(path): |
| + return path |
| + |
| + return None |
| + |
| + |
| +def RunCompiler(args): |
| + java_path = FindCommand('java') |
| + if not java_path: |
| + sys.stderr.write('java: command not found\n') |
| + sys.exit(EXIT_FAILURE) |
| + return subprocess.check_call([java_path, '-jar'] + args) |
| + |
| + |
| +def ParseDepList(dep): |
| + """Parses a depenency list, returns |sources, deps|.""" |
| + assert os.path.isfile(dep), dep[:-11] + ' is not a js_library target' |
|
mbjorge
2017/04/19 01:15:42
I think dpream@ was getting at -11 looks random fo
damargulis
2017/04/19 01:36:19
Done.
|
| + with open(dep, 'r') as dep_list: |
| + lines = dep_list.read().splitlines() |
| + assert 'deps:' in lines, dep + ' is not formated correctly' |
|
mbjorge
2017/04/19 01:15:42
nit: formatted
add something a bit more specific
damargulis
2017/04/19 01:36:19
Done.
|
| + split = lines.index('deps:') |
| + return lines[1:split], lines[split+1:] |
| + |
| + |
| +def CrawlDepsTree(deps, sources): |
| + """Parses the dependency tree creating a post-order listing of sources.""" |
| + for dep in deps: |
| + new_sources, new_deps = ParseDepList(dep) |
| + |
| + sources = CrawlDepsTree(new_deps, sources) |
| + sources = sources + [source for source in new_sources |
| + if source not in sources] |
| + return sources |
| + |
| + |
| +def main(): |
| + parser = ArgumentParser() |
| + parser.add_argument('-c', '--compiler', required=True, |
| + help='Path to compiler') |
| + parser.add_argument('-s', '--sources', nargs='*', default=[], |
| + help='List of js source files') |
| + parser.add_argument('-o', '--output', required=True, |
| + help='Compile to output') |
| + parser.add_argument('-d', '--deps', nargs='*', default = [], |
| + help='List of js_libarary dependencies') |
| + parser.add_argument('-b', '--bootstrap', |
| + help='A file to include before all others') |
| + parser.add_argument('-cf', '--config', nargs='*', default = [], |
| + help='A list of files to include after bootstrap and' \ |
| + 'before all others') |
| + parser.add_argument('-f', '--flags', nargs='*', default = [], |
| + help='A list of custom flags to pass to the compiler. ' \ |
| + 'Do not include leading dashes') |
| + parser.add_argument('-e', '--externs', nargs='*', default = [], |
| + help='A list of extern files to pass to the compiler') |
| + |
| + args = parser.parse_args() |
| + sources = CrawlDepsTree(args.deps, []) + args.sources |
| + |
| + flags = ['--' + flag for flag in args.flags] |
|
mbjorge
2017/04/19 01:15:42
nit: match the style below '--%s' % flag
damargulis
2017/04/19 01:36:19
Done.
|
| + compiler_args = [args.compiler] + flags |
| + compiler_args += ['--externs=%s' % e for e in args.externs] |
| + compiler_args += [ |
| + '--js_output_file', |
| + args.output, |
| + '--js', |
| + ] |
| + if args.bootstrap: |
| + compiler_args += [args.bootstrap] |
| + compiler_args += args.config |
| + compiler_args += sources |
| + RunCompiler(compiler_args) |
| + |
| + |
| +if __name__ == '__main__': |
| + main() |