From 0f1b92747c1ae625fa9b61cad3f8f68442f0597d Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Fri, 6 Jan 2017 21:29:35 -0800 Subject: [PATCH 1/3] Drop unused methods from utils.dart 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/ --- lib/src/utils.dart | 147 ------------------------------- test/analysis_utils.dart | 111 +++++++++++++++++++++++ test/annotation_test.dart | 2 +- test/find_libraries_test.dart | 2 +- test/json_serializable_test.dart | 1 + test/utils_test.dart | 107 ---------------------- 6 files changed, 114 insertions(+), 256 deletions(-) create mode 100644 test/analysis_utils.dart delete mode 100644 test/utils_test.dart diff --git a/lib/src/utils.dart b/lib/src/utils.dart index f3b4a3a5..15e50618 100644 --- a/lib/src/utils.dart +++ b/lib/src/utils.dart @@ -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; @@ -76,60 +37,6 @@ String friendlyNameForElement(Element element) { return names.join(' '); } -/// [foundFiles] is the list of files to consider for the context. -Future getAnalysisContextForProjectPath( - String projectPath, List 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 getElementsFromLibraryElement(LibraryElement unit) sync* { @@ -141,40 +48,6 @@ Iterable getElementsFromLibraryElement(LibraryElement unit) sync* { } } -Set getLibraries( - AnalysisContext context, Iterable filePaths) { - return filePaths.fold(new Set(), (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 _getElements(CompilationUnitMember member) { if (member is TopLevelVariableDeclaration) { return member.variables.variables.map((v) => v.element); @@ -188,23 +61,3 @@ Iterable _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 _getLibraryElements( - List dartFiles, AnalysisContext context) => - dartFiles - .map((path) => _getLibraryElement(path, context)) - .where((lib) => lib != null); diff --git a/test/analysis_utils.dart b/test/analysis_utils.dart new file mode 100644 index 00000000..22743746 --- /dev/null +++ b/test/analysis_utils.dart @@ -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 getAnalysisContextForProjectPath( + String projectPath, List 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 _getLibraryElements( + List 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); +} diff --git a/test/annotation_test.dart b/test/annotation_test.dart index 48ecb815..4dd60e29 100644 --- a/test/annotation_test.dart +++ b/test/annotation_test.dart @@ -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'; diff --git a/test/find_libraries_test.dart b/test/find_libraries_test.dart index 55409056..9a8696f9 100644 --- a/test/find_libraries_test.dart +++ b/test/find_libraries_test.dart @@ -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'; diff --git a/test/json_serializable_test.dart b/test/json_serializable_test.dart index ec176ea5..fc29dd31 100644 --- a/test/json_serializable_test.dart +++ b/test/json_serializable_test.dart @@ -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'; diff --git a/test/utils_test.dart b/test/utils_test.dart deleted file mode 100644 index b3b76f27..00000000 --- a/test/utils_test.dart +++ /dev/null @@ -1,107 +0,0 @@ -// 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. - -@TestOn('!browser') -library source_gen.test.utils_test; - -import 'package:analyzer/src/generated/engine.dart'; - -import 'package:path/path.dart' as p; -import 'package:test/test.dart'; -import 'package:source_gen/src/utils.dart'; - -import 'src/io.dart'; -import 'test_utils.dart'; - -void main() { - group('libraries for files', () { - AnalysisContext ctx; - - setUp(() async { - if (ctx == null) { - var projectPath = getPackagePath(); - var foundFiles = await getDartFiles(projectPath, - searchList: [p.join(getPackagePath(), 'test', 'test_files')]); - - ctx = await getAnalysisContextForProjectPath(projectPath, foundFiles); - } - }); - - test('part -> library', () { - var libs = getLibraries(ctx, [_part1path]); - - expect(libs.single.name, _lib1Name); - }); - - test('library -> library', () { - var libs = getLibraries(ctx, [_lib1path]); - - expect(libs.single.name, _lib1Name); - }); - - test('part + library -> library', () { - var libs = getLibraries(ctx, [_part1path, _lib1path]); - - expect(libs.single.name, _lib1Name); - }); - - test('part2 + library -> library + library 2', () { - var libs = getLibraries(ctx, [_part2path, _lib1path]); - - expect(libs.map((lib) => lib.name).toList(), - unorderedEquals([_lib1Name, _lib2Name])); - }); - - test('part + library2 -> library + library 2', () { - var libs = getLibraries(ctx, [_part1path, _lib2path]); - - expect(libs.map((lib) => lib.name).toList(), - unorderedEquals([_lib1Name, _lib2Name])); - }); - - test('everything -> library + library 2', () { - var libs = - getLibraries(ctx, [_lib1path, _lib2path, _part1path, _part2path]); - - expect(libs.map((lib) => lib.name).toList(), - unorderedEquals([_lib1Name, _lib2Name])); - }); - }); - - group('find part of', () { - test("after comments", () { - var index = findPartOf('''// commant -//more comment -part of foo;'''); - expect(index, 'part of foo;'); - }); - - test('after comments and whitespace', () { - var index = findPartOf(''' - -// that two blank linse -// a comment line - -// another blank line -part of monkey; -class bar{}'''); - expect(index, 'part of monkey;\nclass bar{}'); - }); - - test("not there", () { - var index = findPartOf('//\n//\n\n'); - expect(index, null); - }); - }); -} - -final _testFilesLib = p.join(getPackagePath(), 'test', 'test_files'); - -final _lib1Name = 'source_gen.test.annotation_test.classes'; -final _lib2Name = 'source_gen.test.annotation_test.defs'; - -final _lib1path = p.join(_testFilesLib, 'annotated_classes.dart'); -final _part1path = p.join(_testFilesLib, 'annotated_classes_part.dart'); -final _lib2path = p.join(_testFilesLib, 'annotations.dart'); -final _part2path = p.join(_testFilesLib, 'annotation_part.dart'); From b0a6fb9b3965441158f8181aef0b7768ef266cdd Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Fri, 6 Jan 2017 21:43:10 -0800 Subject: [PATCH 2/3] Move cli_util to dev_dependencies --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 1395edd5..4c260eb3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 From 816992565f100401776d4b21bd1ade00406b546a Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Fri, 6 Jan 2017 21:43:33 -0800 Subject: [PATCH 3/3] Add changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b6bb8ba..203c24de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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.