<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Text.RegularExpressions;
using Microsoft.Win32;

namespace AlephNote.AutoUpdater
{
	public class ANUpdater
	{
		private static readonly string[] FILEPATTERN =
		{
			"AlephNote.exe",
			"*.dll",
			"Plugins/*.dll",
			"Themes/*.xml",
			"Themes/*.zip",
		};

		private readonly Action<int, int, string> _setState;
		private readonly Action<string, bool> _showMessage;
		private readonly Action<string> _fail;
		private readonly string _sourcePath;
		private readonly string _targetPath;

		private bool _deleteNotesFolder = false;

		public ANUpdater(Action<int, int, string> setFull, Action<string> fail, Action<string, bool> showMsg, string src, string dst)
		{
			_setState = setFull;
			_showMessage = showMsg;
			_fail = fail;
			_sourcePath = src;
			_targetPath = dst;
		}

		public void Run()
		{
			_setState(0, 100, "Enumerating files");

			List<Tuple<string, string>> files = FILEPATTERN
				.SelectMany(pattern => Directory
					.EnumerateFiles(Path.Combine(new[] { _sourcePath }.Concat(pattern.Split('/').Reverse().Skip(1).Reverse()).ToArray()), pattern.Split('/').Last())
					.Where(f => (Path.GetFileNameWithoutExtension(f) ?? "").ToLower() != "autoupdater")
					.Select(file => Tuple.Create(file, Path.Combine(_targetPath, MakeRelative(file, _sourcePath)))))
				.ToList();

			int max = 0;
			max += 1;           // stop running progresses
			max += 1;           // migrating
			max += files.Count; // copy
			max += 1;           // repo-migration
			max += 1;           // restarting

			int progress = 1;

			_setState(progress++, max, "Stop running process");
			KillProcess();

			_setState(progress++, max, "Migration");
			Migrate(out var versionOld);

			foreach (var ffile in files)
			{
				var file = ffile;

				_setState(progress++, max, Path.GetFileName(file.Item1));
				
				var start = Environment.TickCount;

				try
				{
					Directory.CreateDirectory(Path.GetDirectoryName(file.Item2) ?? "");
					File.Copy(file.Item1, file.Item2, true);
				}
				catch (Exception)
				{
					_fail("Could not copy " + Path.GetFileName(file.Item1));
				}

				var delta = 111 - (Environment.TickCount - start);
				if (delta > 0) Thread.Sleep(delta);
			}

			var versionNew = File.Exists(Path.Combine(_targetPath, "AlephNote.exe"))
				? new Version(FileVersionInfo.GetVersionInfo(Path.Combine(_targetPath, "AlephNote.exe")).FileVersion)
				: null;

			_setState(progress++, max, "Migrate repository");
			RepoMigrate(versionOld, versionNew);

			_setState(progress++, max, "Restarting");
			Process.Start(Path.Combine(_targetPath, "AlephNote.exe"), $"--updated --migration_from={versionOld?.ToString()??"NULL"} --migration_to={versionNew?.ToString()??"NULL"}");
			Thread.Sleep(500);<commit_msg>Delete old dll files in ANUpdater<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Text.RegularExpressions;
using Microsoft.Win32;

namespace AlephNote.AutoUpdater
{
	public class ANUpdater
	{
		private static readonly string[] FILEPATTERN =
		{
			"AlephNote.exe",
			"*.dll",
			"Plugins/*.dll",
			"Themes/*.xml",
			"Themes/*.zip",
		};

		private readonly Action<int, int, string> _setState;
		private readonly Action<string, bool> _showMessage;
		private readonly Action<string> _fail;
		private readonly string _sourcePath;
		private readonly string _targetPath;

		private bool _deleteNotesFolder = false;

		public ANUpdater(Action<int, int, string> setFull, Action<string> fail, Action<string, bool> showMsg, string src, string dst)
		{
			_setState = setFull;
			_showMessage = showMsg;
			_fail = fail;
			_sourcePath = src;
			_targetPath = dst;
		}

		public void Run()
		{
			_setState(0, 100, "Enumerating files");

			List<Tuple<string, string>> files = FILEPATTERN
				.SelectMany(pattern => Directory
					.EnumerateFiles(Path.Combine(new[] { _sourcePath }.Concat(pattern.Split('/').Reverse().Skip(1).Reverse()).ToArray()), pattern.Split('/').Last())
					.Where(f => (Path.GetFileNameWithoutExtension(f) ?? "").ToLower() != "autoupdater")
					.Select(file => Tuple.Create(file, Path.Combine(_targetPath, MakeRelative(file, _sourcePath)))))
				.ToList();

			List<string> fileDelete = Directory.EnumerateFiles(_targetPath) // old dll files in main directory
				.Where(p => p.ToLower().EndsWith(".dll"))
				.Where(p => !File.Exists(Path.Combine(_sourcePath, Path.GetFileName(p))))
				.ToList();

			int max = 0;
			max += 1;                // stop running progresses
			max += 1;                // migrating
			max += files.Count;      // copy
			max += fileDelete.Count; // delete
			max += 1;                // repo-migration
			max += 1;                // restarting

			int progress = 1;

			_setState(progress++, max, "Stop running process");
			KillProcess();

			_setState(progress++, max, "Migration");
			Migrate(out var versionOld);

			foreach (var ffile in files)
			{
				var file = ffile;

				_setState(progress++, max, Path.GetFileName(file.Item1));
				
				var start = Environment.TickCount;

				try
				{
					Directory.CreateDirectory(Path.GetDirectoryName(file.Item2) ?? "");
					File.Copy(file.Item1, file.Item2, true);
				}
				catch (Exception)
				{
					_fail("Could not copy " + Path.GetFileName(file.Item1));
				}

				var delta = 111 - (Environment.TickCount - start);
				if (delta > 0) Thread.Sleep(delta);
			}

			foreach (var dfile in fileDelete)
			{
				_setState(progress++, max, "Delete " + Path.GetFileName(dfile));
				
				var start = Environment.TickCount;

				try
				{
					File.Delete(dfile);
				}
				catch (Exception)
				{
					_fail("Could not delete " + Path.GetFileName(dfile));
				}

				var delta = 111 - (Environment.TickCount - start);
				if (delta > 0) Thread.Sleep(delta);
			}

			var versionNew = File.Exists(Path.Combine(_targetPath, "AlephNote.exe"))
				? new Version(FileVersionInfo.GetVersionInfo(Path.Combine(_targetPath, "AlephNote.exe")).FileVersion)
				: null;

			_setState(progress++, max, "Migrate repository");
			RepoMigrate(versionOld, versionNew);

			_setState(progress++, max, "Restarting");
			Process.Start(Path.Combine(_targetPath, "AlephNote.exe"), $"--updated --migration_from={versionOld?.ToString()??"NULL"} --migration_to={versionNew?.ToString()??"NULL"}");
			Thread.Sleep(500);
{'fraction_non_alphanumeric': 0.14611239414934565, 'fraction_numerical': 0.008314087759815243, 'mean_word_length': 3.5236768802228413, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6328354', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2055, 'n_words': 453}