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 path = context.args.get(2, "");
  if (path.isEmpty) {
    error(
      "[path] is not specified. Please enter [path] according to the following command.\r\nkatana code page [path]\r\n",
    );
    return;
  }
  final existsMain = File("lib/main.dart").existsSync();
  label("Create a page class in `$directory/$path.dart`.");
  final parentPath = path.parentPath();
  if (parentPath.isNotEmpty) {
    final parentDir = Directory("$directory/$parentPath");
    if (!parentDir.existsSync()) {
      await parentDir.create(recursive: true);
    }
    final parentTestDir = Directory("$testDirectory/$parentPath");
    if (!parentTestDir.existsSync()) {
      await parentTestDir.create(recursive: true);
    }
  }
  await generateDartCode(
    "$directory/$path",
    path,
    filter: (value) {
      if (existsMain) {
        return value;
      } else {
        return """$value
/// [RouteQueryBuilder], which is also available externally.
///
/// ```dart
/// @PagePath(
///   "test",
///   implementType: ${path.split("/").distinct().join("_").toPascalCase()}PageQuery,
/// )
/// class TestPage extends PageScopedWidget {
/// }
/// ```
typedef ${path.split("/").distinct().join("_").toPascalCase()}PageQuery = _\$${path.split("/").distinct().join("_").toPascalCase()}PageQuery;
""";
      }
    },
  );
  await generateDartTestCode("$testDirectory/$path", path);
}