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 {
  try {
    final bin = context.yaml.getAsMap("bin");
    final npm = bin.get("npm", "npm");
    final dart = bin.get("dart", "dart");
    final which =
        bin.get("which", Platform.isWindows ? "where.exe" : "which");
    label(
      "Check the installation status of the commands required for the `katana` command.",
    );
    for (final com in _mainCommands.entries) {
      final res = await Process.run(
        which,
        [com.key],
        runInShell: true,
      );
      final path = (res.stdout as String).trim();
      if (path.isEmpty) {
        print("[x] `${com.key}` not exists\n${com.value}\n\n");
      } else {
        print("[o] `${com.key}` exists at `$path`");
      }
    }
    if (Platform.isMacOS) {
      for (final com in _macOSMainCommand.entries) {
        final res = await Process.run(
          which,
          [com.key],
          runInShell: true,
        );
        final path = (res.stdout as String).trim();
        if (path.isEmpty) {
          print("[x] `${com.key}` not exists\n\n${com.value}\n\n");
        } else {
          print("[o] `${com.key}` exists at `$path`");
        }
      }
    }
    label(
        "Check the installation status of the npm commands required for the `katana` command.");
    for (final com in _npmCommand.entries) {
      final res = await Process.run(
        which,
        [com.key],
        runInShell: true,
      );
      final path = (res.stdout as String).trim();
      if (path.isEmpty) {
        print("[x] `${com.key}` not exists. Try to install.");
        await command(
          "Install `${com.value}`",
          [npm, "install", "-g", com.value],
          runInShell: true,
          catchError: true,
        );
      } else {
        print("[o] `${com.key}` exists at `$path`");
      }
    }
    label(
        "Check the installation status of the flutter commands required for the `katana` command.");
    for (final com in _flutterCommand.entries) {
      final res = await Process.run(
        which,
        [com.key],
        runInShell: true,
      );
      final path = (res.stdout as String).trim();
      if (path.isEmpty) {
        print("[x] `${com.key}` not exists. Try to install.");
        await command(
          "Install `${com.value}`",
          [dart, "pub", "global", "activate", com.value],
          runInShell: true,
          catchError: true,
        );
      } else {
        print("[o] `${com.key}` exists at `$path`");
      }
    }
    print("\n");
  } catch (e) {
    print(e);
    print(
      "\n\nIn case of a permission error, please run the following with administrator privileges.\n`sudo kata doctor`.\n\nパーミッションエラーの場合は、下記のように管理者権限で実行してください。\n`sudo kata doctor`\n\n",
    );
  }
}