mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 00:19:48 +00:00
Add dart_messages package.
R=johnniwinther@google.com, sigmund@google.com Review URL: https://codereview.chromium.org/1514333002 .
This commit is contained in:
parent
41b9d35750
commit
6ecea2f559
|
@ -4,13 +4,14 @@
|
||||||
|
|
||||||
library dart2js.messages;
|
library dart2js.messages;
|
||||||
|
|
||||||
|
import 'package:dart_messages/shared_messages.dart' as shared_messages;
|
||||||
|
|
||||||
import '../tokens/token.dart' show ErrorToken, Token;
|
import '../tokens/token.dart' show ErrorToken, Token;
|
||||||
|
|
||||||
import 'invariant.dart' show invariant;
|
import 'invariant.dart' show invariant;
|
||||||
import 'spannable.dart' show CURRENT_ELEMENT_SPANNABLE;
|
import 'spannable.dart' show CURRENT_ELEMENT_SPANNABLE;
|
||||||
|
|
||||||
import 'dart2js_messages.dart' as dart2js_messages;
|
import 'dart2js_messages.dart' as dart2js_messages;
|
||||||
import 'shared_messages.dart' as shared_messages;
|
|
||||||
|
|
||||||
/// Keys for the [MessageTemplate]s.
|
/// Keys for the [MessageTemplate]s.
|
||||||
enum MessageKind {
|
enum MessageKind {
|
||||||
|
|
|
@ -17,6 +17,8 @@ dependencies:
|
||||||
path: ../../../../dart2js_info
|
path: ../../../../dart2js_info
|
||||||
lookup_map:
|
lookup_map:
|
||||||
path: ../lookup_map
|
path: ../lookup_map
|
||||||
|
dart_messages:
|
||||||
|
path: ../dart_messages
|
||||||
|
|
||||||
# Uncomment if running gclient, so you can depend directly on the downloaded
|
# Uncomment if running gclient, so you can depend directly on the downloaded
|
||||||
# versions of dart2js's transitive dependencies:
|
# versions of dart2js's transitive dependencies:
|
||||||
|
|
20
pkg/dart_messages/bin/json_converter.dart
Normal file
20
pkg/dart_messages/bin/json_converter.dart
Normal file
|
@ -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");
|
||||||
|
}
|
35
pkg/dart_messages/bin/message_id.dart
Normal file
35
pkg/dart_messages/bin/message_id.dart
Normal file
|
@ -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");
|
||||||
|
}
|
|
@ -2,10 +2,11 @@
|
||||||
// for details. All rights reserved. Use of this source code is governed by a
|
// 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.
|
// BSD-style license that can be found in the LICENSE file.
|
||||||
|
|
||||||
// This file is parsed by JavaScript and must not use fancy Dart constructs.
|
// An update to this file must be followed by regenerating the corresponding
|
||||||
// It can contain JSON like constructs and "//" comments (but not "/*" "*/").
|
// json file. Use `json_converter.dart` in the bin directory.
|
||||||
// It must have one assignment (`final MESSAGES =`).
|
//
|
||||||
// All strings must be raw strings.
|
// 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:
|
// 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
|
// 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].
|
// combine the first two in [template] and the last in [howToFix].
|
||||||
|
|
||||||
final MESSAGES = {
|
final Map<String, Map> MESSAGES = {
|
||||||
};
|
};
|
1
pkg/dart_messages/lib/shared_messages.json
Normal file
1
pkg/dart_messages/lib/shared_messages.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{}
|
4
pkg/dart_messages/pubspec.yaml
Normal file
4
pkg/dart_messages/pubspec.yaml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
# This package is not intended to be published.
|
||||||
|
name: dart_messages
|
||||||
|
#version: do-not-upload
|
||||||
|
dependencies:
|
40
pkg/dart_messages/test/dart_messages_test.dart
Normal file
40
pkg/dart_messages/test/dart_messages_test.dart
Normal file
|
@ -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();
|
||||||
|
}
|
Loading…
Reference in a new issue