This commit is contained in:
thosakwe 2016-12-10 13:52:14 -05:00
parent 2a212c9d40
commit b92ebdeb21
7 changed files with 159 additions and 82 deletions

View file

@ -1,8 +0,0 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Service" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application" singleton="true">
<option name="arguments" value="service" />
<option name="filePath" value="$PROJECT_DIR$/bin/angel.dart" />
<option name="workingDirectory" value="$PROJECT_DIR$/sample_project" />
<method />
</configuration>
</component>

View file

@ -11,9 +11,12 @@ main(List<String> args) {
var runner = var runner =
new CommandRunner("angel", "Command-line tools for the Angel framework."); new CommandRunner("angel", "Command-line tools for the Angel framework.");
runner.addCommand(new DoctorCommand()); runner
runner.addCommand(new ServiceCommand()); ..addCommand(new DoctorCommand())
runner.addCommand(new InitCommand()); ..addCommand(new ServiceCommand())
..addCommand(new InitCommand())
..addCommand(new TestCommand())
..addCommand(new PluginCommand());
return runner.run(args).then((_) {}).catchError((exc) { return runner.run(args).then((_) {}).catchError((exc) {
stderr.writeln("Oops, something went wrong: $exc"); stderr.writeln("Oops, something went wrong: $exc");

View file

@ -2,4 +2,6 @@ library angel_cli.commands;
export "doctor.dart"; export "doctor.dart";
export "init.dart"; export "init.dart";
export "plugin.dart";
export "service.dart"; export "service.dart";
export "test.dart";

View file

@ -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();
}
}

View file

@ -1,7 +1,6 @@
import "dart:io"; import "dart:io";
import "package:args/command_runner.dart"; import "package:args/command_runner.dart";
import "package:console/console.dart"; import "package:console/console.dart";
import "package:mustache4dart/mustache4dart.dart";
class ServiceCommand extends Command { class ServiceCommand extends Command {
final String CUSTOM = "Custom"; final String CUSTOM = "Custom";
@ -228,82 +227,36 @@ class ${name}Service extends MongoTypedService<$name> {
_generateTests(String name, String type) { _generateTests(String name, String type) {
return ''' return '''
import 'dart:io'; 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:angel_framework/angel_framework.dart';
import 'package:http/http.dart' as http; import 'package:angel_test/angel_test.dart';
import 'package:json_god/json_god.dart' as god;${_includeMongo(type)}
import 'package:test/test.dart'; import 'package:test/test.dart';
main() { main() async {
group('$name', () {
Angel app; Angel app;
http.Client client; TestClient client;
HttpServer server;
String url;
HookedService ${name}s;${_createDb(type)}
setUp(() async { setUp(() async {
app = new Angel(); app = await createServer();
client = new http.Client();${_openDb(type)} client = await connectTo(app, saveSession: false);
app.use('/${name.toLowerCase()}s', new ${name}Service(${_dbCollection(name, type)}));
${name}s = app.service("${name.toLowerCase()}s");
server = await app.startServer(null, 0);
url = "http://\${server.address.host}:\${server.port}";
}); });
tearDown(() async { tearDown(() async {
await client.close();
app = null; app = null;
url = null; });
client.close();
client = null; test('index via REST', () async {
${name}s = null; final response = await client.get('/api/${name.toLowerCase()}');
await server.close(force: true); expect(response, hasStatus(HttpStatus.OK));
}); });
test('Index ${name.toLowerCase()}s', () async { test('Index ${name.toLowerCase()}s', () async {
var indexed${name}s = await ${name}s.index(); final ${name.toLowerCase()}s = await client.service('api/${name.toLowerCase()}').index();
}); print(${name.toLowerCase()}s);
test('Index via REST', () async {
var response = await client.get('\$url/${name.toLowerCase()}s');
print(god.deserialize(response.body));
});
}); });
} }
'''
.trim();
}
_createDb(String type) { '''.trim();
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 "";
} }
} }

View file

@ -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 '''
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="$name Tests" type="DartTestRunConfigurationType" factoryName="Dart Test" singleton="true">
<option name="filePath" value="\$PROJECT_DIR\$/test/${lower}_test.dart" />
<method />
</configuration>
</component>
'''
.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();
}
}

View file

@ -1,5 +1,5 @@
name: angel_cli name: angel_cli
version: 1.0.0-dev+4 version: 1.0.0-dev+5
description: Command-line tools for the Angel framework. description: Command-line tools for the Angel framework.
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_cli homepage: https://github.com/angel-dart/angel_cli
@ -9,5 +9,4 @@ dependencies:
analyzer: ">=0.28.1 <1.0.0" analyzer: ">=0.28.1 <1.0.0"
args: ">=0.3.4 <1.0.0" args: ">=0.3.4 <1.0.0"
console: ">=2.2.3 <3.0.0" console: ">=2.2.3 <3.0.0"
mustache4dart: ">=1.0.0 <3.0.0"
yaml: ">=2.0.0 <3.0.0" yaml: ">=2.0.0 <3.0.0"