blob: efafa1ee9de9962cc770c48c30b733ecb78ddaba [file]
// 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 'package:meta/meta.dart';
import 'package:unified_analytics/unified_analytics.dart';
import '../base/common.dart';
import '../base/file_system.dart';
import '../build_info.dart';
import '../build_system/build_system.dart';
import '../bundle_builder.dart';
import '../context/tool_context.dart';
import '../features.dart';
import '../project.dart';
import '../runner/flutter_command.dart';
import 'build.dart';
class BuildBundleCommand extends BuildSubCommand {
BuildBundleCommand({
required this.buildSystem,
required this.featureFlags,
required ToolContext toolContext,
required super.verboseHelp,
BundleBuilder? bundleBuilder,
}) : _bundleBuilder = bundleBuilder ?? BundleBuilder(),
super(
logger: toolContext.logger,
outputPreferences: toolContext.outputPreferences,
toolContext: toolContext,
) {
usesTargetOption();
usesFilesystemOptions(hide: !verboseHelp);
usesBuildNumberOption();
addBuildModeFlags(verboseHelp: verboseHelp, defaultToRelease: false);
usesDartDefineOption();
usesExtraDartFlagOptions(verboseHelp: verboseHelp);
argParser
..addOption(
'depfile',
defaultsTo: toolContext.fs.path.join(
getBuildDirectory(toolContext.config, toolContext.fs),
'snapshot_blob.bin.d',
),
help:
'A file path where a depfile will be written. '
'This contains all build inputs and outputs in a Make-style syntax.',
)
..addOption(
'target-platform',
defaultsTo: 'android-arm',
allowed: const <String>[
'android-arm',
'android-arm64',
'android-x64',
'ios',
'darwin',
'linux-x64',
'linux-arm64',
'linux-riscv64',
'windows-x64',
'windows-arm64',
],
help: 'The architecture for which to build the application.',
)
..addOption(
'asset-dir',
defaultsTo: getAssetBuildDirectory(toolContext.config, toolContext.fs),
help:
'The output directory for the kernel_blob.bin file, the native snapshot, the assets, etc. '
'Can be used to redirect the output when driving the Flutter toolchain from another build system.',
)
..addFlag(
'tree-shake-icons',
hide: !verboseHelp,
help: '(deprecated) Icon font tree shaking is not supported by this command.',
);
usesPubOption();
usesTrackWidgetCreation(verboseHelp: verboseHelp);
}
final BuildSystem buildSystem;
final BundleBuilder _bundleBuilder;
final FeatureFlags featureFlags;
@visibleForTesting
BundleBuilder get bundleBuilder => _bundleBuilder;
@override
ToolContext get toolContext => super.toolContext!;
@override
final name = 'bundle';
@override
final description = 'Build the Flutter assets directory from your app.';
@override
final usageFooter =
'The Flutter assets directory contains your '
'application code and resources; they are used by some Flutter Android and'
' iOS runtimes.';
@override
Future<Event> unifiedAnalyticsUsageValues(String commandPath) async {
final ToolContext(:FileSystem fs, :FlutterProjectFactory projectFactory) = toolContext;
final String projectDir = fs.file(targetFile).parent.parent.path;
final FlutterProject flutterProject = projectFactory.fromDirectory(fs.directory(projectDir));
return Event.commandUsageValues(
workflow: commandPath,
commandHasTerminal: hasTerminal,
buildBundleTargetPlatform: stringArg('target-platform'),
buildBundleIsModule: flutterProject.isModule,
);
}
@override
Future<void> validateCommand() async {
if (boolArg('tree-shake-icons')) {
throwToolExit(
'The "--tree-shake-icons" flag is deprecated for "build bundle" and will be removed in a future version of Flutter.',
);
}
return super.validateCommand();
}
@override
Future<FlutterCommandResult> runCommand() async {
final String targetPlatform = stringArg('target-platform')!;
final platform = TargetPlatform.fromName(targetPlatform);
// Check for target platforms that are only allowed via feature flags.
switch (platform) {
case TargetPlatform.darwin:
if (!featureFlags.isMacOSEnabled) {
throwToolExit('macOS is not a supported target platform.');
}
case TargetPlatform.windows_x64:
case TargetPlatform.windows_arm64:
if (!featureFlags.isWindowsEnabled) {
throwToolExit('Windows is not a supported target platform.');
}
case TargetPlatform.linux_x64:
case TargetPlatform.linux_arm64:
case TargetPlatform.linux_riscv64:
if (!featureFlags.isLinuxEnabled) {
throwToolExit('Linux is not a supported target platform.');
}
case TargetPlatform.android:
case TargetPlatform.android_arm:
case TargetPlatform.android_arm64:
case TargetPlatform.android_x64:
case TargetPlatform.fuchsia_arm64:
case TargetPlatform.fuchsia_x64:
case TargetPlatform.ios:
case TargetPlatform.tester:
case TargetPlatform.web_javascript:
break;
case TargetPlatform.unsupported:
TargetPlatform.throwUnsupportedTarget();
}
final BuildInfo buildInfo = await getBuildInfo();
await _bundleBuilder.build(
buildInfo: buildInfo,
platform: platform,
assetDirPath: stringArg('asset-dir'),
buildSystem: buildSystem,
depfilePath: stringArg('depfile'),
mainPath: targetFile,
);
return FlutterCommandResult.success();
}
}