Merge pull request #41 from j4qfrost/master
nullsafety
diff --git a/lib/pub_cache.dart b/lib/pub_cache.dart
index 4b8a87f..9538d36 100644
--- a/lib/pub_cache.dart
+++ b/lib/pub_cache.dart
@@ -31,12 +31,12 @@
// The location of the pub cache.
final Directory location;
- List<Application> _applications;
- List<PackageRef> _packageRefs;
+ late List<Application> _applications;
+ late List<PackageRef> _packageRefs;
/// Create a pubcache instance. [dir] defaults to the default platform pub
/// cache location.
- PubCache([Directory dir])
+ PubCache([Directory? dir])
: location = dir == null ? getSystemCacheLocation() : dir {
_parse();
}
@@ -69,7 +69,7 @@
/// cache. This method will prefer to return only release verions. If
/// [includePreRelease] is true, then the very latest verision will be
/// returned, include pre-release versions.
- PackageRef getLatestVersion(String packageName,
+ PackageRef? getLatestVersion(String packageName,
{bool includePreRelease: false}) {
List<PackageRef> refs = getAllPackageVersions(packageName);
@@ -102,7 +102,7 @@
_applications = globalPackagesDir
.listSync()
.where((item) => item is Directory)
- .map((dir) => new Application._(this, dir))
+ .map((dir) => new Application._(this, dir as Directory))
.toList();
}
@@ -115,7 +115,7 @@
_packageRefs.addAll(dartlangDir
.listSync()
.where((dir) => dir is Directory)
- .map((dir) => new DirectoryPackageRef('hosted', dir)));
+ .map((dir) => new DirectoryPackageRef('hosted', dir as Directory)));
}
// Scan for git packages (ignore the git/cache directory).
@@ -126,7 +126,7 @@
.listSync()
.where(
(dir) => dir is Directory && path.basename(dir.path) != 'cache')
- .map((dir) => new GitDirectoryPackageRef(dir));
+ .map((dir) => new GitDirectoryPackageRef(dir as Directory));
_packageRefs.addAll(gitRefs);
}
}
@@ -147,7 +147,7 @@
final PubCache _cache;
final Directory _dir;
- List<PackageRef> _packageRefs;
+ List<PackageRef>? _packageRefs;
Application._(this._cache, this._dir);
@@ -156,13 +156,13 @@
/// The version of the application and of the defining package.
Version get version {
- PackageRef ref = getDefiningPackageRef();
+ final ref = getDefiningPackageRef();
return ref == null ? Version.none : ref.version;
}
/// Return the reference to the defining package. This is the package that
/// defines the application.
- PackageRef getDefiningPackageRef() {
+ PackageRef? getDefiningPackageRef() {
for (PackageRef ref in getPackageRefs()) {
if (ref.name == name) return ref;
}
@@ -173,7 +173,7 @@
/// defining package as well as the direct and transitive dependencies.
List<PackageRef> getPackageRefs() {
if (_packageRefs == null) _parsePubspecLock();
- return _packageRefs;
+ return _packageRefs!;
}
String toString() => '${name} ${version}';
@@ -223,10 +223,11 @@
/// Resolve the package reference into the actual package, including the
/// location on disk.
- Package resolve();
+ Package? resolve();
bool operator ==(other) {
- return this.sourceType == other.sourceType &&
+ return other is PackageRef &&
+ this.sourceType == other.sourceType &&
this.name == other.name &&
this.version == other.version;
}
diff --git a/lib/src/impl.dart b/lib/src/impl.dart
index 2542ada..3fc6836 100644
--- a/lib/src/impl.dart
+++ b/lib/src/impl.dart
@@ -17,7 +17,7 @@
final String name;
final Version version;
- Function _resolver;
+ Function? _resolver;
PackageRefImpl(this.sourceType, this.name, String ver)
: version = new Version.parse(ver);
@@ -35,7 +35,7 @@
return new PathPackageRefImpl(name, ver, description);
}
- Package resolve() => _resolver == null ? null : _resolver(this);
+ Package? resolve() => _resolver == null ? null : _resolver!(this);
}
class GitPackageRefImpl extends PackageRefImpl {
@@ -67,7 +67,7 @@
bool get relative => _description['relative'] == true;
- Package resolve() {
+ Package? resolve() {
Directory dir = new Directory(path);
return dir.existsSync() ? new Package(dir, name, version) : null;
}
@@ -81,8 +81,8 @@
final String sourceType;
final Directory directory;
- String _name;
- Version _version;
+ late String _name;
+ late Version _version;
DirectoryPackageRef(this.sourceType, this.directory) {
_name = path.basename(this.directory.path);
@@ -107,9 +107,9 @@
final String sourceType;
final Directory directory;
- String _name;
- Version _version;
- String _resolvedRef;
+ late String _name;
+ late Version _version;
+ String? _resolvedRef;
GitDirectoryPackageRef(this.directory) : sourceType = 'git' {
_name = path.basename(this.directory.path);
@@ -135,7 +135,7 @@
Version get version => _version;
/// The git commit.
- String get resolvedRef => _resolvedRef;
+ String? get resolvedRef => _resolvedRef;
Package resolve() => new Package(directory, name, version);
diff --git a/pubspec.yaml b/pubspec.yaml
index 2a5047c..c8e29df 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -5,12 +5,12 @@
homepage: https://github.com/google/pub_cache
environment:
- sdk: '>=2.0.0 <3.0.0'
+ sdk: '>=2.12.0-0 <3.0.0'
dependencies:
- path: ^1.0.0
- pub_semver: ^1.0.0
- yaml: ^2.0.0
+ path: ^1.8.0
+ pub_semver: ^2.0.0
+ yaml: ^3.0.0
dev_dependencies:
- test: ^1.0.0
+ test: ^1.16.4
diff --git a/test/pub_cache_test.dart b/test/pub_cache_test.dart
index 131bd8d..4a1ddc5 100644
--- a/test/pub_cache_test.dart
+++ b/test/pub_cache_test.dart
@@ -97,7 +97,7 @@
group('Application', () {
PubCache cache;
- Application app;
+ late Application app;
setUp(() {
cache = new PubCache();
@@ -113,7 +113,7 @@
});
test('getDefiningPackageRef', () {
- expect(app.getDefiningPackageRef().name, app.name);
+ expect(app.getDefiningPackageRef()!.name, app.name);
});
test('getPackageRefs', () {
@@ -127,8 +127,8 @@
group('PackageRef', () {
PubCache cache;
- Application app;
- PackageRef ref;
+ late Application app;
+ late PackageRef ref;
setUp(() {
cache = new PubCache();
@@ -160,7 +160,7 @@
group('Package', () {
test('toString', () {
PubCache cache = new PubCache();
- Package p = cache.getPackageRefs().first.resolve();
+ Package? p = cache.getPackageRefs().first.resolve();
expect(p, isNotNull);
expect(p.toString(), isNotEmpty);
});