OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 import 'dart:async'; |
| 6 |
| 7 import 'package:pub_semver/pub_semver.dart'; |
| 8 |
| 9 import '../exceptions.dart'; |
| 10 import '../flutter.dart' as flutter; |
| 11 import '../io.dart'; |
| 12 import '../package.dart'; |
| 13 import '../pubspec.dart'; |
| 14 import '../source.dart'; |
| 15 import '../system_cache.dart'; |
| 16 |
| 17 /// A package [Source] that gets packages from a hard-coded SDK. |
| 18 class SdkSource extends Source { |
| 19 final name = 'sdk'; |
| 20 |
| 21 BoundSource bind(SystemCache systemCache) => |
| 22 new BoundSdkSource(this, systemCache); |
| 23 |
| 24 /// Returns a reference to an SDK package named [name] from [sdk]. |
| 25 PackageRef refFor(String name, String sdk) => new PackageRef(name, this, sdk); |
| 26 |
| 27 /// Returns an ID for an SDK package with the given [name] and [version] from |
| 28 /// [sdk]. |
| 29 PackageId idFor(String name, Version version, String sdk) => |
| 30 new PackageId(name, this, version, sdk); |
| 31 |
| 32 /// Parses an SDK dependency. |
| 33 PackageRef parseRef(String name, description, {String containingPath}) { |
| 34 if (description is! String) { |
| 35 throw new FormatException("The description must be an SDK name."); |
| 36 } |
| 37 |
| 38 return new PackageRef(name, this, description); |
| 39 } |
| 40 |
| 41 PackageId parseId(String name, Version version, description) { |
| 42 if (description is! String) { |
| 43 throw new FormatException("The description must be an SDK name."); |
| 44 } |
| 45 |
| 46 return new PackageId(name, this, version, description); |
| 47 } |
| 48 |
| 49 bool descriptionsEqual(description1, description2) => |
| 50 description1 == description2; |
| 51 |
| 52 int hashDescription(description) => description.hashCode; |
| 53 } |
| 54 |
| 55 /// The [BoundSource] for [SdkSource]. |
| 56 class BoundSdkSource extends BoundSource { |
| 57 final SdkSource source; |
| 58 |
| 59 final SystemCache systemCache; |
| 60 |
| 61 BoundSdkSource(this.source, this.systemCache); |
| 62 |
| 63 Future<List<PackageId>> doGetVersions(PackageRef ref) async { |
| 64 var sdk = ref.description as String; |
| 65 if (sdk == 'dart') { |
| 66 throw new PackageNotFoundException( |
| 67 'Could not find package ${ref.name} in the Dart SDK.'); |
| 68 } else if (sdk != 'flutter') { |
| 69 throw new PackageNotFoundException('Unknown SDK "$sdk".'); |
| 70 } |
| 71 |
| 72 var pubspec = _loadPubspec(ref.name); |
| 73 var id = new PackageId(ref.name, source, pubspec.version, sdk); |
| 74 memoizePubspec(id, pubspec); |
| 75 return [id]; |
| 76 } |
| 77 |
| 78 Future<Pubspec> doDescribe(PackageId id) async => _loadPubspec(id.name); |
| 79 |
| 80 /// Loads the pubspec for the Flutter package named [name]. |
| 81 /// |
| 82 /// Throws a [PackageNotFoundException] if Flutter is unavaialable or doesn't |
| 83 /// contain the package. |
| 84 Pubspec _loadPubspec(String name) => |
| 85 new Pubspec.load(_verifiedPackagePath(name), systemCache.sources, |
| 86 expectedName: name); |
| 87 |
| 88 Future get(PackageId id, String symlink) async { |
| 89 createPackageSymlink(id.name, _verifiedPackagePath(id.name), symlink); |
| 90 } |
| 91 |
| 92 /// Returns the path in the Flutter SDK for the package named [name]. |
| 93 /// |
| 94 /// Throws a [PackageNotFoundException] if Flutter is unavailable or doesn't |
| 95 /// contain the package. |
| 96 String _verifiedPackagePath(String name) { |
| 97 if (!flutter.isAvailable) { |
| 98 throw new PackageNotFoundException('The Flutter SDK is not available.'); |
| 99 } |
| 100 |
| 101 var path = flutter.packagePath(name); |
| 102 if (dirExists(path)) return path; |
| 103 |
| 104 throw new PackageNotFoundException( |
| 105 'Could not find package $name in the Flutter SDK.'); |
| 106 } |
| 107 |
| 108 String getDirectory(PackageId id) => flutter.packagePath(id.name); |
| 109 } |
OLD | NEW |