Chromium Code Reviews| Index: pkg/dart_messages/bin/publish.dart |
| diff --git a/pkg/dart_messages/bin/publish.dart b/pkg/dart_messages/bin/publish.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0403ff054c3cd364dede316fcb598a566b8e7f4a |
| --- /dev/null |
| +++ b/pkg/dart_messages/bin/publish.dart |
| @@ -0,0 +1,157 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import 'dart:convert'; |
| +import 'dart:io'; |
| + |
| +import '../lib/shared_messages.dart'; |
| + |
| +const String jsonPath = '../lib/shared_messages.json'; |
| +const String dart2jsPath = |
| + '../../compiler/lib/src/diagnostics/generated/shared_messages.dart'; |
| +const String analyzerPath = |
| + '../../analyzer/lib/src/generated/generated/shared_messages.dart'; |
| + |
| +const String dontEditWarning = ''' |
| +/* |
| +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.
|
| +DON'T EDIT. GENERATED. DON'T EDIT. |
| +DON'T EDIT. GENERATED. DON'T EDIT. |
| +DON'T EDIT. GENERATED. DON'T EDIT. |
| +DON'T EDIT. GENERATED. DON'T EDIT. |
| +DON'T EDIT. GENERATED. DON'T EDIT. |
| +DON'T EDIT. GENERATED. DON'T EDIT. |
| +DON'T EDIT. GENERATED. DON'T EDIT. |
| +DON'T EDIT. GENERATED. DON'T EDIT. |
| +DON'T EDIT. GENERATED. DON'T EDIT. |
| +DON'T EDIT. GENERATED. DON'T EDIT. |
| +DON'T EDIT. GENERATED. DON'T EDIT. |
| +DON'T EDIT. GENERATED. DON'T EDIT. |
| +*/'''; |
| + |
| +const String copyrightHeader = ''' |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file.'''; |
| + |
| +void markAsReadonly(String path) { |
| + // TODO(15078): mark as read-only. Currently not possible: |
| + // http://dartbug.com/15078. |
| +} |
| + |
| +void emitJson() { |
| + var input = MESSAGES; |
| + var outPath = Platform.script.resolve(jsonPath).toFilePath(); |
| + print("Emitting JSON:"); |
| + print(" Input: ${input.length} entries"); |
| + print(" Output: $outPath"); |
| + new File(outPath).writeAsStringSync(messagesAsJson); |
| + print("Emitting JSON done."); |
| +} |
| + |
| +String escapeString(String str) { |
| + return JSON.encode(str); |
| +} |
| + |
| +/// Emits the messages in dart2js format. |
| +/// |
| +/// The dart2js-format consists of two entities: |
| +/// 1. the `MessageKind` enum, and |
| +/// 2. the MessageKind-to-Template map `TEMPLATES`. |
| +/// |
| +/// The template is an instance of MessageTemplate: |
| +/// |
| +/// const MessageTemplate( |
| +/// this.kind, |
| +/// this.template, |
| +/// {this.howToFix, |
| +/// this.examples, |
| +/// this.options: const <String>[]}); |
| +/// |
| +/// A sample output thus looks as follows: |
| +/// |
| +/// enum MessageKind { |
| +/// EXAMPLE_MESSAGE, |
| +/// } |
| +/// |
| +/// const Map<MessageKind, MessageTemplate> { |
| +/// EXAMPLE_MESSAGE: const MessageTemplate( |
| +/// EXAMPLE_MESSAGE, |
| +/// "Don't use #foo with #bar", |
| +/// howToFix: "Just don't do it", |
| +/// options: const ['--some-flag']), |
| +/// examples: const [''' |
| +/// some example with bad code;'''], |
| +/// }; |
| +void emitDart2js() { |
| + var input = MESSAGES; |
| + var outPath = Platform.script.resolve(dart2jsPath).toFilePath(); |
| + print("Emitting dart2js:"); |
| + print(" Input: ${input.length} entries"); |
| + print(" Output: $outPath"); |
| + |
| + var enumIds = input.keys.toList(); |
| + |
| + StringBuffer out = new StringBuffer(); |
| + out.writeln(dontEditWarning); |
| + out.writeln(copyrightHeader); |
| + out.writeln("import '../messages.dart' show MessageTemplate;"); |
| + out.writeln(); |
| + out.write(("enum SharedMessageKind {")); |
| + // We generate on one line on purpose, so that users are less likely to |
| + // modify the generated file. |
| + out.write(enumIds.join(",")); |
| + out.writeln("}"); |
| + out.writeln(); |
| + out.writeln("const Map<SharedMessageKind, MessageTemplate> TEMPLATES = " |
| + "const <SharedMessageKind, MessageTemplate>{ "); |
| + input.forEach((name, message) { |
| + 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.
|
| + // TODO(floitsch): include id. |
| + out.write("SharedMessageKind.$name, "); |
| + out.write(escapeString(message.template)); |
| + if (message.howToFix != null) { |
| + 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.
|
| + } |
| + if (message.options != null) { |
| + out.write(", options: const ["); |
| + out.write(message.options.map(escapeString).join(",")); |
| + out.write("]"); |
| + } |
| + if (message.examples != null) { |
| + out.write(", examples: const ["); |
| + for (var example in message.examples) { |
| + if (example is String) { |
| + 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.
|
| + } else if (example is Map) { |
| + out.write("const {"); |
| + example.forEach((String fileName, String content) { |
| + out.write("'$fileName':"); |
| + out.write(escapeString(content)); |
| + out.write(","); |
| + }); |
| + out.write("}"); |
| + } |
| + out.write(","); |
| + } |
| + out.write("]"); |
| + } |
| + out.writeln("),"); |
| + }); |
| + out.writeln("};"); |
| + |
| + new File(outPath).writeAsStringSync(out.toString()); |
| + print("Emitting dart2js done."); |
| +} |
| + |
| +/// Translates the shared messages in `../lib/shared_messages.dart` to JSON, |
| +/// dart2js, and analyzer formats. |
| +/// |
| +/// Emits the json-output to [jsonPath], the dart2js-output to [dart2jsPath], |
| +/// and the analyzer-output to [analyzerPath]. |
| +void main() { |
| + emitJson(); |
| + emitDart2js(); |
| + // TODO(floitsch): emit analyzer. |
| +} |