| 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..af571ba857b1c5fd8020d354988e9ce474e6b153
|
| --- /dev/null
|
| +++ b/third_party/closure_compiler/js_binary.py
|
| @@ -0,0 +1,120 @@
|
| +# 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 --defs argument which will add custom flags to the comiler. Any extern
|
| +files can also be passed in using the --extern flag.
|
| +"""
|
| +
|
| +from argparse import ArgumentParser
|
| +import os
|
| +import subprocess
|
| +import sys
|
| +
|
| +EXIT_SUCCESS = 0
|
| +EXIT_FAILURE = 1
|
| +
|
| +def IsExecutable(path):
|
| + """Returns whether file at |path| exists and is executable."""
|
| + return os.path.isfile(path) and os.access(path, os.X_OK)
|
| +
|
| +def FindCommand(command):
|
| + """Looks up for |commmand| in PATH."""
|
| + 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):
|
| + """Runs the closure compiler with |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'
|
| + with open(dep, 'r') as dep_list:
|
| + lines = [line.strip() for line in dep_list.readlines()]
|
| + split = lines.index('deps:')
|
| + return lines[1:split], lines[split+1:]
|
| +
|
| +def PostOrder(deps, sources):
|
| + """Parses the dependency tree creating a post-order listing of sources."""
|
| + for dep in deps:
|
| + new_sources, new_deps = ParseDepList(dep)
|
| +
|
| + sources = PostOrder(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('-df', '--defs', 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 = PostOrder(args.deps, args.sources)
|
| +
|
| + defs = ['--' + flag for flag in args.defs]
|
| +
|
| + compiler_args = [
|
| + args.compiler,
|
| + ] + defs
|
| +
|
| + if(args.externs):
|
| + compiler_args += ['--externs'] + 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__':
|
| + sys.exit(main())
|
|
|