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 sendgrid = context.yaml.getAsMap("sendgrid");
  final sendGridApiKey = sendgrid.get("api_key", "");
  final cloudflare = context.yaml.getAsMap("cloudflare");
  final enableCloudflareWorkers =
      cloudflare.getAsMap("workers").get("enable", false);
  if (sendGridApiKey.isEmpty) {
    error(
      "If [sendgrid]->[enable] is enabled, please include [sendgrid]->[api_key].",
    );
    return;
  }
  // Cloudflare Workersが有効な場合はCloudflare側に設定する。
  if (enableCloudflareWorkers) {
    await _execCloudflare(context, sendGridApiKey);
    return;
  }
  final firebase = context.yaml.getAsMap("firebase");
  final projectId = firebase.get("project_id", "");
  if (projectId.isEmpty) {
    error(
      "The item [firebase]->[project_id] is missing. Please provide the Firebase project ID for the configuration.",
    );
    return;
  }
  final firebaseDir = Directory("firebase");
  if (!firebaseDir.existsSync()) {
    error(
      "The directory `firebase` does not exist. Initialize Firebase by executing Firebase init.",
    );
    return;
  }
  final functionsDir = Directory("firebase/functions");
  if (!functionsDir.existsSync()) {
    error(
      "The directory `firebase/functions` does not exist. Initialize Firebase by executing Firebase init.",
    );
    return;
  }
  await addFlutterImport(
    [
      "masamune_mail",
      "katana_functions_firebase",
    ],
  );
  label("Add firebase functions");
  final functions = Functions();
  await functions.load();
  if (!functions.imports
      .any((e) => e.contains("@mathrunet/masamune_mail_sendgrid"))) {
    functions.imports.add(
        "import * as sendgrid from \"@mathrunet/masamune_mail_sendgrid\";");
  }
  if (!functions.functions
      .any((e) => e.startsWith("sendgrid.Functions.sendGrid"))) {
    functions.functions.add("sendgrid.Functions.sendGrid()");
  }
  await functions.save();
  label("Set firebase functions config.");
  final env = FunctionsEnv();
  await env.load();
  env["MAIL_SENDGRID_APIKEY"] = sendgrid.get("api_key", "");
  await env.save();
  context.requestFirebaseDeploy(FirebaseDeployPostActionType.functions);
}