Skip to content

Commit 1305f68

Browse files
authored
Drop unused methods from utils.dart (#118)
This file is not exported so there (hopefully) shouldn't be usage of any of these methods outside of this package. - Drop the methods that have no usage in this package - Move the methods used only from tests to a library under test/ - Move cli_util to dev_dependencies
1 parent d9b85d4 commit 1305f68

File tree

8 files changed

+123
-257
lines changed

8 files changed

+123
-257
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 0.5.3
2+
3+
* Internal cleanup
4+
* Drop some unused utility methods
5+
* Move cli_util to dev_dependencies
6+
* Avoid some deprecated analyzer apis
7+
* Syntax tweaks
8+
19
## 0.5.2
210

311
* Use library URIs (not names) to look up annotations in the mirror system.

lib/src/utils.dart

Lines changed: 0 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,8 @@
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';
114
import 'package:analyzer/dart/ast/ast.dart';
125
import '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

467
String 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.
13542
Iterable<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-
17851
Iterable<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);

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ environment:
88
dependencies:
99
analyzer: '>=0.28.0 <0.30.0'
1010
build: '>=0.2.1 <0.7.0'
11-
cli_util: ^0.0.1
1211
dart_style: '>=0.1.7 <0.3.0'
1312
path: ^1.3.2
1413
dev_dependencies:
1514
build_runner: ^0.1.0
1615
build_test: ^0.3.0
16+
cli_util: ^0.0.1
1717
collection: ^1.1.2
1818
mockito: '>=0.11.0 <2.0.0'
1919
test: ^0.12.3

test/analysis_utils.dart

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import 'dart:async';
2+
import 'dart:io';
3+
4+
import 'package:analyzer/dart/element/element.dart';
5+
import 'package:analyzer/file_system/file_system.dart' hide File;
6+
import 'package:analyzer/file_system/physical_file_system.dart';
7+
import 'package:analyzer/source/package_map_resolver.dart';
8+
import 'package:analyzer/source/pub_package_map_provider.dart';
9+
import 'package:analyzer/src/dart/sdk/sdk.dart' show FolderBasedDartSdk;
10+
import 'package:analyzer/src/generated/engine.dart';
11+
import 'package:analyzer/src/generated/java_io.dart';
12+
import 'package:analyzer/src/generated/sdk.dart' show DartSdk;
13+
import 'package:analyzer/src/generated/source.dart';
14+
import 'package:analyzer/src/generated/source_io.dart';
15+
import 'package:cli_util/cli_util.dart' as cli;
16+
import 'package:path/path.dart' as p;
17+
18+
/// [foundFiles] is the list of files to consider for the context.
19+
Future<AnalysisContext> getAnalysisContextForProjectPath(
20+
String projectPath, List<String> foundFiles) async {
21+
// TODO: fail more clearly if this...fails
22+
var sdkPath = cli.getSdkDir().path;
23+
24+
JavaSystemIO.setProperty("com.google.dart.sdk", sdkPath);
25+
var resourceProvider = PhysicalResourceProvider.INSTANCE;
26+
DartSdk sdk = new FolderBasedDartSdk(
27+
resourceProvider, resourceProvider.getFolder(sdkPath));
28+
29+
var packageResolver = _getPackageResolver(projectPath, sdk);
30+
31+
var resolvers = [
32+
new DartUriResolver(sdk),
33+
new ResourceUriResolver(PhysicalResourceProvider.INSTANCE),
34+
packageResolver
35+
];
36+
37+
AnalysisEngine.instance.processRequiredPlugins();
38+
39+
var options = new AnalysisOptionsImpl()..analyzeFunctionBodies = false;
40+
41+
var context = AnalysisEngine.instance.createAnalysisContext()
42+
..analysisOptions = options
43+
..sourceFactory = new SourceFactory(resolvers);
44+
45+
// ensures all libraries defined by the set of files are resolved
46+
_getLibraryElements(foundFiles, context).toList();
47+
48+
return context;
49+
}
50+
51+
UriResolver _getPackageResolver(String projectPath, DartSdk sdk) {
52+
var dotPackagesPath = p.join(projectPath, '.packages');
53+
54+
if (!FileSystemEntity.isFileSync(dotPackagesPath)) {
55+
throw new StateError('A package configuration file was not found at the '
56+
'expectetd location. $dotPackagesPath');
57+
}
58+
59+
var pubPackageMapProvider =
60+
new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk);
61+
var packageMapInfo = pubPackageMapProvider
62+
.computePackageMap(PhysicalResourceProvider.INSTANCE.getResource('.'));
63+
var packageMap = packageMapInfo.packageMap;
64+
if (packageMap == null) {
65+
throw new StateError('An error occurred getting the package map.');
66+
}
67+
68+
return new PackageMapUriResolver(
69+
PhysicalResourceProvider.INSTANCE, packageMap);
70+
}
71+
72+
LibraryElement getLibraryElementForSourceFile(
73+
AnalysisContext context, String sourcePath) {
74+
Source source = new FileBasedSource(new JavaFile(sourcePath));
75+
76+
var libs = context.getLibrariesContaining(source);
77+
78+
if (libs.length > 1) {
79+
throw new Exception("We don't support multiple libraries for a source.");
80+
}
81+
82+
if (libs.isEmpty) {
83+
return null;
84+
}
85+
86+
var libSource = libs.single;
87+
88+
// using `getLibraryElement` because the library should already be computed
89+
// If not, it's a bug in usage
90+
return context.getLibraryElement(libSource);
91+
}
92+
93+
// may return `null` if [path] doesn't refer to a library.
94+
/// [dartFiles] is a [Stream] of paths to [.dart] files.
95+
Iterable<LibraryElement> _getLibraryElements(
96+
List<String> dartFiles, AnalysisContext context) =>
97+
dartFiles
98+
.map((path) => _getLibraryElement(path, context))
99+
.where((lib) => lib != null);
100+
101+
LibraryElement _getLibraryElement(String path, AnalysisContext context) {
102+
Source source = new FileBasedSource(new JavaFile(path));
103+
if (context.computeKindOf(source) == SourceKind.LIBRARY) {
104+
return context.computeLibraryElement(source);
105+
}
106+
return null;
107+
}
108+
109+
String getFileBasedSourcePath(FileBasedSource source) {
110+
return p.fromUri(source.uri);
111+
}

test/annotation_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import 'package:mockito/mockito.dart';
1717
import 'package:path/path.dart' as p;
1818
import 'package:source_gen/generators/json_serializable.dart';
1919
import 'package:source_gen/src/annotation.dart';
20-
import 'package:source_gen/src/utils.dart';
2120
import 'package:test/test.dart';
2221

22+
import 'analysis_utils.dart';
2323
import 'src/io.dart';
2424
import 'test_files/annotations.dart' as defs;
2525
import 'test_utils.dart';

test/find_libraries_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ library source_gen.test.find_libraries;
77

88
import 'package:analyzer/src/generated/engine.dart';
99
import 'package:path/path.dart' as p;
10-
import 'package:source_gen/src/utils.dart';
1110
import 'package:test/test.dart';
1211

12+
import 'analysis_utils.dart';
1313
import 'src/io.dart';
1414
import 'test_utils.dart';
1515

test/json_serializable_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'package:source_gen/generators/json_serializable_generator.dart';
1717
import 'package:source_gen/src/utils.dart';
1818
import 'package:test/test.dart';
1919

20+
import 'analysis_utils.dart';
2021
import 'src/io.dart';
2122
import 'test_utils.dart';
2223

0 commit comments

Comments
 (0)