From 6ecea2f559912ca6a04d7397e5995d21481c77b8 Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Thu, 17 Dec 2015 18:15:17 +0100 Subject: [PATCH] Add dart_messages package. R=johnniwinther@google.com, sigmund@google.com Review URL: https://codereview.chromium.org/1514333002 . --- .../lib/src/diagnostics/messages.dart | 3 +- pkg/compiler/pubspec.yaml | 2 + pkg/dart_messages/bin/json_converter.dart | 20 ++++++++++ pkg/dart_messages/bin/message_id.dart | 35 ++++++++++++++++ .../lib}/shared_messages.dart | 11 ++--- pkg/dart_messages/lib/shared_messages.json | 1 + pkg/dart_messages/pubspec.yaml | 4 ++ .../test/dart_messages_test.dart | 40 +++++++++++++++++++ 8 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 pkg/dart_messages/bin/json_converter.dart create mode 100644 pkg/dart_messages/bin/message_id.dart rename pkg/{compiler/lib/src/diagnostics => dart_messages/lib}/shared_messages.dart (90%) create mode 100644 pkg/dart_messages/lib/shared_messages.json create mode 100644 pkg/dart_messages/pubspec.yaml create mode 100644 pkg/dart_messages/test/dart_messages_test.dart diff --git a/pkg/compiler/lib/src/diagnostics/messages.dart b/pkg/compiler/lib/src/diagnostics/messages.dart index 200b63f1282..e65106386e9 100644 --- a/pkg/compiler/lib/src/diagnostics/messages.dart +++ b/pkg/compiler/lib/src/diagnostics/messages.dart @@ -4,13 +4,14 @@ library dart2js.messages; +import 'package:dart_messages/shared_messages.dart' as shared_messages; + import '../tokens/token.dart' show ErrorToken, Token; import 'invariant.dart' show invariant; import 'spannable.dart' show CURRENT_ELEMENT_SPANNABLE; import 'dart2js_messages.dart' as dart2js_messages; -import 'shared_messages.dart' as shared_messages; /// Keys for the [MessageTemplate]s. enum MessageKind { diff --git a/pkg/compiler/pubspec.yaml b/pkg/compiler/pubspec.yaml index c885986350e..3b7e3ea79f4 100644 --- a/pkg/compiler/pubspec.yaml +++ b/pkg/compiler/pubspec.yaml @@ -17,6 +17,8 @@ dependencies: path: ../../../../dart2js_info lookup_map: path: ../lookup_map + dart_messages: + path: ../dart_messages # Uncomment if running gclient, so you can depend directly on the downloaded # versions of dart2js's transitive dependencies: diff --git a/pkg/dart_messages/bin/json_converter.dart b/pkg/dart_messages/bin/json_converter.dart new file mode 100644 index 00000000000..8d9b8caac01 --- /dev/null +++ b/pkg/dart_messages/bin/json_converter.dart @@ -0,0 +1,20 @@ +// 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' as shared_messages; + +/// Translates the shared messages in `../lib/shared_messages` to JSON and +/// emits it into `../lib/shared_messages.json`. +void main() { + var input = shared_messages.MESSAGES; + var outPath = + Platform.script.resolve('../lib/shared_messages.json').toFilePath(); + print("Input: ${input.length} entries"); + print("Output: $outPath"); + new File(outPath).writeAsStringSync(JSON.encode(shared_messages.MESSAGES)); + print("Done"); +} diff --git a/pkg/dart_messages/bin/message_id.dart b/pkg/dart_messages/bin/message_id.dart new file mode 100644 index 00000000000..2d7ed5387cf --- /dev/null +++ b/pkg/dart_messages/bin/message_id.dart @@ -0,0 +1,35 @@ +// 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:math' as math; + +import '../lib/shared_messages.dart' as shared_messages; + +math.Random random = new math.Random(); + +const idLength = 6; +final $A = "A".codeUnitAt(0); +final $Z = "Z".codeUnitAt(0); + +String computeId() { + List charCodes = []; + for (int i = 0; i < idLength; i++) { + charCodes.add($A + random.nextInt($Z - $A)); + } + return new String.fromCharCodes(charCodes); +} + +/// Computes a random message ID that hasn't been used before. +void main() { + var usedIds = + shared_messages.MESSAGES.values.map((entry) => entry['id']).toSet(); + + print("${usedIds.length} existing ids"); + + var newId; + do { + newId = computeId(); + } while (!usedIds.contains(newId)); + print("Available id: $newId"); +} diff --git a/pkg/compiler/lib/src/diagnostics/shared_messages.dart b/pkg/dart_messages/lib/shared_messages.dart similarity index 90% rename from pkg/compiler/lib/src/diagnostics/shared_messages.dart rename to pkg/dart_messages/lib/shared_messages.dart index 0a4d2ab4d93..bd51f581934 100644 --- a/pkg/compiler/lib/src/diagnostics/shared_messages.dart +++ b/pkg/dart_messages/lib/shared_messages.dart @@ -2,10 +2,11 @@ // 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. -// This file is parsed by JavaScript and must not use fancy Dart constructs. -// It can contain JSON like constructs and "//" comments (but not "/*" "*/"). -// It must have one assignment (`final MESSAGES =`). -// All strings must be raw strings. +// An update to this file must be followed by regenerating the corresponding +// json file. Use `json_converter.dart` in the bin directory. +// +// Every message in this file must have an id. Use `message_id.dart` in the +// bin directory to generate a fresh one. // The messages in this file should meet the following guide lines: // @@ -57,5 +58,5 @@ // 1. what is wrong, 2. why is it wrong, 3. how do I fix it. However, we // combine the first two in [template] and the last in [howToFix]. -final MESSAGES = { +final Map MESSAGES = { }; diff --git a/pkg/dart_messages/lib/shared_messages.json b/pkg/dart_messages/lib/shared_messages.json new file mode 100644 index 00000000000..9e26dfeeb6e --- /dev/null +++ b/pkg/dart_messages/lib/shared_messages.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/pkg/dart_messages/pubspec.yaml b/pkg/dart_messages/pubspec.yaml new file mode 100644 index 00000000000..2df3361a39d --- /dev/null +++ b/pkg/dart_messages/pubspec.yaml @@ -0,0 +1,4 @@ +# This package is not intended to be published. +name: dart_messages +#version: do-not-upload +dependencies: diff --git a/pkg/dart_messages/test/dart_messages_test.dart b/pkg/dart_messages/test/dart_messages_test.dart new file mode 100644 index 00000000000..3983773b88f --- /dev/null +++ b/pkg/dart_messages/test/dart_messages_test.dart @@ -0,0 +1,40 @@ +// 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'; + +void testJsonIsUpdated() { + var packageRoot = Platform.packageRoot; + if (packageRoot == null || packageRoot == "") { + throw new UnsupportedError("This test requires a package root."); + } + var jsonUri = + Uri.parse(packageRoot).resolve('dart_messages/shared_messages.json'); + var jsonPath = jsonUri.toFilePath(); + var content = new File(jsonPath).readAsStringSync(); + if (JSON.encode(MESSAGES) != content) { + print("The content of the Dart messages and the corresponding JSON file"); + print("is not the same."); + print("Please run bin/json_converter to update the JSON file."); + throw "Content is not the same"; + } +} + +void testIdsAreUnique() { + var usedIds = new Set(); + for (var entry in MESSAGES.values) { + var id = entry['id']; + if (!usedIds.add(id)) { + throw "Id appears twice: $id"; + } + } +} + +void main() { + testJsonIsUpdated(); + testIdsAreUnique(); +}