generateGitInfo function
Refresh the IDE file atomically so readers never see partially written JSON.
Implementation
Future<int> generateGitInfo(List<String> args) async {
if (args.length == 1 && (args.single == '--help' || args.single == '-h')) {
stdout.writeln('Usage: dart run ume:git_info [output.json]');
return 0;
}
if (args.length > 1) {
stderr.writeln('Usage: dart run ume:git_info [output.json]');
return 64;
}
final output = File(
args.isEmpty ? '.dart_tool/ume_git_info.json' : args.single,
);
Map<String, String> info;
try {
info = await captureGitInfo();
} catch (error) {
info = {};
stderr.writeln(
'UME: Git information unavailable; continuing without it: $error',
);
}
Directory? temporary;
try {
output.parent.createSync(recursive: true);
temporary = output.parent.createTempSync('.ume_git_');
final staged = File('${temporary.path}/git_info.json');
staged.writeAsStringSync(
'${const JsonEncoder.withIndent(' ').convert(info)}\n',
flush: true,
);
staged.renameSync(output.path);
stdout.writeln('UME Git info written to ${output.path}');
return 0;
} on FileSystemException catch (error) {
stderr.writeln('Failed to write UME Git info file: $error');
// Invalidate stale data where possible, without masking the original error.
try {
if (output.existsSync()) output.deleteSync();
} on FileSystemException catch (cleanupError) {
stderr.writeln('UME: Could not remove stale Git info: $cleanupError');
}
return 1;
} finally {
if (temporary != null) cleanupGitDirectory(temporary);
}
}