From 84716b69d5388f1ef299928f73dcb9e30b7e1917 Mon Sep 17 00:00:00 2001 From: Anas Fikhi Date: Wed, 6 Dec 2023 12:46:36 +0100 Subject: [PATCH] [ Add ] added comments for contorllers --- lib/src/etc/controllers/config_file.dart | 23 +++++++++++++++-------- lib/src/etc/controllers/json.dart | 3 +++ lib/src/etc/controllers/yaml.dart | 16 ++++++++++------ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/src/etc/controllers/config_file.dart b/lib/src/etc/controllers/config_file.dart index 91af6b8..08303d4 100644 --- a/lib/src/etc/controllers/config_file.dart +++ b/lib/src/etc/controllers/config_file.dart @@ -1,31 +1,37 @@ import 'dart:io'; import 'package:args/src/arg_results.dart'; +import 'package:langsync/src/etc/controllers/json.dart'; +import 'package:langsync/src/etc/controllers/yaml.dart'; import 'package:langsync/src/etc/extensions.dart'; import 'package:mason_logger/mason_logger.dart'; import 'package:meta/meta.dart'; -import 'package:langsync/src/etc/controllers/json.dart'; -import 'package:langsync/src/etc/controllers/yaml.dart'; - /// Decided and manages the LangSync config file controller. abstract class ConfigFileController { - /// Created the convenable [ConfigFileController] from the [argResults] of the CLI call. + /// Created the convenable [ConfigFileController] from the [argResults]. /// If no [argResults] are provided, the default controller is returned. static ConfigFileController fromArgResults(ArgResults argResults) { final isJson = argResults['json'] == true; final isYaml = argResults['yaml'] == true; - if (isJson) { - return JsonController(); - } else if (isYaml) { - return YamlController(); + final existentsControllers = _controllers.entries + .where( + (entry) => + isJson && entry.key.contains('json') || + isYaml && entry.key.contains('yaml'), + ) + .toList(); + + if (existentsControllers.isNotEmpty) { + return existentsControllers.first.value; } else { return defaultController; } } /// The controllers that are supported by LangSync, the goal of this implementation is to make it easy to locate the expected config file name of each and to be able to add more in the future. + @protected static final Map _controllers = [ YamlController(), JsonController(), @@ -88,6 +94,7 @@ abstract class ConfigFileController { } /// Validates the config file fields of LangSync, this is universal and static because it relies on the config file content parsed as a map. + @mustCallSuper bool validateConfigFields(Map parsedConfigAsMap) { final langsyncConfig = parsedConfigAsMap['langsync'] as Map?; diff --git a/lib/src/etc/controllers/json.dart b/lib/src/etc/controllers/json.dart index 6552214..f737019 100644 --- a/lib/src/etc/controllers/json.dart +++ b/lib/src/etc/controllers/json.dart @@ -6,9 +6,11 @@ import 'package:langsync/src/etc/extensions.dart'; /// A JSON config file controller. class JsonController extends ConfigFileController { + /// The config file reference. @override File get configFileRef => File('./langsync.json'); + /// The config parsed as a [Map]. @override Future> parsed() { return super.parsedConfigFileControllerContent( @@ -18,6 +20,7 @@ class JsonController extends ConfigFileController { ); } + /// Writes the new [config] to the config file. @override void writeNewConfig(Map config) { return super.writeToConfigFileController( diff --git a/lib/src/etc/controllers/yaml.dart b/lib/src/etc/controllers/yaml.dart index 04b56b2..d54dc48 100644 --- a/lib/src/etc/controllers/yaml.dart +++ b/lib/src/etc/controllers/yaml.dart @@ -4,10 +4,13 @@ import 'package:langsync/src/etc/controllers/config_file.dart'; import 'package:langsync/src/etc/extensions.dart'; import 'package:yaml/yaml.dart' as yaml; +/// A YAML config file controller. class YamlController extends ConfigFileController { + /// The config file reference. @override File get configFileRef => File('./langsync.yaml'); + /// The config parsed as a [Map]. @override Future> parsed() async { return super.parsedConfigFileControllerContent( @@ -17,19 +20,20 @@ class YamlController extends ConfigFileController { ); } + /// Writes the new [config] to the config file. @override void writeNewConfig(Map config) async { super.writeToConfigFileController('langsync:\n'); - - await _iterateAndWriteToConfigFileController(config); + return _iterateAndWriteToConfigFileController(config); } - Future _iterateAndWriteToConfigFileController( + /// Iterates over the [config] and writes it to the config file. + void _iterateAndWriteToConfigFileController( Map config, - ) async { - super.iterateOverConfig( + ) { + return super.iterateOverConfig( config, - callback: (entry) async { + callback: (entry) { if (entry.value is String) { if ((entry.value as String).isPathToFileOrFolder()) { super.writeToConfigFileController(