OLD | NEW |
---|---|
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 """ | 4 """ |
5 Generator that produces an externs file for the Closure Compiler. | 5 Generator that produces an externs file for the Closure Compiler. |
6 Note: This is a work in progress, and generated externs may require tweaking. | 6 Note: This is a work in progress, and generated externs may require tweaking. |
7 | 7 |
8 See https://developers.google.com/closure/compiler/docs/api-tutorial3#externs | 8 See https://developers.google.com/closure/compiler/docs/api-tutorial3#externs |
9 """ | 9 """ |
10 | 10 |
11 from code import Code | 11 from code import Code |
12 from model import * | 12 from model import * |
13 from schema_util import * | 13 from schema_util import * |
14 | 14 |
15 import os | 15 import os |
16 from datetime import datetime | 16 from datetime import datetime |
17 import re | |
17 | 18 |
18 LICENSE = ("""// Copyright %s The Chromium Authors. All rights reserved. | 19 LICENSE = ("""// Copyright %s The Chromium Authors. All rights reserved. |
19 // Use of this source code is governed by a BSD-style license that can be | 20 // Use of this source code is governed by a BSD-style license that can be |
20 // found in the LICENSE file. | 21 // found in the LICENSE file. |
21 """ % datetime.now().year) | 22 """ % datetime.now().year) |
22 | 23 |
23 class JsExternsGenerator(object): | 24 class JsExternsGenerator(object): |
24 def Generate(self, namespace): | 25 def Generate(self, namespace): |
25 return _Generator(namespace).Generate() | 26 return _Generator(namespace).Generate() |
26 | 27 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 | 65 |
65 def _GenerateEnumJsDoc(self, js_type): | 66 def _GenerateEnumJsDoc(self, js_type): |
66 """ Given an Enum Type object, returns the Code for the enum's definition. | 67 """ Given an Enum Type object, returns the Code for the enum's definition. |
67 """ | 68 """ |
68 c = Code() | 69 c = Code() |
69 (c.Sblock(line='/**', line_prefix=' * ') | 70 (c.Sblock(line='/**', line_prefix=' * ') |
70 .Append('@enum {string}') | 71 .Append('@enum {string}') |
71 .Append(self._GenerateSeeLink('type', js_type.simple_name)) | 72 .Append(self._GenerateSeeLink('type', js_type.simple_name)) |
72 .Eblock(' */')) | 73 .Eblock(' */')) |
73 c.Append('chrome.%s.%s = {' % (self._namespace.name, js_type.name)) | 74 c.Append('chrome.%s.%s = {' % (self._namespace.name, js_type.name)) |
75 | |
76 def get_property_name(e): | |
Dan Beam
2015/04/28 00:21:46
nit: def scare_small_children(e):
https://google-s
Devlin
2015/04/28 00:25:08
Haha I don't think I've ever seen that (phrasing,
| |
77 # Enum properties are normified to be in ALL_CAPS_STYLE. | |
78 # Assume enum '1ring-rulesThemAll'. | |
79 # Transform to '1ring-rules_Them_All'. | |
80 e = re.sub(r'([a-z])([A-Z])', r'\1_\2', e) | |
81 # Transform to '1ring_rules_Them_All'. | |
82 e = re.sub(r'\W', '_', e) | |
83 # Transform to '_1ring_rules_Them_All'. | |
84 e = re.sub(r'^(\d)', r'_\1', e) | |
85 # Transform to '_1RING_RULES_THEM_ALL'. | |
86 return e.upper() | |
87 | |
74 c.Append('\n'.join( | 88 c.Append('\n'.join( |
75 [" %s: '%s'," % (v.name, v.name) for v in js_type.enum_values])) | 89 [" %s: '%s'," % (get_property_name(v.name), v.name) |
90 for v in js_type.enum_values])) | |
76 c.Append('};') | 91 c.Append('};') |
77 return c | 92 return c |
78 | 93 |
79 def _IsTypeConstructor(self, js_type): | 94 def _IsTypeConstructor(self, js_type): |
80 """Returns true if the given type should be a @constructor. If this returns | 95 """Returns true if the given type should be a @constructor. If this returns |
81 false, the type is a typedef. | 96 false, the type is a typedef. |
82 """ | 97 """ |
83 return any(prop.type_.property_type is PropertyType.FUNCTION | 98 return any(prop.type_.property_type is PropertyType.FUNCTION |
84 for prop in js_type.properties.values()) | 99 for prop in js_type.properties.values()) |
85 | 100 |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
307 """Generates a @see link for a given API 'object' (type, method, or event). | 322 """Generates a @see link for a given API 'object' (type, method, or event). |
308 """ | 323 """ |
309 | 324 |
310 # NOTE(devlin): This is kind of a hack. Some APIs will be hosted on | 325 # NOTE(devlin): This is kind of a hack. Some APIs will be hosted on |
311 # developer.chrome.com/apps/ instead of /extensions/, and some APIs have | 326 # developer.chrome.com/apps/ instead of /extensions/, and some APIs have |
312 # '.'s in them (like app.window), which should resolve to 'app_window'. | 327 # '.'s in them (like app.window), which should resolve to 'app_window'. |
313 # Luckily, the doc server has excellent url resolution, and knows exactly | 328 # Luckily, the doc server has excellent url resolution, and knows exactly |
314 # what we mean. This saves us from needing any complicated logic here. | 329 # what we mean. This saves us from needing any complicated logic here. |
315 return ('@see https://developer.chrome.com/extensions/%s#%s-%s' % | 330 return ('@see https://developer.chrome.com/extensions/%s#%s-%s' % |
316 (self._namespace.name, object_type, object_name)) | 331 (self._namespace.name, object_type, object_name)) |
OLD | NEW |