1#!/usr/bin/env python
2
3# Copyright 2014 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7''' Generates a deps.js file based on an input list of javascript files using
8Closure style provide/require calls.
9'''
10
11import optparse
12import os
13import sys
14
15from jsbundler import PathRewriter
16
17_SCRIPT_DIR = os.path.realpath(os.path.dirname(__file__))
18_CHROME_SOURCE = os.path.realpath(
19    os.path.join(_SCRIPT_DIR, *[os.path.pardir] * 6))
20sys.path.insert(0, os.path.join(
21    _CHROME_SOURCE, ('chrome/third_party/chromevox/third_party/' +
22                     'closure-library/closure/bin/build')))
23import source
24
25
26def main():
27  parser = optparse.OptionParser(description=__doc__)
28  parser.add_option('-w', '--rewrite_prefix', action='append', default=[],
29                    dest='prefix_map', metavar='SPEC',
30                    help=('Two path prefixes, separated by colons ' +
31                          'specifying that a file whose (relative) path ' +
32                          'name starts with the first prefix should have ' +
33                          'that prefix replaced by the second prefix to ' +
34                          'form a path relative to the output directory. ' +
35                          'The resulting path is used in the deps mapping ' +
36                          'file path to a list of provided and required ' +
37                          'namespaces.'))
38  parser.add_option('-o', '--output_file', action='store', default=[],
39                    metavar='SPEC',
40                    help=('Where to output the generated deps file.'))
41  options, args = parser.parse_args()
42
43  path_rewriter = PathRewriter(options.prefix_map)
44
45  # Create the generated deps file's parent directory.
46  output_dir = os.path.dirname(os.path.abspath(options.output_file))
47  if not os.path.exists(output_dir):
48    os.makedirs(output_dir)
49
50  # Write the generated deps file.
51  with open(options.output_file, 'w') as output:
52    for path in args:
53      js_deps = source.Source(source.GetFileContents(path))
54      path = path_rewriter.RewritePath(path)
55      line = 'goog.addDependency(\'%s\', %s, %s);\n' % (
56          path, sorted(js_deps.provides), sorted(js_deps.requires))
57      output.write(line)
58
59
60if __name__ == '__main__':
61  main()
62