Skip to content

Commit

Permalink
Merge pull request #8 from Jaguar-dart/master
Browse files Browse the repository at this point in the history
RouteGroup implementation
  • Loading branch information
tejainece authored Nov 9, 2016
2 parents aa664bc + c20ad67 commit a1662aa
Show file tree
Hide file tree
Showing 20 changed files with 321 additions and 99 deletions.
29 changes: 15 additions & 14 deletions example/forum/forum.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions example/silly/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@ import 'package:jaguar/jaguar.dart';

part 'main.g.dart';

@RouteGroup(path: '/myGroup')
class MyGroup extends Object with _$JaguarMyGroup {
@Get('/')
String get() => "get my group";
}

@RouteGroup(path: '/mySecondGroup')
class MySecondGroup extends Object with _$JaguarMySecondGroup {
@Get('/')
String get() => "get mys second group";
}

/// Example of basic API class
@Api(path: '/api')
class ExampleApi extends _$JaguarExampleApi {
@RouteGroup(path: '/api')
class ExampleApi extends Object with _$JaguarExampleApi {
int _pingCount = 0;

/// Example of basic route
Expand All @@ -32,6 +44,12 @@ class ExampleApi extends _$JaguarExampleApi {
Future websocket(WebSocket ws) async {
ws.listen((data) => ws.add(data));
}

@Group()
MyGroup myGroup = new MyGroup();

@Group()
MySecondGroup mySecondGroup = new MySecondGroup();
}

Future<Null> main(List<String> args) async {
Expand Down
88 changes: 80 additions & 8 deletions example/silly/main.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jaguar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ apis:
- 'example/silly/main.dart'
- 'test/jaguar/route/route.dart'
- 'test/jaguar/group/main.dart'
- 'test/jaguar/group/book.dart'
- 'test/jaguar/websocket/websocket.dart'
- 'test/interceptor/param/param.dart'
2 changes: 1 addition & 1 deletion lib/generator/hook/api/import.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ApiGenerator extends GeneratorForAnnotation<ant.Api> {
List<ExceptionHandlerInfo> exceptions = collectExceptionHandlers(element);

List<RouteInfo> routes =
collectAllRoutes(classElement, prefix, interceptors, exceptions, []);
collectRoutes(classElement, prefix, interceptors, exceptions, []);

writer.addGroups(collectGroups(classElement));

Expand Down
48 changes: 48 additions & 0 deletions lib/generator/hook/route_group/import.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
library jaguar.generator.hook.route_group;

import 'dart:async';

import 'package:analyzer/dart/element/element.dart';
import 'package:build/src/builder/build_step.dart';
import 'package:source_gen/source_gen.dart';

import 'package:jaguar/generator/writer/import.dart';

import 'package:jaguar/src/annotations/import.dart' as ant;

import 'package:jaguar/generator/parser/import.dart';

class RouteGroupGenerator extends GeneratorForAnnotation<ant.RouteGroup> {
const RouteGroupGenerator();

/// Generator
@override
Future<String> generateForAnnotatedElement(
Element element, ant.RouteGroup routeGroup, BuildStep buildStep) async {
if (element is! ClassElement) {
throw new Exception("Api annotation can only be defined on a class.");
}

ClassElement classElement = element;
String className = classElement.name;

print("Generating for RouteGroup class $className ...");

Writer writer = new Writer(className, forGroupRoute: true);

final String prefix = routeGroup.path;

List<InterceptorInfo> interceptors = parseInterceptor(element);

List<ExceptionHandlerInfo> exceptions = collectExceptionHandlers(element);

List<RouteInfo> routes =
collectRoutes(classElement, prefix, interceptors, exceptions, []);

writer.addGroups(collectGroups(classElement));

writer.addAllRoutes(routes);

return writer.toString();
}
}
6 changes: 3 additions & 3 deletions lib/generator/phase/import.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:source_gen/source_gen.dart';
import 'package:yaml/yaml.dart';

import 'package:jaguar/generator/hook/api/import.dart';
import 'package:jaguar/generator/hook/route_group/import.dart';

String getProjectName() {
File pubspec = new File('./pubspec.yaml');
Expand All @@ -26,9 +27,8 @@ List<String> getAnnotations() {
Phase apisPhase(String projectName, List<String> apis) {
return new Phase()
..addAction(
new GeneratorBuilder(const [
const ApiGenerator(),
]),
new GeneratorBuilder(
const [const ApiGenerator(), const RouteGroupGenerator()]),
new InputSet(projectName, apis));
}

Expand Down
Loading

0 comments on commit a1662aa

Please sign in to comment.