diff --git a/.idea/runConfigurations/Service.xml b/.idea/runConfigurations/Service.xml deleted file mode 100644 index 5888795..0000000 --- a/.idea/runConfigurations/Service.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - \ No newline at end of file diff --git a/bin/angel.dart b/bin/angel.dart index 0433165..fd9e44d 100644 --- a/bin/angel.dart +++ b/bin/angel.dart @@ -11,9 +11,12 @@ main(List args) { var runner = new CommandRunner("angel", "Command-line tools for the Angel framework."); - runner.addCommand(new DoctorCommand()); - runner.addCommand(new ServiceCommand()); - runner.addCommand(new InitCommand()); + runner + ..addCommand(new DoctorCommand()) + ..addCommand(new ServiceCommand()) + ..addCommand(new InitCommand()) + ..addCommand(new TestCommand()) + ..addCommand(new PluginCommand()); return runner.run(args).then((_) {}).catchError((exc) { stderr.writeln("Oops, something went wrong: $exc"); diff --git a/lib/src/commands/commands.dart b/lib/src/commands/commands.dart index e6c5fae..1404153 100644 --- a/lib/src/commands/commands.dart +++ b/lib/src/commands/commands.dart @@ -2,4 +2,6 @@ library angel_cli.commands; export "doctor.dart"; export "init.dart"; -export "service.dart"; \ No newline at end of file +export "plugin.dart"; +export "service.dart"; +export "test.dart"; \ No newline at end of file diff --git a/lib/src/commands/plugin.dart b/lib/src/commands/plugin.dart new file mode 100644 index 0000000..6a731cd --- /dev/null +++ b/lib/src/commands/plugin.dart @@ -0,0 +1,46 @@ +import 'dart:io'; +import 'package:args/command_runner.dart'; +import "package:console/console.dart"; + +class PluginCommand extends Command { + final TextPen _pen = new TextPen(); + + @override + String get name => "plugin"; + + @override + String get description => "Creates a new plugin within the given project."; + + @override + run() async { + final name = await readInput("Name of Plugin: "), lower = name.toLowerCase(); + final testDir = new Directory("lib/src/config/plugins"); + final pluginFile = new File.fromUri( + testDir.uri.resolve("$lower.dart")); + + if (!await pluginFile.exists()) + await pluginFile.create(recursive: true); + + await pluginFile.writeAsString(_generatePlugin(lower)); + + _pen.green(); + _pen("${Icon.CHECKMARK} Successfully generated plugin $name."); + _pen(); + } + + String _generatePlugin(String name) { + + return ''' +import 'dart:async'; +import 'package:angel_framework/angel_framework.dart'; + +class $name extends AngelPlugin { + @override + Future call(Angel app) async { + // Work some magic... + } +} + ''' + .trim(); + } +} diff --git a/lib/src/commands/service.dart b/lib/src/commands/service.dart index 69a4b18..6c4bdcf 100644 --- a/lib/src/commands/service.dart +++ b/lib/src/commands/service.dart @@ -1,7 +1,6 @@ import "dart:io"; import "package:args/command_runner.dart"; import "package:console/console.dart"; -import "package:mustache4dart/mustache4dart.dart"; class ServiceCommand extends Command { final String CUSTOM = "Custom"; @@ -228,82 +227,36 @@ class ${name}Service extends MongoTypedService<$name> { _generateTests(String name, String type) { return ''' import 'dart:io'; -import 'package:angel/src/services/${name.toLowerCase()}.dart'; +import 'package:angel/angel.dart'; import 'package:angel_framework/angel_framework.dart'; -import 'package:http/http.dart' as http; -import 'package:json_god/json_god.dart' as god;${_includeMongo(type)} +import 'package:angel_test/angel_test.dart'; import 'package:test/test.dart'; -main() { - group('$name', () { - Angel app; - http.Client client; - HttpServer server; - String url; - HookedService ${name}s;${_createDb(type)} +main() async { + Angel app; + TestClient client; - setUp(() async { - app = new Angel(); - client = new http.Client();${_openDb(type)} - app.use('/${name.toLowerCase()}s', new ${name}Service(${_dbCollection(name, type)})); - ${name}s = app.service("${name.toLowerCase()}s"); + setUp(() async { + app = await createServer(); + client = await connectTo(app, saveSession: false); + }); - server = await app.startServer(null, 0); - url = "http://\${server.address.host}:\${server.port}"; - }); + tearDown(() async { + await client.close(); + app = null; + }); - tearDown(() async { - app = null; - url = null; - client.close(); - client = null; - ${name}s = null; - await server.close(force: true); - }); + test('index via REST', () async { + final response = await client.get('/api/${name.toLowerCase()}'); + expect(response, hasStatus(HttpStatus.OK)); + }); - test('Index ${name.toLowerCase()}s', () async { - var indexed${name}s = await ${name}s.index(); - }); - - test('Index via REST', () async { - var response = await client.get('\$url/${name.toLowerCase()}s'); - print(god.deserialize(response.body)); - }); + test('Index ${name.toLowerCase()}s', () async { + final ${name.toLowerCase()}s = await client.service('api/${name.toLowerCase()}').index(); + print(${name.toLowerCase()}s); }); } - ''' - .trim(); - } - _createDb(String type) { - if (type == MONGO || type == MONGO_TYPED) { - return "\n Db db;"; - } - - return ""; - } - - _dbCollection(String name, String type) { - if (type == MONGO || type == MONGO_TYPED) { - return "db.collection('${name.toLowerCase()}s')"; - } - - return ""; - } - - _includeMongo(String type) { - if (type == MONGO || type == MONGO_TYPED) { - return "\nimport 'package:mongo_dart/mongo_dart.dart';"; - } - - return ""; - } - - _openDb(String type) { - if (type == MONGO || type == MONGO_TYPED) { - return "\n await db.open();"; - } - - return ""; + '''.trim(); } } diff --git a/lib/src/commands/test.dart b/lib/src/commands/test.dart new file mode 100644 index 0000000..01063e4 --- /dev/null +++ b/lib/src/commands/test.dart @@ -0,0 +1,82 @@ +import 'dart:io'; +import 'package:args/command_runner.dart'; +import "package:console/console.dart"; + +class TestCommand extends Command { + final TextPen _pen = new TextPen(); + + @override + String get name => "test"; + + @override + String get description => "Creates a new test within the given project."; + + @override + run() async { + final name = await readInput("Name of Test: "), lower = name.toLowerCase(); + final testDir = new Directory("test/services"); + final testFile = new File.fromUri( + testDir.uri.resolve("${lower}_test.dart")); + + if (!await testFile.exists()) + await testFile.create(recursive: true); + + await testFile.writeAsString(_generateTest(lower)); + + final runConfig = new File('./.idea/runConfigurations/${name}_tests.xml'); + + if (!await runConfig.exists()) { + await runConfig.create(recursive: true); + await runConfig.writeAsString(_generateRunConfiguration(name)); + } + + _pen.green(); + _pen("${Icon.CHECKMARK} Successfully generated test $name."); + _pen(); + } + + _generateRunConfiguration(String name) { + final lower = name.toLowerCase(); + + return ''' + + + + +''' + .trim(); + } + + String _generateTest(String lower) { + return ''' +import 'dart:io'; +import 'package:angel/angel.dart'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_test/angel_test.dart'; +import 'package:test/test.dart'; + +main() async { + Angel app; + TestClient client; + + setUp(() async { + app = await createServer(); + client = await connectTo(app, saveSession: false); + }); + + tearDown(() async { + await client.close(); + app = null; + }); + + test('$lower', () async { + final response = await client.get('/$lower'); + expect(response, hasStatus(HttpStatus.OK)); + }); +} + ''' + .trim(); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 7de21a4..e434f18 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_cli -version: 1.0.0-dev+4 +version: 1.0.0-dev+5 description: Command-line tools for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_cli @@ -9,5 +9,4 @@ dependencies: analyzer: ">=0.28.1 <1.0.0" args: ">=0.3.4 <1.0.0" console: ">=2.2.3 <3.0.0" - mustache4dart: ">=1.0.0 <3.0.0" yaml: ">=2.0.0 <3.0.0" \ No newline at end of file