Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/command/add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Specify multiple sdk packages with descriptors.''');
solveResult = await resolveVersions(
SolveType.upgrade,
cache,
Package.inMemory(resolutionPubspec),
Package(resolutionPubspec, entrypoint.rootDir),
);
} on GitException {
final name = updates.first.ref.name;
Expand Down
28 changes: 16 additions & 12 deletions lib/src/command/dependency_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ class DependencyServicesReportCommand extends PubCommand {
final breakingPubspec = stripVersionBounds(compatiblePubspec);

final compatiblePackagesResult = await _tryResolve(
compatiblePubspec,
Package(compatiblePubspec, entrypoint.rootDir),
cache,
additionalConstraints: additionalConstraints,
);

final breakingPackagesResult = await _tryResolve(
breakingPubspec,
Package(breakingPubspec, entrypoint.rootDir),
cache,
additionalConstraints: additionalConstraints,
);
Expand Down Expand Up @@ -110,8 +110,10 @@ class DependencyServicesReportCommand extends PubCommand {
dependencySet[package.name] = package
.toRef()
.withConstraint(stripUpperBound(package.toRange().constraint));
final singleBreakingPackagesResult =
await _tryResolve(singleBreakingPubspec, cache);
final singleBreakingPackagesResult = await _tryResolve(
Package(singleBreakingPubspec, entrypoint.rootDir),
cache,
);
singleBreakingVersion = singleBreakingPackagesResult
?.firstWhereOrNull((element) => element.name == package.name);
}
Expand All @@ -126,7 +128,7 @@ class DependencyServicesReportCommand extends PubCommand {
);

final smallestUpgradeResult = await _tryResolve(
atLeastCurrentPubspec,
Package(atLeastCurrentPubspec, entrypoint.rootDir),
cache,
solveType: SolveType.downgrade,
additionalConstraints: additionalConstraints,
Expand Down Expand Up @@ -214,7 +216,8 @@ class DependencyServicesListCommand extends PubCommand {

final currentPackages = fileExists(entrypoint.lockFilePath)
? entrypoint.lockFile.packages.values.toList()
: (await _tryResolve(pubspec, cache) ?? <PackageId>[]);
: (await _tryResolve(Package(pubspec, entrypoint.rootDir), cache) ??
<PackageId>[]);

final dependencies = <Object>[];
final result = <String, Object>{'dependencies': dependencies};
Expand Down Expand Up @@ -418,12 +421,13 @@ class DependencyServicesApplyCommand extends PubCommand {
final solveResult = await resolveVersions(
SolveType.get,
cache,
Package.inMemory(
Package(
Pubspec.parse(
updatedPubspec,
cache.sources,
location: toUri(entrypoint.pubspecPath),
),
entrypoint.rootDir,
),
lockFile: updatedLockfile,
);
Expand Down Expand Up @@ -635,18 +639,18 @@ bool _lockFileHasContentHashes(dynamic lockfile) {
return false;
}

/// Try to solve [pubspec] return [PackageId]s in the resolution or `null` if no
/// Try to solve [package] return [PackageId]s in the resolution or `null` if no
/// resolution was found.
Future<List<PackageId>?> _tryResolve(
Pubspec pubspec,
Package package,
SystemCache cache, {
SolveType solveType = SolveType.upgrade,
Iterable<ConstraintAndCause>? additionalConstraints,
}) async {
final solveResult = await tryResolveVersions(
solveType,
cache,
Package.inMemory(pubspec),
package,
additionalConstraints: additionalConstraints,
);

Expand Down Expand Up @@ -686,7 +690,7 @@ Future<Map<String, PackageId>> _computeCurrentPackages(
if (fileExists(entrypoint.lockFilePath)) {
currentPackages = Map<String, PackageId>.from(entrypoint.lockFile.packages);
} else {
final resolution = await _tryResolve(entrypoint.root.pubspec, cache) ??
final resolution = await _tryResolve(entrypoint.root, cache) ??
(throw DataException('Failed to resolve pubspec'));
currentPackages = Map<String, PackageId>.fromIterable(
resolution,
Expand Down Expand Up @@ -730,7 +734,7 @@ Future<List<Object>> _computeUpgradeSet(
? SolveType.downgrade
: SolveType.get,
cache,
Package.inMemory(pubspec),
Package(pubspec, entrypoint.rootDir),
lockFile: lockFile,
additionalConstraints: additionalConstraints,
);
Expand Down
8 changes: 4 additions & 4 deletions lib/src/command/outdated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ Consider using the Dart 2.19 sdk to migrate to null safety.''');
'Resolving',
() async {
final upgradablePackagesResult = await _tryResolve(
upgradablePubspec,
Package(upgradablePubspec, entrypoint.rootDir),
cache,
lockFile: entrypoint.lockFile,
);
hasUpgradableResolution = upgradablePackagesResult != null;
upgradablePackages = upgradablePackagesResult ?? [];

final resolvablePackagesResult = await _tryResolve(
resolvablePubspec,
Package(resolvablePubspec, entrypoint.rootDir),
cache,
lockFile: entrypoint.lockFile,
);
Expand Down Expand Up @@ -407,14 +407,14 @@ Consider using the Dart 2.19 sdk to migrate to null safety.''');
/// Try to solve [pubspec] return [PackageId]s in the resolution or `null` if no
/// resolution was found.
Future<List<PackageId>?> _tryResolve(
Pubspec pubspec,
Package package,
SystemCache cache, {
LockFile? lockFile,
}) async {
final solveResult = await tryResolveVersions(
SolveType.upgrade,
cache,
Package.inMemory(pubspec),
package,
lockFile: lockFile,
);

Expand Down
7 changes: 5 additions & 2 deletions lib/src/command/upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
return await resolveVersions(
SolveType.upgrade,
cache,
Package.inMemory(resolvablePubspec),
Package(resolvablePubspec, entrypoint.rootDir),
);
},
condition: _shouldShowSpinner,
Expand Down Expand Up @@ -328,7 +328,10 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
final solveResult = await resolveVersions(
SolveType.upgrade,
cache,
Package.inMemory(_updatedPubspec(newPubspecText, entrypoint)),
Package(
_updatedPubspec(newPubspecText, entrypoint),
entrypoint.rootDir,
),
);
changes = tighten(
entrypoint.root.pubspec,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class Entrypoint {
this.rootDir,
this.cache, {
Pubspec? pubspec,
}) : _root = pubspec == null ? null : Package.inMemory(pubspec),
}) : _root = pubspec == null ? null : Package(pubspec, rootDir),
globalDir = null {
if (p.isWithin(cache.rootDir, rootDir)) {
fail('Cannot operate on packages inside the cache.');
Expand All @@ -251,7 +251,7 @@ class Entrypoint {
_example,
_packageGraph,
cache,
Package.inMemory(pubspec),
Package(pubspec, rootDir),
globalDir,
);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/global_packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,13 @@ class GlobalPackages {
final originalLockFile = _describeActive(name, cache);

// Create a dummy package with just [dep] so we can do resolution on it.
var root = Package.inMemory(
var root = Package(
Pubspec(
'pub global activate',
dependencies: [dep],
sources: cache.sources,
),
_packageDir(name),
);

// Resolve it and download its dependencies.
Expand Down
51 changes: 9 additions & 42 deletions lib/src/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import 'pubspec.dart';
import 'system_cache.dart';
import 'utils.dart';

/// A named, versioned, unit of code and resource reuse.
/// A Package is a [Pubspec] and a directory where it belongs that can be used
/// for version solving or as a node in a package graph.
class Package {
/// Compares [a] and [b] orders them by name then version number.
///
Expand All @@ -31,24 +32,8 @@ class Package {
return a.version.compareTo(b.version);
}

final String? _dir;

/// The path to the directory containing the package.
///
/// It is an error to access this on an in-memory package.
String get dir {
if (isInMemory) {
throw UnsupportedError(
'Package directory cannot be used for an in-memory package',
);
}

return _dir!;
}

/// An in-memory package can be created for doing a resolution without having
/// a package on disk. Paths should not be resolved for these.
bool get isInMemory => _dir == null;
final String dir;

/// The name of the package.
String get name => pubspec.name;
Expand Down Expand Up @@ -99,7 +84,7 @@ class Package {
late final bool inGitRepo = computeInGitRepoCache();

bool computeInGitRepoCache() {
if (isInMemory || !git.isInstalled) {
if (!git.isInstalled) {
return false;
} else {
// If the entire package directory is ignored, don't consider it part of a
Expand Down Expand Up @@ -134,21 +119,14 @@ class Package {
expectedName: name,
allowOverridesFile: withPubspecOverrides,
);
return Package._(dir, pubspec);
return Package(pubspec, dir);
}

Package._(
this._dir,
this.pubspec,
);

/// Constructs a package with the given pubspec.
/// Creates a package with [pubspec] associated with [dir].
///
/// The package will have no directory associated with it.
Package.inMemory(this.pubspec) : _dir = null;

/// Creates a package with [pubspec] located at [dir].
Package(this.pubspec, String this._dir);
/// For temporary resolution attempts [pubspec] does not have to correspond
/// to the one at disk.
Package(this.pubspec, this.dir);

/// Given a relative path within this package, returns its absolute path.
///
Expand All @@ -164,20 +142,12 @@ class Package {
String? part6,
String? part7,
]) {
if (isInMemory) {
throw StateError("Package $name is in-memory and doesn't have paths "
'on disk.');
}
return p.join(dir, part1, part2, part3, part4, part5, part6, part7);
}

/// Given an absolute path within this package (such as that returned by
/// [path] or [listFiles]), returns it relative to the package root.
String relative(String path) {
if (isInMemory) {
throw StateError("Package $name is in-memory and doesn't have paths "
'on disk.');
}
return p.relative(path, from: dir);
}

Expand Down Expand Up @@ -209,9 +179,6 @@ class Package {
///
/// To convert them to paths relative to the package root, use [p.relative].
List<String> listFiles({String? beneath, bool recursive = true}) {
// An in-memory package has no files.
if (isInMemory) return [];

var packageDir = dir;
var root = git.repoRoot(packageDir) ?? packageDir;
beneath = p
Expand Down
4 changes: 2 additions & 2 deletions lib/src/package_name.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PackageRef {

/// Creates a reference to the given root package.
static PackageRef root(Package package) =>
PackageRef(package.name, RootDescription(package));
PackageRef(package.name, RootDescription(package.dir));

@override
String toString([PackageDetail? detail]) {
Expand Down Expand Up @@ -88,7 +88,7 @@ class PackageId {
static PackageId root(Package package) => PackageId(
package.name,
package.version,
ResolvedRootDescription(RootDescription(package)),
ResolvedRootDescription(RootDescription(package.dir)),
);

@override
Expand Down
Loading