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 {
  final app = context.yaml.getAsMap("app");
  if (app.isEmpty) {
    error("The item [app] is missing. Please add an item.");
    return;
  }
  final icon = app.getAsMap("icon");
  if (icon.isEmpty) {
    error("The item [app]->[icon] is missing. Please add an item.");
    return;
  }
  final path = icon.get("path", "");
  if (path.isEmpty) {
    error(
      "The item [app]->[icon]->[path] is missing. Please provide the path to the original icon.",
    );
    return;
  }
  final adaptive = icon.getAsMap("adaptive_icon");
  final adaptiveBackground = adaptive.get("background", "");
  final adaptiveForeground = adaptive.get("foreground", "");
  label("Load a file from $path");
  final iconFile = File(path);
  if (!iconFile.existsSync()) {
    error("Icon file not found in $path.");
    return;
  }
  final iconImage = decodeImage(iconFile.readAsBytesSync())!;
  if (iconImage.width != 1024 || iconImage.height != 1024) {
    error("Icon files should be 1024 x 1024.");
    return;
  }
  for (final tmp in _sizeList.entries) {
    label("Resize & Save to ${tmp.key}");
    final dir = Directory(tmp.key.parentPath());
    if (!dir.existsSync()) {
      await dir.create(recursive: true);
    }
    final file = File(tmp.key);
    if (file.existsSync()) {
      await file.delete();
    }
    final resized = copyResize(
      iconImage,
      height: tmp.value,
      width: tmp.value,
      interpolation: Interpolation.average,
    );
    await file.writeAsBytes(encodePng(resized, level: 9));
  }
  if (adaptiveBackground.isNotEmpty && adaptiveForeground.isNotEmpty) {
    label("Load a file from $adaptiveBackground");
    final backgroundIconFile = File(adaptiveBackground);
    final backgroundIsColor = !backgroundIconFile.existsSync();
    label("Load a file from $adaptiveForeground");
    final foregroundIconFile = File(adaptiveForeground);
    if (!foregroundIconFile.existsSync()) {
      error("Icon file not found in $adaptiveForeground.");
      return;
    }
    final backgroundIconImage = backgroundIsColor
        ? null
        : decodeImage(backgroundIconFile.readAsBytesSync())!;
    if (backgroundIconImage != null) {
      if (backgroundIconImage.width != 1024 ||
          backgroundIconImage.height != 1024) {
        error("Icon files should be 1024 x 1024.");
        return;
      }
      for (final tmp in _sizeListAdaptiveBackground.entries) {
        label("Resize & Save to ${tmp.key}");
        final dir = Directory(tmp.key.parentPath());
        if (!dir.existsSync()) {
          await dir.create(recursive: true);
        }
        final file = File(tmp.key);
        if (file.existsSync()) {
          await file.delete();
        }
        final resized = copyResize(
          backgroundIconImage,
          height: tmp.value,
          width: tmp.value,
          interpolation: Interpolation.average,
        );
        await file.writeAsBytes(encodePng(resized, level: 9));
      }
    }
    final foregroundIconImage =
        decodeImage(foregroundIconFile.readAsBytesSync())!;
    if (foregroundIconImage.width != 1024 ||
        foregroundIconImage.height != 1024) {
      error("Icon files should be 1024 x 1024.");
      return;
    }
    for (final tmp in _sizeListAdaptiveForeground.entries) {
      label("Resize & Save to ${tmp.key}");
      final dir = Directory(tmp.key.parentPath());
      if (!dir.existsSync()) {
        await dir.create(recursive: true);
      }
      final file = File(tmp.key);
      if (file.existsSync()) {
        await file.delete();
      }
      final resized = copyResize(
        foregroundIconImage,
        height: tmp.value,
        width: tmp.value,
        interpolation: Interpolation.average,
      );
      await file.writeAsBytes(encodePng(resized, level: 9));
    }
    label("Create a ic_launcher.xml");
    await IcLauncherCliCode(
      backgroundIsColor: backgroundIsColor,
    ).generateFile("ic_launcher.xml");
    if (backgroundIsColor) {
      await ColorValueCliCode(
        backgroundColorCode: adaptiveBackground.trimString("#"),
      ).generateFile("colors.xml");
    }
  }
  final icoFile = File("web/favicon.ico");
  if (icoFile.existsSync()) {
    await icoFile.delete();
  }
  final ico = IcoEncoder();
  await icoFile.writeAsBytes(
    ico.encodeImages(_faviconSize.map((e) {
      return copyResize(
        iconImage,
        height: e,
        width: e,
        interpolation: Interpolation.average,
      );
    }).toList()),
  );
}