Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:convert'; | |
| 6 import 'dart:io'; | |
| 7 | |
| 8 import '../lib/shared_messages.dart'; | |
| 9 | |
| 10 const String jsonPath = '../lib/shared_messages.json'; | |
| 11 const String dart2jsPath = | |
| 12 '../../compiler/lib/src/diagnostics/generated/shared_messages.dart'; | |
| 13 const String analyzerPath = | |
| 14 '../../analyzer/lib/src/generated/generated/shared_messages.dart'; | |
| 15 | |
| 16 const String dontEditWarning = ''' | |
| 17 /* | |
| 18 DON'T EDIT. GENERATED. DON'T EDIT. | |
|
Siggi Cherem (dart-lang)
2016/01/14 17:57:40
I think it's ok to only say this once or twice :)
floitsch
2016/01/15 17:20:38
Done.
| |
| 19 DON'T EDIT. GENERATED. DON'T EDIT. | |
| 20 DON'T EDIT. GENERATED. DON'T EDIT. | |
| 21 DON'T EDIT. GENERATED. DON'T EDIT. | |
| 22 DON'T EDIT. GENERATED. DON'T EDIT. | |
| 23 DON'T EDIT. GENERATED. DON'T EDIT. | |
| 24 DON'T EDIT. GENERATED. DON'T EDIT. | |
| 25 DON'T EDIT. GENERATED. DON'T EDIT. | |
| 26 DON'T EDIT. GENERATED. DON'T EDIT. | |
| 27 DON'T EDIT. GENERATED. DON'T EDIT. | |
| 28 DON'T EDIT. GENERATED. DON'T EDIT. | |
| 29 DON'T EDIT. GENERATED. DON'T EDIT. | |
| 30 DON'T EDIT. GENERATED. DON'T EDIT. | |
| 31 */'''; | |
| 32 | |
| 33 const String copyrightHeader = ''' | |
| 34 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 35 // for details. All rights reserved. Use of this source code is governed by a | |
| 36 // BSD-style license that can be found in the LICENSE file.'''; | |
| 37 | |
| 38 void markAsReadonly(String path) { | |
| 39 // TODO(15078): mark as read-only. Currently not possible: | |
| 40 // http://dartbug.com/15078. | |
| 41 } | |
| 42 | |
| 43 void emitJson() { | |
| 44 var input = MESSAGES; | |
| 45 var outPath = Platform.script.resolve(jsonPath).toFilePath(); | |
| 46 print("Emitting JSON:"); | |
| 47 print(" Input: ${input.length} entries"); | |
| 48 print(" Output: $outPath"); | |
| 49 new File(outPath).writeAsStringSync(messagesAsJson); | |
| 50 print("Emitting JSON done."); | |
| 51 } | |
| 52 | |
| 53 String escapeString(String str) { | |
| 54 return JSON.encode(str); | |
| 55 } | |
| 56 | |
| 57 /// Emits the messages in dart2js format. | |
| 58 /// | |
| 59 /// The dart2js-format consists of two entities: | |
| 60 /// 1. the `MessageKind` enum, and | |
| 61 /// 2. the MessageKind-to-Template map `TEMPLATES`. | |
| 62 /// | |
| 63 /// The template is an instance of MessageTemplate: | |
| 64 /// | |
| 65 /// const MessageTemplate( | |
| 66 /// this.kind, | |
| 67 /// this.template, | |
| 68 /// {this.howToFix, | |
| 69 /// this.examples, | |
| 70 /// this.options: const <String>[]}); | |
| 71 /// | |
| 72 /// A sample output thus looks as follows: | |
| 73 /// | |
| 74 /// enum MessageKind { | |
| 75 /// EXAMPLE_MESSAGE, | |
| 76 /// } | |
| 77 /// | |
| 78 /// const Map<MessageKind, MessageTemplate> { | |
| 79 /// EXAMPLE_MESSAGE: const MessageTemplate( | |
| 80 /// EXAMPLE_MESSAGE, | |
| 81 /// "Don't use #foo with #bar", | |
| 82 /// howToFix: "Just don't do it", | |
| 83 /// options: const ['--some-flag']), | |
| 84 /// examples: const [''' | |
| 85 /// some example with bad code;'''], | |
| 86 /// }; | |
| 87 void emitDart2js() { | |
| 88 var input = MESSAGES; | |
| 89 var outPath = Platform.script.resolve(dart2jsPath).toFilePath(); | |
| 90 print("Emitting dart2js:"); | |
| 91 print(" Input: ${input.length} entries"); | |
| 92 print(" Output: $outPath"); | |
| 93 | |
| 94 var enumIds = input.keys.toList(); | |
| 95 | |
| 96 StringBuffer out = new StringBuffer(); | |
| 97 out.writeln(dontEditWarning); | |
| 98 out.writeln(copyrightHeader); | |
| 99 out.writeln("import '../messages.dart' show MessageTemplate;"); | |
| 100 out.writeln(); | |
| 101 out.write(("enum SharedMessageKind {")); | |
| 102 // We generate on one line on purpose, so that users are less likely to | |
| 103 // modify the generated file. | |
| 104 out.write(enumIds.join(",")); | |
| 105 out.writeln("}"); | |
| 106 out.writeln(); | |
| 107 out.writeln("const Map<SharedMessageKind, MessageTemplate> TEMPLATES = " | |
| 108 "const <SharedMessageKind, MessageTemplate>{ "); | |
| 109 input.forEach((name, message) { | |
| 110 out.write("SharedMessageKind.$name: const MessageTemplate("); | |
|
Siggi Cherem (dart-lang)
2016/01/14 17:57:40
indent the output here.
floitsch
2016/01/15 17:20:39
Done.
| |
| 111 // TODO(floitsch): include id. | |
| 112 out.write("SharedMessageKind.$name, "); | |
| 113 out.write(escapeString(message.template)); | |
| 114 if (message.howToFix != null) { | |
| 115 out.write(", howToFix: ${escapeString(message.howToFix)}"); | |
|
Siggi Cherem (dart-lang)
2016/01/14 17:57:40
split each name argument to a separate line (and i
floitsch
2016/01/15 17:20:39
Done.
| |
| 116 } | |
| 117 if (message.options != null) { | |
| 118 out.write(", options: const ["); | |
| 119 out.write(message.options.map(escapeString).join(",")); | |
| 120 out.write("]"); | |
| 121 } | |
| 122 if (message.examples != null) { | |
| 123 out.write(", examples: const ["); | |
| 124 for (var example in message.examples) { | |
| 125 if (example is String) { | |
| 126 out.write(escapeString(example)); | |
|
Siggi Cherem (dart-lang)
2016/01/14 17:57:40
maybe switch to use escapeString for ''' here and
floitsch
2016/01/15 17:20:39
Done.
| |
| 127 } else if (example is Map) { | |
| 128 out.write("const {"); | |
| 129 example.forEach((String fileName, String content) { | |
| 130 out.write("'$fileName':"); | |
| 131 out.write(escapeString(content)); | |
| 132 out.write(","); | |
| 133 }); | |
| 134 out.write("}"); | |
| 135 } | |
| 136 out.write(","); | |
| 137 } | |
| 138 out.write("]"); | |
| 139 } | |
| 140 out.writeln("),"); | |
| 141 }); | |
| 142 out.writeln("};"); | |
| 143 | |
| 144 new File(outPath).writeAsStringSync(out.toString()); | |
| 145 print("Emitting dart2js done."); | |
| 146 } | |
| 147 | |
| 148 /// Translates the shared messages in `../lib/shared_messages.dart` to JSON, | |
| 149 /// dart2js, and analyzer formats. | |
| 150 /// | |
| 151 /// Emits the json-output to [jsonPath], the dart2js-output to [dart2jsPath], | |
| 152 /// and the analyzer-output to [analyzerPath]. | |
| 153 void main() { | |
| 154 emitJson(); | |
| 155 emitDart2js(); | |
| 156 // TODO(floitsch): emit analyzer. | |
| 157 } | |
| OLD | NEW |