Skip to content

Commit

Permalink
update cli for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
necessarylion committed Dec 16, 2023
1 parent 2aedf31 commit cef0161
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 29 deletions.
30 changes: 17 additions & 13 deletions packages/dox-cli/lib/src/tools/create_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Model> to return';
Future<dynamic> 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
}
}
Expand All @@ -32,34 +34,36 @@ import 'package:dox_core/dox_core.dart';
class ${className}Controller {
/// GET /resource
index(DoxRequest req) async {}
Future<dynamic> index(DoxRequest req) async {}
/// GET /resource/create
create(DoxRequest req) async {}
Future<dynamic> create(DoxRequest req) async {}
/// POST /resource
store(DoxRequest req) async {}
Future<dynamic> store(DoxRequest req) async {}
/// GET /resource/{id}
show(DoxRequest req, String id) async {}
Future<dynamic> show(DoxRequest req, String id) async {}
/// GET /resource/{id}/edit
edit(DoxRequest req, String id) async {}
Future<dynamic> edit(DoxRequest req, String id) async {}
/// PUT|PATCH /resource/{id}
update(DoxRequest req, String id) async {}
Future<dynamic> update(DoxRequest req, String id) async {}
/// DELETE /resource/{id}
destroy(DoxRequest req, String id) async {}
Future<dynamic> destroy(DoxRequest req, String id) async {}
}
${className}Controller ${toPascalWithFirstLetterLowerCase(className)}Controller = ${className}Controller();
''';
}

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');

Expand All @@ -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');

Expand Down
8 changes: 5 additions & 3 deletions packages/dox-cli/lib/src/tools/create_middleware.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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');

Expand Down
4 changes: 2 additions & 2 deletions packages/dox-cli/lib/src/tools/create_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ part '$filename.model.g.dart';
@DoxModel()
class $className extends ${className}Generator {
@override
List<String> get hidden => [];
List<String> get hidden => <String>[];
}
''';
}

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()) {
Expand Down
2 changes: 1 addition & 1 deletion packages/dox-cli/lib/src/tools/create_project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ createProject(projectName) async {
Process.runSync('git', [
'clone',
'-b',
'main',
'v2.x',
'https://github.com/necessarylion/dox-sample.git',
projectName
]);
Expand Down
6 changes: 3 additions & 3 deletions packages/dox-cli/lib/src/tools/create_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class ${className}Request extends FormRequest {
@override
Map<String, String> rules() {
return {};
return <String, String>{};
}
@override
Map<String, String> messages() {
return {};
return <String, String>{};
}
}
''';
Expand All @@ -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');

Expand Down
6 changes: 4 additions & 2 deletions packages/dox-cli/lib/src/tools/create_serializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> convert($className m) {
return {};
return <String, dynamic>{};
}
}
''';
Expand All @@ -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');

Expand Down
14 changes: 12 additions & 2 deletions packages/dox-cli/lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,26 @@ String pascalToSnake(String input) {
}
}
String finalString = result.toString().replaceAll(RegExp('_+'), '_');
finalString = finalString.endsWith('_')
? finalString.substring(0, finalString.length - 1)
: finalString;
return finalString;
}

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<String, String> loadEnv() {
Expand Down
19 changes: 16 additions & 3 deletions packages/dox-cli/test/dox_test.dart
Original file line number Diff line number Diff line change
@@ -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');
});
});
}

0 comments on commit cef0161

Please sign in to comment.