diff --git a/.travis.yml b/.travis.yml index ed3a1ccfae..9e9e74595f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: dart sudo: false -dart: +dart: - dev - - stable +# - stable env: - GEN_SDK_DOCS=true - GEN_SDK_DOCS=false diff --git a/lib/dartdoc.dart b/lib/dartdoc.dart index c0e132e954..e07401c097 100644 --- a/lib/dartdoc.dart +++ b/lib/dartdoc.dart @@ -18,7 +18,6 @@ import 'package:analyzer/src/generated/source.dart'; import 'package:analyzer/src/generated/source_io.dart'; import 'generator.dart'; -import 'resource_loader.dart' as loader; import 'src/html_generator.dart'; import 'src/io_utils.dart'; import 'src/model.dart'; @@ -70,8 +69,6 @@ class DartDoc { Future generateDocs() async { _stopwatch = new Stopwatch()..start(); - if (packageRootDir != null) loader.packageRootPath = packageRootDir.path; - List files = packageMeta.isSdk ? [] : findFilesToDocumentInPackage(rootDir.path); @@ -151,7 +148,7 @@ class DartDoc { if (name.startsWith(Platform.pathSeparator)) name = name.substring(1); } print('parsing ${name}...'); - Source source = new FileBasedSource.con1(new JavaFile(filePath)); + Source source = new FileBasedSource(new JavaFile(filePath)); sources.add(source); if (context.computeKindOf(source) == SourceKind.LIBRARY) { LibraryElement library = context.computeLibraryElement(source); diff --git a/lib/resource_loader.dart b/lib/resource_loader.dart index 74814875cf..079068bb55 100644 --- a/lib/resource_loader.dart +++ b/lib/resource_loader.dart @@ -2,8 +2,6 @@ // 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. -// TODO: Consider making this a stand-alone package, if useful. - /// Make it possible to load resources, independent of how the Dart app is run. /// /// Future getTemplateFile(String templatePath) { @@ -12,163 +10,20 @@ /// library dartdoc.resource_loader; -import 'dart:async' show Future; -import 'dart:io' show Platform, File, Directory; -import 'dart:typed_data'; - -import 'package:http/http.dart' as http; -import 'package:path/path.dart' as p; -import 'package:pub_cache/pub_cache.dart'; - -/// Optional package root path. -String packageRootPath; +import 'dart:async'; /// Loads a `package:` resource as a String. -Future loadAsString(String path) async { +Future loadAsString(String path) { if (!path.startsWith('package:')) { throw new ArgumentError('path must begin with package:'); } - Uint8List bytes = await _doLoad(path); - return new String.fromCharCodes(bytes); + return new Resource(path).readAsString(); } -/// Loads a `package:` resource as an [Uint8List]. -Future loadAsBytes(String path) { +/// Loads a `package:` resource as a [List]. +Future> loadAsBytes(String path) { if (!path.startsWith('package:')) { throw new ArgumentError('path must begin with package:'); } - return _doLoad(path); -} - -/// Determine how to do the load. HTTP? Snapshotted? From source? -Future _doLoad(final String path) { - var scriptUri = Platform.script; - if (scriptUri.toString().startsWith('http')) { - return _doLoadOverHttp(path); - } else if (scriptUri.toString().endsWith('.snapshot')) { - return _doLoadWhenSnapshot(path); - } else if (packageRootPath != null) { - return _doLoadFromPackageRoot(path); - } else { - return _doLoadFromFileFromPackagesDir(path); - } -} - -Future _doLoadWhenSnapshot(final String resourcePath) { - var scriptFilePath = Platform.script.toFilePath(); - // Check if we're running as a pub globally installed package - // Jump back out to where our source is - var cacheDirPath = PubCache.getSystemCacheLocation().path; - if (scriptFilePath.startsWith(cacheDirPath)) { - // find packages installed with pub - var appName = _appNameWhenGloballyInstalled(); - var installedApplication = new PubCache() - .getGlobalApplications() - .firstWhere((app) => app.name == appName); - if (installedApplication == null) { - throw new StateError( - 'Could not find globally installed app $appName. Are you running as a snapshot from the global_packages directory?'); - } - var resourcePackageName = _packageNameForResource(resourcePath); - var resourcePackageRef = installedApplication - .getPackageRefs() - .firstWhere((ref) => ref.name == resourcePackageName); - if (resourcePackageRef == null) { - throw new StateError( - 'Could not find package dependency for $resourcePackageName'); - } - var resourcePackage = resourcePackageRef.resolve(); - var resourcePackageDir = resourcePackage.location; - var fullPath = resourcePackageDir.path; - return _doLoadOverFileFromLocation(resourcePath, p.join(fullPath, "lib")); - } else { - // maybe we're a snapshot next to a packages/ dir? - return _doLoadFromFileFromPackagesDir(resourcePath); - } -} - -Future _doLoadOverHttp(final String resourcePath) { - var scriptUri = Platform.script; - var convertedResourcePath = _convertPackageSchemeToPackagesDir(resourcePath); - // strip file name from script uri, append path to resource - var segmentsToResource = scriptUri.pathSegments - .sublist(0, scriptUri.pathSegments.length - 1) - ..addAll(p.split(convertedResourcePath)); - var fullPath = scriptUri.replace(pathSegments: segmentsToResource); - - return http.readBytes(fullPath); -} - -Future _doLoadOverFileFromLocation( - final String resourcePath, final String baseDir) { - var convertedPath = _convertPackageSchemeToPackagesDir(resourcePath); - // remove 'packages' and package name - var pathInsideLib = p.split(convertedPath).sublist(2); - // put the baseDir in front - pathInsideLib.insert(0, baseDir); - // put it all back together - var fullPath = p.joinAll(pathInsideLib); - return _readFile(resourcePath, fullPath); -} - -/// First, try a packages/ dir next to the entry point (Platform.script). -/// If that doesn't exist, try a packages/ dir inside of the current -/// working directory. -Future _doLoadFromFileFromPackagesDir(final String resourcePath) { - var convertedPath = _convertPackageSchemeToPackagesDir(resourcePath); - var scriptFile = new File(Platform.script.toFilePath()); - String baseDir = p.dirname(scriptFile.path); - - if (!new Directory(p.join(baseDir, 'packages')).existsSync()) { - // try CWD - baseDir = Directory.current.path; - } - - var fullPath = p.join(baseDir, convertedPath); - return _readFile(resourcePath, fullPath); -} - -Future _doLoadFromPackageRoot(final String resourcePath) { - var withoutScheme = _removePackageScheme(resourcePath); - var fullPath = p.join(packageRootPath, withoutScheme); - return _readFile(resourcePath, fullPath); -} - -Future _readFile( - final String resourcePath, final String fullPath) async { - var file = new File(fullPath); - if (!file.existsSync()) { - throw new ArgumentError('$resourcePath does not exist, tried $fullPath'); - } - var bytes = await file.readAsBytes(); - return new Uint8List.fromList(bytes); -} - -String _convertPackageSchemeToPackagesDir(String resourcePath) { - var withoutScheme = _removePackageScheme(resourcePath); - return p.join('packages', withoutScheme); -} - -String _removePackageScheme(final String resourcePath) { - return resourcePath.substring('package:'.length, resourcePath.length); -} - -/// Tries to determine the app name, which is the same as the directory -/// name when globally installed. -/// -/// Only call this if your app is globally installed. -String _appNameWhenGloballyInstalled() { - var parts = p.split(Platform.script.toFilePath()); - var marker = parts.indexOf('global_packages'); - if (marker < 0) { - throw new StateError( - '${Platform.script.toFilePath()} does not include global_packages'); - } - return parts[marker + 1]; -} - -String _packageNameForResource(final String resourcePath) { - var parts = p.split(_convertPackageSchemeToPackagesDir(resourcePath)); - // first part is 'packages', second part is the package name - return parts[1]; + return new Resource(path).readAsBytes(); } diff --git a/pubspec.lock b/pubspec.lock index ad21c4e703..10fb03bc96 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -4,7 +4,7 @@ packages: analyzer: description: analyzer source: hosted - version: "0.25.2" + version: "0.26.0" ansicolor: description: ansicolor source: hosted @@ -88,7 +88,7 @@ packages: markdown: description: markdown source: hosted - version: "0.7.1+2" + version: "0.7.1+3" matcher: description: matcher source: hosted @@ -125,10 +125,6 @@ packages: description: pool source: hosted version: "1.1.0" - pub_cache: - description: pub_cache - source: hosted - version: "0.1.0" pub_package_data: description: pub_package_data source: hosted @@ -148,7 +144,7 @@ packages: shelf_static: description: shelf_static source: hosted - version: "0.2.2" + version: "0.2.3" shelf_web_socket: description: shelf_web_socket source: hosted @@ -180,7 +176,7 @@ packages: test: description: test source: hosted - version: "0.12.3+8" + version: "0.12.3+9" unscripted: description: unscripted source: hosted diff --git a/pubspec.yaml b/pubspec.yaml index 68f192e635..d541ba853f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,9 +5,9 @@ author: Dart Team description: A documentation generator for Dart. homepage: https://github.com/dart-lang/dartdoc environment: - sdk: '>=1.9.0 <2.0.0' # when we go to 1.12, bump analyzer version + sdk: '>=1.12.0-dev.5.0 <2.0.0' dependencies: - analyzer: ^0.25.0 + analyzer: ^0.26.0 args: ^0.13.0 cli_util: ^0.0.1 html: ^0.12.1 @@ -16,7 +16,6 @@ dependencies: markdown: ^0.7.1 mustache4dart: ^1.0.9 path: ^1.3.0 - pub_cache: ^0.1.0 quiver: '>=0.18.0 <0.22.0' yaml: ^2.1.0 dev_dependencies: diff --git a/test/resource_loader_test.dart b/test/resource_loader_test.dart index 916b575699..c977c3d3cb 100644 --- a/test/resource_loader_test.dart +++ b/test/resource_loader_test.dart @@ -4,9 +4,6 @@ library dartdoc.resource_loader_test; -import 'dart:io'; - -import 'package:path/path.dart'; import 'package:dartdoc/resource_loader.dart' as loader; import 'package:test/test.dart'; @@ -17,19 +14,5 @@ void main() { await loader.loadAsString('package:dartdoc/templates/index.html'); expect(contents, isNotNull); }); - - test('package root setting', () { - var root = Directory.current; - loader.packageRootPath = root.path; - expect(loader.packageRootPath, equals(root.path)); - }); - - test('load from packages with package root setting', () async { - var root = Directory.current; - loader.packageRootPath = join(root.path, 'packages'); - var contents = - await loader.loadAsString('package:dartdoc/templates/index.html'); - expect(contents, isNotNull); - }); }); } diff --git a/test/test_utils.dart b/test/test_utils.dart index 2484a0f8b8..a8ed652448 100644 --- a/test/test_utils.dart +++ b/test/test_utils.dart @@ -65,7 +65,7 @@ class AnalyzerHelper { } Source addSource(String filePath) { - Source source = new FileBasedSource.con1(new JavaFile(filePath)); + Source source = new FileBasedSource(new JavaFile(filePath)); ChangeSet changeSet = new ChangeSet(); changeSet.addedSource(source); context.applyChanges(changeSet);