Add dart_messages package.

R=johnniwinther@google.com, sigmund@google.com

Review URL: https://codereview.chromium.org/1514333002 .
This commit is contained in:
Florian Loitsch 2015-12-17 18:15:17 +01:00
parent 41b9d35750
commit 6ecea2f559
8 changed files with 110 additions and 6 deletions

View file

@ -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 {

View file

@ -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:

View 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");
}

View 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");
}

View file

@ -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<String, Map> MESSAGES = {
};

View file

@ -0,0 +1 @@
{}

View file

@ -0,0 +1,4 @@
# This package is not intended to be published.
name: dart_messages
#version: do-not-upload
dependencies:

View 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();
}