Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.5.3

* Internal cleanup
* Drop some unused utility methods
* Move cli_util to dev_dependencies
* Avoid some deprecated analyzer apis
* Syntax tweaks

## 0.5.2

* Use library URIs (not names) to look up annotations in the mirror system.
Expand Down
147 changes: 0 additions & 147 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -1,47 +1,8 @@
// Copyright (c) 2015, 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.

library source_gen.utils;

import 'dart:async';
import 'dart:io';

import 'package:analyzer/analyzer.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/file_system/file_system.dart' hide File;
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer/source/package_map_provider.dart';
import 'package:analyzer/source/package_map_resolver.dart';
import 'package:analyzer/source/pub_package_map_provider.dart';
import 'package:analyzer/src/dart/sdk/sdk.dart' show FolderBasedDartSdk;
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/java_io.dart';
import 'package:analyzer/src/generated/sdk.dart' show DartSdk;
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:cli_util/cli_util.dart' as cli;
import 'package:path/path.dart' as p;

String findPartOf(String source) {
try {
var unit = parseCompilationUnit(source);

var partOf = unit.directives
.firstWhere((d) => d is PartOfDirective, orElse: () => null);

if (partOf == null) {
return null;
}

var offset = partOf.offset;

return source.substring(offset);
} on AnalyzerErrorGroup {
return null;
}
}

String friendlyNameForElement(Element element) {
var friendlyName = element.displayName;
Expand Down Expand Up @@ -76,60 +37,6 @@ String friendlyNameForElement(Element element) {
return names.join(' ');
}

/// [foundFiles] is the list of files to consider for the context.
Future<AnalysisContext> getAnalysisContextForProjectPath(
String projectPath, List<String> foundFiles) async {
// TODO: fail more clearly if this...fails
var sdkPath = cli.getSdkDir().path;

JavaSystemIO.setProperty("com.google.dart.sdk", sdkPath);
var resourceProvider = PhysicalResourceProvider.INSTANCE;
DartSdk sdk = new FolderBasedDartSdk(
resourceProvider, resourceProvider.getFolder(sdkPath));

var packageResolver = _getPackageResolver(projectPath, sdk);

var resolvers = [
new DartUriResolver(sdk),
new ResourceUriResolver(PhysicalResourceProvider.INSTANCE),
packageResolver
];

AnalysisEngine.instance.processRequiredPlugins();

var options = new AnalysisOptionsImpl()..analyzeFunctionBodies = false;

var context = AnalysisEngine.instance.createAnalysisContext()
..analysisOptions = options
..sourceFactory = new SourceFactory(resolvers);

// ensures all libraries defined by the set of files are resolved
_getLibraryElements(foundFiles, context).toList();

return context;
}

UriResolver _getPackageResolver(String projectPath, DartSdk sdk) {
var dotPackagesPath = p.join(projectPath, '.packages');

if (!FileSystemEntity.isFileSync(dotPackagesPath)) {
throw new StateError('A package configuration file was not found at the '
'expectetd location. $dotPackagesPath');
}

var pubPackageMapProvider =
new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk);
var packageMapInfo = pubPackageMapProvider
.computePackageMap(PhysicalResourceProvider.INSTANCE.getResource('.'));
var packageMap = packageMapInfo.packageMap;
if (packageMap == null) {
throw new StateError('An error occurred getting the package map.');
}

return new PackageMapUriResolver(
PhysicalResourceProvider.INSTANCE, packageMap);
}

/// Returns all of the declarations in [unit], including [unit] as the first
/// item.
Iterable<Element> getElementsFromLibraryElement(LibraryElement unit) sync* {
Expand All @@ -141,40 +48,6 @@ Iterable<Element> getElementsFromLibraryElement(LibraryElement unit) sync* {
}
}

Set<LibraryElement> getLibraries(
AnalysisContext context, Iterable<String> filePaths) {
return filePaths.fold(new Set<LibraryElement>(), (set, path) {
var elementLibrary = getLibraryElementForSourceFile(context, path);

if (elementLibrary != null) {
set.add(elementLibrary);
}

return set;
});
}

LibraryElement getLibraryElementForSourceFile(
AnalysisContext context, String sourcePath) {
Source source = new FileBasedSource(new JavaFile(sourcePath));

var libs = context.getLibrariesContaining(source);

if (libs.length > 1) {
throw new Exception("We don't support multiple libraries for a source.");
}

if (libs.isEmpty) {
return null;
}

var libSource = libs.single;

// using `getLibraryElement` because the library should already be computed
// If not, it's a bug in usage
return context.getLibraryElement(libSource);
}

Iterable<Element> _getElements(CompilationUnitMember member) {
if (member is TopLevelVariableDeclaration) {
return member.variables.variables.map((v) => v.element);
Expand All @@ -188,23 +61,3 @@ Iterable<Element> _getElements(CompilationUnitMember member) {

return [element];
}

LibraryElement _getLibraryElement(String path, AnalysisContext context) {
Source source = new FileBasedSource(new JavaFile(path));
if (context.computeKindOf(source) == SourceKind.LIBRARY) {
return context.computeLibraryElement(source);
}
return null;
}

String getFileBasedSourcePath(FileBasedSource source) {
return p.fromUri(source.uri);
}

// may return `null` if [path] doesn't refer to a library.
/// [dartFiles] is a [Stream] of paths to [.dart] files.
Iterable<LibraryElement> _getLibraryElements(
List<String> dartFiles, AnalysisContext context) =>
dartFiles
.map((path) => _getLibraryElement(path, context))
.where((lib) => lib != null);
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ environment:
dependencies:
analyzer: '>=0.28.0 <0.30.0'
build: '>=0.2.1 <0.7.0'
cli_util: ^0.0.1
dart_style: '>=0.1.7 <0.3.0'
path: ^1.3.2
dev_dependencies:
build_runner: ^0.1.0
build_test: ^0.3.0
cli_util: ^0.0.1
collection: ^1.1.2
mockito: '>=0.11.0 <2.0.0'
test: ^0.12.3
111 changes: 111 additions & 0 deletions test/analysis_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import 'dart:async';
import 'dart:io';

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/file_system/file_system.dart' hide File;
import 'package:analyzer/file_system/physical_file_system.dart';
import 'package:analyzer/source/package_map_resolver.dart';
import 'package:analyzer/source/pub_package_map_provider.dart';
import 'package:analyzer/src/dart/sdk/sdk.dart' show FolderBasedDartSdk;
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/java_io.dart';
import 'package:analyzer/src/generated/sdk.dart' show DartSdk;
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:cli_util/cli_util.dart' as cli;
import 'package:path/path.dart' as p;

/// [foundFiles] is the list of files to consider for the context.
Future<AnalysisContext> getAnalysisContextForProjectPath(
String projectPath, List<String> foundFiles) async {
// TODO: fail more clearly if this...fails
var sdkPath = cli.getSdkDir().path;

JavaSystemIO.setProperty("com.google.dart.sdk", sdkPath);
var resourceProvider = PhysicalResourceProvider.INSTANCE;
DartSdk sdk = new FolderBasedDartSdk(
resourceProvider, resourceProvider.getFolder(sdkPath));

var packageResolver = _getPackageResolver(projectPath, sdk);

var resolvers = [
new DartUriResolver(sdk),
new ResourceUriResolver(PhysicalResourceProvider.INSTANCE),
packageResolver
];

AnalysisEngine.instance.processRequiredPlugins();

var options = new AnalysisOptionsImpl()..analyzeFunctionBodies = false;

var context = AnalysisEngine.instance.createAnalysisContext()
..analysisOptions = options
..sourceFactory = new SourceFactory(resolvers);

// ensures all libraries defined by the set of files are resolved
_getLibraryElements(foundFiles, context).toList();

return context;
}

UriResolver _getPackageResolver(String projectPath, DartSdk sdk) {
var dotPackagesPath = p.join(projectPath, '.packages');

if (!FileSystemEntity.isFileSync(dotPackagesPath)) {
throw new StateError('A package configuration file was not found at the '
'expectetd location. $dotPackagesPath');
}

var pubPackageMapProvider =
new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk);
var packageMapInfo = pubPackageMapProvider
.computePackageMap(PhysicalResourceProvider.INSTANCE.getResource('.'));
var packageMap = packageMapInfo.packageMap;
if (packageMap == null) {
throw new StateError('An error occurred getting the package map.');
}

return new PackageMapUriResolver(
PhysicalResourceProvider.INSTANCE, packageMap);
}

LibraryElement getLibraryElementForSourceFile(
AnalysisContext context, String sourcePath) {
Source source = new FileBasedSource(new JavaFile(sourcePath));

var libs = context.getLibrariesContaining(source);

if (libs.length > 1) {
throw new Exception("We don't support multiple libraries for a source.");
}

if (libs.isEmpty) {
return null;
}

var libSource = libs.single;

// using `getLibraryElement` because the library should already be computed
// If not, it's a bug in usage
return context.getLibraryElement(libSource);
}

// may return `null` if [path] doesn't refer to a library.
/// [dartFiles] is a [Stream] of paths to [.dart] files.
Iterable<LibraryElement> _getLibraryElements(
List<String> dartFiles, AnalysisContext context) =>
dartFiles
.map((path) => _getLibraryElement(path, context))
.where((lib) => lib != null);

LibraryElement _getLibraryElement(String path, AnalysisContext context) {
Source source = new FileBasedSource(new JavaFile(path));
if (context.computeKindOf(source) == SourceKind.LIBRARY) {
return context.computeLibraryElement(source);
}
return null;
}

String getFileBasedSourcePath(FileBasedSource source) {
return p.fromUri(source.uri);
}
2 changes: 1 addition & 1 deletion test/annotation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import 'package:mockito/mockito.dart';
import 'package:path/path.dart' as p;
import 'package:source_gen/generators/json_serializable.dart';
import 'package:source_gen/src/annotation.dart';
import 'package:source_gen/src/utils.dart';
import 'package:test/test.dart';

import 'analysis_utils.dart';
import 'src/io.dart';
import 'test_files/annotations.dart' as defs;
import 'test_utils.dart';
Expand Down
2 changes: 1 addition & 1 deletion test/find_libraries_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ library source_gen.test.find_libraries;

import 'package:analyzer/src/generated/engine.dart';
import 'package:path/path.dart' as p;
import 'package:source_gen/src/utils.dart';
import 'package:test/test.dart';

import 'analysis_utils.dart';
import 'src/io.dart';
import 'test_utils.dart';

Expand Down
1 change: 1 addition & 0 deletions test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:source_gen/generators/json_serializable_generator.dart';
import 'package:source_gen/src/utils.dart';
import 'package:test/test.dart';

import 'analysis_utils.dart';
import 'src/io.dart';
import 'test_utils.dart';

Expand Down
Loading