Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: pkg/docgen/lib/src/package_helpers.dart

Issue 304203004: Correctly handle case where we have a library outside of a package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/docgen/lib/src/models/library.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library docgen.package_helpers; 5 library docgen.package_helpers;
6 6
7 import 'exports/source_mirrors.dart'; 7 import 'exports/source_mirrors.dart';
8 import 'generator.dart' show pubScript;
8 9
9 import 'dart:io'; 10 import 'dart:io';
10 import 'package:path/path.dart' as path; 11 import 'package:path/path.dart' as path;
11 import 'package:yaml/yaml.dart'; 12 import 'package:yaml/yaml.dart';
12 13
13 /// Helper accessor to determine the full pathname of the root of the dart 14 /// Helper accessor to determine the full pathname of the root of the dart
14 /// checkout. We can be in one of three situations: 15 /// checkout. We can be in one of three situations:
15 /// 1) Running from pkg/docgen/bin/docgen.dart 16 /// 1) Running from pkg/docgen/bin/docgen.dart
16 /// 2) Running from a snapshot in a build, 17 /// 2) Running from a snapshot in a build,
17 /// e.g. xcodebuild/ReleaseIA32/dart-sdk/bin 18 /// e.g. xcodebuild/ReleaseIA32/dart-sdk/bin
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 // We only want components that are somewhere underneath the lib directory. 52 // We only want components that are somewhere underneath the lib directory.
52 var subPath = path.relative(file, from: possiblePackage); 53 var subPath = path.relative(file, from: possiblePackage);
53 var subPathComponents = path.split(subPath); 54 var subPathComponents = path.split(subPath);
54 if (subPathComponents.isNotEmpty && subPathComponents.first == 'lib') { 55 if (subPathComponents.isNotEmpty && subPathComponents.first == 'lib') {
55 return possiblePackage; 56 return possiblePackage;
56 } else { 57 } else {
57 return null; 58 return null;
58 } 59 }
59 } 60 }
60 61
61 /// Read a pubspec and return the library name, given a directory 62 Map _getPubspec(String directoryName) {
62 String packageNameFor(String directoryName) {
63 var pubspecName = path.join(directoryName, 'pubspec.yaml'); 63 var pubspecName = path.join(directoryName, 'pubspec.yaml');
64 File pubspec = new File(pubspecName); 64 File pubspec = new File(pubspecName);
65 if (!pubspec.existsSync()) return ''; 65 if (!pubspec.existsSync()) return {'name': ''};
66 var contents = pubspec.readAsStringSync(); 66 var contents = pubspec.readAsStringSync();
67 return loadYaml(contents);
68 }
69
70 /// Read a pubspec and return the library name, given a directory
71 String packageNameFor(String directoryName) =>
72 _getPubspec(directoryName)['name'];
73
74 /// Read a pubspec and return the library name, given a directory
75 String _packageVersionFor(String directoryName) {
76 var spec = _getPubspec(directoryName);
77 return '${spec['name']}-${spec['version']}';
78 }
79
80 /// Look in the pubspec.lock to determine what version of a package we are
81 /// documenting (null if not applicable).
82 String packageVersion(LibraryMirror mirror) {
83 if (mirror.uri.scheme == 'file') {
84 String packageDirectory = getPackageDirectory(mirror);
85 if (packageDirectory == null) return '';
Emily Fortuna 2014/05/29 16:34:47 This is the one line that was added from the previ
86 if (packageDirectory.contains('.pub-cache')) {
87 return path.basename(packageDirectory);
88 }
89 String packageName = packageNameFor(packageDirectory);
90 return _packageVersionFor(packageDirectory);
91 } else if (mirror.uri.scheme == 'dart') {
92 // If this item is from the SDK, use what version of pub we're running to
93 // ascertain the SDK version.
94 var pubVersion = Process.runSync(pubScript, ['--version'],
95 runInShell: true);
96 if (pubVersion.exitCode != 0) {
97 print(pubVersion.stderr);
98 }
99 return pubVersion.stdout.replaceAll('Pub ', '').trim();
100 }
101 return '';
102 }
103
104 String _packageVersionHelper(String packageDirectory, String packageName) {
105 var publockName = path.join(packageDirectory, 'pubspec.lock');
106 File publock = new File(publockName);
107 if (!publock.existsSync()) return '';
108 var contents = publock.readAsStringSync();
67 var spec = loadYaml(contents); 109 var spec = loadYaml(contents);
68 return spec["name"]; 110 return spec['packages'][packageName];
69 } 111 }
70 112
71 /// Recursively walk up from directory name looking for a pubspec. Return 113 /// Recursively walk up from directory name looking for a pubspec. Return
72 /// the directory that contains it, or null if none is found. 114 /// the directory that contains it, or null if none is found.
73 String _packageDirectoryFor(String directoryName) { 115 String _packageDirectoryFor(String directoryName) {
74 var dir = directoryName; 116 var dir = directoryName;
75 while (!_pubspecFor(dir).existsSync()) { 117 while (!_pubspecFor(dir).existsSync()) {
76 var newDir = path.dirname(dir); 118 var newDir = path.dirname(dir);
77 if (newDir == dir) return null; 119 if (newDir == dir) return null;
78 dir = newDir; 120 dir = newDir;
79 } 121 }
80 return dir; 122 return dir;
81 } 123 }
82 124
83 File _pubspecFor(String directoryName) => 125 File _pubspecFor(String directoryName) =>
84 new File(path.join(directoryName, 'pubspec.yaml')); 126 new File(path.join(directoryName, 'pubspec.yaml'));
OLDNEW
« no previous file with comments | « pkg/docgen/lib/src/models/library.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698