Skip to content

Commit

Permalink
fix: Query system code generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Feb 21, 2023
1 parent 55ab4e9 commit 8a5c793
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
81 changes: 81 additions & 0 deletions packages/katana_cli/lib/command/code/cache.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
part of katana_cli.code;

/// Create a `ScopedQuery`.
///
/// `ScopedQuery`を作成します。
class CodeCacheCliCommand extends CliCodeCommand {
/// Create a `ScopedQuery`.
///
/// `ScopedQuery`を作成します。
const CodeCacheCliCommand();

@override
String get name => "cache";

@override
String get prefix => "cache";

@override
String get directory => "lib/controllers";

@override
String get description =>
"Create a `ScopedQuery` in `$directory/(filepath).dart`. `ScopedQuery`を`$directory/(filepath).dart`に作成します。";

@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 cache [path]\r\n",
);
return;
}
label("Create a ScopedQuery in `$directory/$path.dart`.");
final parentPath = path.parentPath();
if (parentPath.isNotEmpty) {
final parentDir = Directory("$directory/$parentPath");
if (!parentDir.existsSync()) {
await parentDir.create(recursive: true);
}
}
await generateDartCode("$directory/$path");
}

@override
String import(String path, String baseName, String className) {
return """
// ignore: unused_import, unnecessary_import
import 'package:flutter/material.dart';
// ignore: unused_import, unnecessary_import
import 'package:masamune/masamune.dart';
// ignore: unused_import, unnecessary_import
import '/main.dart';
""";
}

@override
String header(String path, String baseName, String className) {
return "";
}

@override
String body(String path, String baseName, String className) {
return """
/// [ScopedQuery] for $className.
///
/// It can be used as follows
///
/// ```dart
/// final ${className.toCamelCase()} = ref.app.query(${className.toCamelCase()}Query);
/// ```
final ${className.toCamelCase()}Query = ScopedQuery(
(ref) {
// TODO: Define the process of creating the Object.
return \${1};
}
);
""";
}
}
4 changes: 4 additions & 0 deletions packages/katana_cli/lib/command/code/code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ part 'prefs.dart';
part 'widget.dart';
part 'stateless.dart';
part 'stateful.dart';
part 'query.dart';
part 'cache.dart';

class CodeCliCommand extends CliCommandGroup {
const CodeCliCommand();
Expand All @@ -47,5 +49,7 @@ class CodeCliCommand extends CliCommandGroup {
"widget": CodeWidgetCliCommand(),
"stateless": CodeStatelessCliCommand(),
"stateful": CodeStatefulCliCommand(),
"query": CodeQueryCliCommand(),
"cache": CodeCacheCliCommand(),
};
}
81 changes: 81 additions & 0 deletions packages/katana_cli/lib/command/code/query.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
part of katana_cli.code;

/// Create a `ChangeNotifierScopedQuery`.
///
/// `ChangeNotifierScopedQuery`を作成します。
class CodeQueryCliCommand extends CliCodeCommand {
/// Create a `ChangeNotifierScopedQuery`.
///
/// `ChangeNotifierScopedQuery`を作成します。
const CodeQueryCliCommand();

@override
String get name => "query";

@override
String get prefix => "query";

@override
String get directory => "lib/controllers";

@override
String get description =>
"Create a `ChangeNotifierScopedQuery` in `$directory/(filepath).dart`. `ChangeNotifierScopedQuery`を`$directory/(filepath).dart`に作成します。";

@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 query [path]\r\n",
);
return;
}
label("Create a ChangeNotifierScopedQuery in `$directory/$path.dart`.");
final parentPath = path.parentPath();
if (parentPath.isNotEmpty) {
final parentDir = Directory("$directory/$parentPath");
if (!parentDir.existsSync()) {
await parentDir.create(recursive: true);
}
}
await generateDartCode("$directory/$path");
}

@override
String import(String path, String baseName, String className) {
return """
// ignore: unused_import, unnecessary_import
import 'package:flutter/material.dart';
// ignore: unused_import, unnecessary_import
import 'package:masamune/masamune.dart';
// ignore: unused_import, unnecessary_import
import '/main.dart';
""";
}

@override
String header(String path, String baseName, String className) {
return "";
}

@override
String body(String path, String baseName, String className) {
return """
/// [ChangeNotifierScopedQuery] for $className.
///
/// It can be used as follows
///
/// ```dart
/// final ${className.toCamelCase()} = ref.app.query(${className.toCamelCase()}Query);
/// ```
final ${className.toCamelCase()}Query = ChangeNotifierScopedQuery(
(ref) {
// TODO: Define the process of creating the ChangeNotifier.
return \${1};
}
);
""";
}
}

0 comments on commit 8a5c793

Please sign in to comment.