-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathbuilder.dart
850 lines (785 loc) · 29.6 KB
/
builder.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:collection';
import 'dart:core';
import 'package:analyzer/dart/analysis/analysis_context.dart' as api;
import 'package:analyzer/dart/analysis/context_locator.dart' as api;
import 'package:analyzer/dart/analysis/declared_variables.dart';
import 'package:analyzer/file_system/file_system.dart';
import 'package:analyzer/src/analysis_options/analysis_options_provider.dart';
import 'package:analyzer/src/command_line/arguments.dart'
show
applyAnalysisOptionFlags,
bazelAnalysisOptionsPath,
flutterAnalysisOptionsPath;
import 'package:analyzer/src/context/context.dart';
import 'package:analyzer/src/context/context_root.dart';
import 'package:analyzer/src/dart/analysis/byte_store.dart';
import 'package:analyzer/src/dart/analysis/context_root.dart' as api;
import 'package:analyzer/src/dart/analysis/driver.dart'
show AnalysisDriver, AnalysisDriverScheduler;
import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart'
as api;
import 'package:analyzer/src/dart/analysis/file_state.dart';
import 'package:analyzer/src/dart/analysis/performance_logger.dart';
import 'package:analyzer/src/dart/sdk/sdk.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/sdk.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/hint/sdk_constraint_extractor.dart';
import 'package:analyzer/src/lint/registry.dart';
import 'package:analyzer/src/plugin/resolver_provider.dart';
import 'package:analyzer/src/summary/package_bundle_reader.dart';
import 'package:analyzer/src/summary/summary_sdk.dart';
import 'package:analyzer/src/task/options.dart';
import 'package:analyzer/src/util/uri.dart';
import 'package:analyzer/src/workspace/basic.dart';
import 'package:analyzer/src/workspace/bazel.dart';
import 'package:analyzer/src/workspace/gn.dart';
import 'package:analyzer/src/workspace/package_build.dart';
import 'package:analyzer/src/workspace/pub.dart';
import 'package:analyzer/src/workspace/workspace.dart';
import 'package:args/args.dart';
import 'package:package_config/packages.dart';
import 'package:package_config/packages_file.dart';
import 'package:package_config/src/packages_impl.dart';
import 'package:path/src/context.dart';
import 'package:yaml/yaml.dart';
/**
* A utility class used to build an analysis context for a given directory.
*
* The construction of analysis contexts is as follows:
*
* 1. Determine how package: URI's are to be resolved. This follows the lookup
* algorithm defined by the [package specification][1].
*
* 2. Using the results of step 1, look in each package for an embedder file
* (_embedder.yaml). If one exists then it defines the SDK. If multiple such
* files exist then use the first one found. Otherwise, use the default SDK.
*
* 3. Look in each package for an SDK extension file (_sdkext). For each such
* file, add the specified files to the SDK.
*
* 4. Look for an analysis options file (`analysis_options.yaml` or
* `.analysis_options`) and process the options in the file.
*
* 5. Create a new context. Initialize its source factory based on steps 1, 2
* and 3. Initialize its analysis options from step 4.
*
* [1]: https://github.com/dart-lang/dart_enhancement_proposals/blob/master/Accepted/0005%20-%20Package%20Specification/DEP-pkgspec.md.
*/
class ContextBuilder {
/**
* A callback for when analysis drivers are created, which takes all the same
* arguments as the dart analysis driver constructor so that plugins may
* create their own drivers with the same tools, in theory. Here as a stopgap
* until the official plugin API is complete
*/
static Function onCreateAnalysisDriver = null;
/**
* The [ResourceProvider] by which paths are converted into [Resource]s.
*/
final ResourceProvider resourceProvider;
/**
* The manager used to manage the DartSdk's that have been created so that
* they can be shared across contexts.
*/
final DartSdkManager sdkManager;
/**
* The cache containing the contents of overlaid files. If this builder will
* be used to build analysis drivers, set the [fileContentOverlay] instead.
*/
final ContentCache contentCache;
/**
* The options used by the context builder.
*/
final ContextBuilderOptions builderOptions;
/**
* The resolver provider used to create a package: URI resolver, or `null` if
* the normal (Package Specification DEP) lookup mechanism is to be used.
*/
ResolverProvider packageResolverProvider;
/**
* The resolver provider used to create a file: URI resolver, or `null` if
* the normal file URI resolver is to be used.
*/
ResolverProvider fileResolverProvider;
/**
* The scheduler used by any analysis drivers created through this interface.
*/
AnalysisDriverScheduler analysisDriverScheduler;
/**
* The performance log used by any analysis drivers created through this
* interface.
*/
PerformanceLog performanceLog;
/**
* The byte store used by any analysis drivers created through this interface.
*/
ByteStore byteStore;
/**
* The file content overlay used by analysis drivers. If this builder will be
* used to build analysis contexts, set the [contentCache] instead.
*/
FileContentOverlay fileContentOverlay;
/**
* Whether any analysis driver created through this interface should support
* indexing and search.
*/
bool enableIndex = false;
/**
* Initialize a newly created builder to be ready to build a context rooted in
* the directory with the given [rootDirectoryPath].
*/
ContextBuilder(this.resourceProvider, this.sdkManager, this.contentCache,
{ContextBuilderOptions options})
: builderOptions = options ?? new ContextBuilderOptions();
/**
* Return an analysis context that is configured correctly to analyze code in
* the directory with the given [path].
*
* *Note:* This method is not yet fully implemented and should not be used.
*/
AnalysisContext buildContext(String path) {
InternalAnalysisContext context =
AnalysisEngine.instance.createAnalysisContext();
AnalysisOptionsImpl options = getAnalysisOptions(path);
context.contentCache = contentCache;
context.sourceFactory = createSourceFactory(path, options);
context.analysisOptions = options;
context.name = path;
//_processAnalysisOptions(context, optionMap);
declareVariables(context);
return context;
}
/**
* Return an analysis driver that is configured correctly to analyze code in
* the directory with the given [path].
*/
AnalysisDriver buildDriver(ContextRoot contextRoot) {
String path = contextRoot.root;
AnalysisOptions options =
getAnalysisOptions(path, contextRoot: contextRoot);
//_processAnalysisOptions(context, optionMap);
SummaryDataStore summaryData;
if (builderOptions.librarySummaryPaths != null) {
summaryData = SummaryDataStore(builderOptions.librarySummaryPaths);
}
final sf = createSourceFactory(path, options, summaryData: summaryData);
AnalysisDriver driver = new AnalysisDriver(
analysisDriverScheduler,
performanceLog,
resourceProvider,
byteStore,
fileContentOverlay,
contextRoot,
sf,
options,
enableIndex: enableIndex,
externalSummaries: summaryData);
// Set API AnalysisContext for the driver.
var apiContextRoots = api.ContextLocator(
resourceProvider: resourceProvider,
).locateRoots(
includedPaths: [contextRoot.root],
excludedPaths: contextRoot.exclude,
);
driver.analysisContext = api.DriverBasedAnalysisContext(
resourceProvider,
apiContextRoots.first,
driver,
);
// temporary plugin support:
if (onCreateAnalysisDriver != null) {
onCreateAnalysisDriver(driver, analysisDriverScheduler, performanceLog,
resourceProvider, byteStore, fileContentOverlay, path, sf, options);
}
declareVariablesInDriver(driver);
return driver;
}
Map<String, List<Folder>> convertPackagesToMap(Packages packages) {
Map<String, List<Folder>> folderMap = new HashMap<String, List<Folder>>();
if (packages != null && packages != Packages.noPackages) {
var pathContext = resourceProvider.pathContext;
packages.asMap().forEach((String packageName, Uri uri) {
String path = fileUriToNormalizedPath(pathContext, uri);
folderMap[packageName] = [resourceProvider.getFolder(path)];
});
}
return folderMap;
}
// void _processAnalysisOptions(
// AnalysisContext context, Map<String, YamlNode> optionMap) {
// List<OptionsProcessor> optionsProcessors =
// AnalysisEngine.instance.optionsPlugin.optionsProcessors;
// try {
// optionsProcessors.forEach(
// (OptionsProcessor p) => p.optionsProcessed(context, optionMap));
//
// // Fill in lint rule defaults in case lints are enabled and rules are
// // not specified in an options file.
// if (context.analysisOptions.lint && !containsLintRuleEntry(optionMap)) {
// setLints(context, linterPlugin.contributedRules);
// }
//
// // Ask engine to further process options.
// if (optionMap != null) {
// configureContextOptions(context, optionMap);
// }
// } on Exception catch (e) {
// optionsProcessors.forEach((OptionsProcessor p) => p.onError(e));
// }
// }
/**
* Return an analysis options object containing the default option values.
*/
AnalysisOptions createDefaultOptions() {
AnalysisOptions defaultOptions = builderOptions.defaultOptions;
if (defaultOptions == null) {
return new AnalysisOptionsImpl();
}
return new AnalysisOptionsImpl.from(defaultOptions);
}
Packages createPackageMap(String rootDirectoryPath) {
String filePath = builderOptions.defaultPackageFilePath;
if (filePath != null) {
File configFile = resourceProvider.getFile(filePath);
List<int> bytes = configFile.readAsBytesSync();
Map<String, Uri> map = parse(bytes, configFile.toUri());
resolveSymbolicLinks(map);
return new MapPackages(map);
}
String directoryPath = builderOptions.defaultPackagesDirectoryPath;
if (directoryPath != null) {
Folder folder = resourceProvider.getFolder(directoryPath);
return getPackagesFromFolder(folder);
}
return findPackagesFromFile(rootDirectoryPath);
}
SourceFactory createSourceFactory(String rootPath, AnalysisOptions options,
{SummaryDataStore summaryData}) {
Workspace workspace =
ContextBuilder.createWorkspace(resourceProvider, rootPath, this);
DartSdk sdk = findSdk(workspace.packageMap, options);
if (summaryData != null && sdk is SummaryBasedDartSdk) {
summaryData.addBundle(null, sdk.bundle);
}
return workspace.createSourceFactory(sdk, summaryData);
}
/**
* Add any [declaredVariables] to the list of declared variables used by the
* given [context].
*/
void declareVariables(AnalysisContextImpl context) {
Map<String, String> variables = builderOptions.declaredVariables;
if (variables != null && variables.isNotEmpty) {
context.declaredVariables = new DeclaredVariables.fromMap(variables);
}
}
/**
* Add any [declaredVariables] to the list of declared variables used by the
* given analysis [driver].
*/
void declareVariablesInDriver(AnalysisDriver driver) {
Map<String, String> variables = builderOptions.declaredVariables;
if (variables != null && variables.isNotEmpty) {
driver.declaredVariables = new DeclaredVariables.fromMap(variables);
}
}
/**
* Finds a package resolution strategy for the directory at the given absolute
* [path].
*
* This function first tries to locate a `.packages` file in the directory. If
* that is not found, it instead checks for the presence of a `packages/`
* directory in the same place. If that also fails, it starts checking parent
* directories for a `.packages` file, and stops if it finds it. Otherwise it
* gives up and returns [Packages.noPackages].
*/
Packages findPackagesFromFile(String path) {
Resource location = _findPackagesLocation(path);
if (location is File) {
List<int> fileBytes = location.readAsBytesSync();
Map<String, Uri> map;
try {
map =
parse(fileBytes, resourceProvider.pathContext.toUri(location.path));
} catch (exception) {
// If we cannot read the file, then we respond as if the file did not
// exist.
return Packages.noPackages;
}
resolveSymbolicLinks(map);
return new MapPackages(map);
} else if (location is Folder) {
return getPackagesFromFolder(location);
}
return Packages.noPackages;
}
/**
* Return the SDK that should be used to analyze code. Use the given
* [packageMap] and [analysisOptions] to locate the SDK.
*/
DartSdk findSdk(
Map<String, List<Folder>> packageMap, AnalysisOptions analysisOptions) {
String summaryPath = builderOptions.dartSdkSummaryPath;
if (summaryPath != null) {
return new SummaryBasedDartSdk(summaryPath, true,
resourceProvider: resourceProvider);
} else if (packageMap != null) {
SdkExtensionFinder extFinder = new SdkExtensionFinder(packageMap);
List<String> extFilePaths = extFinder.extensionFilePaths;
EmbedderYamlLocator locator = new EmbedderYamlLocator(packageMap);
Map<Folder, YamlMap> embedderYamls = locator.embedderYamls;
EmbedderSdk embedderSdk =
new EmbedderSdk(resourceProvider, embedderYamls);
if (embedderSdk.sdkLibraries.length > 0) {
//
// There is an embedder file that defines the content of the SDK and
// there might be an extension file that extends it.
//
List<String> paths = <String>[];
for (Folder folder in embedderYamls.keys) {
paths.add(folder
.getChildAssumingFile(EmbedderYamlLocator.EMBEDDER_FILE_NAME)
.path);
}
paths.addAll(extFilePaths);
SdkDescription description = new SdkDescription(paths, analysisOptions);
DartSdk dartSdk = sdkManager.getSdk(description, () {
if (extFilePaths.isNotEmpty) {
embedderSdk.addExtensions(extFinder.urlMappings);
}
embedderSdk.analysisOptions = analysisOptions;
embedderSdk.useSummary = sdkManager.canUseSummaries;
return embedderSdk;
});
return dartSdk;
} else if (extFilePaths != null && extFilePaths.isNotEmpty) {
//
// We have an extension file, but no embedder file.
//
String sdkPath = sdkManager.defaultSdkDirectory;
List<String> paths = <String>[sdkPath];
paths.addAll(extFilePaths);
SdkDescription description = new SdkDescription(paths, analysisOptions);
return sdkManager.getSdk(description, () {
FolderBasedDartSdk sdk = new FolderBasedDartSdk(
resourceProvider, resourceProvider.getFolder(sdkPath));
if (extFilePaths.isNotEmpty) {
sdk.addExtensions(extFinder.urlMappings);
}
sdk.analysisOptions = analysisOptions;
sdk.useSummary = sdkManager.canUseSummaries;
return sdk;
});
}
}
String sdkPath = sdkManager.defaultSdkDirectory;
SdkDescription description =
new SdkDescription(<String>[sdkPath], analysisOptions);
return sdkManager.getSdk(description, () {
FolderBasedDartSdk sdk = new FolderBasedDartSdk(
resourceProvider, resourceProvider.getFolder(sdkPath), true);
sdk.analysisOptions = analysisOptions;
sdk.useSummary = sdkManager.canUseSummaries;
return sdk;
});
}
/**
* Return the analysis options that should be used to analyze code in the
* directory with the given [path]. Use [verbosePrint] to echo verbose
* information about the analysis options selection process.
*/
AnalysisOptions getAnalysisOptions(String path,
{void verbosePrint(String text), ContextRoot contextRoot}) {
void verbose(String text) {
if (verbosePrint != null) {
verbosePrint(text);
}
}
// TODO(danrubel) restructure so that we don't create a workspace
// both here and in createSourceFactory
Workspace workspace =
ContextBuilder.createWorkspace(resourceProvider, path, this);
SourceFactory sourceFactory = workspace.createSourceFactory(null, null);
AnalysisOptionsProvider optionsProvider =
new AnalysisOptionsProvider(sourceFactory);
AnalysisOptionsImpl options = createDefaultOptions();
File optionsFile = getOptionsFile(path);
YamlMap optionMap;
if (optionsFile != null) {
try {
optionMap = optionsProvider.getOptionsFromFile(optionsFile);
if (contextRoot != null) {
contextRoot.optionsFilePath = optionsFile.path;
}
verbose('Loaded analysis options from ${optionsFile.path}');
} catch (e) {
// Ignore exceptions thrown while trying to load the options file.
verbose('Exception: $e\n when loading ${optionsFile.path}');
}
} else {
// Search for the default analysis options.
// TODO(danrubel) determine if bazel or gn project depends upon flutter
Source source;
if (workspace.hasFlutterDependency) {
source = sourceFactory.forUri(flutterAnalysisOptionsPath);
}
if (source == null || !source.exists()) {
source = sourceFactory.forUri(bazelAnalysisOptionsPath);
}
if (source != null && source.exists()) {
try {
optionMap = optionsProvider.getOptionsFromSource(source);
if (contextRoot != null) {
contextRoot.optionsFilePath = source.fullName;
}
verbose('Loaded analysis options from ${source.fullName}');
} catch (e) {
// Ignore exceptions thrown while trying to load the options file.
verbose('Exception: $e\n when loading ${source.fullName}');
}
}
}
if (optionMap != null) {
applyToAnalysisOptions(options, optionMap);
if (builderOptions.argResults != null) {
applyAnalysisOptionFlags(options, builderOptions.argResults,
verbosePrint: verbosePrint);
// If lints turned on but none specified, then enable default lints
if (options.lint && options.lintRules.isEmpty) {
options.lintRules = Registry.ruleRegistry.defaultRules;
verbose('Using default lint rules');
}
}
} else {
verbose('Using default analysis options');
}
var pubspecFile = _findPubspecFile(path);
if (pubspecFile != null) {
var extractor = new SdkConstraintExtractor(pubspecFile);
var sdkVersionConstraint = extractor.constraint();
if (sdkVersionConstraint != null) {
options.sdkVersionConstraint = sdkVersionConstraint;
}
}
return options;
}
/**
* Return the analysis options file that should be used when analyzing code in
* the directory with the given [path].
*
* If [forceSearch] is true, then don't return the default analysis options
* path. This allows cli to locate what *would* have been the analysis options
* file path, and super-impose the defaults over it in-place.
*/
File getOptionsFile(String path, {bool forceSearch: false}) {
if (!forceSearch) {
String filePath = builderOptions.defaultAnalysisOptionsFilePath;
if (filePath != null) {
return resourceProvider.getFile(filePath);
}
}
Folder root = resourceProvider.getFolder(path);
for (Folder folder = root; folder != null; folder = folder.parent) {
File file =
folder.getChildAssumingFile(AnalysisEngine.ANALYSIS_OPTIONS_FILE);
if (file.exists) {
return file;
}
file = folder
.getChildAssumingFile(AnalysisEngine.ANALYSIS_OPTIONS_YAML_FILE);
if (file.exists) {
return file;
}
}
return null;
}
/**
* Create a [Packages] object for a 'package' directory ([folder]).
*
* Package names are resolved as relative to sub-directories of the package
* directory.
*/
Packages getPackagesFromFolder(Folder folder) {
Context pathContext = resourceProvider.pathContext;
Map<String, Uri> map = new HashMap<String, Uri>();
for (Resource child in folder.getChildren()) {
if (child is Folder) {
// Inline resolveSymbolicLinks for performance reasons.
String packageName = pathContext.basename(child.path);
String folderPath = resolveSymbolicLink(child);
String uriPath = pathContext.join(folderPath, '.');
map[packageName] = pathContext.toUri(uriPath);
}
}
return new MapPackages(map);
}
/**
* Resolve any symbolic links encoded in the path to the given [folder].
*/
String resolveSymbolicLink(Folder folder) {
try {
return folder.resolveSymbolicLinksSync().path;
} on FileSystemException {
return folder.path;
}
}
/**
* Resolve any symbolic links encoded in the URI's in the given [map] by
* replacing the values in the map.
*/
void resolveSymbolicLinks(Map<String, Uri> map) {
Context pathContext = resourceProvider.pathContext;
for (String packageName in map.keys) {
var uri = map[packageName];
String path = fileUriToNormalizedPath(pathContext, uri);
Folder folder = resourceProvider.getFolder(path);
String folderPath = resolveSymbolicLink(folder);
// Add a '.' so that the URI is suitable for resolving relative URI's
// against it.
String uriPath = pathContext.join(folderPath, '.');
map[packageName] = pathContext.toUri(uriPath);
}
}
/**
* Find the location of the package resolution file/directory for the
* directory at the given absolute [path].
*
* Checks for a `.packages` file in the [path]. If not found,
* checks for a `packages` directory in the same directory. If still not
* found, starts checking parent directories for `.packages` until reaching
* the root directory.
*
* Return a [File] object representing a `.packages` file if one is found, a
* [Folder] object for the `packages/` directory if that is found, or `null`
* if neither is found.
*/
Resource _findPackagesLocation(String path) {
Folder folder = resourceProvider.getFolder(path);
if (!folder.exists) {
return null;
}
File checkForConfigFile(Folder folder) {
File file = folder.getChildAssumingFile('.packages');
if (file.exists) {
return file;
}
return null;
}
// Check for $cwd/.packages
File packagesCfgFile = checkForConfigFile(folder);
if (packagesCfgFile != null) {
return packagesCfgFile;
}
// Check for $cwd/packages/
Folder packagesDir = folder.getChildAssumingFolder("packages");
if (packagesDir.exists) {
return packagesDir;
}
// Check for cwd(/..)+/.packages
Folder parentDir = folder.parent;
while (parentDir != null) {
packagesCfgFile = checkForConfigFile(parentDir);
if (packagesCfgFile != null) {
return packagesCfgFile;
}
parentDir = parentDir.parent;
}
return null;
}
/**
* Return the `pubspec.yaml` file that should be used when analyzing code in
* the directory with the given [path], possibly `null`.
*/
File _findPubspecFile(String path) {
var resource = resourceProvider.getResource(path);
while (resource != null) {
if (resource is Folder) {
File pubspecFile = resource.getChildAssumingFile('pubspec.yaml');
if (pubspecFile.exists) {
return pubspecFile;
}
}
resource = resource.parent;
}
return null;
}
/**
* Return `true` if either the directory at [rootPath] or a parent of that
* directory contains a `.packages` file.
*/
static bool _hasPackageFileInPath(
ResourceProvider resourceProvider, String rootPath) {
Folder folder = resourceProvider.getFolder(rootPath);
while (folder != null) {
File file = folder.getChildAssumingFile('.packages');
if (file.exists) {
return true;
}
folder = folder.parent;
}
return false;
}
static Workspace createWorkspace(ResourceProvider resourceProvider,
String rootPath, ContextBuilder contextBuilder) {
if (_hasPackageFileInPath(resourceProvider, rootPath)) {
// A Bazel or Gn workspace that includes a '.packages' file is treated
// like a normal (non-Bazel/Gn) directory. But may still use
// package:build or Pub.
return PackageBuildWorkspace.find(
resourceProvider, rootPath, contextBuilder) ??
PubWorkspace.find(resourceProvider, rootPath, contextBuilder) ??
BasicWorkspace.find(resourceProvider, rootPath, contextBuilder);
}
Workspace workspace = BazelWorkspace.find(resourceProvider, rootPath);
workspace ??= GnWorkspace.find(resourceProvider, rootPath);
workspace ??=
PackageBuildWorkspace.find(resourceProvider, rootPath, contextBuilder);
workspace ??= PubWorkspace.find(resourceProvider, rootPath, contextBuilder);
return workspace ??
BasicWorkspace.find(resourceProvider, rootPath, contextBuilder);
}
}
/**
* Options used by a [ContextBuilder].
*/
class ContextBuilderOptions {
/**
* The results of parsing the command line arguments as defined by
* [defineAnalysisArguments] or `null` if none.
*/
ArgResults argResults;
/**
* The file path of the file containing the summary of the SDK that should be
* used to "analyze" the SDK. This option should only be specified by
* command-line tools such as 'dartanalyzer' or 'ddc'.
*/
String dartSdkSummaryPath;
/**
* The file path of the analysis options file that should be used in place of
* any file in the root directory or a parent of the root directory, or `null`
* if the normal lookup mechanism should be used.
*/
String defaultAnalysisOptionsFilePath;
/**
* A table mapping variable names to values for the declared variables, or
* `null` if no additional variables should be declared.
*/
Map<String, String> declaredVariables;
/**
* The default analysis options that should be used unless some or all of them
* are overridden in the analysis options file, or `null` if the default
* defaults should be used.
*/
AnalysisOptions defaultOptions;
/**
* The file path of the .packages file that should be used in place of any
* file found using the normal (Package Specification DEP) lookup mechanism,
* or `null` if the normal lookup mechanism should be used.
*/
String defaultPackageFilePath;
/**
* The file path of the packages directory that should be used in place of any
* file found using the normal (Package Specification DEP) lookup mechanism,
* or `null` if the normal lookup mechanism should be used.
*/
String defaultPackagesDirectoryPath;
/**
* A list of the paths of summary files that are to be used, or `null` if no
* summary information is available.
*/
List<String> librarySummaryPaths;
/**
* Initialize a newly created set of options
*/
ContextBuilderOptions();
}
/**
* Given a package map, check in each package's lib directory for the existence
* of an `_embedder.yaml` file. If the file contains a top level YamlMap, it
* will be added to the [embedderYamls] map.
*/
class EmbedderYamlLocator {
/**
* The name of the embedder files being searched for.
*/
static const String EMBEDDER_FILE_NAME = '_embedder.yaml';
/**
* A mapping from a package's library directory to the parsed YamlMap.
*/
final Map<Folder, YamlMap> embedderYamls = new HashMap<Folder, YamlMap>();
/**
* Initialize a newly created locator by processing the packages in the given
* [packageMap].
*/
EmbedderYamlLocator(Map<String, List<Folder>> packageMap) {
if (packageMap != null) {
_processPackageMap(packageMap);
}
}
/**
* Programmatically add an `_embedder.yaml` mapping.
*/
void addEmbedderYaml(Folder libDir, String embedderYaml) {
_processEmbedderYaml(libDir, embedderYaml);
}
/**
* Refresh the map of located files to those found by processing the given
* [packageMap].
*/
void refresh(Map<String, List<Folder>> packageMap) {
// Clear existing.
embedderYamls.clear();
if (packageMap != null) {
_processPackageMap(packageMap);
}
}
/**
* Given the yaml for an embedder ([embedderYaml]) and a folder ([libDir]),
* setup the uri mapping.
*/
void _processEmbedderYaml(Folder libDir, String embedderYaml) {
try {
YamlNode yaml = loadYaml(embedderYaml);
if (yaml is YamlMap) {
embedderYamls[libDir] = yaml;
}
} catch (_) {
// Ignored
}
}
/**
* Given a package [name] and a list of folders ([libDirs]), process any
* `_embedder.yaml` files that are found in any of the folders.
*/
void _processPackage(String name, List<Folder> libDirs) {
for (Folder libDir in libDirs) {
String embedderYaml = _readEmbedderYaml(libDir);
if (embedderYaml != null) {
_processEmbedderYaml(libDir, embedderYaml);
}
}
}
/**
* Process each of the entries in the [packageMap].
*/
void _processPackageMap(Map<String, List<Folder>> packageMap) {
packageMap.forEach(_processPackage);
}
/**
* Read and return the contents of [libDir]/[EMBEDDER_FILE_NAME], or `null` if
* the file doesn't exist.
*/
String _readEmbedderYaml(Folder libDir) {
File file = libDir.getChild(EMBEDDER_FILE_NAME);
try {
return file.readAsStringSync();
} on FileSystemException {
// File can't be read.
return null;
}
}
}