findWorkspaceRoot static method
Finds the workspace root directory above the project directory.
Returns a tuple with the workspace root directory and a Pubspec
object
for its pubspec.yaml file.
Throws WorkspaceException if no workspace root is found.
Implementation
static (Directory, Pubspec) findWorkspaceRoot(
final Directory projectDir,
) {
var currentDir = projectDir.absolute;
do {
currentDir = currentDir.parent;
final pubspecFile = File(p.join(currentDir.path, 'pubspec.yaml'));
if (pubspecFile.existsSync()) {
try {
final pubspec = Pubspec.parse(pubspecFile.readAsStringSync());
if (pubspec.workspace != null) {
return (currentDir, pubspec);
}
} on Exception catch (e, s) {
_throwWorkspaceException(
message: 'Failed to parse $pubspecFile.',
nestedException: e,
nestedStackTrace: s,
);
}
}
} while (currentDir.path != currentDir.parent.path);
_throwWorkspaceException(messages: [
'Could not find the workspace root directory.',
'Ensure the project is part of a valid Dart workspace.',
]);
}