| 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; | |
| 9 | 8 |
| 10 import 'dart:io'; | 9 import 'dart:io'; |
| 11 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| 12 import 'package:yaml/yaml.dart'; | 11 import 'package:yaml/yaml.dart'; |
| 13 | 12 |
| 14 /// Helper accessor to determine the full pathname of the root of the dart | 13 /// Helper accessor to determine the full pathname of the root of the dart |
| 15 /// checkout. We can be in one of three situations: | 14 /// checkout. We can be in one of three situations: |
| 16 /// 1) Running from pkg/docgen/bin/docgen.dart | 15 /// 1) Running from pkg/docgen/bin/docgen.dart |
| 17 /// 2) Running from a snapshot in a build, | 16 /// 2) Running from a snapshot in a build, |
| 18 /// e.g. xcodebuild/ReleaseIA32/dart-sdk/bin | 17 /// e.g. xcodebuild/ReleaseIA32/dart-sdk/bin |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 // We only want components that are somewhere underneath the lib directory. | 51 // We only want components that are somewhere underneath the lib directory. |
| 53 var subPath = path.relative(file, from: possiblePackage); | 52 var subPath = path.relative(file, from: possiblePackage); |
| 54 var subPathComponents = path.split(subPath); | 53 var subPathComponents = path.split(subPath); |
| 55 if (subPathComponents.isNotEmpty && subPathComponents.first == 'lib') { | 54 if (subPathComponents.isNotEmpty && subPathComponents.first == 'lib') { |
| 56 return possiblePackage; | 55 return possiblePackage; |
| 57 } else { | 56 } else { |
| 58 return null; | 57 return null; |
| 59 } | 58 } |
| 60 } | 59 } |
| 61 | 60 |
| 62 Map _getPubspec(String directoryName) { | 61 /// Read a pubspec and return the library name, given a directory |
| 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 {'name': ''}; | 65 if (!pubspec.existsSync()) return ''; |
| 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(); | |
| 108 var spec = loadYaml(contents); | 67 var spec = loadYaml(contents); |
| 109 return spec['packages'][packageName]; | 68 return spec["name"]; |
| 110 } | 69 } |
| 111 | 70 |
| 112 /// Recursively walk up from directory name looking for a pubspec. Return | 71 /// Recursively walk up from directory name looking for a pubspec. Return |
| 113 /// the directory that contains it, or null if none is found. | 72 /// the directory that contains it, or null if none is found. |
| 114 String _packageDirectoryFor(String directoryName) { | 73 String _packageDirectoryFor(String directoryName) { |
| 115 var dir = directoryName; | 74 var dir = directoryName; |
| 116 while (!_pubspecFor(dir).existsSync()) { | 75 while (!_pubspecFor(dir).existsSync()) { |
| 117 var newDir = path.dirname(dir); | 76 var newDir = path.dirname(dir); |
| 118 if (newDir == dir) return null; | 77 if (newDir == dir) return null; |
| 119 dir = newDir; | 78 dir = newDir; |
| 120 } | 79 } |
| 121 return dir; | 80 return dir; |
| 122 } | 81 } |
| 123 | 82 |
| 124 File _pubspecFor(String directoryName) => | 83 File _pubspecFor(String directoryName) => |
| 125 new File(path.join(directoryName, 'pubspec.yaml')); | 84 new File(path.join(directoryName, 'pubspec.yaml')); |
| OLD | NEW |