Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(901)

Side by Side Diff: pkg/intl/test/message_extraction/extract_to_json.dart

Issue 23596007: Remove usage of dart:json. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebase. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 #!/usr/bin/env dart 1 #!/usr/bin/env dart
2 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 2 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
3 // for details. All rights reserved. Use of this source code is governed by a 3 // for details. All rights reserved. Use of this source code is governed by a
4 // BSD-style license that can be found in the LICENSE file. 4 // BSD-style license that can be found in the LICENSE file.
5 5
6 /** 6 /**
7 * This script uses the extract_messages.dart library to find the Intl.message 7 * This script uses the extract_messages.dart library to find the Intl.message
8 * calls in the target dart files and produces intl_messages.json containing the 8 * calls in the target dart files and produces intl_messages.json containing the
9 * information on those messages. It uses the analyzer-experimental parser 9 * information on those messages. It uses the analyzer-experimental parser
10 * to find the information. 10 * to find the information.
11 * 11 *
12 * This is intended to test the basic functioning of extracting messages and 12 * This is intended to test the basic functioning of extracting messages and
13 * serve as an example for how a program to extract them to a translation 13 * serve as an example for how a program to extract them to a translation
14 * file format could work. In the tests, this file is then run through a 14 * file format could work. In the tests, this file is then run through a
15 * simulated translation and the results of that are used to generate code. See 15 * simulated translation and the results of that are used to generate code. See
16 * message_extraction_test.dart 16 * message_extraction_test.dart
17 * 17 *
18 * If the environment variable INTL_MESSAGE_OUTPUT is set then it will use 18 * If the environment variable INTL_MESSAGE_OUTPUT is set then it will use
19 * that as the output directory, otherwise it will use the working directory. 19 * that as the output directory, otherwise it will use the working directory.
20 */ 20 */
21 library extract_to_json; 21 library extract_to_json;
22 22
23 import 'dart:convert';
23 import 'dart:io'; 24 import 'dart:io';
24 import 'package:intl/extract_messages.dart'; 25 import 'package:intl/extract_messages.dart';
25 import 'dart:json' as json;
26 import 'package:path/path.dart' as path; 26 import 'package:path/path.dart' as path;
27 import 'package:intl/src/intl_message.dart'; 27 import 'package:intl/src/intl_message.dart';
28 import 'package:args/args.dart'; 28 import 'package:args/args.dart';
29 29
30 main() { 30 main() {
31 var args = new Options().arguments; 31 var args = new Options().arguments;
32 var targetDir; 32 var targetDir;
33 var parser = new ArgParser(); 33 var parser = new ArgParser();
34 parser.addFlag("suppress-warnings", defaultsTo: false, 34 parser.addFlag("suppress-warnings", defaultsTo: false,
35 callback: (x) => suppressWarnings = x); 35 callback: (x) => suppressWarnings = x);
36 parser.addFlag("warnings-are-errors", defaultsTo: false, 36 parser.addFlag("warnings-are-errors", defaultsTo: false,
37 callback: (x) => warningsAreErrors = x); 37 callback: (x) => warningsAreErrors = x);
38 38
39 parser.addOption("output-dir", defaultsTo: '.', 39 parser.addOption("output-dir", defaultsTo: '.',
40 callback: (value) => targetDir = value); 40 callback: (value) => targetDir = value);
41 parser.parse(args); 41 parser.parse(args);
42 if (args.length == 0) { 42 if (args.length == 0) {
43 print('Usage: extract_to_json [--output-dir=<dir>] [files.dart]'); 43 print('Usage: extract_to_json [--output-dir=<dir>] [files.dart]');
44 print('Accepts Dart files and produces intl_messages.json'); 44 print('Accepts Dart files and produces intl_messages.json');
45 exit(0); 45 exit(0);
46 } 46 }
47 var allMessages = []; 47 var allMessages = [];
48 for (var arg in args.where((x) => x.contains(".dart"))) { 48 for (var arg in args.where((x) => x.contains(".dart"))) {
49 var messages = parseFile(new File(arg)); 49 var messages = parseFile(new File(arg));
50 messages.forEach((k, v) => allMessages.add(toJson(v))); 50 messages.forEach((k, v) => allMessages.add(toJson(v)));
51 } 51 }
52 var file = new File(path.join(targetDir, 'intl_messages.json')); 52 var file = new File(path.join(targetDir, 'intl_messages.json'));
53 file.writeAsStringSync(json.stringify(allMessages)); 53 file.writeAsStringSync(JSON.encode(allMessages));
54 if (hasWarnings && warningsAreErrors) { 54 if (hasWarnings && warningsAreErrors) {
55 exit(1); 55 exit(1);
56 } 56 }
57 } 57 }
58 58
59 /** 59 /**
60 * This is a placeholder for transforming a parameter substitution from 60 * This is a placeholder for transforming a parameter substitution from
61 * the translation file format into a Dart interpolation. In our case we 61 * the translation file format into a Dart interpolation. In our case we
62 * store it to the file in Dart interpolation syntax, so the transformation 62 * store it to the file in Dart interpolation syntax, so the transformation
63 * is trivial. 63 * is trivial.
64 */ 64 */
65 String leaveTheInterpolationsInDartForm(MainMessage msg, chunk) { 65 String leaveTheInterpolationsInDartForm(MainMessage msg, chunk) {
66 if (chunk is String) return chunk; 66 if (chunk is String) return chunk;
67 if (chunk is int) return "\$${msg.arguments[chunk]}"; 67 if (chunk is int) return "\$${msg.arguments[chunk]}";
68 return chunk.toCode(); 68 return chunk.toCode();
69 } 69 }
70 70
71 /** 71 /**
72 * Convert the [MainMessage] to a trivial JSON format. 72 * Convert the [MainMessage] to a trivial JSON format.
73 */ 73 */
74 Map toJson(MainMessage message) { 74 Map toJson(MainMessage message) {
75 var result = new Map<String, Object>(); 75 var result = new Map<String, Object>();
76 for (var attribute in message.attributeNames) { 76 for (var attribute in message.attributeNames) {
77 result[attribute] = message[attribute]; 77 result[attribute] = message[attribute];
78 } 78 }
79 result["message"] = message.expanded(leaveTheInterpolationsInDartForm); 79 result["message"] = message.expanded(leaveTheInterpolationsInDartForm);
80 return result; 80 return result;
81 } 81 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698