Skip to content

Commit d77d7c3

Browse files
authored
Add a warning/additional handlers for parsingsynthetic-package. (#157934)
Closes flutter/flutter#157928. Closes flutter/flutter#157929. | Condition | Expectation | | --------- | ------------ | | `synthetic-packages: true` && `--implicit-pubpsec-resolution` | Generates `flutter_gen` with warning. | `<no synthetic-packages key>` && `--implicit-pubspec-resolution` | Generates `flutter_gen` with warning. | `synthetic-packages: false` && `--implicit-pubpsec-resolution` | Does not generate `flutter_gen`. | `synthetic-packages: true` && `--no-implicit-pubpsec-resolution` | Error. | `<no synthetic-packages key>` && `--no-implicit-pubspec-resolution` | Does not generate `flutter_gen`. | `synthetic-packages: false` && `--no-implicit-pubpsec-resolution` | Generates `flutter_gen` with warning.
1 parent 088e357 commit d77d7c3

File tree

3 files changed

+209
-5
lines changed

3 files changed

+209
-5
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ class GenerateLocalizationsCommand extends FlutterCommand {
147147
'generated as a synthetic package or at a specified directory in '
148148
'the Flutter project.\n'
149149
'\n'
150+
'DEPRECATED: https://flutter.dev/to/flutter-gen-deprecation.\n'
151+
'\n'
150152
'This flag is set to true by default.\n'
151153
'\n'
152154
'When synthetic-package is set to false, it will generate the '

packages/flutter_tools/lib/src/dart/generate_synthetic_packages.dart

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ Future<void> generateLocalizationsSyntheticPackage({
1515
required BuildSystem buildSystem,
1616
required BuildTargets buildTargets,
1717
}) async {
18-
1918
final FileSystem fileSystem = environment.fileSystem;
2019
final File l10nYamlFile = fileSystem.file(
21-
fileSystem.path.join(environment.projectDir.path, 'l10n.yaml'));
20+
fileSystem.path.join(environment.projectDir.path, 'l10n.yaml'),
21+
);
2222

2323
// If pubspec.yaml has generate:true and if l10n.yaml exists in the
2424
// root project directory, check to see if a synthetic package should
@@ -30,7 +30,7 @@ Future<void> generateLocalizationsSyntheticPackage({
3030
final YamlNode yamlNode = loadYamlNode(l10nYamlFile.readAsStringSync());
3131
if (yamlNode.value != null && yamlNode is! YamlMap) {
3232
throwToolExit(
33-
'Expected ${l10nYamlFile.path} to contain a map, instead was $yamlNode'
33+
'Expected ${l10nYamlFile.path} to contain a map, instead was $yamlNode',
3434
);
3535
}
3636

@@ -41,8 +41,7 @@ Future<void> generateLocalizationsSyntheticPackage({
4141
final Object? value = yamlMap['synthetic-package'];
4242
if (value is! bool && value != null) {
4343
throwToolExit(
44-
'Expected "synthetic-package" to have a bool value, '
45-
'instead was "$value"'
44+
'Expected "synthetic-package" to have a bool value, instead was "$value"',
4645
);
4746
}
4847

@@ -52,8 +51,30 @@ Future<void> generateLocalizationsSyntheticPackage({
5251
if (isSyntheticL10nPackage == false) {
5352
return;
5453
}
54+
} else if (!environment.useImplicitPubspecResolution) {
55+
// --no-implicit-pubspec-resolution was passed, and synthetic-packages: true was not.
56+
return;
5557
}
5658

59+
if (!environment.useImplicitPubspecResolution) {
60+
throwToolExit(
61+
'Cannot generate a synthetic package when --no-implicit-pubspec-resolution is passed.\n'
62+
'\n'
63+
'Synthetic package output (package:flutter_gen) is deprecated: '
64+
'https://flutter.dev/to/flutter-gen-deprecation. If you are seeing this '
65+
'message either you have provided --no-implicit-pubspec-resolution, or '
66+
'it is the default value (see flutter --verbose --help).',
67+
);
68+
}
69+
70+
// Log a warning: synthetic-package: true (or implicit true) is deprecated.
71+
environment.logger.printWarning(
72+
'Synthetic package output (package:flutter_gen) is deprecated: '
73+
'https://flutter.dev/to/flutter-gen-deprecation. In a future release, '
74+
'synthetic-package will default to `false` and will later be removed '
75+
'entirely.',
76+
);
77+
5778
final BuildResult result = await buildSystem.build(
5879
buildTargets.generateLocalizationsTarget,
5980
environment,

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

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,185 @@ void main() {
267267
throwsToolExit(message: 'to have a bool value, instead was "nonBoolValue"'),
268268
);
269269
});
270+
271+
testWithoutContext('synthetic-package: true (implicit) logs a deprecation warning', () async {
272+
// Project directory setup for gen_l10n logic.
273+
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
274+
275+
// Add generate:true to pubspec.yaml.
276+
final File pubspecFile = fileSystem.file('pubspec.yaml')..createSync();
277+
final String content = pubspecFile.readAsStringSync().replaceFirst(
278+
'\nflutter:\n',
279+
'\nflutter:\n generate: true\n',
280+
);
281+
pubspecFile.writeAsStringSync(content);
282+
283+
// Create a blank l10n.yaml file.
284+
fileSystem.file('l10n.yaml').writeAsStringSync('');
285+
286+
final BufferLogger mockBufferLogger = BufferLogger.test();
287+
final Environment environment = Environment.test(
288+
fileSystem.currentDirectory,
289+
fileSystem: fileSystem,
290+
logger: mockBufferLogger,
291+
artifacts: Artifacts.test(),
292+
processManager: FakeProcessManager.any(),
293+
);
294+
final TestBuildSystem buildSystem = TestBuildSystem.all(BuildResult(success: true));
295+
296+
await generateLocalizationsSyntheticPackage(
297+
environment: environment,
298+
buildSystem: buildSystem,
299+
buildTargets: const BuildTargetsImpl(),
300+
);
301+
302+
expect(
303+
mockBufferLogger.warningText,
304+
contains('https://flutter.dev/to/flutter-gen-deprecation'),
305+
);
306+
});
307+
308+
testWithoutContext('synthetic-package: true (explicit) logs a deprecation warning', () async {
309+
// Project directory setup for gen_l10n logic.
310+
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
311+
312+
// Add generate:true to pubspec.yaml.
313+
final File pubspecFile = fileSystem.file('pubspec.yaml')..createSync();
314+
final String content = pubspecFile.readAsStringSync().replaceFirst(
315+
'\nflutter:\n',
316+
'\nflutter:\n generate: true\n',
317+
);
318+
pubspecFile.writeAsStringSync(content);
319+
fileSystem.file('l10n.yaml').writeAsStringSync('synthetic-package: true');
320+
321+
final BufferLogger mockBufferLogger = BufferLogger.test();
322+
final Environment environment = Environment.test(
323+
fileSystem.currentDirectory,
324+
fileSystem: fileSystem,
325+
logger: mockBufferLogger,
326+
artifacts: Artifacts.test(),
327+
processManager: FakeProcessManager.any(),
328+
);
329+
final TestBuildSystem buildSystem = TestBuildSystem.all(BuildResult(success: true));
330+
331+
await generateLocalizationsSyntheticPackage(
332+
environment: environment,
333+
buildSystem: buildSystem,
334+
buildTargets: const BuildTargetsImpl(),
335+
);
336+
337+
expect(
338+
mockBufferLogger.warningText,
339+
contains('https://flutter.dev/to/flutter-gen-deprecation'),
340+
);
341+
});
342+
343+
testWithoutContext('synthetic-package: false has no deprecation warning', () async {
344+
// Project directory setup for gen_l10n logic
345+
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
346+
347+
// Add generate:true to pubspec.yaml.
348+
final File pubspecFile = fileSystem.file('pubspec.yaml')..createSync();
349+
final String content = pubspecFile.readAsStringSync().replaceFirst(
350+
'\nflutter:\n',
351+
'\nflutter:\n generate: true\n',
352+
);
353+
pubspecFile.writeAsStringSync(content);
354+
fileSystem.file('l10n.yaml').writeAsStringSync('synthetic-package: false');
355+
356+
final BufferLogger mockBufferLogger = BufferLogger.test();
357+
final Environment environment = Environment.test(
358+
fileSystem.currentDirectory,
359+
fileSystem: fileSystem,
360+
logger: mockBufferLogger,
361+
artifacts: Artifacts.test(),
362+
processManager: FakeProcessManager.any(),
363+
);
364+
final TestBuildSystem buildSystem = TestBuildSystem.all(BuildResult(success: true));
365+
366+
await generateLocalizationsSyntheticPackage(
367+
environment: environment,
368+
buildSystem: buildSystem,
369+
buildTargets: const BuildTargetsImpl(),
370+
);
371+
372+
expect(
373+
mockBufferLogger.warningText,
374+
isNot(contains('https://flutter.dev/to/flutter-gen-deprecation')),
375+
);
376+
});
377+
378+
testWithoutContext('synthetic-package: true with --no-implicit-pubspec-resolution is an error', () async {
379+
// Project directory setup for gen_l10n logic
380+
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
381+
382+
// Add generate:true to pubspec.yaml.
383+
final File pubspecFile = fileSystem.file('pubspec.yaml')..createSync();
384+
final String content = pubspecFile.readAsStringSync().replaceFirst(
385+
'\nflutter:\n',
386+
'\nflutter:\n generate: true\n',
387+
);
388+
pubspecFile.writeAsStringSync(content);
389+
390+
// Create an l10n.yaml file
391+
fileSystem.file('l10n.yaml').writeAsStringSync('synthetic-package: true');
392+
393+
final BufferLogger mockBufferLogger = BufferLogger.test();
394+
final Environment environment = Environment.test(
395+
fileSystem.currentDirectory,
396+
fileSystem: fileSystem,
397+
logger: mockBufferLogger,
398+
artifacts: Artifacts.test(),
399+
processManager: FakeProcessManager.any(),
400+
useImplicitPubspecResolution: false,
401+
);
402+
// Will throw if build is called.
403+
final TestBuildSystem buildSystem = TestBuildSystem.all(null);
404+
405+
await expectLater(
406+
() => generateLocalizationsSyntheticPackage(
407+
environment: environment,
408+
buildSystem: buildSystem,
409+
buildTargets: const NoOpBuildTargets(),
410+
),
411+
throwsToolExit(message: 'Cannot generate a synthetic package when --no-implicit-pubspec-resolution is passed'),
412+
);
413+
});
414+
415+
testWithoutContext('synthetic-package defaults to false if --no-implicit-pubspec-resolution is passed', () async {
416+
// Project directory setup for gen_l10n logic
417+
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
418+
419+
// Add generate:true to pubspec.yaml.
420+
final File pubspecFile = fileSystem.file('pubspec.yaml')..createSync();
421+
final String content = pubspecFile.readAsStringSync().replaceFirst(
422+
'\nflutter:\n',
423+
'\nflutter:\n generate: true\n',
424+
);
425+
pubspecFile.writeAsStringSync(content);
426+
427+
// Create an l10n.yaml file
428+
fileSystem.file('l10n.yaml').writeAsStringSync('');
429+
430+
final BufferLogger mockBufferLogger = BufferLogger.test();
431+
final Environment environment = Environment.test(
432+
fileSystem.currentDirectory,
433+
fileSystem: fileSystem,
434+
logger: mockBufferLogger,
435+
artifacts: Artifacts.test(),
436+
processManager: FakeProcessManager.any(),
437+
useImplicitPubspecResolution: false,
438+
);
439+
// Will throw if build is called.
440+
final TestBuildSystem buildSystem = TestBuildSystem.all(null);
441+
442+
await expectLater(
443+
() => generateLocalizationsSyntheticPackage(
444+
environment: environment,
445+
buildSystem: buildSystem,
446+
buildTargets: const NoOpBuildTargets(),
447+
),
448+
returnsNormally,
449+
);
450+
});
270451
}

0 commit comments

Comments
 (0)