readPackageVersion function
Reads version: from this package's pubspec.yaml.
Resolves via Isolate.resolvePackageUri so it works for dart run, path/global
activation, and compiled snapshots (not only when Platform.script sits near pubspec).
Implementation
Future<String> readPackageVersion() async {
final fromPackage = await _versionFromResolvedPackageRoot();
if (fromPackage != null) return fromPackage;
final scriptPath = Platform.script.toFilePath();
var dir = Directory(path.dirname(scriptPath));
while (true) {
final version = await _readVersionFile(
File(path.join(dir.path, 'pubspec.yaml')),
);
if (version != null) return version;
final parent = dir.parent;
if (parent.path == dir.path) break;
dir = parent;
}
return 'unknown';
}