Skip to content

Commit 381bdc6

Browse files
Added services
1 parent 65bf890 commit 381bdc6

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

lib/commands/add/add.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import 'package:cwa_plugin_core/cwa_plugin_core.dart';
44
import 'package:react_native/commands/add/add_features.dart';
55
import 'package:react_native/commands/add/add_utils.dart';
66

7+
import 'add_service.dart';
8+
79
class ReactNativeAdd extends Command {
810
ReactNativeAdd(super.args);
911

@@ -16,6 +18,7 @@ class ReactNativeAdd extends Command {
1618
CWLogger.i.stdout('Please select :');
1719
Menu menu = Menu([
1820
'Add a Utility',
21+
'Add a Service',
1922
'Add a Feature',
2023
]);
2124

@@ -25,6 +28,8 @@ class ReactNativeAdd extends Command {
2528
case 0:
2629
await ReactNativeUtils(args).run();
2730
case 1:
31+
await ReactNativeService(args).run();
32+
case 2:
2833
await ReactNativeFeature(args).run();
2934
default:
3035
exit(2);

lib/commands/add/add_service.dart

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}

lib/model/specification.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class SpecificationUpdater {
2828
await _updateAppJsonIos(specificationData['appJsonIos']);
2929
await _updateAppJsonAndroid(specificationData['appJsonAndroid']);
3030
await _updatePlugins(specificationData['plugins']);
31-
await _updateAppWrapper(specificationData['jsxModifications']);
31+
await _updateAppWrapper(specificationData['jsxModifications']??{});
3232
await PackageJsonUtils.runYarnInstall();
3333
} else {
3434
CWLogger.namedLog('Failed to fetch specification_config.json');

0 commit comments

Comments
 (0)