Skip to content

Commit 6da6a9b

Browse files
authored
[flutter_tools] Remove globals from macOS build, migration, and Xcode project (flutter#193652)
Migrates macOS build utilities, `XcodeProjectInterpreter`, `IosProject`, `MacOSProject`, and `XcodeProjectMigration` away from `globals.dart` to explicit dependency injection via `ToolContext`. Part of flutter#47161 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing.
1 parent 614df5a commit 6da6a9b

29 files changed

Lines changed: 1887 additions & 1853 deletions

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class BuildCommand extends FlutterCommand {
161161
);
162162
_addSubcommand(
163163
BuildMacosCommand(
164+
appleContext: appleContext,
164165
buildSystem: buildSystem,
165166
featureFlags: featureFlags,
166167
toolContext: toolContext,

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import '../base/file_system.dart';
1010
import '../base/logger.dart';
1111
import '../build_info.dart';
1212
import '../build_system/build_system.dart';
13+
import '../context/apple_context.dart';
1314
import '../context/tool_context.dart';
1415
import '../features.dart';
1516
import '../macos/build_macos.dart';
@@ -20,6 +21,7 @@ import 'build.dart';
2021
/// A command to build a macOS desktop target through a build shell script.
2122
class BuildMacosCommand extends BuildSubCommand {
2223
BuildMacosCommand({
24+
required this.appleContext,
2325
required this.buildSystem,
2426
required this.featureFlags,
2527
required ToolContext super.toolContext,
@@ -37,6 +39,9 @@ class BuildMacosCommand extends BuildSubCommand {
3739
], verboseHelp: verboseHelp);
3840
}
3941

42+
/// Holds Apple-specific dependencies.
43+
final AppleContext appleContext;
44+
4045
/// The build system used to execute targets.
4146
final BuildSystem buildSystem;
4247

@@ -68,9 +73,7 @@ class BuildMacosCommand extends BuildSubCommand {
6873

6974
@override
7075
Future<FlutterCommandResult> runCommand() async {
71-
final FileSystem fs = toolContext.fs;
72-
final Logger logger = toolContext.logger;
73-
76+
final ToolContext(:FileSystem fs, :Logger logger) = toolContext;
7477
final BuildInfo buildInfo = await getBuildInfo();
7578
if (!featureFlags.isMacOSEnabled) {
7679
throwToolExit(
@@ -82,19 +85,22 @@ class BuildMacosCommand extends BuildSubCommand {
8285
}
8386

8487
await buildMacOS(
85-
flutterProject: project,
88+
analytics: analytics,
89+
appleContext: appleContext,
8690
buildInfo: buildInfo,
87-
targetOverride: targetFile,
88-
verboseLogging: logger.isVerbose || globalResults?[FlutterGlobalOptions.kVerboseFlag] == true,
8991
configOnly: configOnly,
92+
featureFlags: featureFlags,
93+
flutterProject: project,
9094
sizeAnalyzer: SizeAnalyzer(
9195
fileSystem: fs,
9296
logger: logger,
9397
appFilenamePattern: 'App',
9498
analytics: analytics,
9599
),
96-
usingCISystem: usingCISystem,
100+
targetOverride: targetFile,
97101
toolContext: toolContext,
102+
usingCISystem: usingCISystem,
103+
verboseLogging: logger.isVerbose || globalResults?[FlutterGlobalOptions.kVerboseFlag] == true,
98104
);
99105
return FlutterCommandResult.success();
100106
}

‎packages/flutter_tools/lib/src/context/tool_dependencies.dart‎

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,43 @@ class ToolDependencies {
341341
LocalSignals.instance.configureExit(analytics: finalAnalytics, logger: finalLogger);
342342

343343
// 9. Project Factory and Operating System Utilities
344+
final String projectRoot = findProjectRoot(finalFS) ?? finalFS.currentDirectory.path;
345+
final FlutterManifest? projectManifest = FlutterManifest.createFromPath(
346+
finalFS.path.join(projectRoot, 'pubspec.yaml'),
347+
fileSystem: finalFS,
348+
logger: finalLogger,
349+
);
350+
351+
final FeatureFlags finalFeatureFlags =
352+
featureFlags ??
353+
FlutterFeatureFlags(
354+
flutterVersion: finalFlutterVersion,
355+
featuresConfig: FlutterFeaturesConfig(
356+
globalConfig: finalConfig,
357+
platform: finalPlatform,
358+
projectManifest: projectManifest,
359+
),
360+
platform: finalPlatform,
361+
);
362+
363+
final PlistParser finalPlistParser =
364+
plistParser ??
365+
PlistParser(fileSystem: finalFS, processManager: finalProcessManager, logger: finalLogger);
366+
367+
late final XcodeProjectInterpreter finalXcodeProjectInterpreter;
368+
late final Xcode finalXcode;
369+
344370
final FlutterProjectFactory finalProjectFactory =
345371
projectFactory ??
346-
FlutterProjectFactory(logger: finalLogger, fileSystem: finalFS, config: finalConfig);
372+
FlutterProjectFactory(
373+
config: finalConfig,
374+
featureFlags: finalFeatureFlags,
375+
fileSystem: finalFS,
376+
logger: finalLogger,
377+
plistParser: finalPlistParser,
378+
xcodeBuilder: () => finalXcode,
379+
xcodeProjectInterpreterBuilder: () => finalXcodeProjectInterpreter,
380+
);
347381

348382
final finalOS = OperatingSystemUtils(
349383
fileSystem: finalFS,
@@ -443,11 +477,11 @@ class ToolDependencies {
443477
);
444478

445479
// 11. AppleContext Dependencies
446-
final XcodeProjectInterpreter finalXcodeProjectInterpreter =
480+
finalXcodeProjectInterpreter =
447481
xcodeProjectInterpreter ??
448482
XcodeProjectInterpreter(analytics: finalAnalytics, toolContext: finalToolContext);
449483

450-
final Xcode finalXcode =
484+
finalXcode =
451485
xcode ??
452486
Xcode(
453487
platform: finalPlatform,
@@ -487,33 +521,10 @@ class ToolDependencies {
487521
xcode: finalXcode,
488522
);
489523

490-
final String projectRoot = findProjectRoot(finalFS) ?? finalFS.currentDirectory.path;
491-
final FlutterManifest? projectManifest = FlutterManifest.createFromPath(
492-
finalFS.path.join(projectRoot, 'pubspec.yaml'),
493-
fileSystem: finalFS,
494-
logger: finalLogger,
495-
);
496-
497-
final FeatureFlags finalFeatureFlags =
498-
featureFlags ??
499-
FlutterFeatureFlags(
500-
flutterVersion: finalFlutterVersion,
501-
featuresConfig: FlutterFeaturesConfig(
502-
globalConfig: finalConfig,
503-
platform: finalPlatform,
504-
projectManifest: projectManifest,
505-
),
506-
platform: finalPlatform,
507-
);
508-
509524
final IOSWorkflow finalIOSWorkflow =
510525
iosWorkflow ??
511526
IOSWorkflow(featureFlags: finalFeatureFlags, xcode: finalXcode, platform: finalPlatform);
512527

513-
final PlistParser finalPlistParser =
514-
plistParser ??
515-
PlistParser(fileSystem: finalFS, processManager: finalProcessManager, logger: finalLogger);
516-
517528
final IOSSimulatorUtils finalIOSSimulatorUtils =
518529
iosSimulatorUtils ??
519530
IOSSimulatorUtils(
@@ -522,6 +533,7 @@ class ToolDependencies {
522533
toolContext: finalToolContext,
523534
xcode: finalXcode,
524535
);
536+
525537
// 12. AndroidContext Dependencies
526538
final AndroidSdk? Function() finalAndroidSdkBuilder =
527539
androidSdkBuilder ??
@@ -607,6 +619,17 @@ class ToolDependencies {
607619
logger: finalLogger,
608620
);
609621

622+
final appleContext = AppleContext(
623+
cocoaPods: finalCocoaPods,
624+
cocoapodsValidator: finalCocoapodsValidator,
625+
iosSimulatorUtils: finalIOSSimulatorUtils,
626+
iosWorkflow: finalIOSWorkflow,
627+
plistParser: finalPlistParser,
628+
xcdevice: finalXCDevice,
629+
xcode: finalXcode,
630+
xcodeProjectInterpreter: finalXcodeProjectInterpreter,
631+
);
632+
610633
final extensionBuildManager = ExtensionBuildManager(
611634
extensionManager: extensionManager,
612635
featureFlags: finalFeatureFlags,
@@ -625,6 +648,7 @@ class ToolDependencies {
625648
analytics: finalAnalytics,
626649
androidSdkBuilder: () => finalAndroidContext.androidSdk,
627650
androidWorkflow: finalAndroidWorkflow,
651+
appleContext: appleContext,
628652
buildSystem: finalBuildSystem,
629653
buildTargets: finalBuildTargets,
630654
extensionBuildManager: extensionBuildManager,
@@ -641,16 +665,7 @@ class ToolDependencies {
641665
return ToolDependencies(
642666
analytics: finalAnalytics,
643667
androidContext: finalAndroidContext,
644-
appleContext: AppleContext(
645-
cocoaPods: finalCocoaPods,
646-
cocoapodsValidator: finalCocoapodsValidator,
647-
iosSimulatorUtils: finalIOSSimulatorUtils,
648-
iosWorkflow: finalIOSWorkflow,
649-
plistParser: finalPlistParser,
650-
xcdevice: finalXCDevice,
651-
xcode: finalXcode,
652-
xcodeProjectInterpreter: finalXcodeProjectInterpreter,
653-
),
668+
appleContext: appleContext,
654669
buildSystem: finalBuildSystem,
655670
buildTargets: finalBuildTargets,
656671
crashReporter: finalCrashReporter,

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'base/platform.dart';
1616
import 'base/user_messages.dart';
1717
import 'build_system/build_system.dart';
1818
import 'build_system/build_targets.dart';
19+
import 'context/apple_context.dart';
1920
import 'context/tool_context.dart';
2021
import 'custom_devices/custom_device.dart';
2122
import 'device.dart';
@@ -43,6 +44,7 @@ class FlutterDeviceManager extends DeviceManager {
4344
required Analytics analytics,
4445
required AndroidSdk? Function() androidSdkBuilder,
4546
required AndroidWorkflow androidWorkflow,
47+
required AppleContext appleContext,
4648
required BuildSystem buildSystem,
4749
required BuildTargets buildTargets,
4850
required ExtensionBuildManager extensionBuildManager,
@@ -88,7 +90,13 @@ class FlutterDeviceManager extends DeviceManager {
8890
buildTargets: buildTargets,
8991
nativeAssetsBuilder: nativeAssetsBuilder,
9092
),
91-
MacOSDevices(macOSWorkflow: macOSWorkflow, toolContext: toolContext),
93+
MacOSDevices(
94+
analytics: analytics,
95+
appleContext: appleContext,
96+
featureFlags: featureFlags,
97+
macOSWorkflow: macOSWorkflow,
98+
toolContext: toolContext,
99+
),
92100
MacOSDesignedForIPadDevices(iosWorkflow: iosWorkflow, toolContext: toolContext),
93101
LinuxDevices(analytics: analytics, featureFlags: featureFlags, toolContext: toolContext),
94102
WindowsDevices(

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class PluginPlatformConfig {
3030

3131
/// A wrapper around the `flutter` section in the `pubspec.yaml` file.
3232
class FlutterManifest {
33-
FlutterManifest._({required this._logger});
33+
FlutterManifest._({required this.logger});
3434

3535
/// Returns an empty manifest.
3636
factory FlutterManifest.empty({required Logger logger}) = FlutterManifest._;
@@ -83,7 +83,7 @@ class FlutterManifest {
8383
List<DeferredComponent>? deferredComponents,
8484
bool removeDependencies = false,
8585
}) {
86-
final copy = FlutterManifest._(logger: _logger);
86+
final copy = FlutterManifest._(logger: logger);
8787
copy._descriptor = <String, Object?>{..._descriptor};
8888
if (removeDependencies) {
8989
// Remove the non-Flutter SDK dependencies if they're going to be added back later.
@@ -126,10 +126,8 @@ class FlutterManifest {
126126
return copy;
127127
}
128128

129-
final Logger _logger;
130-
131129
/// The logger associated with this manifest.
132-
Logger get logger => _logger;
130+
final Logger logger;
133131

134132
/// A map representation of the entire `pubspec.yaml` file.
135133
var _descriptor = <String, Object?>{};
@@ -181,7 +179,7 @@ class FlutterManifest {
181179
version = Version.parse(verStr);
182180
} on Exception {
183181
if (!_hasShowInvalidVersionMsg) {
184-
_logger.printStatus(_invalidVersionSettingHintMessage(verStr), emphasis: true);
182+
logger.printStatus(_invalidVersionSettingHintMessage(verStr), emphasis: true);
185183
_hasShowInvalidVersionMsg = true;
186184
}
187185
}
@@ -302,7 +300,7 @@ class FlutterManifest {
302300
}
303301
for (final Object? component in deferredComponents) {
304302
if (component is! YamlMap) {
305-
_logger.printError('Expected deferred component manifest to be a map.');
303+
logger.printError('Expected deferred component manifest to be a map.');
306304
continue;
307305
}
308306
components.add(
@@ -403,19 +401,19 @@ class FlutterManifest {
403401
final fontFiles = fontFamily['fonts'] as YamlList?;
404402
final familyName = fontFamily['family'] as String?;
405403
if (familyName == null) {
406-
_logger.printWarning('Warning: Missing family name for font.', emphasis: true);
404+
logger.printWarning('Warning: Missing family name for font.', emphasis: true);
407405
continue;
408406
}
409407
if (fontFiles == null) {
410-
_logger.printWarning('Warning: No fonts specified for font $familyName', emphasis: true);
408+
logger.printWarning('Warning: No fonts specified for font $familyName', emphasis: true);
411409
continue;
412410
}
413411

414412
final fontAssets = <FontAsset>[];
415413
for (final Map<Object?, Object?> fontFile in fontFiles.cast<Map<Object?, Object?>>()) {
416414
final asset = fontFile['asset'] as String?;
417415
if (asset == null) {
418-
_logger.printWarning('Warning: Missing asset in fonts for $familyName', emphasis: true);
416+
logger.printWarning('Warning: Missing asset in fonts for $familyName', emphasis: true);
419417
continue;
420418
}
421419

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import 'cache.dart';
3535
import 'custom_devices/custom_devices_config.dart';
3636
import 'device.dart';
3737
import 'doctor.dart';
38+
import 'features.dart';
3839
import 'git.dart';
3940
import 'ios/ios_workflow.dart';
4041
import 'ios/plist_parser.dart';
@@ -270,7 +271,15 @@ CocoaPods? get cocoaPods => context.get<CocoaPods>();
270271

271272
FlutterProjectFactory get projectFactory {
272273
return context.get<FlutterProjectFactory>() ??
273-
FlutterProjectFactory(logger: logger, fileSystem: fs, config: config);
274+
FlutterProjectFactory(
275+
config: config,
276+
featureFlags: featureFlags,
277+
fileSystem: fs,
278+
logger: logger,
279+
plistParser: plistParser,
280+
xcode: xcode,
281+
xcodeProjectInterpreter: xcodeProjectInterpreter,
282+
);
274283
}
275284

276285
CustomDevicesConfig get customDevicesConfig => context.get<CustomDevicesConfig>()!;

‎packages/flutter_tools/lib/src/ios/mac.dart‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,11 @@ Future<XcodeBuildResult> buildXcodeProject({
149149
app.project,
150150
darwinPlatform,
151151
buildInfo,
152-
xcodeProjectInterpreter: globals.xcodeProjectInterpreter!,
153-
logger: globals.logger,
154-
fileSystem: globals.fs,
155-
plistParser: globals.plistParser,
156-
config: globals.config,
157152
analytics: globals.analytics,
158-
hostPlatform: globals.platform,
159-
operatingSystemUtils: globals.os,
160-
flutterVersion: globals.flutterVersion,
153+
plistParser: globals.plistParser,
161154
reportCrashes: !await globals.isRunningOnBot,
155+
toolContext: toolContext,
156+
xcodeProjectInterpreter: globals.xcodeProjectInterpreter!,
162157
),
163158
SwiftPackageManagerGitignoreMigration(project, globals.logger),
164159
MetalAPIValidationMigrator.ios(app.project, globals.logger),

0 commit comments

Comments
 (0)