11// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
4-
5- library source_gen.utils;
6-
7- import 'dart:async' ;
8- import 'dart:io' ;
9-
10- import 'package:analyzer/analyzer.dart' ;
114import 'package:analyzer/dart/ast/ast.dart' ;
125import 'package:analyzer/dart/element/element.dart' ;
13- import 'package:analyzer/file_system/file_system.dart' hide File;
14- import 'package:analyzer/file_system/physical_file_system.dart' ;
15- import 'package:analyzer/source/package_map_provider.dart' ;
16- import 'package:analyzer/source/package_map_resolver.dart' ;
17- import 'package:analyzer/source/pub_package_map_provider.dart' ;
18- import 'package:analyzer/src/dart/sdk/sdk.dart' show FolderBasedDartSdk;
19- import 'package:analyzer/src/generated/engine.dart' ;
20- import 'package:analyzer/src/generated/java_io.dart' ;
21- import 'package:analyzer/src/generated/sdk.dart' show DartSdk;
22- import 'package:analyzer/src/generated/source.dart' ;
23- import 'package:analyzer/src/generated/source_io.dart' ;
24- import 'package:cli_util/cli_util.dart' as cli;
25- import 'package:path/path.dart' as p;
26-
27- String findPartOf (String source) {
28- try {
29- var unit = parseCompilationUnit (source);
30-
31- var partOf = unit.directives
32- .firstWhere ((d) => d is PartOfDirective , orElse: () => null );
33-
34- if (partOf == null ) {
35- return null ;
36- }
37-
38- var offset = partOf.offset;
39-
40- return source.substring (offset);
41- } on AnalyzerErrorGroup {
42- return null ;
43- }
44- }
456
467String friendlyNameForElement (Element element) {
478 var friendlyName = element.displayName;
@@ -76,60 +37,6 @@ String friendlyNameForElement(Element element) {
7637 return names.join (' ' );
7738}
7839
79- /// [foundFiles] is the list of files to consider for the context.
80- Future <AnalysisContext > getAnalysisContextForProjectPath (
81- String projectPath, List <String > foundFiles) async {
82- // TODO: fail more clearly if this...fails
83- var sdkPath = cli.getSdkDir ().path;
84-
85- JavaSystemIO .setProperty ("com.google.dart.sdk" , sdkPath);
86- var resourceProvider = PhysicalResourceProvider .INSTANCE ;
87- DartSdk sdk = new FolderBasedDartSdk (
88- resourceProvider, resourceProvider.getFolder (sdkPath));
89-
90- var packageResolver = _getPackageResolver (projectPath, sdk);
91-
92- var resolvers = [
93- new DartUriResolver (sdk),
94- new ResourceUriResolver (PhysicalResourceProvider .INSTANCE ),
95- packageResolver
96- ];
97-
98- AnalysisEngine .instance.processRequiredPlugins ();
99-
100- var options = new AnalysisOptionsImpl ()..analyzeFunctionBodies = false ;
101-
102- var context = AnalysisEngine .instance.createAnalysisContext ()
103- ..analysisOptions = options
104- ..sourceFactory = new SourceFactory (resolvers);
105-
106- // ensures all libraries defined by the set of files are resolved
107- _getLibraryElements (foundFiles, context).toList ();
108-
109- return context;
110- }
111-
112- UriResolver _getPackageResolver (String projectPath, DartSdk sdk) {
113- var dotPackagesPath = p.join (projectPath, '.packages' );
114-
115- if (! FileSystemEntity .isFileSync (dotPackagesPath)) {
116- throw new StateError ('A package configuration file was not found at the '
117- 'expectetd location. $dotPackagesPath ' );
118- }
119-
120- var pubPackageMapProvider =
121- new PubPackageMapProvider (PhysicalResourceProvider .INSTANCE , sdk);
122- var packageMapInfo = pubPackageMapProvider
123- .computePackageMap (PhysicalResourceProvider .INSTANCE .getResource ('.' ));
124- var packageMap = packageMapInfo.packageMap;
125- if (packageMap == null ) {
126- throw new StateError ('An error occurred getting the package map.' );
127- }
128-
129- return new PackageMapUriResolver (
130- PhysicalResourceProvider .INSTANCE , packageMap);
131- }
132-
13340/// Returns all of the declarations in [unit] , including [unit] as the first
13441/// item.
13542Iterable <Element > getElementsFromLibraryElement (LibraryElement unit) sync * {
@@ -141,40 +48,6 @@ Iterable<Element> getElementsFromLibraryElement(LibraryElement unit) sync* {
14148 }
14249}
14350
144- Set <LibraryElement > getLibraries (
145- AnalysisContext context, Iterable <String > filePaths) {
146- return filePaths.fold (new Set <LibraryElement >(), (set , path) {
147- var elementLibrary = getLibraryElementForSourceFile (context, path);
148-
149- if (elementLibrary != null ) {
150- set .add (elementLibrary);
151- }
152-
153- return set ;
154- });
155- }
156-
157- LibraryElement getLibraryElementForSourceFile (
158- AnalysisContext context, String sourcePath) {
159- Source source = new FileBasedSource (new JavaFile (sourcePath));
160-
161- var libs = context.getLibrariesContaining (source);
162-
163- if (libs.length > 1 ) {
164- throw new Exception ("We don't support multiple libraries for a source." );
165- }
166-
167- if (libs.isEmpty) {
168- return null ;
169- }
170-
171- var libSource = libs.single;
172-
173- // using `getLibraryElement` because the library should already be computed
174- // If not, it's a bug in usage
175- return context.getLibraryElement (libSource);
176- }
177-
17851Iterable <Element > _getElements (CompilationUnitMember member) {
17952 if (member is TopLevelVariableDeclaration ) {
18053 return member.variables.variables.map ((v) => v.element);
@@ -188,23 +61,3 @@ Iterable<Element> _getElements(CompilationUnitMember member) {
18861
18962 return [element];
19063}
191-
192- LibraryElement _getLibraryElement (String path, AnalysisContext context) {
193- Source source = new FileBasedSource (new JavaFile (path));
194- if (context.computeKindOf (source) == SourceKind .LIBRARY ) {
195- return context.computeLibraryElement (source);
196- }
197- return null ;
198- }
199-
200- String getFileBasedSourcePath (FileBasedSource source) {
201- return p.fromUri (source.uri);
202- }
203-
204- // may return `null` if [path] doesn't refer to a library.
205- /// [dartFiles] is a [Stream] of paths to [.dart] files.
206- Iterable <LibraryElement > _getLibraryElements (
207- List <String > dartFiles, AnalysisContext context) =>
208- dartFiles
209- .map ((path) => _getLibraryElement (path, context))
210- .where ((lib) => lib != null );
0 commit comments