getCurrentVersion static method
Get current version from pubspec.yaml or from installed package
Implementation
static String getCurrentVersion() {
// First, try to get version from the saved version file (most reliable)
final savedVersion = getInstalledVersionFromFile();
if (savedVersion != null) {
return savedVersion;
}
try {
// Second, try to get version from dart pub global list
try {
final result = Process.runSync(
'dart',
['pub', 'global', 'list'],
runInShell: true,
);
if (result.exitCode == 0) {
final output = result.stdout.toString();
// Look for vgv_cli in the output
final lines = output.split('\n');
for (final line in lines) {
if (line.contains('vgv_cli')) {
// Format is usually: "vgv_cli 1.10.6 from git ..."
final versionMatch = RegExp(r'vgv_cli\s+(\d+\.\d+\.\d+)').firstMatch(line);
if (versionMatch != null) {
final version = versionMatch.group(1)!;
// Only save if version file doesn't exist (don't overwrite existing)
if (getInstalledVersionFromFile() == null) {
saveInstalledVersion(version);
}
return version;
}
}
}
}
} catch (e) {
// Continue to fallback methods
}
// Fallback: try to find pubspec.yaml
final pubspecPath = _findPubspecPath();
if (pubspecPath != null) {
final file = File(pubspecPath);
if (file.existsSync()) {
final content = file.readAsStringSync();
final versionMatch = RegExp(r'version:\s*(\d+\.\d+\.\d+)').firstMatch(content);
if (versionMatch != null) {
final version = versionMatch.group(1)!;
// Only save if version file doesn't exist (don't overwrite existing)
if (getInstalledVersionFromFile() == null) {
saveInstalledVersion(version);
}
return version;
}
}
}
// Last resort: try to get from Git repository
try {
final gitVersion = getLatestCLIVersionFromGitSync();
if (gitVersion != null) {
// Don't save Git version as installed version, it's just a fallback
return gitVersion;
}
} catch (e) {
// Ignore errors
}
} catch (e) {
// Fallback to default version
}
return '1.0.0';
}