<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using FlubuCore.Context;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NuGet.Packaging;

namespace FlubuCore.Tasks.Text
{
    public class UpdateJsonFileTask : TaskBase
    {
        private readonly string _fileName;
        private readonly Dictionary<string, JValue> _updates = new Dictionary<string, JValue>();
        private string _output;
        private bool _failIfNotFound = true;
        private bool _failOnTypeMismatch;

        public UpdateJsonFileTask(string fileName)
        {
            _fileName = fileName;
        }

        public UpdateJsonFileTask Output(string fullFilePath)
        {
            _output = fullFilePath;
            return this;
        }

        public UpdateJsonFileTask Update(string path, string value)
        {
            _updates.Add(path, new JValue(value));
            return this;
        }

        public UpdateJsonFileTask Update(string path, int value)
        {
            _updates.Add(path, new JValue(value));
            return this;
        }

        public UpdateJsonFileTask Update(string path, long value)
        {
            _updates.Add(path, new JValue(value));
            return this;
        }

        public UpdateJsonFileTask Update(string path, double value)
        {
            _updates.Add(path, new JValue(value));
            return this;
        }

        public UpdateJsonFileTask Update(string path, decimal value)
        {
            _updates.Add(path, new JValue(value));
            return this;
        }

        public UpdateJsonFileTask Update(string path, DateTime value)
        {
            _updates.Add(path, new JValue(value));
            return this;
        }

        public UpdateJsonFileTask Update(params KeyValuePair<string, JValue>[] args)
        {
            _updates.AddRange(args);
            return this;
        }

        public UpdateJsonFileTask FailIfPropertyNotFound(bool fail)
        {
            _failIfNotFound = fail;
            return this;
        }

        public UpdateJsonFileTask FailOnTypeMismatch(bool fail)
        {
            _failOnTypeMismatch = fail;
            return this;
        }

        protected override int DoExecute(ITaskContext context)
        {
            if (!File.Exists(_fileName))
            {
                context.Fail($"JSON file {_fileName} not found!", 1);
                return 1;
            }

            if (_updates.Count <= 0)
            {
                context.Fail($"Nothing to update in file {_fileName}!", 2);
                return 2;
            }

            context.LogInfo($"Update JSON file {_fileName} to file {_output ?? _fileName}");
            string file = File.ReadAllText(_fileName);
            JObject json = JObject.Parse(file);
            int res = 0;
            foreach (KeyValuePair<string, JValue> pair in _updates)
            {
                JToken token = json.SelectToken(pair.Key, false);

                if (token == null)
                {
                    if (_failIfNotFound)
                        context.Fail($"Propety {pair.Key} not found in {_fileName}", 3);
                    else
                        context.LogInfo($"Propety {pair.Key} not found in {_fileName}");

                    res = 3;
                    break;
                }

                if (token.Type != pair.Value.Type)
                {
                    if (_failOnTypeMismatch)
                    {
                        context.Fail($"Propety {pair.Key} type mismatch.", 4);
                        break;
                    }

                    context.LogInfo($"Propety {pair.Key} type mismatch.");

                    res = 4;
                }

                context.LogInfo($"Replacing {token.Path} with {pair.Value}.");
                token.Replace(pair.Value);
            }

            if (string.IsNullOrEmpty(_output))<commit_msg>fixed additional properties replacement in json file<commit_after>﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Text;
using FlubuCore.Context;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NuGet.Packaging;

namespace FlubuCore.Tasks.Text
{
    public class UpdateJsonFileTask : TaskBase
    {
        private readonly string _fileName;
        private readonly Dictionary<string, JValue> _updates = new Dictionary<string, JValue>();
        private string _output;
        private bool _failIfNotFound = true;
        private bool _failOnTypeMismatch;

        public UpdateJsonFileTask(string fileName)
        {
            _fileName = fileName;
        }

        public UpdateJsonFileTask Output(string fullFilePath)
        {
            _output = fullFilePath;
            return this;
        }

        public UpdateJsonFileTask Update(string path, string value)
        {
            _updates.Add(path, new JValue(value));
            return this;
        }

        public UpdateJsonFileTask Update(string path, int value)
        {
            _updates.Add(path, new JValue(value));
            return this;
        }

        public UpdateJsonFileTask Update(string path, long value)
        {
            _updates.Add(path, new JValue(value));
            return this;
        }

        public UpdateJsonFileTask Update(string path, double value)
        {
            _updates.Add(path, new JValue(value));
            return this;
        }

        public UpdateJsonFileTask Update(string path, decimal value)
        {
            _updates.Add(path, new JValue(value));
            return this;
        }

        public UpdateJsonFileTask Update(string path, DateTime value)
        {
            _updates.Add(path, new JValue(value));
            return this;
        }

        public UpdateJsonFileTask Update(params KeyValuePair<string, JValue>[] args)
        {
            _updates.AddRange(args);
            return this;
        }

        public UpdateJsonFileTask FailIfPropertyNotFound(bool fail)
        {
            _failIfNotFound = fail;
            return this;
        }

        public UpdateJsonFileTask FailOnTypeMismatch(bool fail)
        {
            _failOnTypeMismatch = fail;
            return this;
        }

        protected override int DoExecute(ITaskContext context)
        {
            if (!File.Exists(_fileName))
            {
                context.Fail($"JSON file {_fileName} not found!", 1);
                return 1;
            }

            if (_updates.Count <= 0)
            {
                context.Fail($"Nothing to update in file {_fileName}!", 2);
                return 2;
            }

            context.LogInfo($"Update JSON file {_fileName} to file {_output ?? _fileName}. With {_updates.Count} updates");
            string file = File.ReadAllText(_fileName);
            JObject json = JObject.Parse(file);
            int res = 0;

            foreach (KeyValuePair<string, JValue> pair in _updates)
            {
                JToken token = json.SelectToken(pair.Key, false);

                if (token == null)
                {
                    if (_failIfNotFound)
                        context.Fail($"Propety {pair.Key} not found in {_fileName}", 3);
                    else
                        context.LogInfo($"Propety {pair.Key} not found in {_fileName}");

                    res = 3;
                    continue;
                }

                if (token.Type != pair.Value.Type)
                {
                    if (_failOnTypeMismatch)
                    {
                        context.Fail($"Propety {pair.Key} type mismatch.", 4);
                        continue;
                    }

                    context.LogInfo($"Propety {pair.Key} type mismatch.");

                    res = 4;
                }

                context.LogInfo($"Replacing {token.Path} with {pair.Value}.");
                token.Replace(pair.Value);
            }

            if (string.IsNullOrEmpty(_output))
{'fraction_non_alphanumeric': 0.08491149894788959, 'fraction_numerical': 0.0024755539051862854, 'mean_word_length': 1.5093167701863355, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3005309', 'n_tokens_mistral': 2117, 'n_tokens_neox': 2027, 'n_words': 506}