updateTesterPubspec static method
Implementation
static Future<void> updateTesterPubspec(
String testerDir,
String testerName,
String packageName,
bool verbose,
) async {
final pubspecFile = File(path.join(testerDir, Constants.pubspecFileName));
if (!pubspecFile.existsSync()) {
if (verbose) {
stdout.writeln(
'${Constants.errorMessage} pubspec.yaml not found in tester');
}
return;
}
// Replace the entire content with our template content
final root = CopyUtils.findTemplatePath();
final templatePath = path.join(root, 'templates',
Constants.baseKitTesterTemplate, Constants.pubspecFileName);
final templateFile = File(templatePath);
String content;
if (templateFile.existsSync()) {
content = await templateFile.readAsString();
// Update project name
content =
content.replaceAll('name: base_kit_tester', 'name: $testerName');
// Update package dependency
content = content.replaceAll('base_kit_package:', '$packageName:');
content = content.replaceAll(
'path: ../base_kit_package', 'path: ../$packageName');
} else {
// Fallback to original content if template not found
content = await pubspecFile.readAsString();
content =
content.replaceAll('name: base_kit_tester', 'name: $testerName');
// Add package dependency if it doesn't exist
if (!content.contains('$packageName:')) {
final lines = content.split('\n');
final dependenciesIndex =
lines.indexWhere((line) => line.trim() == 'dependencies:');
if (dependenciesIndex != -1) {
// Add package dependency after flutter dependency
final flutterIndex = lines.indexWhere(
(line) => line.trim().startsWith('flutter:'), dependenciesIndex);
if (flutterIndex != -1) {
lines.insert(flutterIndex + 1, ' $packageName:');
lines.insert(flutterIndex + 2, ' path: ../$packageName');
} else {
// If flutter dependency not found, add after dependencies:
lines.insert(dependenciesIndex + 1, ' $packageName:');
lines.insert(dependenciesIndex + 2, ' path: ../$packageName');
}
content = lines.join('\n');
}
}
}
await pubspecFile.writeAsString(content);
if (verbose) {
stdout.writeln('${Constants.updateMessage} Updated tester pubspec.yaml');
}
}