| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:convert'; | 5 import 'dart:convert'; |
| 6 import 'dart:io'; | 6 import 'dart:io' as io; |
| 7 | 7 |
| 8 import '../lib/shared_messages.dart'; | 8 import '../lib/shared_messages.dart'; |
| 9 | 9 |
| 10 const String jsonPath = '../lib/generated/shared_messages.json'; | 10 const String jsonPath = '../lib/generated/shared_messages.json'; |
| 11 const String dart2jsPath = | 11 const String dart2jsPath = |
| 12 '../../compiler/lib/src/diagnostics/generated/shared_messages.dart'; | 12 '../../compiler/lib/src/diagnostics/generated/shared_messages.dart'; |
| 13 const String analyzerPath = | 13 const String analyzerPath = |
| 14 '../../analyzer/lib/src/generated/generated/shared_messages.dart'; | 14 '../../analyzer/lib/src/generated/generated/shared_messages.dart'; |
| 15 | 15 |
| 16 final String dontEditWarning = """ | 16 final String dontEditWarning = """ |
| (...skipping 11 matching lines...) Expand all Loading... |
| 28 // for details. All rights reserved. Use of this source code is governed by a | 28 // for details. All rights reserved. Use of this source code is governed by a |
| 29 // BSD-style license that can be found in the LICENSE file.'''; | 29 // BSD-style license that can be found in the LICENSE file.'''; |
| 30 | 30 |
| 31 void markAsReadonly(String path) { | 31 void markAsReadonly(String path) { |
| 32 // TODO(15078): mark as read-only. Currently not possible: | 32 // TODO(15078): mark as read-only. Currently not possible: |
| 33 // http://dartbug.com/15078. | 33 // http://dartbug.com/15078. |
| 34 } | 34 } |
| 35 | 35 |
| 36 void emitJson() { | 36 void emitJson() { |
| 37 var input = MESSAGES; | 37 var input = MESSAGES; |
| 38 var outPath = Platform.script.resolve(jsonPath).toFilePath(); | 38 var outPath = io.Platform.script.resolve(jsonPath).toFilePath(); |
| 39 print("Emitting JSON:"); | 39 print("Emitting JSON:"); |
| 40 print(" Input: ${input.length} entries"); | 40 print(" Input: ${input.length} entries"); |
| 41 print(" Output: $outPath"); | 41 print(" Output: $outPath"); |
| 42 new File(outPath).writeAsStringSync(messagesAsJson); | 42 new io.File(outPath).writeAsStringSync(messagesAsJson); |
| 43 print("Emitting JSON done."); | 43 print("Emitting JSON done."); |
| 44 } | 44 } |
| 45 | 45 |
| 46 /// Escapes the given string [str]. | 46 /// Escapes the given string [str]. |
| 47 /// | 47 /// |
| 48 /// The parameter [str] may be `null` in which case the result is "null". | 48 /// The parameter [str] may be `null` in which case the result is "null". |
| 49 String escapeString(String str) { | 49 String escapeString(String str) { |
| 50 return JSON.encode(str); | 50 return JSON.encode(str); |
| 51 } | 51 } |
| 52 | 52 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 75 /// EXAMPLE_MESSAGE: const MessageTemplate( | 75 /// EXAMPLE_MESSAGE: const MessageTemplate( |
| 76 /// EXAMPLE_MESSAGE, | 76 /// EXAMPLE_MESSAGE, |
| 77 /// "Don't use #foo with #bar", | 77 /// "Don't use #foo with #bar", |
| 78 /// howToFix: "Just don't do it", | 78 /// howToFix: "Just don't do it", |
| 79 /// options: const ['--some-flag']), | 79 /// options: const ['--some-flag']), |
| 80 /// examples: const [''' | 80 /// examples: const [''' |
| 81 /// some example with bad code;'''], | 81 /// some example with bad code;'''], |
| 82 /// }; | 82 /// }; |
| 83 void emitDart2js() { | 83 void emitDart2js() { |
| 84 var input = MESSAGES; | 84 var input = MESSAGES; |
| 85 var outPath = Platform.script.resolve(dart2jsPath).toFilePath(); | 85 var outPath = io.Platform.script.resolve(dart2jsPath).toFilePath(); |
| 86 print("Emitting dart2js:"); | 86 print("Emitting dart2js:"); |
| 87 print(" Input: ${input.length} entries"); | 87 print(" Input: ${input.length} entries"); |
| 88 print(" Output: $outPath"); | 88 print(" Output: $outPath"); |
| 89 | 89 |
| 90 var enumIds = input.keys.toList(); | |
| 91 | |
| 92 StringBuffer out = new StringBuffer(); | 90 StringBuffer out = new StringBuffer(); |
| 93 out.writeln(copyrightHeader); | 91 out.writeln(copyrightHeader); |
| 94 out.writeln(dontEditWarning); | 92 out.writeln(dontEditWarning); |
| 95 out.writeln("import '../messages.dart' show MessageTemplate;"); | 93 out.writeln("import '../messages.dart' show MessageKind, MessageTemplate;"); |
| 96 out.writeln(); | 94 out.writeln(); |
| 97 out.write(("enum SharedMessageKind {\n ")); | 95 out.writeln("const Map<MessageKind, MessageTemplate> TEMPLATES = " |
| 98 // We generate on one line on purpose, so that users are less likely to | 96 "const <MessageKind, MessageTemplate>{ "); |
| 99 // modify the generated file. | |
| 100 out.writeln(enumIds.join(",\n ")); | |
| 101 out.writeln("}"); | |
| 102 out.writeln(); | |
| 103 out.writeln("const Map<SharedMessageKind, MessageTemplate> TEMPLATES = " | |
| 104 "const <SharedMessageKind, MessageTemplate>{ "); | |
| 105 input.forEach((name, message) { | 97 input.forEach((name, message) { |
| 106 out.writeln(" SharedMessageKind.$name: const MessageTemplate("); | 98 if (!message.usedBy.contains(Platform.dart2js)) return; |
| 99 |
| 100 out.writeln(" MessageKind.$name: const MessageTemplate("); |
| 107 // TODO(floitsch): include id. | 101 // TODO(floitsch): include id. |
| 108 out.writeln(" SharedMessageKind.$name,"); | 102 out.writeln(" MessageKind.$name,"); |
| 109 out.write(" "); | 103 out.write(" "); |
| 110 out.write(escapeString(message.template)); | 104 out.write(escapeString(message.template)); |
| 111 if (message.howToFix != null) { | 105 if (message.howToFix != null) { |
| 112 out.write(",\n howToFix: ${escapeString(message.howToFix)}"); | 106 out.write(",\n howToFix: ${escapeString(message.howToFix)}"); |
| 113 } | 107 } |
| 114 if (message.options != null) { | 108 if (message.options != null) { |
| 115 out.write(",\n options: const ["); | 109 out.write(",\n options: const ["); |
| 116 out.write(message.options.map(escapeString).join(",")); | 110 out.write(message.options.map(escapeString).join(",")); |
| 117 out.writeln("]"); | 111 out.writeln("]"); |
| 118 } | 112 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 133 out.write(" }"); | 127 out.write(" }"); |
| 134 } | 128 } |
| 135 out.writeln(","); | 129 out.writeln(","); |
| 136 } | 130 } |
| 137 out.writeln(" ]"); | 131 out.writeln(" ]"); |
| 138 } | 132 } |
| 139 out.writeln(" ), // Generated. Don't edit."); | 133 out.writeln(" ), // Generated. Don't edit."); |
| 140 }); | 134 }); |
| 141 out.writeln("};"); | 135 out.writeln("};"); |
| 142 | 136 |
| 143 new File(outPath).writeAsStringSync(out.toString()); | 137 new io.File(outPath).writeAsStringSync(out.toString()); |
| 144 print("Emitting dart2js done."); | 138 print("Emitting dart2js done."); |
| 145 } | 139 } |
| 146 | 140 |
| 147 String convertToAnalyzerTemplate(String template, holeOrder) { | 141 String convertToAnalyzerTemplate(String template, holeOrder) { |
| 148 var holeMap; | 142 var holeMap; |
| 149 if (holeOrder != null) { | 143 if (holeOrder != null) { |
| 150 holeMap = {}; | 144 holeMap = {}; |
| 151 for (int i = 0; i < holeOrder.length; i++) { | 145 for (int i = 0; i < holeOrder.length; i++) { |
| 152 holeMap[holeOrder[i]] = i; | 146 holeMap[holeOrder[i]] = i; |
| 153 } | 147 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 175 /// All instances are stored as top-level const variables. | 169 /// All instances are stored as top-level const variables. |
| 176 /// | 170 /// |
| 177 /// A sample output looks as follows: | 171 /// A sample output looks as follows: |
| 178 /// | 172 /// |
| 179 /// const FooCategoryErrorCode EXAMPLE_MESSAGE = const FooCategoryErrorCode( | 173 /// const FooCategoryErrorCode EXAMPLE_MESSAGE = const FooCategoryErrorCode( |
| 180 /// "EXAMPLE_MESSAGE", | 174 /// "EXAMPLE_MESSAGE", |
| 181 /// "Don't use {0} with {1}", | 175 /// "Don't use {0} with {1}", |
| 182 /// "Just don't do it"); | 176 /// "Just don't do it"); |
| 183 void emitAnalyzer() { | 177 void emitAnalyzer() { |
| 184 var input = MESSAGES; | 178 var input = MESSAGES; |
| 185 var outPath = Platform.script.resolve(analyzerPath).toFilePath(); | 179 var outPath = io.Platform.script.resolve(analyzerPath).toFilePath(); |
| 186 print("Emitting analyzer:"); | 180 print("Emitting analyzer:"); |
| 187 print(" Input: ${input.length} entries"); | 181 print(" Input: ${input.length} entries"); |
| 188 print(" Output: $outPath"); | 182 print(" Output: $outPath"); |
| 189 | 183 |
| 190 StringBuffer out = new StringBuffer(); | 184 StringBuffer out = new StringBuffer(); |
| 191 out.writeln(copyrightHeader); | 185 out.writeln(copyrightHeader); |
| 192 out.writeln(dontEditWarning); | 186 out.writeln(dontEditWarning); |
| 193 out.writeln("import 'package:analyzer/src/generated/error.dart';"); | 187 out.writeln("import 'package:analyzer/src/generated/error.dart';"); |
| 194 out.writeln(); | 188 out.writeln("import 'package:analyzer/src/generated/parser.dart' " |
| 189 "show ParserErrorCode;"); |
| 195 input.forEach((name, message) { | 190 input.forEach((name, message) { |
| 191 if (!message.usedBy.contains(Platform.analyzer)) return; |
| 192 |
| 196 Category category = message.category; | 193 Category category = message.category; |
| 197 String className = category.name + "Code"; | 194 String className = category.name + "Code"; |
| 195 out.writeln(); |
| 198 out.writeln("const $className $name = const $className("); | 196 out.writeln("const $className $name = const $className("); |
| 199 out.writeln(" '$name',"); | 197 out.writeln(" '$name',"); |
| 200 | 198 |
| 201 String template = message.template; | 199 String template = message.template; |
| 202 List holeOrder = message.templateHoleOrder; | 200 List holeOrder = message.templateHoleOrder; |
| 203 String analyzerTemplate = convertToAnalyzerTemplate(template, holeOrder); | 201 String analyzerTemplate = convertToAnalyzerTemplate(template, holeOrder); |
| 204 out.write(" "); | 202 out.write(" "); |
| 205 out.write(escapeString(analyzerTemplate)); | 203 out.write(escapeString(analyzerTemplate)); |
| 206 out.write(",\n "); | 204 out.write(",\n "); |
| 207 out.write(escapeString(message.howToFix)); | 205 out.write(escapeString(message.howToFix)); |
| 208 out.writeln("); // Generated. Don't edit."); | 206 out.writeln("); // Generated. Don't edit."); |
| 209 }); | 207 }); |
| 210 | 208 |
| 211 new File(outPath).writeAsStringSync(out.toString()); | 209 new io.File(outPath).writeAsStringSync(out.toString()); |
| 212 print("Emitting analyzer done."); | 210 print("Emitting analyzer done."); |
| 213 } | 211 } |
| 214 | 212 |
| 215 /// Translates the shared messages in `../lib/shared_messages.dart` to JSON, | 213 /// Translates the shared messages in `../lib/shared_messages.dart` to JSON, |
| 216 /// dart2js, and analyzer formats. | 214 /// dart2js, and analyzer formats. |
| 217 /// | 215 /// |
| 218 /// Emits the json-output to [jsonPath], the dart2js-output to [dart2jsPath], | 216 /// Emits the json-output to [jsonPath], the dart2js-output to [dart2jsPath], |
| 219 /// and the analyzer-output to [analyzerPath]. | 217 /// and the analyzer-output to [analyzerPath]. |
| 220 void main() { | 218 void main() { |
| 221 emitJson(); | 219 emitJson(); |
| 222 emitDart2js(); | 220 emitDart2js(); |
| 223 emitAnalyzer(); | 221 emitAnalyzer(); |
| 224 } | 222 } |
| OLD | NEW |