Skip to content

Commit

Permalink
Make hasMultipleVersions a property of the description
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdm committed Nov 15, 2024
1 parent 4cb927e commit 9bc9e65
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/src/command/upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
final declaredUpgradableDependencies = [
...package.dependencies.values,
...package.devDependencies.values,
].where((dep) => dep.source.hasMultipleVersions);
].where((dep) => dep.description.hasMultipleVersions);
for (final dep in declaredUpgradableDependencies) {
final resolvedPackage = resolvedPackages[dep.name]!;
if (!toUpgrade.contains(dep.name)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/package_name.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class PackageRange {
bool get _showVersionConstraint {
if (isRoot) return false;
if (!constraint.isAny) return true;
return description.source.hasMultipleVersions;
return description.hasMultipleVersions;
}

/// Returns a copy of `this` with the same semantics, but with a `^`-style
Expand Down
5 changes: 4 additions & 1 deletion lib/src/solver/version_solver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,10 @@ class VersionSolver {
// can't be downgraded.
if (_type == SolveType.downgrade) {
final locked = _lockFile.packages[package];
if (locked != null && !locked.source.hasMultipleVersions) return locked;
if (locked != null &&
!locked.description.description.hasMultipleVersions) {
return locked;
}
}

if (_unlock.isEmpty || _unlock.contains(package)) return null;
Expand Down
11 changes: 5 additions & 6 deletions lib/src/source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ abstract class Source {
/// all sources.
String get name;

/// Whether this source can choose between multiple versions of the same
/// package during version solving.
///
/// Defaults to `false`.
bool get hasMultipleVersions => false;

/// Parses a [PackageRef] from a name and a user-provided [description].
///
/// When a [Pubspec] is parsed, it reads in the description for each
Expand Down Expand Up @@ -190,6 +184,11 @@ abstract class Source {
/// with a version constraint.
abstract class Description {
Source get source;

/// Whether the source can choose between multiple versions of this
/// package during version solving.
bool get hasMultipleVersions;

Object? serializeForPubspec({
required String? containingDir,
required LanguageVersion languageVersion,
Expand Down
69 changes: 42 additions & 27 deletions lib/src/source/git.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class GitSource extends CachedSource {

@override
final name = 'git';
@override
final hasMultipleVersions = true;

@override
PackageRef parseRef(
Expand Down Expand Up @@ -343,42 +341,56 @@ class GitSource extends CachedSource {
return await _pool.withResource(() async {
await _ensureRepoCache(description, cache);
final path = _repoCachePath(description, cache);
final List<String> revisions;
final result = <PackageId>[];
if (description.tagPattern case final String tagPattern) {
revisions = await _listRevisionsWithTagPattern(path, tagPattern);
final revisions = await _listRevisionsWithTagPattern(path, tagPattern);
final seenVersions = <Version, String>{};
for (final revision in revisions) {
final Pubspec pubspec;
try {
pubspec = await _describeUncached(ref, revision, cache);
result.add(
PackageId(
ref.name,
pubspec.version,
ResolvedGitDescription(description, revision),
),
);
} on Exception catch (e) {
log.fine('No mathcing pubspec at $revision, $e');
continue;
}
final prev = seenVersions[pubspec.version];

if (prev != null) {
log.fine(
'Repeated version ${pubspec.version} of ${ref.name} '
'at $revision and $prev',
);
}
seenVersions[pubspec.version] = revision;
}
return result;
} else {
revisions = [await _firstRevision(path, description.ref)];
}
final revision = await _firstRevision(path, description.ref);

final result = <PackageId>[];
final seenVersions = <Version, String>{};
for (final revision in revisions) {
final Pubspec pubspec;
try {
pubspec = await _describeUncached(ref, revision, cache);
} on Exception {
log.fine(
'Found no valid pubspec of ${ref.name} at $revision, ignoring,',
);
continue;
}
final prev = seenVersions[pubspec.version];
if (prev != null) {
log.fine(
'Repeated version ${pubspec.version} of ${ref.name} '
'at $revision and $prev',
);
}
seenVersions[pubspec.version] = revision;
pubspec = await _describeUncached(ref, revision, cache);
result.add(
PackageId(
ref.name,
pubspec.version,
ResolvedGitDescription(description, revision),
),
);
return [
PackageId(
ref.name,
pubspec.version,
ResolvedGitDescription(description, revision),
),
];
}
return result;
});
}

Expand Down Expand Up @@ -950,6 +962,9 @@ class GitDescription extends Description {
}
return p.prettyUri(url);
}

@override
bool get hasMultipleVersions => tagPattern != null;
}

class ResolvedGitDescription extends ResolvedDescription {
Expand Down Expand Up @@ -979,7 +994,7 @@ class ResolvedGitDescription extends ResolvedDescription {
return {
'url': url,
'ref': description.ref,
'tag_pattern': description.tagPattern,
if (description.tagPattern != null) 'tag_pattern': description.tagPattern,
'resolved-ref': resolvedRef,
'path': description.path,
};
Expand Down
5 changes: 3 additions & 2 deletions lib/src/source/hosted.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ class HostedSource extends CachedSource {

@override
final name = 'hosted';
@override
final hasMultipleVersions = true;

static String pubDevUrl = 'https://pub.dev';
static String pubDartlangUrl = 'https://pub.dartlang.org';
Expand Down Expand Up @@ -1820,6 +1818,9 @@ class HostedDescription extends Description {

@override
HostedSource get source => HostedSource.instance;

@override
bool get hasMultipleVersions => true;
}

class ResolvedHostedDescription extends ResolvedDescription {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/source/path.dart
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ class PathDescription extends Description {

@override
int get hashCode => canonicalize(path).hashCode;

@override
bool get hasMultipleVersions => false;
}

class ResolvedPathDescription extends ResolvedDescription {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/source/root.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,7 @@ class RootDescription extends Description {

@override
int get hashCode => 'root'.hashCode;

@override
bool get hasMultipleVersions => false;
}
3 changes: 3 additions & 0 deletions lib/src/source/sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ class SdkDescription extends Description {
bool operator ==(Object other) {
return other is SdkDescription && other.sdk == sdk;
}

@override
bool get hasMultipleVersions => false;
}

class ResolvedSdkDescription extends ResolvedDescription {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/source/unknown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ class UnknownDescription extends Description {

@override
int get hashCode => Object.hash(source.name, json.encode(description));

@override
bool get hasMultipleVersions => false;
}

class ResolvedUnknownDescription extends ResolvedDescription {
Expand Down

0 comments on commit 9bc9e65

Please sign in to comment.