This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 258
/
analyzer_utils.dart
114 lines (97 loc) · 3.6 KB
/
analyzer_utils.dart
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// ignore_for_file: implementation_imports
import 'dart:io';
import 'package:analyzer/dart/analysis/analysis_context.dart';
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/dart/analysis/context_locator.dart';
import 'package:analyzer/file_system/file_system.dart' hide File;
import 'package:analyzer/file_system/overlay_file_system.dart';
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer/src/dart/analysis/analysis_context_collection.dart';
import 'package:analyzer/src/dart/analysis/byte_store.dart';
import 'package:analyzer/src/dart/analysis/file_byte_store.dart';
import 'package:file/local.dart';
import 'package:glob/glob.dart';
import 'package:path/path.dart';
import 'exclude_utils.dart';
AnalysisContextCollection createAnalysisContextCollection(
Iterable<String> folders,
String rootFolder,
String? sdkPath,
) {
final includedPaths =
folders.map((path) => normalize(join(rootFolder, path))).toList();
final resourceProvider = _prepareAnalysisOptions(includedPaths);
return AnalysisContextCollectionImpl(
sdkPath: sdkPath,
includedPaths: includedPaths,
resourceProvider: resourceProvider,
byteStore: createByteStore(resourceProvider),
);
}
Set<String> getFilePaths(
Iterable<String> folders,
AnalysisContext context,
String rootFolder,
Iterable<Glob> excludes,
) {
final rootPath = context.contextRoot.root.path;
final contextFolders = folders.where((path) {
final folderPath = normalize(join(rootFolder, path));
return folderPath == rootPath || folderPath.startsWith('$rootPath/');
}).toList();
return _extractDartFilesFromFolders(contextFolders, rootFolder, excludes);
}
/// If the state location can be accessed, return the file byte store,
/// otherwise return the memory byte store.
ByteStore createByteStore(ResourceProvider resourceProvider) {
const miB = 1024 * 1024 /*1 MiB*/;
const giB = 1024 * 1024 * 1024 /*1 GiB*/;
const memoryCacheSize = miB * 128;
final stateLocation = resourceProvider.getStateLocation('.dart-code-metrics');
if (stateLocation != null) {
return MemoryCachingByteStore(
EvictingFileByteStore(stateLocation.path, giB),
memoryCacheSize,
);
}
return MemoryCachingByteStore(NullByteStore(), memoryCacheSize);
}
Set<String> _extractDartFilesFromFolders(
Iterable<String> folders,
String rootFolder,
Iterable<Glob> globalExcludes,
) =>
folders
.expand((fileSystemEntity) => (fileSystemEntity.endsWith('.dart')
? Glob(fileSystemEntity)
: Glob('$fileSystemEntity/**.dart'))
.listFileSystemSync(
const LocalFileSystem(),
root: rootFolder,
followLinks: false,
)
.whereType<File>()
.where((entity) => !isExcluded(
relative(entity.path, from: rootFolder),
globalExcludes,
))
.map((entity) => normalize(entity.path)))
.toSet();
ResourceProvider _prepareAnalysisOptions(List<String> includedPaths) {
final resourceProvider =
OverlayResourceProvider(PhysicalResourceProvider.INSTANCE);
final contextLocator = ContextLocator(resourceProvider: resourceProvider);
final roots = contextLocator
.locateRoots(includedPaths: includedPaths, excludedPaths: []);
for (final root in roots) {
final path = root.optionsFile?.path;
if (path != null) {
resourceProvider.setOverlay(
path,
content: '',
modificationStamp: DateTime.now().millisecondsSinceEpoch,
);
}
}
return resourceProvider;
}