// ignore_for_file: avoid_print import 'dart:convert'; import 'dart:io'; // Script Usage: modify_app.dart // Purpose: This script automates the modification of your 'app.dart' file after creating a new service using Stacked. // Instructions: // 1. Placement: Place this script in the root directory of your Flutter project. // 2. Execution: Execute this script immediately after using the command `stacked create service `. // Example: // ```bash // stacked create service auth && dart modify_app_file.dart auth void main(List arguments) async { // 1. Argument Handling: Verify that the script was provided with a service name. if (arguments.isEmpty) { print("Error: Please provide the name of the service created."); exit(1); } // 2. Confirmation Prompt bool confirmed = getConfirmation(); if (confirmed) { // 3. edit the app.dart file await _editAppFile(arguments); // 4. Delete the 'test' folder await _deleteTestFolder(); // 5. Run the "stacked generate" command await _runStackedGenerate(); } } /// Prompts the user for confirmation. bool getConfirmation() { print('Proceed with the following actions:\n' ' 1. Modify app.dart file\n' ' 2. Delete the test folder\n' ' 3. Run "stacked generate" command\n' 'Do you want to continue? (y/n): '); final confirm = stdin.readLineSync()?.toLowerCase(); return confirm == 'y'; } /// Edits the 'app.dart' file to import and register a newly created service. Future _editAppFile(List arguments) async { const appFilePath = 'lib/app/app.dart'; final serviceName = arguments.first; final serviceFilePath = 'lib/services/${serviceName}_service.dart'; // 2. File Existence Checks final appFile = File(appFilePath); if (!await appFile.exists()) { print("Error: $appFilePath not found."); exit(1); } final serviceFile = File(serviceFilePath); if (!await serviceFile.exists()) { print("Error: $serviceFilePath not found."); exit(1); } final importStatement = "import '../services/${serviceName}_service.dart';\n"; // Capitalize the first letter of serviceName final serviceNameCapitalized = serviceName.isNotEmpty ? serviceName[0].toUpperCase() + serviceName.substring(1) : serviceName; final registrationStatement = " LazySingleton(classType: ${serviceNameCapitalized}Service),\n"; try { final lines = await appFile.readAsLines(); // Find targets for insertion final importTargetIndex = lines.indexWhere((line) => line.contains('// @stacked-import')); final registrationTargetIndex = lines.indexWhere((line) => line.contains('// @stacked-service')); if (importTargetIndex != -1 && registrationTargetIndex != -1) { // Check if the import and registration statements already exist final importExists = lines.any((line) => line.trim() == importStatement.trim()); final registrationExists = lines.any((line) => line.trim() == registrationStatement.trim()); // Insert the import statement (if missing) if (!importExists) { lines.insert(importTargetIndex, importStatement); print('Import added to $appFilePath'); } // Insert the registration statement (if missing) if (!registrationExists) { lines.insert(registrationTargetIndex + 1, registrationStatement); print('LazySingleton registration added to $appFilePath'); } // Write changes to the file await appFile.writeAsString(lines.join('\n')); } else { print('Error: Could not find the necessary comments in $appFilePath'); } } catch (error) { print('Error modifying file: $error'); } } /// Deletes the 'test' folder if it exists. Future _deleteTestFolder() async { const testFolderPath = 'test'; // Path to the 'test' folder try { final testFolder = Directory(testFolderPath); if (await testFolder.exists()) { await testFolder.delete(recursive: true); print('Test folder deleted successfully!'); } else { print('Test folder not found.'); } } catch (error) { print('Error deleting folder: $error'); } } /// Executes the "stacked generate" command and prints its output. Future _runStackedGenerate() async { try { Process process = await Process.start('stacked', ['generate'], workingDirectory: Directory.current.path); // Stream the output and error logs process.stdout.transform(utf8.decoder).listen(print); process.stderr.transform(utf8.decoder).listen(print); // Handle exit code int exitCode = await process.exitCode; if (exitCode != 0) { print('Error running stacked generate (exit code $exitCode)'); } } catch (error) { print('Error executing command: $error'); } }