Chromium Code Reviews| OLD | NEW |
|---|---|
| 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:io'; | 23 import 'dart:io'; |
| 24 import 'package:intl/extract_messages.dart'; | 24 import 'package:intl/extract_messages.dart'; |
| 25 import 'dart:json' as json; | 25 import 'dart:json' as json; |
| 26 import 'package:pathos/path.dart' as path; | 26 import 'package:pathos/path.dart' as path; |
| 27 import 'package:intl/src/intl_message.dart'; | 27 import 'package:intl/src/intl_message.dart'; |
| 28 import 'find_output_directory.dart'; | 28 import 'find_output_directory.dart'; |
| 29 import 'package:args/args.dart'; | |
| 29 | 30 |
| 30 main() { | 31 main() { |
| 31 var args = new Options().arguments; | 32 var args = new Options().arguments; |
| 33 var targetDir = findOutputDirectory(args); | |
|
Siggi Cherem (dart-lang)
2013/04/23 02:12:51
why parse it both here and with the output-dir opt
Alan Knight
2013/04/23 17:59:56
Removed. See next point.
| |
| 34 var parser = new ArgParser(); | |
| 35 parser.addFlag("suppress-warnings", defaultsTo: false, | |
| 36 callback: (x) => suppressWarnings = x); | |
| 37 void setTargetDir(value) { if (value != null) targetDir = value;}; | |
|
Siggi Cherem (dart-lang)
2013/04/23 02:12:51
is it possible for it to be null? (given that you
Alan Knight
2013/04/23 17:59:56
Changed it so that we don't have a default value f
Siggi Cherem (dart-lang)
2013/04/23 18:56:37
I guess part of my point is, if you are now using
Alan Knight
2013/04/23 21:37:19
Doh! I'd forgotten that was what that was doing. D
| |
| 38 parser.addOption("output-dir", defaultsTo: targetDir, callback: setTargetDir); | |
| 39 parser.parse(args); | |
| 32 if (args.length == 0) { | 40 if (args.length == 0) { |
| 33 print('Usage: extract_to_json [--output-dir=<dir>] [files.dart]'); | 41 print('Usage: extract_to_json [--output-dir=<dir>] [files.dart]'); |
| 34 print('Accepts Dart files and produces intl_messages.json'); | 42 print('Accepts Dart files and produces intl_messages.json'); |
| 35 exit(0); | 43 exit(0); |
| 36 } | 44 } |
| 37 var allMessages = []; | 45 var allMessages = []; |
| 38 for (var arg in args.where((x) => x.contains(".dart"))) { | 46 for (var arg in args.where((x) => x.contains(".dart"))) { |
| 39 var messages = parseFile(new File(arg)); | 47 var messages = parseFile(new File(arg)); |
| 40 messages.forEach((k, v) => allMessages.add(toJson(v))); | 48 messages.forEach((k, v) => allMessages.add(toJson(v))); |
| 41 } | 49 } |
| 42 var targetDir = findOutputDirectory(args); | |
| 43 var file = new File(path.join(targetDir, 'intl_messages.json')); | 50 var file = new File(path.join(targetDir, 'intl_messages.json')); |
| 44 file.writeAsStringSync(json.stringify(allMessages)); | 51 file.writeAsStringSync(json.stringify(allMessages)); |
| 45 } | 52 } |
| 46 | 53 |
| 47 /** | 54 /** |
| 48 * This is a placeholder for transforming a parameter substitution from | 55 * This is a placeholder for transforming a parameter substitution from |
| 49 * the translation file format into a Dart interpolation. In our case we | 56 * the translation file format into a Dart interpolation. In our case we |
| 50 * store it to the file in Dart interpolation syntax, so the transformation | 57 * store it to the file in Dart interpolation syntax, so the transformation |
| 51 * is trivial. | 58 * is trivial. |
| 52 */ | 59 */ |
| 53 String leaveTheInterpolationsInDartForm(IntlMessage msg, chunk) => | 60 String leaveTheInterpolationsInDartForm(IntlMessage msg, chunk) => |
| 54 (chunk is String) ? chunk : "\$${msg.arguments[chunk]}"; | 61 (chunk is String) ? chunk : "\$${msg.arguments[chunk]}"; |
| 55 | 62 |
| 56 /** | 63 /** |
| 57 * Convert the [IntlMessage] to a trivial JSON format. | 64 * Convert the [IntlMessage] to a trivial JSON format. |
| 58 */ | 65 */ |
| 59 Map toJson(IntlMessage message) { | 66 Map toJson(IntlMessage message) { |
| 60 return { | 67 return { |
| 61 "name" : message.name, | 68 "name" : message.name, |
| 62 "description" : message.description, | 69 "description" : message.description, |
| 63 "message" : message.fullMessage(leaveTheInterpolationsInDartForm), | 70 "message" : message.fullMessage(leaveTheInterpolationsInDartForm), |
| 64 "examples" : message.examples, | 71 "examples" : message.examples, |
| 65 "arguments" : message.arguments | 72 "arguments" : message.arguments |
| 66 }; | 73 }; |
| 67 } | 74 } |
| OLD | NEW |