Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Side by Side Diff: third_party/closure_compiler/compile_modules.py

Issue 369643002: Lay groudwork to Closure compile JavaScript (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more nits Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import argparse
7 from checker import Checker as Checker
8 import os
9 try:
10 import json
11 except:
12 import simplejson as json
13
14
15 class Module(object):
16 def __init__(self, name, sources, depends=[], externs=[]):
17 self.name = name
18 self.sources = sources
19 # TODO(dbeam): support depending on other modules/dependency flattening.
20 self.depends = depends
21 self.externs = externs
22
23 @staticmethod
24 def from_dict(d):
25 keys = d.keys()
26
27 required = ["name", "sources"]
28 assert all(r in keys for r in required), "Module missing name or sources"
29
30 allowed = required + ["depends", "externs"]
31 assert all(k in allowed for k in keys), "Module has unknown key"
32
33 depends = d["depends"] if "depends" in d else []
34 externs = d["externs"] if "externs" in d else []
35 return Module(d["name"], d["sources"], depends=depends, externs=externs)
36
37
38 # TODO(dbeam): should ModuleParser be internal to ModuleCompiler or should we
39 # pass Modules into ModuleCompiler.compile()? Maybe this is fine?
40 class ModuleParser(object):
41 _cache = {}
42
43 def __init__(self, verbose=False):
44 self._verbose = verbose
45
46 def parse(self, file_path):
47 if file_path in self._cache:
48 print "(INFO) Found module file %s in the cache" % file_path
49 return self._cache[file_path]
50
51 file = open(file_path, "r")
52 data = json.load(file)
53 file.close()
54
55 if self._verbose:
56 pretty_json = json.dumps(data, indent=2, separators=(',', ': ')).strip()
57 print "(INFO) Layout: " + os.linesep + pretty_json + os.linesep
58
59 self._cache[file_path] = [Module.from_dict(m) for m in data]
60 return self._cache[file_path]
61
62
63 class ModuleCompiler(object):
64 _checker = None
65 _parser = None
66
67 def __init__(self, verbose=False):
68 self._verbose = verbose
69
70 def _debug(self, msg, prefix="(INFO) ", suffix=""):
71 if self._verbose:
72 print prefix + msg.strip() + suffix
73
74 def compile(self, module_file):
75 self._debug("MODULE FILE: " + module_file, prefix="")
76
77 # NOTE: It's possible but unlikely that |_checker| or |_parser|'s verbosity
78 # isn't the same as |self._verbose| due to this class being called with
79 # verbose=False then verbose=True in the same program.
80 self._parser = self._parser or ModuleParser(verbose=self._verbose)
81 self._checker = self._checker or Checker(verbose=self._verbose)
82
83 current_dir = os.getcwd()
84 module_dir = os.path.dirname(module_file)
85 rel_path = lambda f: f
86
87 if current_dir and module_dir:
88 here_to_module_dir = os.path.relpath(module_dir, current_dir)
89 if here_to_module_dir:
90 rel_path = lambda f: os.path.join(here_to_module_dir, f)
91
92 modules = self._parser.parse(module_file)
93
94 for m in modules:
95 self._debug("MODULE: " + m.name, prefix="", suffix=os.linesep)
96
97 for s in m.sources:
98 depends = [rel_path(d) for d in m.depends]
99 externs = [rel_path(e) for e in m.externs]
100 self._checker.check(rel_path(s), depends=depends, externs=externs)
101
102 if s != m.sources[-1]:
103 self._debug(os.linesep, prefix="")
104
105 if m != modules[-1]:
106 self._debug(os.linesep, prefix="")
107
108
109 def main(opts):
110 module_compiler = ModuleCompiler(verbose=opts.verbose)
111 for module_file in opts.module_file:
112 module_compiler.compile(module_file)
113
114
115 if __name__ == "__main__":
116 parser = argparse.ArgumentParser(
117 description="Typecheck JavaScript using Closure compiler")
118 parser.add_argument("-v", "--verbose", action="store_true",
119 help="Show more information as this script runs")
120 parser.add_argument("module_file", nargs=argparse.ONE_OR_MORE,
121 help="Path to a modules file to check")
122 main(parser.parse_args())
OLDNEW
« no previous file with comments | « third_party/closure_compiler/checker_test.py ('k') | third_party/closure_compiler/compiler/COPYING » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698