Skip to content

Commit f52eaae

Browse files
authored
Allow overriding the native assets yaml file in the resident runner. (#142016)
This is used when the native assets are built by a separate build system. Context: b/286799303
1 parent f04958a commit f52eaae

File tree

6 files changed

+105
-14
lines changed

6 files changed

+105
-14
lines changed

packages/flutter_tools/lib/src/commands/attach.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class AttachCommand extends FlutterCommand {
9090
addEnableExperimentation(hide: !verboseHelp);
9191
addNullSafetyModeOptions(hide: !verboseHelp);
9292
usesInitializeFromDillOption(hide: !verboseHelp);
93+
usesNativeAssetsOption(hide: !verboseHelp);
9394
argParser
9495
..addOption(
9596
'debug-port',
@@ -539,6 +540,7 @@ known, it can be explicitly provided to attach via the command-line, e.g.
539540
dillOutputPath: stringArg('output-dill'),
540541
ipv6: usesIpv6,
541542
flutterProject: flutterProject,
543+
nativeAssetsYamlFile: stringArg(FlutterOptions.kNativeAssetsYamlFile),
542544
analytics: analytics,
543545
)
544546
: ColdRunner(
@@ -572,6 +574,7 @@ class HotRunnerFactory {
572574
bool stayResident = true,
573575
bool ipv6 = false,
574576
FlutterProject? flutterProject,
577+
String? nativeAssetsYamlFile,
575578
required Analytics analytics,
576579
}) => HotRunner(
577580
devices,
@@ -584,6 +587,7 @@ class HotRunnerFactory {
584587
dillOutputPath: dillOutputPath,
585588
stayResident: stayResident,
586589
ipv6: ipv6,
590+
nativeAssetsYamlFile: nativeAssetsYamlFile,
587591
analytics: analytics,
588592
);
589593
}

packages/flutter_tools/lib/src/commands/run.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ class RunCommand extends RunCommandBase {
331331
usesFrontendServerStarterPathOption(verboseHelp: verboseHelp);
332332
addEnableExperimentation(hide: !verboseHelp);
333333
usesInitializeFromDillOption(hide: !verboseHelp);
334+
usesNativeAssetsOption(hide: !verboseHelp);
334335

335336
// By default, the app should to publish the VM service port over mDNS.
336337
// This will allow subsequent "flutter attach" commands to connect to the VM
@@ -641,6 +642,7 @@ class RunCommand extends RunCommandBase {
641642
ipv6: ipv6 ?? false,
642643
multidexEnabled: boolArg('multidex'),
643644
analytics: globals.analytics,
645+
nativeAssetsYamlFile: stringArg(FlutterOptions.kNativeAssetsYamlFile),
644646
);
645647
} else if (webMode) {
646648
return webRunnerFactory!.createWebRunner(

packages/flutter_tools/lib/src/run_hot.dart

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ class HotRunner extends ResidentRunner {
9595
ReloadSourcesHelper reloadSourcesHelper = defaultReloadSourcesHelper,
9696
ReassembleHelper reassembleHelper = _defaultReassembleHelper,
9797
NativeAssetsBuildRunner? buildRunner,
98+
String? nativeAssetsYamlFile,
9899
required Analytics analytics,
99100
}) : _stopwatchFactory = stopwatchFactory,
100101
_reloadSourcesHelper = reloadSourcesHelper,
101102
_reassembleHelper = reassembleHelper,
102103
_buildRunner = buildRunner,
104+
_nativeAssetsYamlFile = nativeAssetsYamlFile,
103105
_analytics = analytics,
104106
super(
105107
hotMode: true,
@@ -140,6 +142,7 @@ class HotRunner extends ResidentRunner {
140142
bool? _emulator;
141143

142144
NativeAssetsBuildRunner? _buildRunner;
145+
final String? _nativeAssetsYamlFile;
143146

144147
String? flavor;
145148

@@ -371,19 +374,24 @@ class HotRunner extends ResidentRunner {
371374
}) async {
372375
await _calculateTargetPlatform();
373376

374-
final Uri projectUri = Uri.directory(projectRootPath);
375-
_buildRunner ??= NativeAssetsBuildRunnerImpl(
376-
projectUri,
377-
debuggingOptions.buildInfo.packageConfig,
378-
fileSystem,
379-
globals.logger,
380-
);
381-
final Uri? nativeAssetsYaml = await dryRunNativeAssets(
382-
projectUri: projectUri,
383-
fileSystem: fileSystem,
384-
buildRunner: _buildRunner!,
385-
flutterDevices: flutterDevices,
386-
);
377+
final Uri? nativeAssetsYaml;
378+
if (_nativeAssetsYamlFile != null) {
379+
nativeAssetsYaml = globals.fs.path.toUri(_nativeAssetsYamlFile);
380+
} else {
381+
final Uri projectUri = Uri.directory(projectRootPath);
382+
_buildRunner ??= NativeAssetsBuildRunnerImpl(
383+
projectUri,
384+
debuggingOptions.buildInfo.packageConfig,
385+
fileSystem,
386+
globals.logger,
387+
);
388+
nativeAssetsYaml = await dryRunNativeAssets(
389+
projectUri: projectUri,
390+
fileSystem: fileSystem,
391+
buildRunner: _buildRunner!,
392+
flutterDevices: flutterDevices,
393+
);
394+
}
387395

388396
final Stopwatch appStartedTimer = Stopwatch()..start();
389397
final File mainFile = globals.fs.file(mainPath);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ abstract final class FlutterOptions {
150150
static const String kAndroidProjectArgs = 'android-project-arg';
151151
static const String kInitializeFromDill = 'initialize-from-dill';
152152
static const String kAssumeInitializeFromDillUpToDate = 'assume-initialize-from-dill-up-to-date';
153+
static const String kNativeAssetsYamlFile = 'native-assets-yaml-file';
153154
static const String kFatalWarnings = 'fatal-warnings';
154155
static const String kUseApplicationBinary = 'use-application-binary';
155156
static const String kWebBrowserFlag = 'web-browser-flag';
@@ -1007,6 +1008,14 @@ abstract class FlutterCommand extends Command<void> {
10071008
);
10081009
}
10091010

1011+
void usesNativeAssetsOption({ required bool hide }) {
1012+
argParser.addOption(FlutterOptions.kNativeAssetsYamlFile,
1013+
help: 'Initializes the resident compiler with a custom native assets '
1014+
'yaml file instead of the default cached location.',
1015+
hide: hide,
1016+
);
1017+
}
1018+
10101019
void addMultidexOption({ bool hide = false }) {
10111020
argParser.addFlag('multidex',
10121021
defaultsTo: true,

packages/flutter_tools/test/commands.shard/hermetic/attach_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,7 @@ class FakeHotRunnerFactory extends Fake implements HotRunnerFactory {
12081208
bool ipv6 = false,
12091209
FlutterProject? flutterProject,
12101210
Analytics? analytics,
1211+
String? nativeAssetsYamlFile,
12111212
}) {
12121213
if (_artifactTester != null) {
12131214
for (final FlutterDevice device in devices) {

packages/flutter_tools/test/general.shard/resident_runner_test.dart

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2455,12 +2455,14 @@ flutter:
24552455
targetPlatform: TargetPlatform.darwin,
24562456
sdkNameAndVersion: 'Macos',
24572457
);
2458+
final FakeResidentCompiler residentCompiler = FakeResidentCompiler();
24582459
final FakeFlutterDevice flutterDevice = FakeFlutterDevice()
24592460
..testUri = testUri
24602461
..vmServiceHost = (() => fakeVmServiceHost)
24612462
..device = device
24622463
.._devFS = devFS
2463-
..targetPlatform = TargetPlatform.darwin;
2464+
..targetPlatform = TargetPlatform.darwin
2465+
..generator = residentCompiler;
24642466

24652467
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[
24662468
listViews,
@@ -2508,6 +2510,67 @@ flutter:
25082510
expect(buildRunner.dryRunInvocations, 1);
25092511
expect(buildRunner.hasPackageConfigInvocations, 1);
25102512
expect(buildRunner.packagesWithNativeAssetsInvocations, 1);
2513+
2514+
expect(residentCompiler.recompileCalled, true);
2515+
expect(residentCompiler.receivedNativeAssetsYaml.toString(), endsWith('native_assets/macos/native_assets.yaml'));
2516+
}),
2517+
overrides: <Type, Generator>{
2518+
ProcessManager: () => FakeProcessManager.any(),
2519+
FeatureFlags: () => TestFeatureFlags(isNativeAssetsEnabled: true, isMacOSEnabled: true),
2520+
});
2521+
2522+
testUsingContext(
2523+
'use the nativeAssetsYamlFile when provided',
2524+
() => testbed.run(() async {
2525+
final FakeDevice device = FakeDevice(
2526+
targetPlatform: TargetPlatform.darwin,
2527+
sdkNameAndVersion: 'Macos',
2528+
);
2529+
final FakeResidentCompiler residentCompiler = FakeResidentCompiler();
2530+
final FakeFlutterDevice flutterDevice = FakeFlutterDevice()
2531+
..testUri = testUri
2532+
..vmServiceHost = (() => fakeVmServiceHost)
2533+
..device = device
2534+
.._devFS = devFS
2535+
..targetPlatform = TargetPlatform.darwin
2536+
..generator = residentCompiler;
2537+
2538+
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[
2539+
listViews,
2540+
listViews,
2541+
]);
2542+
globals.fs
2543+
.file(globals.fs.path.join('lib', 'main.dart'))
2544+
.createSync(recursive: true);
2545+
final FakeNativeAssetsBuildRunner buildRunner = FakeNativeAssetsBuildRunner();
2546+
residentRunner = HotRunner(
2547+
<FlutterDevice>[
2548+
flutterDevice,
2549+
],
2550+
stayResident: false,
2551+
debuggingOptions: DebuggingOptions.enabled(const BuildInfo(
2552+
BuildMode.debug,
2553+
'',
2554+
treeShakeIcons: false,
2555+
trackWidgetCreation: true,
2556+
)),
2557+
target: 'main.dart',
2558+
devtoolsHandler: createNoOpHandler,
2559+
buildRunner: buildRunner,
2560+
analytics: fakeAnalytics,
2561+
nativeAssetsYamlFile: 'foo.yaml',
2562+
);
2563+
2564+
final int? result = await residentRunner.run();
2565+
expect(result, 0);
2566+
2567+
expect(buildRunner.buildInvocations, 0);
2568+
expect(buildRunner.dryRunInvocations, 0);
2569+
expect(buildRunner.hasPackageConfigInvocations, 0);
2570+
expect(buildRunner.packagesWithNativeAssetsInvocations, 0);
2571+
2572+
expect(residentCompiler.recompileCalled, true);
2573+
expect(residentCompiler.receivedNativeAssetsYaml, globals.fs.path.toUri('foo.yaml'));
25112574
}),
25122575
overrides: <Type, Generator>{
25132576
ProcessManager: () => FakeProcessManager.any(),
@@ -2700,6 +2763,8 @@ class FakeDelegateFlutterDevice extends FlutterDevice {
27002763
class FakeResidentCompiler extends Fake implements ResidentCompiler {
27012764
CompilerOutput? nextOutput;
27022765
bool didSuppressErrors = false;
2766+
Uri? receivedNativeAssetsYaml;
2767+
bool recompileCalled = false;
27032768

27042769
@override
27052770
Future<CompilerOutput?> recompile(
@@ -2714,6 +2779,8 @@ class FakeResidentCompiler extends Fake implements ResidentCompiler {
27142779
File? dartPluginRegistrant,
27152780
Uri? nativeAssetsYaml,
27162781
}) async {
2782+
recompileCalled = true;
2783+
receivedNativeAssetsYaml = nativeAssetsYaml;
27172784
didSuppressErrors = suppressErrors;
27182785
return nextOutput ?? const CompilerOutput('foo.dill', 0, <Uri>[]);
27192786
}

0 commit comments

Comments
 (0)