Exemplo n.º 1
0
        public bool AddLanguage(string language)
        {
            if (_configData == null || _configDataJsonNode == null)
            {
                _errorOutput.WriteLine("Error: Database not loaded");
                return(false);
            }
            if (_configDataJsonNode.Languages.Contains(language))
            {
                _errorOutput.WriteLine("Error: Language alreaady exist");
                return(false);
            }
            string translationJsonPath    = _fileSourceSettings.DatabaseTranslateFilePath(language);
            var    newTranslationJsonNode = ConfigDatabase.LoadFromFile <ConfigDataTranslateJsonNode>(translationJsonPath);

            ConfigDataJsonNode.Builder builder = new ConfigDataJsonNode.Builder(_configDataJsonNode);
            builder.Languages.Add(language);
            var updatedJsonNode = builder.Build();

            using var transation = ConfigSaveTransaction.Create(updatedJsonNode, _fileSourceSettings.DatabaseFilePath);
            if (!transation.Apply())
            {
                _errorOutput.WriteLine("Error: Failed add language to list");
                return(false);
            }
            _configDataTranslateJsonNodes[language] = newTranslationJsonNode;
            _configDataJsonNode = updatedJsonNode;
            transation.Commit();
            return(true);
        }
Exemplo n.º 2
0
        public bool RemoveLanguage(string language, bool deleteFile)
        {
            using var trasactionGroup = new TransactionGroup(2);
            if (deleteFile)
            {
                string translationJsonPath = _fileSourceSettings.DatabaseTranslateFilePath(language);
                if (File.Exists(translationJsonPath))
                {
                    trasactionGroup.Add(DeleteFileTransaction.Create(translationJsonPath));
                }
            }
            var updatedJsonNode = _configDataJsonNode;

            if (updatedJsonNode != null && updatedJsonNode.Languages.Contains(language))
            {
                var builder = new ConfigDataJsonNode.Builder(updatedJsonNode);
                builder.Languages.Remove(language);
                updatedJsonNode = builder.Build();
                trasactionGroup.Add(ConfigSaveTransaction.Create(updatedJsonNode, _fileSourceSettings.DatabaseFilePath));
            }
            if (!trasactionGroup.Apply())
            {
                _errorOutput.WriteLine("Error: Failed remove language");
                return(false);
            }
            _configDataTranslateJsonNodes.Remove(language);
            _configDataJsonNode = updatedJsonNode;
            trasactionGroup.Commit();
            return(true);
        }
Exemplo n.º 3
0
        public static ConfigDataJsonNode BuildNode(ConfigData data, ISet <string> languages,
                                                   INetworkSourceSettings?schemaSource = null)
        {
            // build commands node
            var commandsJsonBuilder = new CommandsJsonNode.Builder();

            foreach (var commandCategoryPair in data.CommandCategories)
            {
                var categoryBuilder = new CategoryJsonNode.Builder(commandCategoryPair.Key,
                                                                   commandCategoryPair.Value.Name);
                commandsJsonBuilder.Categories.Add(categoryBuilder.Build());
                foreach (var commandPair in commandCategoryPair.Value.Commands)
                {
                    var command        = commandPair.Value;
                    var commandBuilder = new CommandJsonNode.Builder(command.Key,
                                                                     command.Name, commandCategoryPair.Key);
                    commandBuilder.Description = command.Description;
                    if (command.Parameters != null)
                    {
                        foreach (var parameter in command.Parameters)
                        {
                            var paramBuilder = new ParamJsonNode.Builder(parameter.Name,
                                                                         parameter.GetValuesNode());
                            paramBuilder.Description = parameter.Description;
                            commandBuilder.Parameters.Add(paramBuilder.Build());
                        }
                    }
                    commandsJsonBuilder.Items.Add(commandBuilder.Build());
                }
            }
            // build settings node
            var settingsJsonBuilder = new SettingsJsonNode.Builder();

            foreach (var settingCategoryPair in data.SettingCategories)
            {
                var categoryBuilder = new CategoryJsonNode.Builder(settingCategoryPair.Key,
                                                                   settingCategoryPair.Value.Name);
                settingsJsonBuilder.Categories.Add(categoryBuilder.Build());
                foreach (var settingPair in settingCategoryPair.Value.Settings)
                {
                    var setting        = settingPair.Value;
                    var settingBuilder = new SettingJsonNode.Builder(setting.Key,
                                                                     setting.Name, settingCategoryPair.Key, setting.GetValuesNode());
                    settingBuilder.Description = setting.Description;
                    settingsJsonBuilder.Items.Add(settingBuilder.Build());
                }
            }
            // build final root node
            var builder = new ConfigDataJsonNode.Builder(commandsJsonBuilder.Build(),
                                                         settingsJsonBuilder.Build());

            if (schemaSource != null)
            {
                builder.Schema = schemaSource.JsonSchemaUrl;
            }
            builder.Languages.UnionWith(languages);
            return(builder.Build());
        }
Exemplo n.º 4
0
        public static ConfigDataJsonNode CreateNode(INetworkSourceSettings?schemaSource = null)
        {
            var commandsJsonBuilder = new CommandsJsonNode.Builder();
            var settingsJsonBuilder = new SettingsJsonNode.Builder();
            var builder             = new ConfigDataJsonNode.Builder(commandsJsonBuilder.Build(),
                                                                     settingsJsonBuilder.Build());

            if (schemaSource != null)
            {
                builder.Schema = schemaSource.JsonSchemaUrl;
            }
            return(builder.Build());
        }
Exemplo n.º 5
0
        public bool CreateLanguage(string language)
        {
            if (_configData == null || _configDataJsonNode == null ||
                _configDataJsonNode.Languages.Contains(language))
            {
                _errorOutput.WriteLine("Error: Database not loaded");
                return(false);
            }
            if (_configDataJsonNode.Languages.Contains(language))
            {
                _errorOutput.WriteLine("Error: Language name already exist");
                return(false);
            }
            string translationJsonPath = _fileSourceSettings.DatabaseTranslateFilePath(language);

            if (File.Exists(translationJsonPath))
            {
                _errorOutput.WriteLine("Error: Language folder already exist");
                return(false);
            }
            var newTranslationJsonNode = ConfigDatabase.BuildTranslateNode(_configData, _networkSourceSettings);

            ConfigDataJsonNode.Builder builder = new ConfigDataJsonNode.Builder(_configDataJsonNode);
            builder.Languages.Add(language);
            var updatedJsonNode = builder.Build();

            Directory.CreateDirectory(Path.GetDirectoryName(translationJsonPath));
            using var trasactionGroup = new TransactionGroup(2);
            trasactionGroup.Add(ConfigSaveTransaction.Create(newTranslationJsonNode, translationJsonPath));
            trasactionGroup.Add(ConfigSaveTransaction.Create(updatedJsonNode, _fileSourceSettings.DatabaseFilePath));
            if (!trasactionGroup.Apply())
            {
                _errorOutput.WriteLine("Error: Failed write language files");
                return(false);
            }
            _configDataTranslateJsonNodes[language] = newTranslationJsonNode;
            _configDataJsonNode = updatedJsonNode;
            trasactionGroup.Commit();
            return(true);
        }