Skip to content

Commit

Permalink
Refactor Entrypoint.rootDir preparing for workspaces (#4179)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdm authored Mar 15, 2024
1 parent c60b1a0 commit e519de6
Show file tree
Hide file tree
Showing 41 changed files with 392 additions and 304 deletions.
24 changes: 14 additions & 10 deletions lib/src/command/add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ Specify multiple sdk packages with descriptors.''');
/// Compute a pubspec that will depend on all the given packages, but the
/// actual constraint will only be determined after a resolution decides the
/// best version.
var resolutionPubspec = entrypoint.root.pubspec;
// TODO(https://github.com/dart-lang/pub/issues/4127): This should
// operate on entrypoint.workPackage.
var resolutionPubspec = entrypoint.workspaceRoot.pubspec;
for (final update in updates) {
/// Perform version resolution in-memory.
resolutionPubspec = await _addPackageToPubspec(resolutionPubspec, update);
Expand All @@ -211,8 +213,8 @@ Specify multiple sdk packages with descriptors.''');
cache,
Package(
resolutionPubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
entrypoint.workspaceRoot.dir,
entrypoint.workspaceRoot.workspaceChildren,
),
);
} on GitException {
Expand Down Expand Up @@ -250,12 +252,12 @@ Specify multiple sdk packages with descriptors.''');
/// ensure that the modification timestamp on `pubspec.lock` and
/// `.dart_tool/package_config.json` is newer than `pubspec.yaml`,
/// ensuring that [entrypoint.assertUptoDate] will pass.
writeTextFile(entrypoint.pubspecPath, newPubspecText);
writeTextFile(entrypoint.workspaceRoot.pubspecPath, newPubspecText);
}

String? overridesFileContents;
final overridesPath =
p.join(entrypoint.rootDir, Pubspec.pubspecOverridesFilename);
p.join(entrypoint.workspaceRoot.dir, Pubspec.pubspecOverridesFilename);
try {
overridesFileContents = readTextFile(overridesPath);
} on IOException {
Expand All @@ -270,10 +272,11 @@ Specify multiple sdk packages with descriptors.''');
Pubspec.parse(
newPubspecText,
cache.sources,
location: Uri.parse(entrypoint.pubspecPath),
location: Uri.parse(entrypoint.workspaceRoot.pubspecPath),
overridesFileContents: overridesFileContents,
overridesLocation: Uri.file(overridesPath),
containingDescription: RootDescription(entrypoint.rootDir),
containingDescription:
RootDescription(entrypoint.workspaceRoot.dir),
),
)
.acquireDependencies(
Expand Down Expand Up @@ -680,8 +683,9 @@ Specify multiple sdk packages with descriptors.''');
List<PackageId> resultPackages,
List<_ParseResult> updates,
) {
final yamlEditor = YamlEditor(readTextFile(entrypoint.pubspecPath));
log.io('Reading ${entrypoint.pubspecPath}.');
final yamlEditor =
YamlEditor(readTextFile(entrypoint.workspaceRoot.pubspecPath));
log.io('Reading ${entrypoint.workspaceRoot.pubspecPath}.');
log.fine('Contents:\n$yamlEditor');

for (final update in updates) {
Expand All @@ -701,7 +705,7 @@ Specify multiple sdk packages with descriptors.''');
: VersionConstraint.any),
),
cache,
entrypoint,
entrypoint.workPackage,
);

if (yamlEditor.parseAt(
Expand Down
62 changes: 36 additions & 26 deletions lib/src/command/dependency_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,16 @@ class DependencyServicesReportCommand extends PubCommand {
throw FormatException('"target" should be a String.');
}

final compatiblePubspec = stripDependencyOverrides(entrypoint.root.pubspec);
final compatiblePubspec =
stripDependencyOverrides(entrypoint.workspaceRoot.pubspec);

final breakingPubspec = stripVersionBounds(compatiblePubspec);

final compatiblePackagesResult = await _tryResolve(
Package(
compatiblePubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
entrypoint.workspaceRoot.dir,
entrypoint.workspaceRoot.workspaceChildren,
),
cache,
additionalConstraints: additionalConstraints,
Expand All @@ -82,8 +83,8 @@ class DependencyServicesReportCommand extends PubCommand {
final breakingPackagesResult = await _tryResolve(
Package(
breakingPubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
entrypoint.workspaceRoot.dir,
entrypoint.workspaceRoot.workspaceChildren,
),
cache,
additionalConstraints: additionalConstraints,
Expand Down Expand Up @@ -123,8 +124,8 @@ class DependencyServicesReportCommand extends PubCommand {
final singleBreakingPackagesResult = await _tryResolve(
Package(
singleBreakingPubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
entrypoint.workspaceRoot.dir,
entrypoint.workspaceRoot.workspaceChildren,
),
cache,
);
Expand All @@ -144,8 +145,8 @@ class DependencyServicesReportCommand extends PubCommand {
final smallestUpgradeResult = await _tryResolve(
Package(
atLeastCurrentPubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
entrypoint.workspaceRoot.dir,
entrypoint.workspaceRoot.workspaceChildren,
),
cache,
solveType: SolveType.downgrade,
Expand Down Expand Up @@ -230,15 +231,15 @@ class DependencyServicesListCommand extends PubCommand {

@override
Future<void> runProtected() async {
final pubspec = entrypoint.root.pubspec;
final pubspec = entrypoint.workspaceRoot.pubspec;

final currentPackages = fileExists(entrypoint.lockFilePath)
? entrypoint.lockFile.packages.values.toList()
: (await _tryResolve(
Package(
pubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
entrypoint.workspaceRoot.dir,
entrypoint.workspaceRoot.workspaceChildren,
),
cache,
) ??
Expand Down Expand Up @@ -308,7 +309,7 @@ class DependencyServicesApplyCommand extends PubCommand {

@override
Future<void> runProtected() async {
YamlEditor(readTextFile(entrypoint.pubspecPath));
YamlEditor(readTextFile(entrypoint.workspaceRoot.pubspecPath));
final toApply = <_PackageVersion>[];
final input = json.decode(await utf8.decodeStream(stdin));
for (final change in input['dependencyChanges'] as Iterable) {
Expand All @@ -323,8 +324,9 @@ class DependencyServicesApplyCommand extends PubCommand {
);
}

final pubspec = entrypoint.root.pubspec;
final pubspecEditor = YamlEditor(readTextFile(entrypoint.pubspecPath));
final pubspec = entrypoint.workspaceRoot.pubspec;
final pubspecEditor =
YamlEditor(readTextFile(entrypoint.workspaceRoot.pubspecPath));
final lockFile = fileExists(entrypoint.lockFilePath)
? readTextFile(entrypoint.lockFilePath)
: null;
Expand Down Expand Up @@ -450,16 +452,17 @@ class DependencyServicesApplyCommand extends PubCommand {
Pubspec.parse(
updatedPubspec,
cache.sources,
location: toUri(entrypoint.pubspecPath),
containingDescription: RootDescription(entrypoint.rootDir),
location: toUri(entrypoint.workspaceRoot.pubspecPath),
containingDescription:
RootDescription(entrypoint.workspaceRoot.dir),
),
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
entrypoint.workspaceRoot.dir,
entrypoint.workspaceRoot.workspaceChildren,
),
lockFile: updatedLockfile,
);
if (pubspecEditor.edits.isNotEmpty) {
writeTextFile(entrypoint.pubspecPath, updatedPubspec);
writeTextFile(entrypoint.workspaceRoot.pubspecPath, updatedPubspec);
}
// Only if we originally had a lock-file we write the resulting lockfile back.
if (updatedLockfile != null) {
Expand Down Expand Up @@ -717,14 +720,14 @@ Future<Map<String, PackageId>> _computeCurrentPackages(
if (fileExists(entrypoint.lockFilePath)) {
currentPackages = Map<String, PackageId>.from(entrypoint.lockFile.packages);
} else {
final resolution = await _tryResolve(entrypoint.root, cache) ??
final resolution = await _tryResolve(entrypoint.workspaceRoot, cache) ??
(throw DataException('Failed to resolve pubspec'));
currentPackages = Map<String, PackageId>.fromIterable(
resolution,
key: (e) => (e as PackageId).name,
);
}
currentPackages.remove(entrypoint.root.name);
currentPackages.remove(entrypoint.workspaceRoot.name);
return currentPackages;
}

Expand Down Expand Up @@ -762,7 +765,11 @@ Future<List<Object>> _computeUpgradeSet(
? SolveType.downgrade
: SolveType.get,
cache,
Package(pubspec, entrypoint.rootDir, entrypoint.root.workspaceChildren),
Package(
pubspec,
entrypoint.workspaceRoot.dir,
entrypoint.workspaceRoot.workspaceChildren,
),
lockFile: lockFile,
additionalConstraints: additionalConstraints,
);
Expand All @@ -785,7 +792,7 @@ Future<List<Object>> _computeUpgradeSet(
'name': p.name,
'version': p.versionOrHash(),
'kind': _kindString(pubspec, p.name),
'source': _source(p, containingDir: entrypoint.root.dir),
'source': _source(p, containingDir: entrypoint.workspaceRoot.dir),
'constraintBumped': originalConstraint == null
? null
: upgradeType == _UpgradeType.compatible
Expand All @@ -807,7 +814,10 @@ Future<List<Object>> _computeUpgradeSet(
'previousConstraint': originalConstraint?.toString(),
'previousSource': currentPackage == null
? null
: _source(currentPackage, containingDir: entrypoint.root.dir),
: _source(
currentPackage,
containingDir: entrypoint.workspaceRoot.dir,
),
};
}),
// Find packages that were removed by the resolution
Expand All @@ -825,7 +835,7 @@ Future<List<Object>> _computeUpgradeSet(
'previousConstraint': null,
'previous': _source(
currentPackages[oldPackageName]!,
containingDir: entrypoint.root.dir,
containingDir: entrypoint.workspaceRoot.dir,
),
},
];
Expand Down
44 changes: 23 additions & 21 deletions lib/src/command/deps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class DepsCommand extends PubCommand {
usageException('Cannot combine --json and --style.');
}
final visited = <String>[];
final toVisit = [entrypoint.root.name];
final toVisit = [entrypoint.workspaceRoot.name];
final packagesJson = <dynamic>[];
final graph = await entrypoint.packageGraph;
while (toVisit.isNotEmpty) {
Expand All @@ -98,13 +98,14 @@ class DepsCommand extends PubCommand {
visited.add(current);
final currentPackage =
(await entrypoint.packageGraph).packages[current]!;
final next = (current == entrypoint.root.name
? entrypoint.root.immediateDependencies
final next = (current == entrypoint.workspaceRoot.name
? entrypoint.workspaceRoot.immediateDependencies
: currentPackage.dependencies)
.keys
.toList();
final dependencyType = entrypoint.root.pubspec.dependencyType(current);
final kind = currentPackage == entrypoint.root
final dependencyType =
entrypoint.workspaceRoot.pubspec.dependencyType(current);
final kind = currentPackage == entrypoint.workspaceRoot
? 'root'
: (dependencyType == DependencyType.direct
? 'direct'
Expand All @@ -124,12 +125,12 @@ class DepsCommand extends PubCommand {
}
var executables = [
for (final package in [
entrypoint.root,
...entrypoint.root.immediateDependencies.keys
entrypoint.workspaceRoot,
...entrypoint.workspaceRoot.immediateDependencies.keys
.map((name) => graph.packages[name]),
])
...package!.executableNames.map(
(name) => package == entrypoint.root
(name) => package == entrypoint.workspaceRoot
? ':$name'
: (package.name == name ? name : '${package.name}:$name'),
),
Expand All @@ -138,7 +139,7 @@ class DepsCommand extends PubCommand {
buffer.writeln(
JsonEncoder.withIndent(' ').convert(
{
'root': entrypoint.root.name,
'root': entrypoint.workspaceRoot.name,
'packages': packagesJson,
'sdks': [
for (var sdk in sdks.values)
Expand All @@ -158,7 +159,7 @@ class DepsCommand extends PubCommand {
buffer.writeln("${log.bold('${sdk.name} SDK')} ${sdk.version}");
}

buffer.writeln(_labelPackage(entrypoint.root));
buffer.writeln(_labelPackage(entrypoint.workspaceRoot));

switch (argResults['style']) {
case 'compact':
Expand Down Expand Up @@ -186,7 +187,7 @@ class DepsCommand extends PubCommand {
Future<void> _outputCompact(
StringBuffer buffer,
) async {
var root = entrypoint.root;
var root = entrypoint.workspaceRoot;
await _outputCompactPackages(
'dependencies',
root.dependencies.keys,
Expand Down Expand Up @@ -239,7 +240,7 @@ class DepsCommand extends PubCommand {
/// For each dependency listed, *that* package's immediate dependencies are
/// shown.
Future<void> _outputList(StringBuffer buffer) async {
var root = entrypoint.root;
var root = entrypoint.workspaceRoot;
await _outputListSection('dependencies', root.dependencies.keys, buffer);
if (_includeDev) {
await _outputListSection(
Expand Down Expand Up @@ -300,14 +301,15 @@ class DepsCommand extends PubCommand {
// being added to the tree, and the parent map that will receive that
// package.
var toWalk = Queue<(Package, Map<String, Map>)>();
var visited = <String>{entrypoint.root.name};
var visited = <String>{entrypoint.workspaceRoot.name};

// Start with the root dependencies.
var packageTree = <String, Map>{};
var immediateDependencies =
entrypoint.root.immediateDependencies.keys.toSet();
entrypoint.workspaceRoot.immediateDependencies.keys.toSet();
if (!_includeDev) {
immediateDependencies.removeAll(entrypoint.root.devDependencies.keys);
immediateDependencies
.removeAll(entrypoint.workspaceRoot.devDependencies.keys);
}
for (var name in immediateDependencies) {
toWalk.add((await _getPackage(name), packageTree));
Expand Down Expand Up @@ -343,7 +345,7 @@ class DepsCommand extends PubCommand {
/// Gets the names of the non-immediate dependencies of the root package.
Future<Set<String>> _getTransitiveDependencies() async {
var transitive = await _getAllDependencies();
var root = entrypoint.root;
var root = entrypoint.workspaceRoot;
transitive.remove(root.name);
transitive.removeAll(root.dependencies.keys);
if (_includeDev) {
Expand All @@ -359,8 +361,8 @@ class DepsCommand extends PubCommand {
return graph.packages.keys.toSet();
}

var nonDevDependencies = entrypoint.root.dependencies.keys.toList()
..addAll(entrypoint.root.dependencyOverrides.keys);
var nonDevDependencies = entrypoint.workspaceRoot.dependencies.keys.toList()
..addAll(entrypoint.workspaceRoot.dependencyOverrides.keys);
return nonDevDependencies
.expand(graph.transitiveDependencies)
.map((package) => package.name)
Expand All @@ -384,10 +386,10 @@ class DepsCommand extends PubCommand {
Future<void> _outputExecutables(StringBuffer buffer) async {
final graph = await entrypoint.packageGraph;
var packages = [
entrypoint.root,
entrypoint.workspaceRoot,
...(_includeDev
? entrypoint.root.immediateDependencies
: entrypoint.root.dependencies)
? entrypoint.workspaceRoot.immediateDependencies
: entrypoint.workspaceRoot.dependencies)
.keys
.map((name) => graph.packages[name]!),
];
Expand Down
Loading

0 comments on commit e519de6

Please sign in to comment.