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

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: 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 'package:args/args.dart';
25 import 'find_output_directory.dart'; 26 import 'find_output_directory.dart';
26 27
27 /** 28 /**
28 * Keeps track of all the messages we have processed so far, keyed by message 29 * Keeps track of all the messages we have processed so far, keyed by message
29 * name. 30 * name.
30 */ 31 */
31 Map<String, IntlMessage> messages; 32 Map<String, IntlMessage> messages;
32 33
33 main() { 34 main() {
35 var targetDir;
34 var args = new Options().arguments; 36 var args = new Options().arguments;
37 var parser = new ArgParser();
38 parser.addFlag("suppress-warnings", defaultsTo: false,
39 callback: (x) => suppressWarnings = x);
40 parser.addOption("output-dir", defaultsTo: '.',
41 callback: (x) => targetDir = x);
42 parser.addOption("generated-file-prefix", defaultsTo: '',
43 callback: (x) => generatedFilePrefix = x);
44 parser.parse(args);
35 if (args.length == 0) { 45 if (args.length == 0) {
36 print('Usage: generate_from_json [--output-dir=<dir>] [originalFiles.dart]' 46 print('Usage: generate_from_json [--output-dir=<dir>]'
37 ' [translationFiles.json]'); 47 ' [generated-file-prefix=<prefix>] file1.dart file2.dart ...'
48 ' outputFile.json');
38 exit(0); 49 exit(0);
39 } 50 }
40 51
41 var targetDir = findOutputDirectory(args); 52 var dartFiles = args.where((x) => x.endsWith("dart")).toList();
42 var isDart = new RegExp(r'\.dart'); 53 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 54
55 // We're re-parsing the original files to find the corresponding messages,
56 // so if there are warnings extracting the messages, suppress them.
57 suppressWarnings = true;
47 var allMessages = dartFiles.map((each) => parseFile(new File(each))); 58 var allMessages = dartFiles.map((each) => parseFile(new File(each)));
48 59
49 messages = new Map(); 60 messages = new Map();
50 for (var eachMap in allMessages) { 61 for (var eachMap in allMessages) {
51 eachMap.forEach((key, value) => messages[key] = value); 62 eachMap.forEach((key, value) => messages[key] = value);
52 } 63 }
53 for (var arg in jsonFiles) { 64 for (var arg in jsonFiles) {
54 var file = new File(path.join(targetDir, arg)); 65 var file = new File(arg);
55 var translations = generateLocaleFile(file, targetDir); 66 generateLocaleFile(file, targetDir);
56 } 67 }
57 68
58 var mainImportFile = new File(path.join(targetDir, 'messages_all.dart')); 69 var mainImportFile = new File(path.join(targetDir,
70 '${generatedFilePrefix}messages_all.dart'));
59 mainImportFile.writeAsStringSync(generateMainImportFile()); 71 mainImportFile.writeAsStringSync(generateMainImportFile());
60 } 72 }
61 73
62 /** 74 /**
63 * Create the file of generated code for a particular locale. We read the json 75 * Create the file of generated code for a particular locale. We read the json
64 * data and create [BasicTranslatedMessage] instances from everything, 76 * data and create [BasicTranslatedMessage] instances from everything,
65 * excluding only the special _locale attribute that we use to indicate the 77 * excluding only the special _locale attribute that we use to indicate the
66 * locale. 78 * locale.
67 */ 79 */
68 void generateLocaleFile(File file, String targetDir) { 80 void generateLocaleFile(File file, String targetDir) {
(...skipping 19 matching lines...) Expand all
88 BasicTranslatedMessage(String name, String translated) : 100 BasicTranslatedMessage(String name, String translated) :
89 super(name, translated); 101 super(name, translated);
90 102
91 IntlMessage get originalMessage => 103 IntlMessage get originalMessage =>
92 (super.originalMessage == null) ? _findOriginal() : super.originalMessage; 104 (super.originalMessage == null) ? _findOriginal() : super.originalMessage;
93 105
94 // We know that our [id] is the name of the message, which is used as the 106 // We know that our [id] is the name of the message, which is used as the
95 //key in [messages]. 107 //key in [messages].
96 IntlMessage _findOriginal() => originalMessage = messages[id]; 108 IntlMessage _findOriginal() => originalMessage = messages[id];
97 } 109 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698