<commit_before>using LibGit2Sharp;
using Signum.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Signum.Upgrade
{
    public class CodeUpgradeRunner: IEnumerable<CodeUpgradeBase>
    {
        public List<CodeUpgradeBase> Upgrades = new List<CodeUpgradeBase>();

        public IEnumerator<CodeUpgradeBase> GetEnumerator() => Upgrades.GetEnumerator();
        IEnumerator IEnumerable.GetEnumerator() => Upgrades.GetEnumerator();

        public void Add(CodeUpgradeBase upgrade)
        {
            Upgrades.Add(upgrade);
        }

        public void Run(UpgradeContext uctx)
        {
            var upgradeFile = Path.Combine(uctx.RootFolder, "SignumUpgrade.txt");
            while (true)
            {
                SetExecuted(uctx, upgradeFile);

                if (!Prompt(uctx, upgradeFile))
                    return;
            }
        }

        void SetExecuted(UpgradeContext uctx, string upgradeFile)
        {
            Console.WriteLine();
            if (!File.Exists(upgradeFile))
            {
                SafeConsole.WriteLineColor(ConsoleColor.Yellow, $"File {upgradeFile} not found... let's create one!");
                Console.WriteLine();
                var result = Upgrades.ChooseConsole(a => a.Key, "What do you think is the next upgrade that you should run? (the previous ones will be marked as executed)");

                File.WriteAllLines(upgradeFile, result == null ? new string[0] : Upgrades.TakeWhile(a => a != result).Select(a => a.Key).ToArray());
                Console.WriteLine();
                SafeConsole.WriteLineColor(ConsoleColor.Green, $"File {upgradeFile} created!");
                SafeConsole.WriteLineColor(ConsoleColor.DarkGray, $"(this file contains the Upgrades that have been run, and should be commited to git)");
            }
            else
            {
                SafeConsole.WriteLineColor(ConsoleColor.DarkGray, $"Reading {upgradeFile}...");
            }

            var list = File.ReadAllLines(upgradeFile);
          
            foreach (var v in this.Upgrades)
            {
                v.IsExecuted = list.Contains(v.GetType().Name);
            }
        }

        bool Prompt(UpgradeContext uctx, string signumUpgradeFile)
        {
            Draw();

            if (Upgrades.All(a => a.IsExecuted))
            {
                SafeConsole.WriteLineColor(ConsoleColor.Green, "All Upgrades are executed!");

                return false;
            }<commit_msg>fix CodeUpgradeRunner insertion point<commit_after>using LibGit2Sharp;
using Signum.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Signum.Upgrade
{
    public class CodeUpgradeRunner: IEnumerable<CodeUpgradeBase>
    {
        public List<CodeUpgradeBase> Upgrades = new List<CodeUpgradeBase>();

        public IEnumerator<CodeUpgradeBase> GetEnumerator() => Upgrades.GetEnumerator();
        IEnumerator IEnumerable.GetEnumerator() => Upgrades.GetEnumerator();

        public void Add(CodeUpgradeBase upgrade)
        {
            Upgrades.Add(upgrade);
        }

        public void Run(UpgradeContext uctx)
        {
            var upgradeFile = Path.Combine(uctx.RootFolder, "SignumUpgrade.txt");
            while (true)
            {
                SetExecuted(uctx, upgradeFile);

                if (!Prompt(uctx, upgradeFile))
                    return;
            }
        }

        void SetExecuted(UpgradeContext uctx, string upgradeFile)
        {
            Console.WriteLine();
            if (!File.Exists(upgradeFile))
            {
                SafeConsole.WriteLineColor(ConsoleColor.Yellow, $"File {upgradeFile} not found... let's create one!");
                Console.WriteLine();
                var result = Upgrades.Select(a=>a.Key).And("<< Mark ALL upgrades as executed >>").ChooseConsole(a => a.ToString(), "What do you think is the next upgrade that you should run? (the previous ones will be marked as executed)");

                File.WriteAllLines(upgradeFile, result == null ? new string[0] : Upgrades.TakeWhile(a => a.Key != result).Select(a => a.Key).ToArray());
                Console.WriteLine();
                SafeConsole.WriteLineColor(ConsoleColor.Green, $"File {upgradeFile} created!");
                SafeConsole.WriteLineColor(ConsoleColor.DarkGray, $"(this file contains the Upgrades that have been run, and should be commited to git)");
            }
            else
            {
                SafeConsole.WriteLineColor(ConsoleColor.DarkGray, $"Reading {upgradeFile}...");
            }

            var list = File.ReadAllLines(upgradeFile);
          
            foreach (var v in this.Upgrades)
            {
                v.IsExecuted = list.Contains(v.GetType().Name);
            }
        }

        bool Prompt(UpgradeContext uctx, string signumUpgradeFile)
        {
            Draw();

            if (Upgrades.All(a => a.IsExecuted))
            {
                SafeConsole.WriteLineColor(ConsoleColor.Green, "All Upgrades are executed!");

                return false;
            }
{'fraction_non_alphanumeric': 0.101285248417418, 'fraction_numerical': 0.0007673124880107424, 'mean_word_length': 2.056271981242673, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 26, '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': '802051', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1335, 'n_words': 317}