blob: 3f94441dcc1bc0217011f30cb95d7672827faaf1 [file] [log] [blame]
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '../base/file_system.dart';
import '../cache.dart';
import '../codegen.dart';
import '../convert.dart';
import '../globals.dart' as globals;
import '../project.dart';
import '../runner/flutter_command.dart';
class GenerateCommand extends FlutterCommand {
GenerateCommand() {
String get description => 'run code generators.';
String get name => 'generate';
Future<FlutterCommandResult> runCommand() async {
final FlutterProject flutterProject = FlutterProject.current();
final CodegenDaemon codegenDaemon = await codeGenerator.daemon(flutterProject);
await for (final CodegenStatus codegenStatus in codegenDaemon.buildResults) {
if (codegenStatus == CodegenStatus.Failed) {
globals.printError('Code generation failed.');
if (codegenStatus == CodegenStatus.Succeeded) {
// Check for errors output in the build_runner cache.
final Directory buildDirectory = flutterProject.dartTool.childDirectory('build');
final Directory errorCacheParent = buildDirectory.listSync().whereType<Directory>().firstWhere((Directory dir) {
return dir.childDirectory('error_cache').existsSync();
}, orElse: () => null);
if (errorCacheParent == null) {
return FlutterCommandResult.success();
final Directory errorCache = errorCacheParent.childDirectory('error_cache');
for (final File errorFile in errorCache.listSync(recursive: true).whereType<File>()) {
try {
final List<Object> errorData = json.decode(errorFile.readAsStringSync()) as List<Object>;
final List<Object> stackData = errorData[1] as List<Object>;
globals.printError(errorData.first as String);
globals.printError(stackData[0] as String);
globals.printError(stackData[1] as String);
globals.printError(StackTrace.fromString(stackData[2] as String).toString());
} catch (err) {
globals.printError('Error reading error in ${errorFile.path}');