Skip to content

switch to the new Resources api #769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: dart
sudo: false
dart:
dart:
- dev
- stable
# - stable
env:
- GEN_SDK_DOCS=true
- GEN_SDK_DOCS=false
Expand Down
5 changes: 1 addition & 4 deletions lib/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -70,8 +69,6 @@ class DartDoc {
Future<DartDocResults> generateDocs() async {
_stopwatch = new Stopwatch()..start();

if (packageRootDir != null) loader.packageRootPath = packageRootDir.path;

List<String> files =
packageMeta.isSdk ? [] : findFilesToDocumentInPackage(rootDir.path);

Expand Down Expand Up @@ -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);
Expand Down
157 changes: 6 additions & 151 deletions lib/resource_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> getTemplateFile(String templatePath) {
Expand All @@ -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<String> loadAsString(String path) async {
Future<String> 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<Uint8List> loadAsBytes(String path) {
/// Loads a `package:` resource as a [List<int>].
Future<List<int>> 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<Uint8List> _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<Uint8List> _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<Uint8List> _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<Uint8List> _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<Uint8List> _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<Uint8List> _doLoadFromPackageRoot(final String resourcePath) {
var withoutScheme = _removePackageScheme(resourcePath);
var fullPath = p.join(packageRootPath, withoutScheme);
return _readFile(resourcePath, fullPath);
}

Future<Uint8List> _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();
}
12 changes: 4 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ packages:
analyzer:
description: analyzer
source: hosted
version: "0.25.2"
version: "0.26.0"
ansicolor:
description: ansicolor
source: hosted
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ author: Dart Team <[email protected]>
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
Expand All @@ -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:
Expand Down
17 changes: 0 additions & 17 deletions test/resource_loader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);
});
});
}
2 changes: 1 addition & 1 deletion test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down