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 location = context.yaml.getAsMap("location");
  final geocoding = location.getAsMap("geocoding");
  final geocodingApiKey = geocoding.get("api_key", "");
  final cloudflare = context.yaml.getAsMap("cloudflare");
  final enableCloudflareWorkers =
      cloudflare.getAsMap("workers").get("enable", false);
  if (geocodingApiKey.isEmpty) {
    error(
      "If [location]->[geocoding]->[enable] is enabled, please include [location]->[geocoding]->[api_key].",
    );
    return;
  }
  // Cloudflare Workersが有効な場合はCloudflare側に設定する。
  if (enableCloudflareWorkers) {
    await _execCloudflare(context, geocodingApiKey);
    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_location_geocoding",
      "katana_functions_firebase",
    ],
  );
  label("Add firebase functions");
  final functions = Functions();
  await functions.load();
  if (!functions.imports
      .any((e) => e.contains("@mathrunet/masamune_location_geocoding"))) {
    functions.imports.add(
        "import * as geocoding from \"@mathrunet/masamune_location_geocoding\";");
  }
  if (!functions.functions
      .any((e) => e.startsWith("geocoding.Functions.geocoding"))) {
    functions.functions.add("geocoding.Functions.geocoding()");
  }
  await functions.save();
  label("Set firebase functions config.");
  final env = FunctionsEnv();
  await env.load();
  env["MAP_GEOCODING_APIKEY"] = geocodingApiKey;
  await env.save();
  context.requestFirebaseDeploy(FirebaseDeployPostActionType.functions);
}