blob: f16527c4b370020d75875820b245638d3eb5d99b [file] [log] [blame]
// 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 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:grinder/grinder.dart';
import 'package:path/path.dart' as path;
void main(List<String> args) => grind(args);
@Task('Get packages')
Future<void> pubGet({String directory}) async {
await _runProcess(
['pub', 'get', if (directory != null) directory],
@Task('Format dart files')
Future<void> format({String path = '.'}) async {
await _runProcess('flutter', ['format', path]);
@Task('Generate localizations files')
Future<void> generateLocalizations() async {
final l10nScriptFile = path.join(
await pubGet(directory: l10nScriptFile);, arguments: [
await format(path: path.join('lib', 'l10n'));
@Task('Transform arb to xml for English')
Future<void> l10n() async {
final l10nPath =
path.join(Directory.current.path, 'tool', 'l10n_cli', 'main.dart');;
@Task('Verify xml localizations')
Future<void> 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 =, 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();
if (xmlOutput.trim() != expectedXmlOutput.trim()) {
'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?',
@Task('Update code segments')
Future<void> updateCodeSegments() async {
final codeviewerPath =
path.join(Directory.current.path, 'tool', 'codeviewer_cli', 'main.dart');;
final codeSegmentsPath = path.join('lib', 'codeviewer', 'code_segments.dart');
await format(path: codeSegmentsPath);
@Task('Verify code segments')
Future<void> verifyCodeSegments() async {
final codeviewerPath =
path.join(Directory.current.path, 'tool', 'codeviewer_cli', 'main.dart');
// We use stdin and stdout to write and format the code segments, to avoid
// creating any files.
final codeSegmentsUnformatted =, arguments: ['--dry-run'], quiet: true);
final codeSegmentsFormatted = await _startProcess(
path.normalize(path.join(dartVM.path, '../dartfmt')),
input: codeSegmentsUnformatted,
// Read the original code segments file.
final codeSegmentsPath = path.join(
Directory.current.path, 'lib', 'codeviewer', 'code_segments.dart');
final expectedCodeSegmentsOutput =
await File(codeSegmentsPath).readAsString();
if (codeSegmentsFormatted.trim() != expectedCodeSegmentsOutput.trim()) {
'The contents of $codeSegmentsPath are different from that produced by '
'codeviewer_cli. Did you forget to run `flutter pub run grinder '
'update-code-segments` after updating a demo?',
Future<void> _runProcess(String executable, List<String> arguments) async {
final result = await, arguments);
// Function to make sure we capture all of the stdout.
// Reference:
Future<String> _startProcess(String executable,
{List<String> arguments = const [], String input}) async {
final output = <int>[];
final completer = Completer<int>();
final process = await Process.start(executable, arguments);
(event) {
onDone: () async => completer.complete(await process.exitCode),
await process.stdin.close();
final exitCode = await completer.future;
if (exitCode != 0) {
'Running "$executable ${arguments.join(' ')}" failed with $exitCode.\n',
return Future<String>.value(utf8.decoder.convert(output));
/// Return the flutter root path from the environment variables.
String _flutterRootPath() {
final separator = (Platform.isWindows) ? ';' : ':';
final flutterBinPath =
Platform.environment['PATH'].split(separator).lastWhere((setting) {
return path.canonicalize(setting).endsWith(path.join('flutter', 'bin'));
return Directory(flutterBinPath).parent.path;