blob: 78cb1ee6503b011600e0b28fb38774d89682d99f [file] [log] [blame]
// Copyright 2019 The Chromium 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/common.dart';
import '../base/context.dart';
import '../build_system/build_system.dart';
import '../build_system/targets/assets.dart';
import '../build_system/targets/dart.dart';
import '../build_system/targets/ios.dart';
import '../build_system/targets/linux.dart';
import '../build_system/targets/macos.dart';
import '../build_system/targets/windows.dart';
import '../globals.dart';
import '../project.dart';
import '../runner/flutter_command.dart';
/// The [BuildSystem] instance.
BuildSystem get buildSystem => context.get<BuildSystem>();
/// All currently implemented targets.
const List<Target> _kDefaultTargets = <Target>[
UnpackMacOS(),
UnpackLinux(),
UnpackWindows(),
CopyAssets(),
KernelSnapshot(),
AotElfProfile(),
AotElfRelease(),
AotAssemblyProfile(),
AotAssemblyRelease(),
];
/// Assemble provides a low level API to interact with the flutter tool build
/// system.
class AssembleCommand extends FlutterCommand {
AssembleCommand() {
argParser.addMultiOption(
'define',
abbr: 'd',
help: 'Allows passing configuration to a target with --define=target=key=value.'
);
argParser.addOption(
'build-mode',
allowed: const <String>[
'debug',
'profile',
'release',
],
);
argParser.addOption(
'resource-pool-size',
help: 'The maximum number of concurrent tasks the build system will run.'
);
}
@override
String get description => 'Assemble and build flutter resources.';
@override
String get name => 'assemble';
/// The target we are building.
Target get target {
if (argResults.rest.isEmpty) {
throwToolExit('missing target name for flutter assemble.');
}
final String name = argResults.rest.first;
return _kDefaultTargets.firstWhere((Target target) => target.name == name);
}
/// The environmental configuration for a build invocation.
Environment get environment {
final FlutterProject flutterProject = FlutterProject.current();
final Environment result = Environment(
buildDir: flutterProject.directory
.childDirectory('.dart_tool')
.childDirectory('flutter_build'),
projectDir: flutterProject.directory,
defines: _parseDefines(argResults['define']),
);
return result;
}
static Map<String, String> _parseDefines(List<String> values) {
final Map<String, String> results = <String, String>{};
for (String chunk in values) {
final List<String> parts = chunk.split('=');
if (parts.length != 2) {
throwToolExit('Improperly formatted define flag: $chunk');
}
final String key = parts[0];
final String value = parts[1];
results[key] = value;
}
return results;
}
@override
Future<FlutterCommandResult> runCommand() async {
final BuildResult result = await buildSystem.build(target, environment, buildSystemConfig: BuildSystemConfig(
resourcePoolSize: argResults['resource-pool-size'],
));
if (!result.success) {
for (MapEntry<String, ExceptionMeasurement> data in result.exceptions.entries) {
printError('Target ${data.key} failed: ${data.value.exception}');
printError('${data.value.exception}');
}
throwToolExit('build failed');
} else {
printStatus('build succeeded');
}
return null;
}
}