| 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 interface file for the Closure Compiler. | 5 Generator that produces an interface file for the Closure Compiler. |
| 6 Note: This is a work in progress, and generated interfaces may require tweaking. | 6 Note: This is a work in progress, and generated interfaces may require tweaking. |
| 7 """ | 7 """ |
| 8 | 8 |
| 9 from code import Code | 9 from code import Code |
| 10 from js_util import JsUtil | 10 from js_util import JsUtil |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 self._interface = first + rest | 31 self._interface = first + rest |
| 32 self._js_util = JsUtil() | 32 self._js_util = JsUtil() |
| 33 | 33 |
| 34 def Generate(self): | 34 def Generate(self): |
| 35 """Generates a Code object with the schema for the entire namespace. | 35 """Generates a Code object with the schema for the entire namespace. |
| 36 """ | 36 """ |
| 37 c = Code() | 37 c = Code() |
| 38 (c.Append(self._GetHeader(sys.argv[0], self._namespace.name)) | 38 (c.Append(self._GetHeader(sys.argv[0], self._namespace.name)) |
| 39 .Append()) | 39 .Append()) |
| 40 | 40 |
| 41 c.Cblock(self._GenerateInterfaceObject()) | 41 self._AppendInterfaceObject(c) |
| 42 c.Append() |
| 42 | 43 |
| 43 c.Sblock('%s.prototype = {' % self._interface) | 44 c.Sblock('%s.prototype = {' % self._interface) |
| 44 | 45 |
| 45 for function in self._namespace.functions.values(): | 46 for function in self._namespace.functions.values(): |
| 46 c.Cblock(self._GenerateFunction(function)) | 47 self._AppendFunction(c, function) |
| 47 | 48 |
| 48 for event in self._namespace.events.values(): | 49 for event in self._namespace.events.values(): |
| 49 c.Cblock(self._GenerateEvent(event)) | 50 self._AppendEvent(c, event) |
| 50 | 51 |
| 51 c.TrimTrailingNewlines() | 52 c.TrimTrailingNewlines() |
| 52 | 53 |
| 53 c.Eblock('};') | 54 c.Eblock('};') |
| 54 | 55 |
| 55 return c | 56 return c |
| 56 | 57 |
| 57 def _GetHeader(self, tool, namespace): | 58 def _GetHeader(self, tool, namespace): |
| 58 """Returns the file header text. | 59 """Returns the file header text. |
| 59 """ | 60 """ |
| 60 return (self._js_util.GetLicense() + '\n' + | 61 return (self._js_util.GetLicense() + '\n' + |
| 61 self._js_util.GetInfo(tool) + '\n' + | 62 self._js_util.GetInfo(tool) + '\n' + |
| 62 ('/** @fileoverview Interface for %s that can be overriden. */' % | 63 ('/** @fileoverview Interface for %s that can be overriden. */' % |
| 63 namespace) + '\n' + | 64 namespace) + '\n' + |
| 64 ASSERT); | 65 ASSERT); |
| 65 | 66 |
| 66 def _GenerateInterfaceObject(self): | 67 def _AppendInterfaceObject(self, c): |
| 67 """Generates the code creating the interface object. | 68 """Appends the code creating the interface object. |
| 68 For example: | 69 For example: |
| 69 /** @interface */ | 70 /** @interface */ |
| 70 function SettingsPrivate() {} | 71 function SettingsPrivate() {} |
| 71 """ | 72 """ |
| 72 c = Code() | |
| 73 (c.Append('/** @interface */') | 73 (c.Append('/** @interface */') |
| 74 .Append('function %s() {}' % self._interface)) | 74 .Append('function %s() {}' % self._interface)) |
| 75 return c | |
| 76 | 75 |
| 77 def _GenerateFunction(self, function): | 76 def _AppendFunction(self, c, function): |
| 78 """Generates the inteface for a function, including a JSDoc comment. | 77 """Appends the inteface for a function, including a JSDoc comment. |
| 79 """ | 78 """ |
| 80 c = Code() | |
| 81 if function.deprecated: | 79 if function.deprecated: |
| 82 return c | 80 return |
| 83 | 81 |
| 84 (c.Concat(self._js_util.GenerateFunctionJsDoc(self._namespace.name, | 82 self._js_util.AppendFunctionJsDoc(c, self._namespace.name, function) |
| 85 function)) | 83 c.Append('%s: assertNotReached,' % (function.name)) |
| 86 .Append('%s: assertNotReached,' % (function.name))) | 84 c.Append() |
| 87 | 85 |
| 88 return c | 86 def _AppendEvent(self, c, event): |
| 89 | 87 """Appends the interface for an event. |
| 90 def _GenerateEvent(self, event): | |
| 91 """Generates the interface for an event. | |
| 92 """ | 88 """ |
| 93 c = Code() | |
| 94 c.Sblock(line='/**', line_prefix=' * ') | 89 c.Sblock(line='/**', line_prefix=' * ') |
| 95 if (event.description): | 90 if (event.description): |
| 96 c.Comment(event.description, comment_prefix='') | 91 c.Comment(event.description, comment_prefix='') |
| 97 c.Append('@type {!ChromeEvent}') | 92 c.Append('@type {!ChromeEvent}') |
| 98 c.Append(self._js_util.GenerateSeeLink(self._namespace.name, 'event', | 93 c.Append(self._js_util.GetSeeLink(self._namespace.name, 'event', |
| 99 event.name)) | 94 event.name)) |
| 100 c.Eblock(' */') | 95 c.Eblock(' */') |
| 101 c.Append('%s: new ChromeEvent(),' % (event.name)) | 96 c.Append('%s: new ChromeEvent(),' % (event.name)) |
| 102 return c | 97 c.Append() |
| OLD | NEW |