|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:cli_spin/cli_spin.dart'; |
| 5 | +import 'package:cwa_plugin_core/cwa_plugin_core.dart'; |
| 6 | +import 'package:react_native/config/plugin_config.dart'; |
| 7 | + |
| 8 | +import '../../config/runtime_config.dart'; |
| 9 | +import '../../model/specification.dart'; |
| 10 | + |
| 11 | +class ReactNativeService extends Command { |
| 12 | + ReactNativeService(super.args); |
| 13 | + |
| 14 | + @override |
| 15 | + String get description => "Manage Services for React Native project."; |
| 16 | + |
| 17 | + @override |
| 18 | + Future<void> run() async { |
| 19 | + CWLogger.i.progress("Looking for available Services"); |
| 20 | + |
| 21 | + List<String>? dirs = await GitService.getGitLabBranches( |
| 22 | + ReactNativeConfig.i.archManagerProjectID, |
| 23 | + TokenService().accessToken!, |
| 24 | + ); |
| 25 | + |
| 26 | + if (dirs == null || dirs.isEmpty) { |
| 27 | + CWLogger.namedLog( |
| 28 | + 'No services found!', |
| 29 | + loggerColor: CWLoggerColor.yellow, |
| 30 | + ); |
| 31 | + exit(1); |
| 32 | + } |
| 33 | + List<String> utilsBranches = dirs |
| 34 | + .where((branch) => branch.startsWith('service/')) |
| 35 | + .map((branch) => branch.replaceFirst('service/', '')) |
| 36 | + .toList(); |
| 37 | + |
| 38 | + if (utilsBranches.isEmpty) { |
| 39 | + CWLogger.namedLog( |
| 40 | + 'No services found in the "service/" directory!', |
| 41 | + loggerColor: CWLoggerColor.yellow, |
| 42 | + ); |
| 43 | + exit(1); |
| 44 | + } |
| 45 | + |
| 46 | + CWLogger.i.stdout("Please select the Service you want to use :"); |
| 47 | + Menu featureMenu = Menu(utilsBranches); |
| 48 | + int idx = featureMenu.choose().index; |
| 49 | + |
| 50 | + String selectedService = 'service/${utilsBranches[idx]}'; |
| 51 | + |
| 52 | + await _handleServiceAndSpecification(selectedService); |
| 53 | + } |
| 54 | + |
| 55 | + Future<void> _handleServiceAndSpecification(String serviceName) async { |
| 56 | + CliSpin featureLoader = |
| 57 | + CliSpin(text: "Adding $serviceName to the project").start(); |
| 58 | + |
| 59 | + try { |
| 60 | + String filePath = 'specification_config.json'; |
| 61 | + String? specsFileContent = await GitService.getGitLabFileContent( |
| 62 | + projectId: ReactNativeConfig.i.pilotRepoProjectID, |
| 63 | + filePath: filePath, |
| 64 | + branch: serviceName, |
| 65 | + token: TokenService().accessToken!, |
| 66 | + ); |
| 67 | + |
| 68 | + if (specsFileContent != null) { |
| 69 | + Map<String, dynamic> specificationData = json.decode(specsFileContent); |
| 70 | + List<dynamic> serviceFilePaths = specificationData['filePath']; |
| 71 | + for (String serviceFilePath in serviceFilePaths) { |
| 72 | + String? fileContent = await GitService.getGitLabFileContent( |
| 73 | + projectId: ReactNativeConfig.i.pilotRepoProjectID, |
| 74 | + filePath: serviceFilePath, |
| 75 | + branch: serviceName, |
| 76 | + token: TokenService().accessToken!, |
| 77 | + ); |
| 78 | + |
| 79 | + if (fileContent != null) { |
| 80 | + String localFilePath = |
| 81 | + '${RuntimeConfig().commandExecutionPath}/$serviceFilePath'; |
| 82 | + File localFile = File(localFilePath); |
| 83 | + |
| 84 | + if (localFile.existsSync()) { |
| 85 | + CWLogger.i |
| 86 | + .trace('$serviceFilePath already exists, skipping download.'); |
| 87 | + } else { |
| 88 | + await localFile.create(recursive: true); |
| 89 | + await localFile.writeAsString(fileContent); |
| 90 | + CWLogger.i.trace('Downloaded $serviceFilePath successfully.'); |
| 91 | + } |
| 92 | + } else { |
| 93 | + CWLogger.i.trace('Failed to fetch $serviceFilePath.'); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + await SpecificationUpdater.updateSpecifications(serviceName); |
| 98 | + |
| 99 | + featureLoader.success(); |
| 100 | + } else { |
| 101 | + CWLogger.i.trace('Failed to fetch specification_config.json'); |
| 102 | + } |
| 103 | + } catch (e) { |
| 104 | + featureLoader.fail(); |
| 105 | + CWLogger.namedLog( |
| 106 | + e.toString(), |
| 107 | + loggerColor: CWLoggerColor.red, |
| 108 | + ); |
| 109 | + CWLogger.i.trace( |
| 110 | + 'Error downloading service $serviceName or updating specifications: $e'); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments