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