exec method

  1. @override
Future<void> exec(
  1. ExecContext context
)
override

Run command.

The contents of katana.yaml and the arguments of the command are passed to context.

コマンドを実行します。

contextkatana.yamlの内容やコマンドの引数が渡されます。

Implementation

@override
Future<void> exec(ExecContext context) async {
  await addFlutterImport(
    [
      "masamune_text_to_speech",
    ],
  );
  label("Edit config.properties");
  final configPropertiesFile = File("android/config.properties");
  if (!configPropertiesFile.existsSync()) {
    await configPropertiesFile.writeAsString("");
  }
  final configProperties = await configPropertiesFile.readAsLines();
  if (!configProperties
      .any((element) => element.startsWith("flutter.minSdkVersion"))) {
    await configPropertiesFile.writeAsString([
      ...configProperties,
      "flutter.minSdkVersion=${Config.firebaseMinSdkVersion}",
    ].join("\n"));
  }
  label("Edit build.gradle");
  final gradle = AppGradle();
  await gradle.load();
  if (!gradle.loadProperties.any((e) => e.name == "configProperties")) {
    gradle.loadProperties.add(
      GradleLoadProperties(
        path: "config.properties",
        name: "configProperties",
        file: "configPropertiesFile",
      ),
    );
  }
  gradle.android?.defaultConfig.minSdkVersion =
      "configProperties[\"flutter.minSdkVersion\"].toInteger()";
  await gradle.save();
  label("Edit AndroidManifest.xml.");
  await AndroidManifestQueryType.textToSpeech.enableQuery();
  label("Edit Info.plist.");
  final plist = File("ios/Runner/Info.plist");
  final plistDocument = XmlDocument.parse(await plist.readAsString());
  final dict = plistDocument.findAllElements("dict").firstOrNull;
  if (dict == null) {
    throw Exception(
      "Could not find `dict` element in `ios/Runner/Info.plist`. File is corrupt.",
    );
  }
  final node = dict.children.firstWhereOrNull((p0) {
    return p0 is XmlElement &&
        p0.name.toString() == "key" &&
        p0.innerText == "UIBackgroundModes";
  });
  if (node == null) {
    dict.children.addAll(
      [
        XmlElement(
          XmlName("key"),
          [],
          [XmlText("UIBackgroundModes")],
        ),
        XmlElement(
          XmlName("array"),
          [],
          [
            XmlElement(
              XmlName("string"),
              [],
              [XmlText("audio")],
            )
          ],
        ),
      ],
    );
  } else {
    final next = node.nextElementSibling;
    if (next is XmlElement && next.name.toString() == "array") {
      if (!next.children.any(
        (p1) =>
            p1 is XmlElement &&
            p1.name.toString() == "string" &&
            p1.innerText == "audio",
      )) {
        next.children.add(
          XmlElement(
            XmlName("string"),
            [],
            [XmlText("audio")],
          ),
        );
      }
    } else {
      throw Exception(
        "The `ios/Runner/Info.plist` configuration is broken.",
      );
    }
  }
  await plist.writeAsString(
    plistDocument.toXmlString(pretty: true, indent: "\t", newLine: "\n"),
  );
}