deferred_components.dart 3.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2014 The Flutter Authors. All rights reserved.
// 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:meta/meta.dart';

import '../../android/deferred_components_gen_snapshot_validator.dart';
import '../../base/deferred_component.dart';
11
import '../../build_info.dart';
12 13 14
import '../../project.dart';
import '../build_system.dart';
import '../depfile.dart';
15
import 'android.dart';
16 17 18 19 20 21

/// Creates a [DeferredComponentsGenSnapshotValidator], runs the checks, and displays the validator
/// output to the developer if changes are recommended.
class DeferredComponentsGenSnapshotValidatorTarget extends Target {
  /// Create an [AndroidAotDeferredComponentsBundle] implementation for a given [targetPlatform] and [buildMode].
  DeferredComponentsGenSnapshotValidatorTarget({
22 23
    @required this.deferredComponentsDependencies,
    @required this.nonDeferredComponentsDependencies,
24 25
    this.title,
    this.exitOnFail = true,
26
  });
27

28 29 30
  /// The [AndroidAotDeferredComponentsBundle] derived target instances this rule depends on.
  final List<AndroidAotDeferredComponentsBundle> deferredComponentsDependencies;
  final List<Target> nonDeferredComponentsDependencies;
31 32 33 34 35 36 37 38 39 40

  /// The title of the [DeferredComponentsGenSnapshotValidator] that is
  /// displayed to the developer when logging results.
  final String title;

  /// Whether to exit the tool if a recommended change is found by the
  /// [DeferredComponentsGenSnapshotValidator].
  final bool exitOnFail;

  /// The abis to validate.
41 42 43 44 45 46 47 48 49 50 51
  List<String> get _abis {
    final List<String> abis = <String>[];
    for (final AndroidAotDeferredComponentsBundle target in deferredComponentsDependencies) {
      if (deferredComponentsTargets.contains(target.name)) {
        abis.add(
          getNameForAndroidArch(getAndroidArchForName(getNameForTargetPlatform(target.dependency.targetPlatform)))
        );
      }
    }
    return abis;
  }
52 53

  @override
54
  String get name => 'deferred_components_gen_snapshot_validator';
55 56 57 58 59 60 61 62 63 64 65 66 67

  @override
  List<Source> get inputs => const <Source>[];

  @override
  List<Source> get outputs => const <Source>[];

  @override
  List<String> get depfiles => <String>[
    'flutter_$name.d',
  ];

  @override
68 69 70 71 72
  List<Target> get dependencies {
    final List<Target> deps = <Target>[CompositeTarget(deferredComponentsDependencies)];
    deps.addAll(nonDeferredComponentsDependencies);
    return deps;
  }
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

  @visibleForTesting
  DeferredComponentsGenSnapshotValidator validator;

  @override
  Future<void> build(Environment environment) async {
    final DepfileService depfileService = DepfileService(
      fileSystem: environment.fileSystem,
      logger: environment.logger,
    );
    validator = DeferredComponentsGenSnapshotValidator(
      environment,
      title: title,
      exitOnFail: exitOnFail,
    );

    final List<LoadingUnit> generatedLoadingUnits = LoadingUnit.parseGeneratedLoadingUnits(
        environment.outputDir,
        environment.logger,
92
        abis: _abis
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    );

    validator
      ..checkAppAndroidManifestComponentLoadingUnitMapping(
          FlutterProject.current().manifest.deferredComponents,
          generatedLoadingUnits,
      )
      ..checkAgainstLoadingUnitsCache(generatedLoadingUnits)
      ..writeLoadingUnitsCache(generatedLoadingUnits);

    validator.handleResults();

    depfileService.writeToFile(
      Depfile(validator.inputs, validator.outputs),
      environment.buildDir.childFile('flutter_$name.d'),
    );
  }
}