detectWorkspace function
Detects if the given directory is a Dart workspace root
Implementation
WorkspaceInfo? detectWorkspace(Directory root) {
final pubspecFile = File(path.join(root.path, 'pubspec.yaml'));
if (!pubspecFile.existsSync()) {
return null;
}
try {
final pubspecContent = pubspecFile.readAsStringSync();
final pubspec = loadYaml(pubspecContent) as Map;
// Check if workspace property exists
final workspace = pubspec['workspace'];
if (workspace == null) {
return null;
}
// Workspace can be a list of strings or a map with 'members' key
List<String> memberPaths = [];
if (workspace is YamlList) {
memberPaths = workspace.map((e) => e.toString()).toList();
} else if (workspace is Map && workspace.containsKey('members')) {
final members = workspace['members'];
if (members is YamlList) {
memberPaths = members.map((e) => e.toString()).toList();
}
}
if (memberPaths.isEmpty) {
return null;
}
return WorkspaceInfo(
root: root,
memberPaths: memberPaths,
);
} catch (e) {
logger.detail('Error detecting workspace: $e');
return null;
}
}