peekNextBuildNumber method
Peeks at what the next build number will be without modifying the state.
Returns a 3-digit zero-padded string (e.g., '001').
Implementation
Future<String> peekNextBuildNumber(String environment, {String? baseDir}) async {
final dirPath = baseDir != null ? '$baseDir/$_stateDir' : _stateDir;
final filePath = '$dirPath/$_stateFile';
final file = File(filePath);
if (!file.existsSync()) {
return '001';
}
try {
final contentBytes = file.readAsBytesSync();
String contentString = contentBytes.isEmpty ? '{}' : utf8.decode(contentBytes);
Map<String, dynamic> jsonMap;
try {
jsonMap = json.decode(contentString);
} catch (_) {
jsonMap = {};
}
final state = BuildStateModel.fromJson(jsonMap);
final today = DateFormat('yyyyMMdd').format(DateTime.now());
if (state.lastDate == today) {
final currentCounter = state.counters[environment] ?? 0;
return (currentCounter + 1).toString().padLeft(3, '0');
} else {
return '001';
}
} catch (e) {
return '001'; // Fallback safely
}
}