diff --git a/gallery/README.md b/gallery/README.md index 7f140ae37..d11c85c73 100644 --- a/gallery/README.md +++ b/gallery/README.md @@ -65,7 +65,7 @@ for more details): flutter pub run grinder l10n ``` -To generate code segments (see separate [README](codeviewer_cli/README.md) for +To generate code segments (see separate [README](gallery/tool/codeviewer_cli/README.md) for more details): ``` flutter pub run grinder update-code-segments diff --git a/gallery/gallery/tool/grind.dart b/gallery/gallery/tool/grind.dart index 37863631f..220105d11 100644 --- a/gallery/gallery/tool/grind.dart +++ b/gallery/gallery/tool/grind.dart @@ -47,10 +47,32 @@ Future generateLocalizations() async { @Task('Transform arb to xml for English') @Depends(generateLocalizations) Future l10n() async { - final l10nPath = path.join(Directory.current.parent.path, 'l10n_cli'); - await pubGet(directory: l10nPath); + final l10nPath = + path.join(Directory.current.path, 'tool', 'l10n_cli', 'main.dart'); + Dart.run(l10nPath); +} + +@Task('Verify xml localizations') +Future verifyL10n() async { + final l10nPath = + path.join(Directory.current.path, 'tool', 'l10n_cli', 'main.dart'); + + // Run the tool to transform arb to xml, and write the output to stdout. + final xmlOutput = Dart.run(l10nPath, arguments: ['--dry-run'], quiet: true); + + // Read the original xml file. + final xmlPath = + path.join(Directory.current.path, 'lib', 'l10n', 'intl_en_US.xml'); + final expectedXmlOutput = await File(xmlPath).readAsString(); - Dart.run(path.join(l10nPath, 'bin', 'main.dart')); + if (xmlOutput.trim() != expectedXmlOutput.trim()) { + stderr.writeln( + 'The contents of $xmlPath are different from that produced by ' + 'l10n_cli. Did you forget to run `flutter pub run grinder ' + 'l10n` after updating an .arb file?', + ); + exit(1); + } } @Task('Update code segments') diff --git a/gallery/gallery/tool/l10n_cli/README.md b/gallery/gallery/tool/l10n_cli/README.md new file mode 100644 index 000000000..3499698e4 --- /dev/null +++ b/gallery/gallery/tool/l10n_cli/README.md @@ -0,0 +1,4 @@ +# l10n + +A command-line application that converts .arb files to .xml files for +translation consumption. \ No newline at end of file diff --git a/gallery/l10n_cli/lib/l10n_cli.dart b/gallery/gallery/tool/l10n_cli/l10n_cli.dart similarity index 85% rename from gallery/l10n_cli/lib/l10n_cli.dart rename to gallery/gallery/tool/l10n_cli/l10n_cli.dart index 16c089bcd..2991f1213 100644 --- a/gallery/l10n_cli/lib/l10n_cli.dart +++ b/gallery/gallery/tool/l10n_cli/l10n_cli.dart @@ -37,24 +37,29 @@ String _escapeXml(String xml) { } /// Processes the XML files. -Future englishArbsToXmls() async { +Future englishArbsToXmls({bool isDryRun = false}) async { + IOSink output = + isDryRun ? stdout : File('$_l10nDir/intl_en_US.xml').openWrite(); await generateXmlFromArb( inputArb: File('$_l10nDir/intl_en_US.arb'), - outputXml: File('$_l10nDir/intl_en_US.xml'), + outputXml: output, xmlHeader: _intlHeader, ); + await output.close(); } @visibleForTesting Future generateXmlFromArb({ File inputArb, - File outputXml, + IOSink outputXml, String xmlHeader, }) async { - final Map bundle = jsonDecode(await inputArb.readAsString()); + final Map bundle = + jsonDecode(await inputArb.readAsString()) as Map; + String translationFor(String key) { assert(bundle[key] != null); - return _escapeXml(bundle[key]); + return _escapeXml(bundle[key] as String); } final xml = StringBuffer(xmlHeader); @@ -70,9 +75,9 @@ Future generateXmlFromArb({ final resourceId = key.substring(1); final name = _escapeXml(resourceId); - final Map metaInfo = bundle[key]; + final metaInfo = bundle[key] as Map; assert(metaInfo != null && metaInfo['description'] != null); - var description = _escapeXml(metaInfo['description']); + var description = _escapeXml(metaInfo['description'] as String); if (metaInfo.containsKey('plural')) { // Generate a plurals resource element formatted like this: @@ -110,8 +115,8 @@ Future generateXmlFromArb({ // description's 'parameters' value, are replaced with printf positional // string arguments, like "%1$s". var translation = translationFor(resourceId); - assert(metaInfo['parameters'].trim().isNotEmpty); - final parameters = metaInfo['parameters'] + assert((metaInfo['parameters'] as String).trim().isNotEmpty); + final parameters = (metaInfo['parameters'] as String) .split(',') .map((s) => s.trim()) .toList(); @@ -139,6 +144,5 @@ Future generateXmlFromArb({ } } xml.writeln(''); - - await outputXml.writeAsString(xml.toString()); + outputXml.write(xml.toString()); } diff --git a/gallery/gallery/tool/l10n_cli/main.dart b/gallery/gallery/tool/l10n_cli/main.dart new file mode 100644 index 000000000..6d7869952 --- /dev/null +++ b/gallery/gallery/tool/l10n_cli/main.dart @@ -0,0 +1,17 @@ +// Copyright 2019 The Flutter team. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:args/args.dart'; + +import 'l10n_cli.dart' as l10n_cli; + +void main(List arguments) { + final parser = ArgParser() + ..addFlag( + 'dry-run', + help: 'Write the output to stdout.', + ); + final argResults = parser.parse(arguments); + l10n_cli.englishArbsToXmls(isDryRun: argResults['dry-run'] as bool); +} diff --git a/gallery/l10n_cli/.gitignore b/gallery/l10n_cli/.gitignore deleted file mode 100644 index 474008f49..000000000 --- a/gallery/l10n_cli/.gitignore +++ /dev/null @@ -1,13 +0,0 @@ -# Files and directories created by pub -.dart_tool/ -.packages -# Remove the following pattern if you wish to check in your lock file -pubspec.lock - -# Conventional directory for build outputs -build/ - -# Directory created by dartdoc -doc/api/ - -.DS_Store diff --git a/gallery/l10n_cli/CHANGELOG.md b/gallery/l10n_cli/CHANGELOG.md deleted file mode 100644 index 687440bac..000000000 --- a/gallery/l10n_cli/CHANGELOG.md +++ /dev/null @@ -1,3 +0,0 @@ -## 1.0.0 - -- Initial version, created by Stagehand diff --git a/gallery/l10n_cli/README.md b/gallery/l10n_cli/README.md deleted file mode 100644 index 151472559..000000000 --- a/gallery/l10n_cli/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# l10n - -A command-line application that converts .arb files to .xml files for -translation consumption. - -Created from templates made available by Stagehand under a BSD-style -[license](https://github.com/dart-lang/stagehand/blob/master/LICENSE). diff --git a/gallery/l10n_cli/analysis_options.yaml b/gallery/l10n_cli/analysis_options.yaml deleted file mode 100644 index 4f4d26bc5..000000000 --- a/gallery/l10n_cli/analysis_options.yaml +++ /dev/null @@ -1,39 +0,0 @@ -# Defines a default set of lint rules enforced for -# projects at Google. For details and rationale, -# see https://github.com/dart-lang/pedantic#enabled-lints. -include: package:pedantic/analysis_options.yaml - -# For lint rules and documentation, see http://dart-lang.github.io/linter/lints. -# Uncomment to specify additional rules. -# linter: -# rules: -# - camel_case_types - -analyzer: -# exclude: -# - path/to/excluded/files/** - -linter: - rules: - - avoid_types_on_closure_parameters - - avoid_void_async - - await_only_futures - - camel_case_types - - cancel_subscriptions - - close_sinks - - constant_identifier_names - - control_flow_in_finally - - empty_statements - - hash_and_equals - - implementation_imports - - non_constant_identifier_names - - package_api_docs - - package_names - - package_prefixed_library_names - - test_types_in_equals - - throw_in_finally - - unnecessary_brace_in_string_interps - - unnecessary_getters_setters - - unnecessary_new - - unnecessary_statements - - directives_ordering diff --git a/gallery/l10n_cli/bin/main.dart b/gallery/l10n_cli/bin/main.dart deleted file mode 100644 index b92a74dee..000000000 --- a/gallery/l10n_cli/bin/main.dart +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2019 The Flutter team. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -import 'package:l10n_cli/l10n_cli.dart' as l10n_cli; - -main(List arguments) async { - await l10n_cli.englishArbsToXmls(); -} diff --git a/gallery/l10n_cli/pubspec.yaml b/gallery/l10n_cli/pubspec.yaml deleted file mode 100644 index 234e9876e..000000000 --- a/gallery/l10n_cli/pubspec.yaml +++ /dev/null @@ -1,11 +0,0 @@ -name: l10n_cli -description: A sample command-line application. -# version: 1.0.0 -# author: Rami Abou Ghanem - -environment: - sdk: '>=2.2.0 <3.0.0' - -dev_dependencies: - pedantic: 1.8.0 - test: ^1.0.0 diff --git a/tool/travis_flutter_script.sh b/tool/travis_flutter_script.sh index 347145130..4a8a616aa 100755 --- a/tool/travis_flutter_script.sh +++ b/tool/travis_flutter_script.sh @@ -59,10 +59,14 @@ do done # Test that the code segment widgets that get displayed in the Flutter Material -# gallery have been generated using the latest gallery code. -echo "Run code segments check for 'gallery/gallery'." +# gallery have been generated using the latest gallery code. Also test that +# the localization scripts have been run, so that they are up to date for the +# gallery. pushd gallery/gallery +echo "Run code segments check for 'gallery/gallery'." "${LOCAL_SDK_PATH}/bin/flutter" pub run grinder verify-code-segments +echo "Run localization check for 'gallery/gallery'." +"${LOCAL_SDK_PATH}/bin/flutter" pub run grinder verify-l10n popd echo "-- Success --"