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 bin = context.yaml.getAsMap("bin");
  final flutter = bin.get("flutter", "flutter");
  final packageName = context.args.get(1, "");
  if (packageName.isEmpty) {
    error(
      "Please provide the name of the package.\r\nパッケージ名を記載してください。\r\n\r\nkatana create [package name]",
    );
    return;
  }
  final projectName = packageName.split(".").lastOrNull;
  final domain = packageName
      .split(".")
      .sublist(0, packageName.split(".").length - 1)
      .join(".");
  if (projectName.isEmpty || domain.isEmpty) {
    error(
      "The format of the package name should be specified in the following format.\r\nパッケージ名の形式は下記の形式で指定してください。\r\n\r\n[Domain].[ProjectName]\r\ne.g. net.mathru.website",
    );
    return;
  }
  const main = CreateCliCommand();
  await main.exec(context);

  await command(
    "Create a Flutter project.",
    [
      flutter,
      "create",
      "--org",
      "net.mathru.masamune",
      "--template=package",
      "--project-name",
      "masamune_module_$projectName",
      "module",
    ],
  );
  await command(
    "Import packages.",
    [
      flutter,
      "pub",
      "add",
      ...importPackages,
      "masamune_module",
    ],
    workingDirectory: "module",
  );
  await command(
    "Import dev packages.",
    [
      flutter,
      "pub",
      "add",
      "--dev",
      ...importDevPackages,
    ],
    workingDirectory: "module",
  );
  label("Create a pubspec_overrides.yaml");
  await const PubspecOverridesCliCode()
      .generateFile("module/pubspec_overrides.yaml");
  label("Edit a analysis_options.yaml");
  await const AnalysisOptionsCliCode()
      .generateFile("module/analysis_options.yaml");
  await command(
    "Import additional packages.",
    [
      flutter,
      "pub",
      "add",
      "masamune_module",
      "masamune_module_$projectName:{\"path\":\"./module/\"}"
    ],
  );
  label("Create a masamune_module_${projectName?.toSnakeCase()}");
  await const ModuleMainCliCode().generateDartCode(
      "module/lib/masamune_module_${projectName?.toSnakeCase()}",
      projectName!);
  label("Replace the main.dart");
  await const AppModuleMainCliCode()
      .generateDartCode("lib/main", projectName);
  label("Edit a widget_test.dart");
  await const ModuleWidgetTestCliCode()
      .generateFile("masamune_module_${projectName.toSnakeCase()}_test.dart");
  await command(
    "Run the project's build_runner to generate code.",
    [
      flutter,
      "packages",
      "pub",
      "run",
      "build_runner",
      "build",
      "--delete-conflicting-outputs",
    ],
  );
  await command(
    "Run the project's build_runner to generate code.",
    [
      flutter,
      "packages",
      "pub",
      "run",
      "build_runner",
      "build",
      "--delete-conflicting-outputs",
    ],
    workingDirectory: "module",
  );
}