deferred_components_config.dart 5.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
// 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:file/file.dart';

import '../test_utils.dart';

abstract class DeferredComponentsConfig {
  String get deferredLibrary;
  String get deferredComponentsGolden;
  String get androidSettings;
  String get androidBuild;
  String get androidLocalProperties;
  String get androidGradleProperties;
  String get androidKeyProperties;
  List<int> get androidKey;
  String get appBuild;
  String get appManifest;
  String get appStrings;
  String get appStyles;
  String get appLaunchBackground;
  String get asset1;
  String get asset2;
  List<DeferredComponentModule> get deferredComponents;

  void setUpIn(Directory dir) {
    if (deferredLibrary != null) {
      writeFile(fileSystem.path.join(dir.path, 'lib', 'deferred_library.dart'), deferredLibrary);
    }
    if (deferredComponentsGolden != null) {
      writeFile(fileSystem.path.join(dir.path, 'deferred_components_loading_units.yaml'), deferredComponentsGolden);
    }
    if (androidSettings != null) {
      writeFile(fileSystem.path.join(dir.path, 'android', 'settings.gradle'), androidSettings);
    }
    if (androidBuild != null) {
      writeFile(fileSystem.path.join(dir.path, 'android', 'build.gradle'), androidBuild);
    }
    if (androidLocalProperties != null) {
      writeFile(fileSystem.path.join(dir.path, 'android', 'local.properties'), androidLocalProperties);
    }
    if (androidGradleProperties != null) {
      writeFile(fileSystem.path.join(dir.path, 'android', 'gradle.properties'), androidGradleProperties);
    }
    if (androidKeyProperties != null) {
      writeFile(fileSystem.path.join(dir.path, 'android', 'key.properties'), androidKeyProperties);
    }
    if (androidKey != null) {
      writeBytesFile(fileSystem.path.join(dir.path, 'android', 'app', 'key.jks'), androidKey);
    }
    if (appBuild != null) {
      writeFile(fileSystem.path.join(dir.path, 'android', 'app', 'build.gradle'), appBuild);
    }
    if (appManifest != null) {
      writeFile(fileSystem.path.join(dir.path, 'android', 'app', 'src', 'main', 'AndroidManifest.xml'), appManifest);
    }
    if (appStrings != null) {
      writeFile(fileSystem.path.join(dir.path, 'android', 'app', 'src', 'main', 'res', 'values', 'strings.xml'), appStrings);
    }
    if (appStyles != null) {
      writeFile(fileSystem.path.join(dir.path, 'android', 'app', 'src', 'main', 'res', 'values', 'styles.xml'), appStyles);
    }
    if (appLaunchBackground != null) {
      writeFile(fileSystem.path.join(dir.path, 'android', 'app', 'src', 'main', 'res', 'drawable', 'launch_background.xml'), appLaunchBackground);
    }
    if (asset1 != null) {
      writeFile(fileSystem.path.join(dir.path, 'test_assets/asset1.txt'), asset1);
    }
    if (asset2 != null) {
      writeFile(fileSystem.path.join(dir.path, 'test_assets/asset2.txt'), asset2);
    }
    if (deferredComponents != null) {
      for (final DeferredComponentModule component in deferredComponents) {
        component.setUpIn(dir);
      }
    }
  }
}

class DeferredComponentModule {
  DeferredComponentModule(this.name);

  String name;

  void setUpIn(Directory dir) {
    if (name != null) {
      writeFile(fileSystem.path.join(dir.path, 'android', name, 'build.gradle'), r'''
      def localProperties = new Properties()
      def localPropertiesFile = rootProject.file('local.properties')
      if (localPropertiesFile.exists()) {
          localPropertiesFile.withReader('UTF-8') { reader ->
              localProperties.load(reader)
          }
      }

      def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
      if (flutterVersionCode == null) {
          flutterVersionCode = '1'
      }

      def flutterVersionName = localProperties.getProperty('flutter.versionName')
      if (flutterVersionName == null) {
          flutterVersionName = '1.0'
      }

      apply plugin: "com.android.dynamic-feature"

      android {
          compileSdkVersion 30

          sourceSets {
              applicationVariants.all { variant ->
                  main.assets.srcDirs += "${project.buildDir}/intermediates/flutter/${variant.name}/deferred_assets"
                  main.jniLibs.srcDirs += "${project.buildDir}/intermediates/flutter/${variant.name}/deferred_libs"
              }
          }

          defaultConfig {
              minSdkVersion 16
              targetSdkVersion 30
              versionCode flutterVersionCode.toInteger()
              versionName flutterVersionName
          }
          compileOptions {
              sourceCompatibility 1.8
              targetCompatibility 1.8
          }
      }

      dependencies {
          implementation project(":app")
      }
      ''');

      writeFile(fileSystem.path.join(dir.path, 'android', name, 'src', 'main', 'AndroidManifest.xml'), '''
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:dist="http://schemas.android.com/apk/distribution"
          package="com.example.$name">

          <dist:module
              dist:instant="false"
              dist:title="@string/component1Name">
              <dist:delivery>
                  <dist:on-demand />
              </dist:delivery>
              <dist:fusing dist:include="true" />
          </dist:module>
      </manifest>
      ''');
    }
  }
155
}