addEvent function
Adds one or more events to a BLoC.
If apiPath is provided and event is null or empty, it extracts all public methods
from the specified API class and generates an event for each.
Otherwise, it generates a single event specified by event.
Implementation
Future<void> addEvent(
String domain,
String? name,
String? event,
String? apiPath,
String? method, {
String? writeDir,
bool update = false,
}) async {
// multiple event generate
if (apiPath != null && (event == null || event.trim().isEmpty)) {
final methods = extractMethodListFromClass(apiPath);
if (methods.isNotEmpty) {
print(
'Found ${methods.length} methods in $apiPath. Generating events...',
);
List<dynamic> results = [];
for (final methodName in methods) {
if (methodName.endsWith("WithHttpInfo")) {
// skip ...WithHttpInfo openapi generated method
continue;
}
if (method != null &&
method.trim().isNotEmpty &&
methodName != method) {
// skip method that is not the one specified
continue;
}
results.add(
await _addSingleEvent(
domain,
name,
methodName,
apiPath,
methodName,
writeDir: writeDir,
update: update,
),
);
}
// run build runner and format
sleep(const Duration(milliseconds: 100));
if (results.isNotEmpty) {
String dir = "";
for (final result in results) {
dir = result.$1;
if (dir.isNotEmpty) {
break;
}
}
if (dir.isEmpty) {
printWarning("No events generated.");
return;
}
runBuildRunner(dir);
runDartFormat(dir);
}
printSuccess(' ✅ Finished generating events from $apiPath.');
} else {
printWarning(
'No public methods found in $apiPath to generate events from.',
);
}
return;
}
if (event == null || event.trim().isEmpty) {
printError(
' ❗️ Domain (or event) name is required (or apiPath must be provided).',
);
return;
}
// single event generate
final result = await _addSingleEvent(
domain,
name,
event,
apiPath,
method ?? event,
writeDir: writeDir,
update: update,
);
if (result.$1.isEmpty) {
printWarning("No events generated.");
return;
}
runBuildRunner(result.$1);
runDartFormat(result.$1);
if (apiPath != null && apiPath.trim().isNotEmpty) {
printSuccess(' ✅ Finished generating events from $apiPath.');
} else {
printSuccess(' ✅ Finished generating events.');
}
}