| OLD | NEW |
| 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 Loading... |
| 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.contains('.pub-cache')) { |
| 86 return path.basename(packageDirectory); |
| 87 } |
| 88 String packageName = packageNameFor(packageDirectory); |
| 89 return _packageVersionFor(packageDirectory); |
| 90 } else if (mirror.uri.scheme == 'dart') { |
| 91 // If this item is from the SDK, use what version of pub we're running to |
| 92 // ascertain the SDK version. |
| 93 var pubVersion = Process.runSync(pubScript, ['--version'], |
| 94 runInShell: true); |
| 95 if (pubVersion.exitCode != 0) { |
| 96 print(pubVersion.stderr); |
| 97 } |
| 98 return pubVersion.stdout.replaceAll('Pub ', '').trim(); |
| 99 } |
| 100 return ''; |
| 101 } |
| 102 |
| 103 String _packageVersionHelper(String packageDirectory, String packageName) { |
| 104 var publockName = path.join(packageDirectory, 'pubspec.lock'); |
| 105 File publock = new File(publockName); |
| 106 if (!publock.existsSync()) return ''; |
| 107 var contents = publock.readAsStringSync(); |
| 67 var spec = loadYaml(contents); | 108 var spec = loadYaml(contents); |
| 68 return spec["name"]; | 109 return spec['packages'][packageName]; |
| 69 } | 110 } |
| 70 | 111 |
| 71 /// Recursively walk up from directory name looking for a pubspec. Return | 112 /// Recursively walk up from directory name looking for a pubspec. Return |
| 72 /// the directory that contains it, or null if none is found. | 113 /// the directory that contains it, or null if none is found. |
| 73 String _packageDirectoryFor(String directoryName) { | 114 String _packageDirectoryFor(String directoryName) { |
| 74 var dir = directoryName; | 115 var dir = directoryName; |
| 75 while (!_pubspecFor(dir).existsSync()) { | 116 while (!_pubspecFor(dir).existsSync()) { |
| 76 var newDir = path.dirname(dir); | 117 var newDir = path.dirname(dir); |
| 77 if (newDir == dir) return null; | 118 if (newDir == dir) return null; |
| 78 dir = newDir; | 119 dir = newDir; |
| 79 } | 120 } |
| 80 return dir; | 121 return dir; |
| 81 } | 122 } |
| 82 | 123 |
| 83 File _pubspecFor(String directoryName) => | 124 File _pubspecFor(String directoryName) => |
| 84 new File(path.join(directoryName, 'pubspec.yaml')); | 125 new File(path.join(directoryName, 'pubspec.yaml')); |
| OLD | NEW |