resolveNitroNativePath function
Resolves the absolute path to the installed nitro package's src/native
directory by reading .dart_tool/package_config.json inside pluginDir.
Implementation
String resolveNitroNativePath(String pluginDir) {
// Walk up from pluginDir looking for .dart_tool/package_config.json.
// In Dart workspaces the config lives at the workspace root, not in each
// member's own directory.
var searchDir = Directory(pluginDir);
while (true) {
final configFile = File(p.join(searchDir.path, '.dart_tool', 'package_config.json'));
if (configFile.existsSync()) {
try {
final packages = switch (jsonDecode(configFile.readAsStringSync())) {
{'packages': final List<Object?> list} => list,
_ => const <Object?>[],
};
for (final pkg in packages) {
if (pkg case {'name': 'nitro', 'rootUri': final String rootUri}) {
// rootUri is a file: URI in some SDKs and relative to .dart_tool
// in others.
final root = switch (Uri.parse(rootUri)) {
final u when u.isScheme('file') => u.toFilePath(),
_ => p.normalize(p.join(searchDir.path, '.dart_tool', rootUri)),
};
return p.join(root, 'src', 'native');
}
}
} on FormatException catch (e) {
throw StateError('Failed to parse ${configFile.path} while resolving the nitro native path: ${e.message}');
} on FileSystemException catch (e) {
throw StateError('Failed to read ${configFile.path} while resolving the nitro native path: ${e.message}');
}
}
final parent = searchDir.parent;
if (parent.path == searchDir.path) break;
searchDir = parent;
}
return p.normalize(
p.absolute(p.join(pluginDir, '..', 'packages', 'nitro', 'src', 'native')),
);
}