Skip to content

Commit 8737f11

Browse files
authored
[flutter_tools] Migrate BuildBundleCommand to modular dependency injection (flutter#192462)
## Description This PR splits `BuildBundleCommand` migration out of [flutter#190783](flutter#190783) (part of modular dependency injection migration in flutter_tools). - Migrates `BuildBundleCommand` to modular dependency injection with `BuildSystem`, `FeatureFlags`, and `ToolContext`. - Exposes `FeatureFlags? featureFlags` on `FlutterCommandRunner`. - Updates `FlutterCommand.targetFile` to use `toolContext?.fs`. - Converted `build_bundle_test.dart` from permeable context tests to hermetic `testWithoutContext` tests.
1 parent 5010865 commit 8737f11

8 files changed

Lines changed: 584 additions & 619 deletions

File tree

‎packages/flutter_tools/lib/src/bundle_builder.dart‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'package:meta/meta.dart';
65
import 'package:pool/pool.dart';
76
import 'package:process/process.dart';
87

@@ -38,7 +37,7 @@ class BundleBuilder {
3837
String? applicationKernelFilePath,
3938
String? depfilePath,
4039
String? assetDirPath,
41-
@visibleForTesting BuildSystem? buildSystem,
40+
BuildSystem? buildSystem,
4241
}) async {
4342
project ??= FlutterProject.current();
4443
mainPath ??= defaultMainPath;

‎packages/flutter_tools/lib/src/commands/build.dart‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,14 @@ class BuildCommand extends FlutterCommand {
127127
verboseHelp: verboseHelp,
128128
),
129129
);
130-
_addSubcommand(BuildBundleCommand(logger: logger, verboseHelp: verboseHelp));
130+
_addSubcommand(
131+
BuildBundleCommand(
132+
buildSystem: buildSystem,
133+
featureFlags: featureFlags,
134+
toolContext: toolContext,
135+
verboseHelp: verboseHelp,
136+
),
137+
);
131138
_addSubcommand(
132139
BuildWebCommand(fileSystem: fileSystem, logger: logger, verboseHelp: verboseHelp),
133140
);

‎packages/flutter_tools/lib/src/commands/build_bundle.dart‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,33 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:meta/meta.dart';
56
import 'package:unified_analytics/unified_analytics.dart';
67

78
import '../base/common.dart';
9+
import '../base/file_system.dart';
810
import '../build_info.dart';
9-
import '../bundle.dart';
11+
import '../build_system/build_system.dart';
1012
import '../bundle_builder.dart';
13+
import '../context/tool_context.dart';
1114
import '../features.dart';
12-
import '../globals.dart' as globals;
1315
import '../project.dart';
1416
import '../runner/flutter_command.dart';
1517
import 'build.dart';
1618

1719
class BuildBundleCommand extends BuildSubCommand {
1820
BuildBundleCommand({
19-
required super.logger,
20-
bool verboseHelp = false,
21+
required this.buildSystem,
22+
required this.featureFlags,
23+
required ToolContext toolContext,
24+
required super.verboseHelp,
2125
BundleBuilder? bundleBuilder,
2226
}) : _bundleBuilder = bundleBuilder ?? BundleBuilder(),
23-
super(verboseHelp: verboseHelp) {
27+
super(
28+
logger: toolContext.logger,
29+
outputPreferences: toolContext.outputPreferences,
30+
toolContext: toolContext,
31+
) {
2432
usesTargetOption();
2533
usesFilesystemOptions(hide: !verboseHelp);
2634
usesBuildNumberOption();
@@ -30,7 +38,10 @@ class BuildBundleCommand extends BuildSubCommand {
3038
argParser
3139
..addOption(
3240
'depfile',
33-
defaultsTo: defaultDepfilePath,
41+
defaultsTo: toolContext.fs.path.join(
42+
getBuildDirectory(toolContext.config, toolContext.fs),
43+
'snapshot_blob.bin.d',
44+
),
3445
help:
3546
'A file path where a depfile will be written. '
3647
'This contains all build inputs and outputs in a Make-style syntax.',
@@ -54,7 +65,7 @@ class BuildBundleCommand extends BuildSubCommand {
5465
)
5566
..addOption(
5667
'asset-dir',
57-
defaultsTo: getAssetBuildDirectory(),
68+
defaultsTo: getAssetBuildDirectory(toolContext.config, toolContext.fs),
5869
help:
5970
'The output directory for the kernel_blob.bin file, the native snapshot, the assets, etc. '
6071
'Can be used to redirect the output when driving the Flutter toolchain from another build system.',
@@ -68,7 +79,15 @@ class BuildBundleCommand extends BuildSubCommand {
6879
usesTrackWidgetCreation(verboseHelp: verboseHelp);
6980
}
7081

82+
final BuildSystem buildSystem;
7183
final BundleBuilder _bundleBuilder;
84+
final FeatureFlags featureFlags;
85+
86+
@visibleForTesting
87+
BundleBuilder get bundleBuilder => _bundleBuilder;
88+
89+
@override
90+
ToolContext get toolContext => super.toolContext!;
7291

7392
@override
7493
final name = 'bundle';
@@ -84,10 +103,9 @@ class BuildBundleCommand extends BuildSubCommand {
84103

85104
@override
86105
Future<Event> unifiedAnalyticsUsageValues(String commandPath) async {
87-
final String projectDir = globals.fs.file(targetFile).parent.parent.path;
88-
final FlutterProject flutterProject = FlutterProject.fromDirectory(
89-
globals.fs.directory(projectDir),
90-
);
106+
final ToolContext(:FileSystem fs, :FlutterProjectFactory projectFactory) = toolContext;
107+
final String projectDir = fs.file(targetFile).parent.parent.path;
108+
final FlutterProject flutterProject = projectFactory.fromDirectory(fs.directory(projectDir));
91109
return Event.commandUsageValues(
92110
workflow: commandPath,
93111
commandHasTerminal: hasTerminal,
@@ -144,11 +162,12 @@ class BuildBundleCommand extends BuildSubCommand {
144162
final BuildInfo buildInfo = await getBuildInfo();
145163

146164
await _bundleBuilder.build(
147-
platform: platform,
148165
buildInfo: buildInfo,
149-
mainPath: targetFile,
150-
depfilePath: stringArg('depfile'),
166+
platform: platform,
151167
assetDirPath: stringArg('asset-dir'),
168+
buildSystem: buildSystem,
169+
depfilePath: stringArg('depfile'),
170+
mainPath: targetFile,
152171
);
153172
return FlutterCommandResult.success();
154173
}

‎packages/flutter_tools/lib/src/runner/flutter_command.dart‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ abstract class FlutterCommand extends Command<void> {
394394
if (rest != null && rest.isNotEmpty) {
395395
return rest.first;
396396
}
397-
return bundle.defaultMainPath;
397+
return toolContext?.fs.path.join('lib', 'main.dart') ?? bundle.defaultMainPath;
398398
}
399399

400400
/// Indicates if the current command running has a terminal attached.
@@ -1001,7 +1001,7 @@ abstract class FlutterCommand extends Command<void> {
10011001
/// This is only a default. Gradle injects it when the merged manifest does
10021002
/// not set `io.flutter.embedding.android.EnableHcpp` at all, so an entry in
10031003
/// the manifest wins over it. [explicitEnableHcpp] in turn wins over both.
1004-
bool get enableHcpp => explicitEnableHcpp ?? featureFlags.isHcppEnabled;
1004+
bool get enableHcpp => explicitEnableHcpp ?? runner?.featureFlags?.isHcppEnabled ?? false;
10051005

10061006
void addTestFlag({required bool verboseHelp}) {
10071007
argParser.addDescriptor(DebuggingOptionDescriptors.testFlag, verboseHelp: verboseHelp);
@@ -1228,8 +1228,13 @@ abstract class FlutterCommand extends Command<void> {
12281228
);
12291229
}
12301230

1231-
final String enabledFeatureFlags = featureFlags.allFeatures
1232-
.where((Feature feature) => featureFlags.isEnabled(feature))
1231+
final FeatureFlags? flags = runner?.featureFlags;
1232+
if (flags == null) {
1233+
return;
1234+
}
1235+
1236+
final String enabledFeatureFlags = flags.allFeatures
1237+
.where((Feature feature) => flags.isEnabled(feature))
12331238
.where((Feature feature) => feature.runtimeId != null)
12341239
.map((Feature feature) => feature.runtimeId!)
12351240
.join(',');

‎packages/flutter_tools/lib/src/runner/flutter_command_runner.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ class FlutterCommandRunner extends CommandRunner<void> {
329329
/// The [ToolContext] instance.
330330
ToolContext get toolContext => _toolContext;
331331

332+
/// The [FeatureFlags] instance, if provided.
333+
FeatureFlags? get featureFlags => _featureFlags;
334+
332335
// See https://github.com/flutter/flutter/issues/145158.
333336
late bool _machineFlagPresentInAnyCliArg;
334337

0 commit comments

Comments
 (0)