1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* @author lsy
* @date 2019-09-02
**/
import 'dart:math';
import 'package:analyzer/dart/element/element.dart';
import 'package:build/src/builder/build_step.dart';
import 'package:gmalpha_flutter/Annotations/anno/Router.dart';
import 'package:gmalpha_flutter/Annotations/base/RouterBuildItem.dart';
import 'package:source_gen/source_gen.dart';
import '../RouterCenterRestore.dart';
class RouterGenerator extends GeneratorForAnnotation<Router> {
@override
generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) {
var modelName = annotation.peek("modelName").stringValue;
var typeValue = annotation.peek("impl").typeValue;
var pathSegments = buildStep.inputId.pathSegments;
StringBuffer buffer = new StringBuffer();
String interfaceName;
for (int i = 0; i < pathSegments.length; i++) {
if (pathSegments[i] != "lib" && i != pathSegments.length - 1) {
buffer.write(pathSegments[i] + "/");
} else if (i == pathSegments.length - 1) {
// buffer.write("${typeValue.name}.dart");
interfaceName = pathSegments[i].replaceAll(".dart", "");
}
}
String first =
"import \"package:${buildStep.inputId.package}/${buffer.toString()}";
String second = typeValue.name;
String third = interfaceName;
RouterBuildItem item = new RouterBuildItem(first, second, third);
if (RouterCenterRestore().buildMap[modelName] != null) {
throw Exception("router error have same model name !!! change it name ");
}
RouterCenterRestore().buildMap.putIfAbsent(modelName, () => item);
// return """
//// import ${"package:${buildStep.inputId.package}/${buildStep.inputId.path.replaceFirst('lib/', '')}"}
// class APT {
// Map<String,RouterBaser> map={};
// }
// """;
return null;
}
}