closurebuilder.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2009 The Closure Library Authors. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS-IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Utility for Closure Library dependency calculation.
  17. ClosureBuilder scans source files to build dependency info. From the
  18. dependencies, the script can produce a manifest in dependency order,
  19. a concatenated script, or compiled output from the Closure Compiler.
  20. Paths to files can be expressed as individual arguments to the tool (intended
  21. for use with find and xargs). As a convenience, --root can be used to specify
  22. all JS files below a directory.
  23. usage: %prog [options] [file1.js file2.js ...]
  24. """
  25. __author__ = 'nnaze@google.com (Nathan Naze)'
  26. import io
  27. import logging
  28. import optparse
  29. import os
  30. import sys
  31. import depstree
  32. import jscompiler
  33. import source
  34. import treescan
  35. def _GetOptionsParser():
  36. """Get the options parser."""
  37. parser = optparse.OptionParser(__doc__)
  38. parser.add_option('-i',
  39. '--input',
  40. dest='inputs',
  41. action='append',
  42. default=[],
  43. help='One or more input files to calculate dependencies '
  44. 'for. The namespaces in this file will be combined with '
  45. 'those given with the -n flag to form the set of '
  46. 'namespaces to find dependencies for.')
  47. parser.add_option('-n',
  48. '--namespace',
  49. dest='namespaces',
  50. action='append',
  51. default=[],
  52. help='One or more namespaces to calculate dependencies '
  53. 'for. These namespaces will be combined with those given '
  54. 'with the -i flag to form the set of namespaces to find '
  55. 'dependencies for. A Closure namespace is a '
  56. 'dot-delimited path expression declared with a call to '
  57. 'goog.provide() (e.g. "goog.array" or "foo.bar").')
  58. parser.add_option('--root',
  59. dest='roots',
  60. action='append',
  61. default=[],
  62. help='The paths that should be traversed to build the '
  63. 'dependencies.')
  64. parser.add_option('-o',
  65. '--output_mode',
  66. dest='output_mode',
  67. type='choice',
  68. action='store',
  69. choices=['list', 'script', 'compiled'],
  70. default='list',
  71. help='The type of output to generate from this script. '
  72. 'Options are "list" for a list of filenames, "script" '
  73. 'for a single script containing the contents of all the '
  74. 'files, or "compiled" to produce compiled output with '
  75. 'the Closure Compiler. Default is "list".')
  76. parser.add_option('-c',
  77. '--compiler_jar',
  78. dest='compiler_jar',
  79. action='store',
  80. help='The location of the Closure compiler .jar file.')
  81. parser.add_option('-f',
  82. '--compiler_flags',
  83. dest='compiler_flags',
  84. default=[],
  85. action='append',
  86. help='Additional flags to pass to the Closure compiler. '
  87. 'To pass multiple flags, --compiler_flags has to be '
  88. 'specified multiple times.')
  89. parser.add_option('-j',
  90. '--jvm_flags',
  91. dest='jvm_flags',
  92. default=[],
  93. action='append',
  94. help='Additional flags to pass to the JVM compiler. '
  95. 'To pass multiple flags, --jvm_flags has to be '
  96. 'specified multiple times.')
  97. parser.add_option('--output_file',
  98. dest='output_file',
  99. action='store',
  100. help=('If specified, write output to this path instead of '
  101. 'writing to standard output.'))
  102. return parser
  103. def _GetInputByPath(path, sources):
  104. """Get the source identified by a path.
  105. Args:
  106. path: str, A path to a file that identifies a source.
  107. sources: An iterable collection of source objects.
  108. Returns:
  109. The source from sources identified by path, if found. Converts to
  110. real paths for comparison.
  111. """
  112. for js_source in sources:
  113. # Convert both to real paths for comparison.
  114. if os.path.realpath(path) == os.path.realpath(js_source.GetPath()):
  115. return js_source
  116. def _GetClosureBaseFile(sources):
  117. """Given a set of sources, returns the one base.js file.
  118. Note that if zero or two or more base.js files are found, an error message
  119. will be written and the program will be exited.
  120. Args:
  121. sources: An iterable of _PathSource objects.
  122. Returns:
  123. The _PathSource representing the base Closure file.
  124. """
  125. base_files = [
  126. js_source for js_source in sources if _IsClosureBaseFile(js_source)
  127. ]
  128. if not base_files:
  129. logging.error('No Closure base.js file found.')
  130. sys.exit(1)
  131. if len(base_files) > 1:
  132. logging.error('More than one Closure base.js files found at these paths:')
  133. for base_file in base_files:
  134. logging.error(base_file.GetPath())
  135. sys.exit(1)
  136. return base_files[0]
  137. def _IsClosureBaseFile(js_source):
  138. """Returns true if the given _PathSource is the Closure base.js source."""
  139. return (os.path.basename(js_source.GetPath()) == 'base.js' and
  140. js_source.provides == set(['goog']))
  141. class _PathSource(source.Source):
  142. """Source file subclass that remembers its file path."""
  143. def __init__(self, path):
  144. """Initialize a source.
  145. Args:
  146. path: str, Path to a JavaScript file. The source string will be read
  147. from this file.
  148. """
  149. super(_PathSource, self).__init__(source.GetFileContents(path))
  150. self._path = path
  151. def __str__(self):
  152. return 'PathSource %s' % self._path
  153. def GetPath(self):
  154. """Returns the path."""
  155. return self._path
  156. def _WrapGoogModuleSource(src):
  157. return (u'goog.loadModule(function(exports) {{'
  158. '"use strict";'
  159. '{0}'
  160. '\n' # terminate any trailing single line comment.
  161. ';return exports'
  162. '}});\n').format(src)
  163. def main():
  164. logging.basicConfig(format=(sys.argv[0] + ': %(message)s'),
  165. level=logging.INFO)
  166. options, args = _GetOptionsParser().parse_args()
  167. # Make our output pipe.
  168. if options.output_file:
  169. out = io.open(options.output_file, 'wb')
  170. else:
  171. version = sys.version_info[:2]
  172. if version >= (3, 0):
  173. # Write bytes to stdout
  174. out = sys.stdout.buffer
  175. else:
  176. out = sys.stdout
  177. sources = set()
  178. logging.info('Scanning paths...')
  179. for path in options.roots:
  180. for js_path in treescan.ScanTreeForJsFiles(path):
  181. sources.add(_PathSource(js_path))
  182. # Add scripts specified on the command line.
  183. for js_path in args:
  184. sources.add(_PathSource(js_path))
  185. logging.info('%s sources scanned.', len(sources))
  186. # Though deps output doesn't need to query the tree, we still build it
  187. # to validate dependencies.
  188. logging.info('Building dependency tree..')
  189. tree = depstree.DepsTree(sources)
  190. input_namespaces = set()
  191. inputs = options.inputs or []
  192. for input_path in inputs:
  193. js_input = _GetInputByPath(input_path, sources)
  194. if not js_input:
  195. logging.error('No source matched input %s', input_path)
  196. sys.exit(1)
  197. input_namespaces.update(js_input.provides)
  198. input_namespaces.update(options.namespaces)
  199. if not input_namespaces:
  200. logging.error('No namespaces found. At least one namespace must be '
  201. 'specified with the --namespace or --input flags.')
  202. sys.exit(2)
  203. # The Closure Library base file must go first.
  204. base = _GetClosureBaseFile(sources)
  205. deps = [base] + tree.GetDependencies(input_namespaces)
  206. output_mode = options.output_mode
  207. if output_mode == 'list':
  208. out.writelines([js_source.GetPath() + '\n' for js_source in deps])
  209. elif output_mode == 'script':
  210. for js_source in deps:
  211. src = js_source.GetSource()
  212. if js_source.is_goog_module:
  213. src = _WrapGoogModuleSource(src)
  214. out.write(src.encode('utf-8') + b'\n')
  215. elif output_mode == 'compiled':
  216. logging.warning("""\
  217. Closure Compiler now natively understands and orders Closure dependencies and
  218. is prefererred over using this script for performing JavaScript compilation.
  219. Please migrate your codebase.
  220. See:
  221. https://github.com/google/closure-compiler/wiki/Managing-Dependencies
  222. """)
  223. # Make sure a .jar is specified.
  224. if not options.compiler_jar:
  225. logging.error('--compiler_jar flag must be specified if --output is '
  226. '"compiled"')
  227. sys.exit(2)
  228. # Will throw an error if the compilation fails.
  229. compiled_source = jscompiler.Compile(options.compiler_jar,
  230. [js_source.GetPath()
  231. for js_source in deps],
  232. jvm_flags=options.jvm_flags,
  233. compiler_flags=options.compiler_flags)
  234. logging.info('JavaScript compilation succeeded.')
  235. out.write(compiled_source.encode('utf-8'))
  236. else:
  237. logging.error('Invalid value for --output flag.')
  238. sys.exit(2)
  239. if __name__ == '__main__':
  240. main()