doRun method
Implementation
@override
Future<int> doRun() async {
final config = findConfigAndSetWorkingDir();
final args = argResults!;
final isCheck = args[_argCheck] as bool;
final isJson = args.isJsonFormat();
final files =
_targetFiles(config.agents.files, args[_argFile] as List<String>);
printVerbose('Files: ${files.join(', ')}');
final facts = await ProjectFacts.collect(config);
final section = SectionRenderer.render(
facts: facts,
index: CommandIndex.build(runner!),
version: packageVersion,
);
final results = <String, _Status>{};
for (final path in files) {
final file = File(path);
final exists = file.existsSync();
if (!exists && isCheck) {
results[path] = _Status.missing;
continue;
}
final content = exists ? file.readAsStringSync() : '';
final String updated;
try {
updated = ManagedBlock.apply(content, section);
} on FormatException catch (e) {
return error(2, message: 'Can not update $path: ${e.message}');
}
if (content == updated) {
results[path] = _Status.unchanged;
continue;
}
if (isCheck) {
results[path] = _Status.outdated;
continue;
}
if (!exists) file.parent.createSync(recursive: true);
file.writeAsStringSync(updated);
results[path] = exists ? _Status.updated : _Status.created;
}
final hasProblems = results.values.any((s) => s.isProblem);
if (isJson) {
return jsonResult(
exitCode: hasProblems ? _kExitCodeOutdated : 0,
summary:
results.entries.map((e) => '${e.key}: ${e.value.key}').join(' | '),
data: <String, dynamic>{
'files': results.entries
.map((e) =>
<String, dynamic>{'path': e.key, 'status': e.value.key})
.toList(),
'facts': facts.toJson(),
},
);
} else {
results.forEach((path, status) {
printInfo('$path: ${status.key}');
});
if (hasProblems) {
printError('Agent instructions are outdated. '
'Run "alex agents init" to update them.');
}
}
return hasProblems ? _kExitCodeOutdated : 0;
}