-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathproject_parser.dart
More file actions
69 lines (61 loc) · 2.07 KB
/
project_parser.dart
File metadata and controls
69 lines (61 loc) · 2.07 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart';
import 'package:celest_cli/src/codegen/client_code_generator.dart';
final class CelestProjectParser {
CelestProjectParser({
required String projectRoot,
required this.projectDart,
required DriverBasedAnalysisContext analysisContext,
}) : _analysisContext = analysisContext,
_dependencies = CodegenDependencies(rootDir: projectRoot);
final String projectDart;
final DriverBasedAnalysisContext _analysisContext;
final CodegenDependencies _dependencies;
CodegenDependencies parseDependencies() {
final projectDartLibrary = _analysisContext.currentSession.getParsedLibrary(
projectDart,
);
if (projectDartLibrary is! ParsedLibraryResult) {
throw StateError('Could not parse $projectDart');
}
final topLevelVariables =
projectDartLibrary.units
.expand((unit) => unit.unit.declarations)
.whereType<TopLevelVariableDeclaration>()
.expand((declaration) => declaration.variables.variables)
.toList();
if (_findDatabases(topLevelVariables)) {
_dependencies.add('drift_hrana');
}
if (_findAuth(topLevelVariables)) {
_dependencies.add('celest_cloud_auth');
_dependencies.add('drift_hrana');
}
return _dependencies;
}
bool _findAuth(List<VariableDeclaration> topLevelVariables) {
for (final variable in topLevelVariables) {
if (variable case VariableDeclaration(
initializer: MethodInvocation(
methodName: SimpleIdentifier(name: 'Auth'),
),
)) {
return true;
}
}
return false;
}
bool _findDatabases(List<VariableDeclaration> topLevelVariables) {
for (final variable in topLevelVariables) {
if (variable case VariableDeclaration(
initializer: MethodInvocation(
methodName: SimpleIdentifier(name: 'Database'),
),
)) {
return true;
}
}
return false;
}
}