Unverified Commit a04f45f9 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Migrate deferred_components_gen_snapshot_validator to null safety (#83311)

parent 2bf0627d
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'package:xml/xml.dart'; import 'package:xml/xml.dart';
import 'package:yaml/yaml.dart'; import 'package:yaml/yaml.dart';
...@@ -32,7 +30,7 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator ...@@ -32,7 +30,7 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator
/// change. This defaults to true. /// change. This defaults to true.
DeferredComponentsGenSnapshotValidator(this.env, { DeferredComponentsGenSnapshotValidator(this.env, {
bool exitOnFail = true, bool exitOnFail = true,
String title, String? title,
}) : super(env.projectDir, env.logger, env.platform, exitOnFail: exitOnFail, title: title); }) : super(env.projectDir, env.logger, env.platform, exitOnFail: exitOnFail, title: title);
/// The build environment that should be used to find the input files to run /// The build environment that should be used to find the input files to run
...@@ -101,7 +99,11 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator ...@@ -101,7 +99,11 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator
final Map<int, String> mapping = <int, String>{}; final Map<int, String> mapping = <int, String>{};
for (final DeferredComponent component in components) { for (final DeferredComponent component in components) {
component.assignLoadingUnits(generatedLoadingUnits); component.assignLoadingUnits(generatedLoadingUnits);
for (final LoadingUnit unit in component.loadingUnits) { final Set<LoadingUnit>? loadingUnits = component.loadingUnits;
if (loadingUnits == null) {
continue;
}
for (final LoadingUnit unit in loadingUnits) {
if (!mapping.containsKey(unit.id)) { if (!mapping.containsKey(unit.id)) {
mapping[unit.id] = component.name; mapping[unit.id] = component.name;
} }
...@@ -129,10 +131,10 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator ...@@ -129,10 +131,10 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator
bool modified = false; bool modified = false;
for (final XmlElement application in document.findAllElements('application')) { for (final XmlElement application in document.findAllElements('application')) {
for (final XmlElement metaData in application.findElements('meta-data')) { for (final XmlElement metaData in application.findElements('meta-data')) {
final String name = metaData.getAttribute('android:name'); final String? name = metaData.getAttribute('android:name');
if (name == _mappingKey) { if (name == _mappingKey) {
exists = true; exists = true;
final String storedMappingString = metaData.getAttribute('android:value'); final String? storedMappingString = metaData.getAttribute('android:value');
if (storedMappingString != encodedMapping) { if (storedMappingString != encodedMapping) {
metaData.setAttribute('android:value', encodedMapping); metaData.setAttribute('android:value', encodedMapping);
modified = true; modified = true;
...@@ -182,13 +184,13 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator ...@@ -182,13 +184,13 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator
bool checkAgainstLoadingUnitsCache( bool checkAgainstLoadingUnitsCache(
List<LoadingUnit> generatedLoadingUnits) { List<LoadingUnit> generatedLoadingUnits) {
final List<LoadingUnit> cachedLoadingUnits = _parseLodingUnitsCache(projectDir.childFile(DeferredComponentsValidator.kLoadingUnitsCacheFileName)); final List<LoadingUnit> cachedLoadingUnits = _parseLodingUnitsCache(projectDir.childFile(DeferredComponentsValidator.kLoadingUnitsCacheFileName));
loadingUnitComparisonResults = <String, dynamic>{}; loadingUnitComparisonResults = <String, Object>{};
final Set<LoadingUnit> unmatchedLoadingUnits = <LoadingUnit>{}; final Set<LoadingUnit> unmatchedLoadingUnits = <LoadingUnit>{};
final List<LoadingUnit> newLoadingUnits = <LoadingUnit>[]; final List<LoadingUnit> newLoadingUnits = <LoadingUnit>[];
if (generatedLoadingUnits == null || cachedLoadingUnits == null) { if (generatedLoadingUnits == null || cachedLoadingUnits == null) {
loadingUnitComparisonResults['new'] = newLoadingUnits; loadingUnitComparisonResults!['new'] = newLoadingUnits;
loadingUnitComparisonResults['missing'] = unmatchedLoadingUnits; loadingUnitComparisonResults!['missing'] = unmatchedLoadingUnits;
loadingUnitComparisonResults['match'] = false; loadingUnitComparisonResults!['match'] = false;
return false; return false;
} }
unmatchedLoadingUnits.addAll(cachedLoadingUnits); unmatchedLoadingUnits.addAll(cachedLoadingUnits);
...@@ -207,10 +209,10 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator ...@@ -207,10 +209,10 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator
addedNewIds.add(genUnit.id); addedNewIds.add(genUnit.id);
} }
} }
loadingUnitComparisonResults['new'] = newLoadingUnits; loadingUnitComparisonResults!['new'] = newLoadingUnits;
loadingUnitComparisonResults['missing'] = unmatchedLoadingUnits; loadingUnitComparisonResults!['missing'] = unmatchedLoadingUnits;
loadingUnitComparisonResults['match'] = newLoadingUnits.isEmpty && unmatchedLoadingUnits.isEmpty; loadingUnitComparisonResults!['match'] = newLoadingUnits.isEmpty && unmatchedLoadingUnits.isEmpty;
return loadingUnitComparisonResults['match'] as bool; return loadingUnitComparisonResults!['match']! as bool;
} }
List<LoadingUnit> _parseLodingUnitsCache(File cacheFile) { List<LoadingUnit> _parseLodingUnitsCache(File cacheFile) {
...@@ -232,13 +234,13 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator ...@@ -232,13 +234,13 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator
return loadingUnits; return loadingUnits;
} }
if (data['loading-units'] != null) { if (data['loading-units'] != null) {
for (final dynamic loadingUnitData in data['loading-units']) { for (final Object? loadingUnitData in data['loading-units']) {
if (loadingUnitData is! YamlMap) { if (loadingUnitData is! YamlMap) {
invalidFiles[cacheFile.path] = "Invalid loading units yaml file, 'loading-units' " invalidFiles[cacheFile.path] = "Invalid loading units yaml file, 'loading-units' "
'is not a list of maps.'; 'is not a list of maps.';
return loadingUnits; return loadingUnits;
} }
final YamlMap loadingUnitDataMap = loadingUnitData as YamlMap; final YamlMap loadingUnitDataMap = loadingUnitData;
if (loadingUnitDataMap['id'] == null) { if (loadingUnitDataMap['id'] == null) {
invalidFiles[cacheFile.path] = 'Invalid loading units yaml file, all ' invalidFiles[cacheFile.path] = 'Invalid loading units yaml file, all '
"loading units must have an 'id'"; "loading units must have an 'id'";
...@@ -250,7 +252,7 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator ...@@ -250,7 +252,7 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator
'is not a list.'; 'is not a list.';
return loadingUnits; return loadingUnits;
} }
for (final dynamic node in loadingUnitDataMap['libraries'] as YamlList) { for (final Object? node in loadingUnitDataMap['libraries'] as YamlList) {
if (node is! String) { if (node is! String) {
invalidFiles[cacheFile.path] = "Invalid loading units yaml file, 'libraries' " invalidFiles[cacheFile.path] = "Invalid loading units yaml file, 'libraries' "
'is not a list of strings.'; 'is not a list of strings.';
...@@ -265,17 +267,18 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator ...@@ -265,17 +267,18 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator
// Parse out validated yaml. // Parse out validated yaml.
if (data.containsKey('loading-units')) { if (data.containsKey('loading-units')) {
if (data['loading-units'] != null) { if (data['loading-units'] != null) {
for (final dynamic loadingUnitData in data['loading-units']) { for (final Object? loadingUnitData in data['loading-units']) {
final YamlMap loadingUnitDataMap = loadingUnitData as YamlMap; final YamlMap? loadingUnitDataMap = loadingUnitData as YamlMap?;
final List<String> libraries = <String>[]; final List<String> libraries = <String>[];
if (loadingUnitDataMap['libraries'] != null) { final YamlList? nodes = loadingUnitDataMap?['libraries'] as YamlList?;
for (final dynamic node in loadingUnitDataMap['libraries'] as YamlList) { if (nodes != null) {
for (final Object node in nodes.whereType<Object>()) {
libraries.add(node as String); libraries.add(node as String);
} }
} }
loadingUnits.add( loadingUnits.add(
LoadingUnit( LoadingUnit(
id: loadingUnitDataMap['id'] as int, id: loadingUnitDataMap!['id'] as int,
path: null, path: null,
libraries: libraries, libraries: libraries,
)); ));
...@@ -292,7 +295,7 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator ...@@ -292,7 +295,7 @@ class DeferredComponentsGenSnapshotValidator extends DeferredComponentsValidator
/// mismatching or missing cache will result in a failed validation. This /// mismatching or missing cache will result in a failed validation. This
/// prevents unexpected changes in loading units causing misconfigured /// prevents unexpected changes in loading units causing misconfigured
/// deferred components. /// deferred components.
void writeLoadingUnitsCache(List<LoadingUnit> generatedLoadingUnits) { void writeLoadingUnitsCache(List<LoadingUnit>? generatedLoadingUnits) {
generatedLoadingUnits ??= <LoadingUnit>[]; generatedLoadingUnits ??= <LoadingUnit>[];
final File cacheFile = projectDir.childFile(DeferredComponentsValidator.kLoadingUnitsCacheFileName); final File cacheFile = projectDir.childFile(DeferredComponentsValidator.kLoadingUnitsCacheFileName);
outputs.add(cacheFile); outputs.add(cacheFile);
......
...@@ -2,25 +2,24 @@ ...@@ -2,25 +2,24 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'package:file/memory.dart'; import 'package:file/memory.dart';
import 'package:flutter_tools/src/android/deferred_components_gen_snapshot_validator.dart'; import 'package:flutter_tools/src/android/deferred_components_gen_snapshot_validator.dart';
import 'package:flutter_tools/src/android/deferred_components_validator.dart'; import 'package:flutter_tools/src/android/deferred_components_validator.dart';
import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/deferred_component.dart'; import 'package:flutter_tools/src/base/deferred_component.dart';
import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart'; import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart'; import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/build_system/build_system.dart'; import 'package:flutter_tools/src/build_system/build_system.dart';
import 'package:flutter_tools/src/globals_null_migrated.dart' as globals;
import '../../src/common.dart'; import '../../src/common.dart';
import '../../src/fake_process_manager.dart';
void main() { void main() {
FileSystem fileSystem; late FileSystem fileSystem;
BufferLogger logger; late BufferLogger logger;
Environment env; late Environment env;
Environment createEnvironment() { Environment createEnvironment() {
final Map<String, String> defines = <String, String>{ kDeferredComponents: 'true' }; final Map<String, String> defines = <String, String>{ kDeferredComponents: 'true' };
...@@ -32,10 +31,10 @@ void main() { ...@@ -32,10 +31,10 @@ void main() {
inputs: <String, String>{}, inputs: <String, String>{},
cacheDir: fileSystem.directory('/cache'), cacheDir: fileSystem.directory('/cache'),
flutterRootDir: fileSystem.directory('/flutter_root'), flutterRootDir: fileSystem.directory('/flutter_root'),
artifacts: globals.artifacts, artifacts: Artifacts.test(),
fileSystem: fileSystem, fileSystem: fileSystem,
logger: logger, logger: logger,
processManager: globals.processManager, processManager: FakeProcessManager.any(),
platform: FakePlatform(), platform: FakePlatform(),
engineVersion: 'invalidEngineVersion', engineVersion: 'invalidEngineVersion',
generateDartPluginRegistry: false, generateDartPluginRegistry: false,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment