diff --git a/packages/dox-cli/lib/src/tools/create_controller.dart b/packages/dox-cli/lib/src/tools/create_controller.dart index 29e8030..1f1e982 100644 --- a/packages/dox-cli/lib/src/tools/create_controller.dart +++ b/packages/dox-cli/lib/src/tools/create_controller.dart @@ -7,19 +7,21 @@ String _getSample(className, filename) { import 'package:dox_core/dox_core.dart'; class ${className}Controller { - index(DoxRequest req) async { - return 'Support String, Map, Model, List and List to return'; + Future index(DoxRequest req) async { + /// write your logic here } } + +${className}Controller ${toPascalWithFirstLetterLowerCase(className)}Controller = ${className}Controller(); '''; } String _getWsSample(className, filename) { return ''' -import 'package:dox_core/dox_core.dart'; +import 'package:dox_websocket/dox_websocket.dart'; class ${className}Controller { - index(SocketEmitter emitter, message) async { + void index(WebsocketEmitter emitter, dynamic message) async { /// write your logic here } } @@ -32,26 +34,28 @@ import 'package:dox_core/dox_core.dart'; class ${className}Controller { /// GET /resource - index(DoxRequest req) async {} + Future index(DoxRequest req) async {} /// GET /resource/create - create(DoxRequest req) async {} + Future create(DoxRequest req) async {} /// POST /resource - store(DoxRequest req) async {} + Future store(DoxRequest req) async {} /// GET /resource/{id} - show(DoxRequest req, String id) async {} + Future show(DoxRequest req, String id) async {} /// GET /resource/{id}/edit - edit(DoxRequest req, String id) async {} + Future edit(DoxRequest req, String id) async {} /// PUT|PATCH /resource/{id} - update(DoxRequest req, String id) async {} + Future update(DoxRequest req, String id) async {} /// DELETE /resource/{id} - destroy(DoxRequest req, String id) async {} + Future destroy(DoxRequest req, String id) async {} } + +${className}Controller ${toPascalWithFirstLetterLowerCase(className)}Controller = ${className}Controller(); '''; } @@ -59,7 +63,7 @@ bool createController(String filename, bool resource) { filename = filename.toLowerCase().replaceAll('controller', ''); filename = pascalToSnake(filename); String className = snakeToPascal(filename); - String path = '${Directory.current.path}/lib/http/controllers/'; + String path = '${Directory.current.path}/lib/app/http/controllers/'; String controllerName = '$filename.controller'; final file = File('$path$filename.controller.dart'); @@ -82,7 +86,7 @@ bool createWsController(String filename) { filename = filename.toLowerCase().replaceAll('controller', ''); filename = pascalToSnake(filename); String className = snakeToPascal(filename); - String path = '${Directory.current.path}/lib/ws/controllers/'; + String path = '${Directory.current.path}/lib/app/ws/controllers/'; String controllerName = '$filename.controller'; final file = File('$path$filename.controller.dart'); diff --git a/packages/dox-cli/lib/src/tools/create_middleware.dart b/packages/dox-cli/lib/src/tools/create_middleware.dart index 191e9ec..ca01e27 100644 --- a/packages/dox-cli/lib/src/tools/create_middleware.dart +++ b/packages/dox-cli/lib/src/tools/create_middleware.dart @@ -6,10 +6,12 @@ String _getSample(className, filename) { return ''' import 'package:dox_core/dox_core.dart'; -class ${className}Middleware extends DoxMiddleware { +class ${className}Middleware extends IDoxMiddleware { @override - handle(DoxRequest req) { + dynamic handle(IDoxRequest req) { /// add your logic here + /// return req (IDoxRequest) to process next to the controller + /// or throw an error or return Map, String, List etc to return 200 response return req; } } @@ -20,7 +22,7 @@ bool createMiddleware(String filename) { filename = filename.toLowerCase().replaceAll('middleware', ''); filename = pascalToSnake(filename); String className = snakeToPascal(filename); - String path = '${Directory.current.path}/lib/http/middleware/'; + String path = '${Directory.current.path}/lib/app/http/middleware/'; String middlewareName = '$filename.middleware'; final file = File('$path$filename.middleware.dart'); diff --git a/packages/dox-cli/lib/src/tools/create_model.dart b/packages/dox-cli/lib/src/tools/create_model.dart index 666a1b3..61e9b32 100644 --- a/packages/dox-cli/lib/src/tools/create_model.dart +++ b/packages/dox-cli/lib/src/tools/create_model.dart @@ -11,7 +11,7 @@ part '$filename.model.g.dart'; @DoxModel() class $className extends ${className}Generator { @override - List get hidden => []; + List get hidden => []; } '''; } @@ -19,7 +19,7 @@ class $className extends ${className}Generator { bool createModel(filename) { filename = pascalToSnake(filename); String className = snakeToPascal(filename); - String path = '${Directory.current.path}/lib/models/$filename/'; + String path = '${Directory.current.path}/lib/app/models/$filename/'; final file = File('$path$filename.model.dart'); if (file.existsSync()) { diff --git a/packages/dox-cli/lib/src/tools/create_project.dart b/packages/dox-cli/lib/src/tools/create_project.dart index 3b9fd92..fb389b3 100644 --- a/packages/dox-cli/lib/src/tools/create_project.dart +++ b/packages/dox-cli/lib/src/tools/create_project.dart @@ -12,7 +12,7 @@ createProject(projectName) async { Process.runSync('git', [ 'clone', '-b', - 'main', + 'v2.x', 'https://github.com/necessarylion/dox-sample.git', projectName ]); diff --git a/packages/dox-cli/lib/src/tools/create_request.dart b/packages/dox-cli/lib/src/tools/create_request.dart index 96edd48..ccd02c8 100644 --- a/packages/dox-cli/lib/src/tools/create_request.dart +++ b/packages/dox-cli/lib/src/tools/create_request.dart @@ -12,12 +12,12 @@ class ${className}Request extends FormRequest { @override Map rules() { - return {}; + return {}; } @override Map messages() { - return {}; + return {}; } } '''; @@ -27,7 +27,7 @@ bool createRequest(String filename) { filename = filename.toLowerCase().replaceAll('request', ''); filename = pascalToSnake(filename); String className = snakeToPascal(filename); - String path = '${Directory.current.path}/lib/http/requests/'; + String path = '${Directory.current.path}/lib/app/http/requests/'; String requestName = '$filename.request'; final file = File('$path$filename.request.dart'); diff --git a/packages/dox-cli/lib/src/tools/create_serializer.dart b/packages/dox-cli/lib/src/tools/create_serializer.dart index 7d1eea1..b9ee59b 100644 --- a/packages/dox-cli/lib/src/tools/create_serializer.dart +++ b/packages/dox-cli/lib/src/tools/create_serializer.dart @@ -6,12 +6,14 @@ String _getSample(className, filename) { return ''' import 'package:dox_core/dox_core.dart'; +import '../../../app/models/$filename/$filename.model.dart'; + class ${className}Serializer extends Serializer<$className> { ${className}Serializer(super.data); @override Map convert($className m) { - return {}; + return {}; } } '''; @@ -21,7 +23,7 @@ bool createSerializer(String filename) { filename = filename.toLowerCase().replaceAll('serializer', ''); filename = pascalToSnake(filename); String className = snakeToPascal(filename); - String path = '${Directory.current.path}/lib/http/serializers/'; + String path = '${Directory.current.path}/lib/app/http/serializers/'; String serializerName = '$filename.serializer'; final file = File('$path$filename.serializer.dart'); diff --git a/packages/dox-cli/lib/src/utils/utils.dart b/packages/dox-cli/lib/src/utils/utils.dart index bf67d89..4f4bb12 100644 --- a/packages/dox-cli/lib/src/utils/utils.dart +++ b/packages/dox-cli/lib/src/utils/utils.dart @@ -15,6 +15,9 @@ String pascalToSnake(String input) { } } String finalString = result.toString().replaceAll(RegExp('_+'), '_'); + finalString = finalString.endsWith('_') + ? finalString.substring(0, finalString.length - 1) + : finalString; return finalString; } @@ -22,9 +25,16 @@ String snakeToPascal(String input) { final parts = input.split('_'); final result = StringBuffer(); for (final part in parts) { - result.write('${part[0].toUpperCase()}${part.substring(1)}'); + if (part.isNotEmpty) { + result.write('${part[0].toUpperCase()}${part.substring(1)}'); + } } - return result.toString(); + return result.toString().isEmpty ? input : result.toString(); +} + +String toPascalWithFirstLetterLowerCase(String input) { + String pascalString = snakeToPascal(input); + return '${pascalString[0].toLowerCase()}${pascalString.substring(1)}'; } Map loadEnv() { diff --git a/packages/dox-cli/test/dox_test.dart b/packages/dox-cli/test/dox_test.dart index 031fc3c..3ad386a 100644 --- a/packages/dox-cli/test/dox_test.dart +++ b/packages/dox-cli/test/dox_test.dart @@ -1,9 +1,22 @@ +import 'package:dox/dox.dart'; import 'package:test/test.dart'; void main() { - group('A group of tests', () { - test('First Test', () { - expect(true, true); + group('string format test', () { + test('toPascalWithFirstLetterLowerCase', () { + String res1 = toPascalWithFirstLetterLowerCase('CamelCase'); + String res2 = toPascalWithFirstLetterLowerCase('camel_case'); + String res3 = toPascalWithFirstLetterLowerCase('Camel_Case'); + String res4 = toPascalWithFirstLetterLowerCase('Camel_case'); + String res5 = toPascalWithFirstLetterLowerCase('camelCase'); + String res6 = toPascalWithFirstLetterLowerCase('camel_Case'); + + expect(res1, 'camelCase'); + expect(res2, 'camelCase'); + expect(res3, 'camelCase'); + expect(res4, 'camelCase'); + expect(res5, 'camelCase'); + expect(res6, 'camelCase'); }); }); }