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

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

Issue 13898014: Allow suppressing message extraction warnings, providing a prefix for generated files, and format w… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: More fixes Created 7 years, 8 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 * A main program that takes as input a source Dart file and a number 7 * A main program that takes as input a source Dart file and a number
8 * of JSON files representing translations of messages from the corresponding 8 * of JSON files representing translations of messages from the corresponding
9 * Dart file. See extract_to_json.dart and make_hardcoded_translation.dart. 9 * Dart file. See extract_to_json.dart and make_hardcoded_translation.dart.
10 * 10 *
11 * This produces a series of files named 11 * This produces a series of files named
12 * "messages_<locale>.dart" containing messages for a particular locale 12 * "messages_<locale>.dart" containing messages for a particular locale
13 * and a main import file named "messages_all.dart" which has imports all of 13 * and a main import file named "messages_all.dart" which has imports all of
14 * them and provides an initializeMessages function. 14 * them and provides an initializeMessages function.
15 */ 15 */
16 library generate_from_json; 16 library generate_from_json;
17 17
18 import 'dart:io'; 18 import 'dart:io';
19 import 'package:intl/extract_messages.dart'; 19 import 'package:intl/extract_messages.dart';
20 import 'package:intl/src/intl_message.dart'; 20 import 'package:intl/src/intl_message.dart';
21 import 'extract_to_json.dart'; 21 import 'extract_to_json.dart';
22 import 'package:intl/generate_localized.dart'; 22 import 'package:intl/generate_localized.dart';
23 import 'dart:json' as json; 23 import 'dart:json' as json;
24 import 'package:pathos/path.dart' as path; 24 import 'package:pathos/path.dart' as path;
25 import 'find_output_directory.dart'; 25 import 'package:args/args.dart';
26 26
27 /** 27 /**
28 * Keeps track of all the messages we have processed so far, keyed by message 28 * Keeps track of all the messages we have processed so far, keyed by message
29 * name. 29 * name.
30 */ 30 */
31 Map<String, IntlMessage> messages; 31 Map<String, IntlMessage> messages;
32 32
33 main() { 33 main() {
34 var targetDir;
34 var args = new Options().arguments; 35 var args = new Options().arguments;
36 var parser = new ArgParser();
37 parser.addFlag("suppress-warnings", defaultsTo: false,
38 callback: (x) => suppressWarnings = x);
39 parser.addOption("output-dir", defaultsTo: '.',
40 callback: (x) => targetDir = x);
41 parser.addOption("generated-file-prefix", defaultsTo: '',
42 callback: (x) => generatedFilePrefix = x);
43 parser.parse(args);
35 if (args.length == 0) { 44 if (args.length == 0) {
36 print('Usage: generate_from_json [--output-dir=<dir>] [originalFiles.dart]' 45 print('Usage: generate_from_json [--output-dir=<dir>]'
37 ' [translationFiles.json]'); 46 ' [generated-file-prefix=<prefix>] file1.dart file2.dart ...'
47 ' outputFile.json');
38 exit(0); 48 exit(0);
39 } 49 }
40 50
41 var targetDir = findOutputDirectory(args); 51 var dartFiles = args.where((x) => x.endsWith("dart")).toList();
42 var isDart = new RegExp(r'\.dart'); 52 var jsonFiles = args.where((x) => x.endsWith(".json")).toList();
43 var isJson = new RegExp(r'\.json');
44 var dartFiles = args.where(isDart.hasMatch).toList();
45 var jsonFiles = args.where(isJson.hasMatch).toList();
46 53
54 // We're re-parsing the original files to find the corresponding messages,
55 // so if there are warnings extracting the messages, suppress them.
56 suppressWarnings = true;
47 var allMessages = dartFiles.map((each) => parseFile(new File(each))); 57 var allMessages = dartFiles.map((each) => parseFile(new File(each)));
48 58
49 messages = new Map(); 59 messages = new Map();
50 for (var eachMap in allMessages) { 60 for (var eachMap in allMessages) {
51 eachMap.forEach((key, value) => messages[key] = value); 61 eachMap.forEach((key, value) => messages[key] = value);
52 } 62 }
53 for (var arg in jsonFiles) { 63 for (var arg in jsonFiles) {
54 var file = new File(path.join(targetDir, arg)); 64 var file = new File(arg);
55 var translations = generateLocaleFile(file, targetDir); 65 generateLocaleFile(file, targetDir);
56 } 66 }
57 67
58 var mainImportFile = new File(path.join(targetDir, 'messages_all.dart')); 68 var mainImportFile = new File(path.join(targetDir,
69 '${generatedFilePrefix}messages_all.dart'));
59 mainImportFile.writeAsStringSync(generateMainImportFile()); 70 mainImportFile.writeAsStringSync(generateMainImportFile());
60 } 71 }
61 72
62 /** 73 /**
63 * Create the file of generated code for a particular locale. We read the json 74 * Create the file of generated code for a particular locale. We read the json
64 * data and create [BasicTranslatedMessage] instances from everything, 75 * data and create [BasicTranslatedMessage] instances from everything,
65 * excluding only the special _locale attribute that we use to indicate the 76 * excluding only the special _locale attribute that we use to indicate the
66 * locale. 77 * locale.
67 */ 78 */
68 void generateLocaleFile(File file, String targetDir) { 79 void generateLocaleFile(File file, String targetDir) {
(...skipping 19 matching lines...) Expand all
88 BasicTranslatedMessage(String name, String translated) : 99 BasicTranslatedMessage(String name, String translated) :
89 super(name, translated); 100 super(name, translated);
90 101
91 IntlMessage get originalMessage => 102 IntlMessage get originalMessage =>
92 (super.originalMessage == null) ? _findOriginal() : super.originalMessage; 103 (super.originalMessage == null) ? _findOriginal() : super.originalMessage;
93 104
94 // We know that our [id] is the name of the message, which is used as the 105 // We know that our [id] is the name of the message, which is used as the
95 //key in [messages]. 106 //key in [messages].
96 IntlMessage _findOriginal() => originalMessage = messages[id]; 107 IntlMessage _findOriginal() => originalMessage = messages[id];
97 } 108 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698