Skip to content

Commit 04d2dcf

Browse files
Component's have been added download has been separated for directory and files.
1 parent 784092e commit 04d2dcf

File tree

7 files changed

+259
-150
lines changed

7 files changed

+259
-150
lines changed

lib/commands/add/add_component.dart

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import 'package:cli_spin/cli_spin.dart';
55
import 'package:cwa_plugin_core/cwa_plugin_core.dart';
66
import 'package:react_native/config/plugin_config.dart';
77

8-
import '../../config/runtime_config.dart';
98
import '../../model/specification.dart';
9+
import '../../utils/download_manager.dart';
1010

1111
class ReactNativeComponent extends Command {
1212
ReactNativeComponent(super.args);
1313

1414
@override
1515
String get description => "Manage Components for React Native project.";
16-
16+
DownloadManager downloadManager = DownloadManager();
1717
@override
1818
Future<void> run() async {
1919
CWLogger.i.progress("Looking for available Components");
@@ -75,14 +75,14 @@ class ReactNativeComponent extends Command {
7575
// Handle file paths if available
7676
if (componentFilePaths.isNotEmpty) {
7777
for (String componentFilePath in componentFilePaths) {
78-
await _downloadFile(componentFilePath, componentName);
78+
await downloadManager.downloadFile(componentFilePath, componentName);
7979
}
8080
}
8181

8282
// Handle folder paths if available
8383
if (componentFolderPaths.isNotEmpty) {
8484
for (String componentFolderPath in componentFolderPaths) {
85-
await _downloadDirectory(componentFolderPath, componentName);
85+
await downloadManager.downloadDirectory(componentFolderPath, componentName);
8686
}
8787
}
8888

@@ -103,45 +103,4 @@ class ReactNativeComponent extends Command {
103103
}
104104
}
105105

106-
// Method to download individual file
107-
Future<void> _downloadFile(String componentFilePath, String componentName) async {
108-
String? fileContent = await GitService.getGitLabFileContent(
109-
projectId: ReactNativeConfig.i.pilotRepoProjectID,
110-
filePath: componentFilePath,
111-
branch: componentName,
112-
token: TokenService().accessToken!,
113-
);
114-
115-
if (fileContent != null) {
116-
String localFilePath =
117-
'${RuntimeConfig().commandExecutionPath}/$componentFilePath';
118-
File localFile = File(localFilePath);
119-
120-
if (localFile.existsSync()) {
121-
CWLogger.i.trace('$componentFilePath already exists, skipping download.');
122-
} else {
123-
await localFile.create(recursive: true);
124-
await localFile.writeAsString(fileContent);
125-
CWLogger.i.trace('Downloaded $componentFilePath successfully.');
126-
}
127-
} else {
128-
CWLogger.i.trace('Failed to fetch $componentFilePath.');
129-
}
130-
}
131-
132-
// Method to download a directory
133-
Future<void> _downloadDirectory(String componentFolderPath, String componentName) async {
134-
try {
135-
await GitService.downloadDirectoryContents(
136-
projectId: ReactNativeConfig.i.pilotRepoProjectID,
137-
branch: componentName,
138-
directoryPath: componentFolderPath,
139-
downloadPathBase: RuntimeConfig().commandExecutionPath,
140-
accessToken: TokenService().accessToken!,
141-
);
142-
CWLogger.i.trace('Downloaded directory $componentFolderPath successfully.');
143-
} catch (e) {
144-
CWLogger.i.trace('Failed to download directory $componentFolderPath: $e');
145-
}
146-
}
147106
}

lib/commands/add/add_features.dart

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import 'package:cli_spin/cli_spin.dart';
55
import 'package:cwa_plugin_core/cwa_plugin_core.dart';
66
import 'package:react_native/config/plugin_config.dart';
77

8-
import '../../config/runtime_config.dart';
98
import '../../model/specification.dart';
9+
import '../../utils/download_manager.dart';
1010

1111
class ReactNativeFeature extends Command {
1212
ReactNativeFeature(super.args);
1313

1414
@override
1515
String get description => "";
16+
DownloadManager downloadManager = DownloadManager();
1617

1718
@override
1819
Future<void> run() async {
@@ -77,14 +78,14 @@ class ReactNativeFeature extends Command {
7778
// Handle file paths if available
7879
if (featuresFilePaths.isNotEmpty) {
7980
for (String featureFilePath in featuresFilePaths) {
80-
await _downloadFile(featureFilePath, featureName);
81+
await downloadManager.downloadFile(featureFilePath, featureName);
8182
}
8283
}
8384

8485
// Handle folder paths if available
8586
if (featuresFolderPaths.isNotEmpty) {
8687
for (String featureFolderPath in featuresFolderPaths) {
87-
await _downloadDirectory(featureFolderPath, featureName);
88+
await downloadManager.downloadDirectory(featureFolderPath, featureName);
8889
}
8990
}
9091

@@ -101,45 +102,4 @@ class ReactNativeFeature extends Command {
101102
}
102103
}
103104

104-
// Method to download individual file
105-
Future<void> _downloadFile(String featureFilePath, String featureName) async {
106-
String? fileContent = await GitService.getGitLabFileContent(
107-
projectId: ReactNativeConfig.i.pilotRepoProjectID,
108-
filePath: featureFilePath,
109-
branch: featureName,
110-
token: TokenService().accessToken!,
111-
);
112-
113-
if (fileContent != null) {
114-
String localFilePath =
115-
'${RuntimeConfig().commandExecutionPath}/$featureFilePath';
116-
File localFile = File(localFilePath);
117-
118-
if (localFile.existsSync()) {
119-
CWLogger.i.trace('$featureFilePath already exists, skipping download.');
120-
} else {
121-
await localFile.create(recursive: true);
122-
await localFile.writeAsString(fileContent);
123-
CWLogger.i.trace('Downloaded $featureFilePath successfully.');
124-
}
125-
} else {
126-
CWLogger.i.trace('Failed to fetch $featureFilePath.');
127-
}
128-
}
129-
130-
// Method to download a directory
131-
Future<void> _downloadDirectory(String featureFilePath, String featureName) async {
132-
try {
133-
await GitService.downloadDirectoryContents(
134-
projectId: ReactNativeConfig.i.pilotRepoProjectID,
135-
branch: featureName,
136-
directoryPath: featureFilePath,
137-
downloadPathBase: RuntimeConfig().commandExecutionPath,
138-
accessToken: TokenService().accessToken!,
139-
);
140-
CWLogger.i.trace('Downloaded directory $featureFilePath successfully.');
141-
} catch (e) {
142-
CWLogger.i.trace('Failed to download directory $featureFilePath: $e');
143-
}
144-
}
145105
}

lib/commands/add/add_service.dart

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import 'package:cli_spin/cli_spin.dart';
55
import 'package:cwa_plugin_core/cwa_plugin_core.dart';
66
import 'package:react_native/config/plugin_config.dart';
77

8-
import '../../config/runtime_config.dart';
98
import '../../model/specification.dart';
9+
import '../../utils/download_manager.dart';
1010

1111
class ReactNativeService extends Command {
1212
ReactNativeService(super.args);
1313

1414
@override
1515
String get description => "Manage Services for React Native project.";
16+
DownloadManager downloadManager = DownloadManager();
1617

1718
@override
1819
Future<void> run() async {
@@ -67,30 +68,22 @@ class ReactNativeService extends Command {
6768

6869
if (specsFileContent != null) {
6970
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.');
71+
72+
// Get file and folder paths from the config
73+
List<dynamic> serviceFilePaths = specificationData['filePath'] ?? [];
74+
List<dynamic> serviceFolderPaths = specificationData['folderPath'] ?? [];
75+
76+
// Handle file paths
77+
if (serviceFilePaths.isNotEmpty) {
78+
for (String serviceFilePath in serviceFilePaths) {
79+
await downloadManager.downloadFile(serviceFilePath, serviceName);
80+
}
81+
}
82+
83+
// Handle folder paths
84+
if (serviceFolderPaths.isNotEmpty) {
85+
for (String serviceFolderPath in serviceFolderPaths) {
86+
await downloadManager.downloadDirectory(serviceFolderPath, serviceName);
9487
}
9588
}
9689

@@ -110,4 +103,6 @@ class ReactNativeService extends Command {
110103
'Error downloading service $serviceName or updating specifications: $e');
111104
}
112105
}
106+
107+
113108
}

lib/commands/add/add_utils.dart

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import 'package:cli_spin/cli_spin.dart';
55
import 'package:cwa_plugin_core/cwa_plugin_core.dart';
66
import 'package:react_native/config/plugin_config.dart';
77

8-
import '../../config/runtime_config.dart';
98
import '../../model/specification.dart';
9+
import '../../utils/download_manager.dart';
1010

1111
class ReactNativeUtils extends Command {
1212
ReactNativeUtils(super.args);
1313

1414
@override
1515
String get description => "Manage utilities for React Native project.";
16+
DownloadManager downloadManager = DownloadManager();
1617

1718
@override
1819
Future<void> run() async {
@@ -54,7 +55,7 @@ class ReactNativeUtils extends Command {
5455

5556
Future<void> _handleUtilityAndSpecification(String utilityName) async {
5657
CliSpin featureLoader =
57-
CliSpin(text: "Adding $utilityName to the project").start();
58+
CliSpin(text: "Adding $utilityName to the project").start();
5859

5960
try {
6061
String filePath = 'specification_config.json';
@@ -68,31 +69,21 @@ class ReactNativeUtils extends Command {
6869
if (specsFileContent != null) {
6970
Map<String, dynamic> specificationData = json.decode(specsFileContent);
7071

71-
List<dynamic> utilityFilePaths = specificationData['filePath'];
72-
73-
for (String utilityFilePath in utilityFilePaths) {
74-
String? fileContent = await GitService.getGitLabFileContent(
75-
projectId: ReactNativeConfig.i.pilotRepoProjectID,
76-
filePath: utilityFilePath,
77-
branch: utilityName,
78-
token: TokenService().accessToken!,
79-
);
80-
81-
if (fileContent != null) {
82-
String localFilePath =
83-
'${RuntimeConfig().commandExecutionPath}/$utilityFilePath';
84-
File localFile = File(localFilePath);
85-
86-
if (localFile.existsSync()) {
87-
CWLogger.i
88-
.trace('$utilityFilePath already exists, skipping download.');
89-
} else {
90-
await localFile.create(recursive: true);
91-
await localFile.writeAsString(fileContent);
92-
CWLogger.i.trace('Downloaded $utilityFilePath successfully.');
93-
}
94-
} else {
95-
CWLogger.i.trace('Failed to fetch $utilityFilePath.');
72+
// Get the filePath and folderPath from the config
73+
List<dynamic> utilityFilePaths = specificationData['filePath'] ?? [];
74+
List<dynamic> utilityFolderPaths = specificationData['folderPath'] ?? [];
75+
76+
// Handle file paths if available
77+
if (utilityFilePaths.isNotEmpty) {
78+
for (String utilityFilePath in utilityFilePaths) {
79+
await downloadManager.downloadFile(utilityFilePath, utilityName);
80+
}
81+
}
82+
83+
// Handle folder paths if available
84+
if (utilityFolderPaths.isNotEmpty) {
85+
for (String utilityFolderPath in utilityFolderPaths) {
86+
await downloadManager.downloadDirectory(utilityFolderPath, utilityName);
9687
}
9788
}
9889

@@ -108,4 +99,5 @@ class ReactNativeUtils extends Command {
10899
'Error downloading utility $utilityName or updating specifications: $e');
109100
}
110101
}
102+
111103
}

lib/config/plugin_config.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ class ReactNativeConfig extends PluginConfig {
2121
PluginEnvironment get pluginEnvironment => PluginEnvironment.prod;
2222

2323
@override
24-
Version get version => Version.parse("1.0.1");
24+
Version get version => Version.parse("1.5.0");
2525
}

0 commit comments

Comments
 (0)