<commit_before>﻿using HeroesMatchData.Core.Properties;
using HeroesMatchData.Core.ReleaseNotes;
using HeroesMatchData.Data;
using NLog;
using Squirrel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace HeroesMatchData.Core.Updater
{
    public class AutoUpdater
    {
        private static Logger UpdaterLog = LogManager.GetLogger("UpdateLogFile");

        private UpdateManager UpdateManager;
        private UpdateInfo UpdateInfo;

        private IDatabaseService Database;

        public AutoUpdater(IDatabaseService database)
        {
            Database = database;
        }

        public Version CurrentVersion { get; private set; }
        public string CurrentVersionString => CurrentVersion != null ? $"{CurrentVersion.Major}.{CurrentVersion.Minor}.{CurrentVersion.Build}" : string.Empty;
        public Version LatestVersion { get; private set; }
        public string LatestVersionString => LatestVersion != null ? $"{LatestVersion.Major}.{LatestVersion.Minor}.{LatestVersion.Build}" : string.Empty;

        public static async Task RetrieveReleaseNotes(IDatabaseService database)
        {
            try
            {
                ReleaseNoteHandler releaseNoteHandler = new ReleaseNoteHandler(database);
                await releaseNoteHandler.InitializeClient();

                // save the needed release notes
                releaseNoteHandler.AddApplyReleasesReleaseNotes();
            }
            catch (Exception ex)
            {
                UpdaterLog.Log(LogLevel.Info, ex);
            }
        }

        public static void CopyDatabasesToLatestRelease()
        {
            string newAppDirectory = Path.Combine(Settings.Default.NewLatestDirectory, Data.Properties.Settings.Default.DatabaseFolderName);
            string rootDirectory = Directory.GetParent(Settings.Default.NewLatestDirectory).FullName;
            string backupDirectory = Path.Combine(rootDirectory, "BackupDatabases");

            List<string> databaseFiles = new List<string>
            {
                Data.Properties.Settings.Default.ReplaysDbFileName,
                Data.Properties.Settings.Default.SettingsDbFileName,
                Data.Properties.Settings.Default.ReleaseNotesDbFileName,
            };

            foreach (var dbFile in databaseFiles)
            {
                string dbFilePath = $@"{Data.Properties.Settings.Default.DatabaseFolderName}\{dbFile}";

                try
                {
                    if (!File.Exists(dbFilePath))
                    {
                        UpdaterLog.Log(LogLevel.Info, $"[{dbFile}] Database file not found: {dbFilePath}");
                        UpdaterLog.Log(LogLevel.Info, $"[{dbFile}] Nothing to copy");

                        continue;
                    }

                    Directory.CreateDirectory(newAppDirectory);

                    File.Copy(dbFilePath, Path.Combine(newAppDirectory, dbFile));

                    UpdaterLog.Log(LogLevel.Info, $"[{dbFile}] Database file copied to: {Path.Combine(newAppDirectory, dbFile)}");

                    // backup database
                    if (!Directory.Exists(backupDirectory))
                        Directory.CreateDirectory(backupDirectory);

                    File.Copy(dbFilePath, Path.Combine(backupDirectory, dbFile), true);
                    UpdaterLog.Log(LogLevel.Info, $"[{dbFile}] Database file backup copied to: {Path.Combine(backupDirectory, dbFile)}");
                    UpdaterLog.Log(LogLevel.Info, "==============================================================");
                }
                catch (Exception ex)
                {
                    UpdaterLog.Log(LogLevel.Info, ex);
                    throw;
                }
            }
        }

        /// <summary>
        /// Checks for updates, sets property UpdateInfo to null if no updates found. Returns true is update is available.
        /// </summary>
        /// <returns></returns>
        public async Task<bool> CheckForUpdates()
        {
            try
            {
                using (UpdateManager = await UpdateManager.GitHubUpdateManager(Settings.Default.UpdateUrl))
                {
                    UpdaterLog.Log(LogLevel.Info, "Update Check");

                    UpdateInfo update = await UpdateManager.CheckForUpdate();

                    CurrentVersion = update.CurrentlyInstalledVersion?.Version.Version;
                    LatestVersion = update.FutureReleaseEntry?.Version.Version;

                    UpdaterLog.Log(LogLevel.Info, $"Current Version: {CurrentVersionString}");
                    UpdaterLog.Log(LogLevel.Info, $"Latest Version: {LatestVersionString}");

                    if (CurrentVersion != null && CurrentVersion < LatestVersion)
                    {
                        UpdateInfo = update;
                        return true;
                    }
                    else
                    {
                        UpdateInfo = null;
                        return false;
                    }<commit_msg>Allow updates to prelease<commit_after>﻿using HeroesMatchData.Core.Properties;
using HeroesMatchData.Core.ReleaseNotes;
using HeroesMatchData.Data;
using NLog;
using Squirrel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace HeroesMatchData.Core.Updater
{
    public class AutoUpdater
    {
        private static Logger UpdaterLog = LogManager.GetLogger("UpdateLogFile");

        private UpdateManager UpdateManager;
        private UpdateInfo UpdateInfo;

        private IDatabaseService Database;

        public AutoUpdater(IDatabaseService database)
        {
            Database = database;
        }

        public Version CurrentVersion { get; private set; }
        public string CurrentVersionString => CurrentVersion != null ? $"{CurrentVersion.Major}.{CurrentVersion.Minor}.{CurrentVersion.Build}" : string.Empty;
        public Version LatestVersion { get; private set; }
        public string LatestVersionString => LatestVersion != null ? $"{LatestVersion.Major}.{LatestVersion.Minor}.{LatestVersion.Build}" : string.Empty;

        public static async Task RetrieveReleaseNotes(IDatabaseService database)
        {
            try
            {
                ReleaseNoteHandler releaseNoteHandler = new ReleaseNoteHandler(database);
                await releaseNoteHandler.InitializeClient();

                // save the needed release notes
                releaseNoteHandler.AddApplyReleasesReleaseNotes();
            }
            catch (Exception ex)
            {
                UpdaterLog.Log(LogLevel.Info, ex);
            }
        }

        public static void CopyDatabasesToLatestRelease()
        {
            string newAppDirectory = Path.Combine(Settings.Default.NewLatestDirectory, Data.Properties.Settings.Default.DatabaseFolderName);
            string rootDirectory = Directory.GetParent(Settings.Default.NewLatestDirectory).FullName;
            string backupDirectory = Path.Combine(rootDirectory, "BackupDatabases");

            List<string> databaseFiles = new List<string>
            {
                Data.Properties.Settings.Default.ReplaysDbFileName,
                Data.Properties.Settings.Default.SettingsDbFileName,
                Data.Properties.Settings.Default.ReleaseNotesDbFileName,
            };

            foreach (var dbFile in databaseFiles)
            {
                string dbFilePath = $@"{Data.Properties.Settings.Default.DatabaseFolderName}\{dbFile}";

                try
                {
                    if (!File.Exists(dbFilePath))
                    {
                        UpdaterLog.Log(LogLevel.Info, $"[{dbFile}] Database file not found: {dbFilePath}");
                        UpdaterLog.Log(LogLevel.Info, $"[{dbFile}] Nothing to copy");

                        continue;
                    }

                    Directory.CreateDirectory(newAppDirectory);

                    File.Copy(dbFilePath, Path.Combine(newAppDirectory, dbFile));

                    UpdaterLog.Log(LogLevel.Info, $"[{dbFile}] Database file copied to: {Path.Combine(newAppDirectory, dbFile)}");

                    // backup database
                    if (!Directory.Exists(backupDirectory))
                        Directory.CreateDirectory(backupDirectory);

                    File.Copy(dbFilePath, Path.Combine(backupDirectory, dbFile), true);
                    UpdaterLog.Log(LogLevel.Info, $"[{dbFile}] Database file backup copied to: {Path.Combine(backupDirectory, dbFile)}");
                    UpdaterLog.Log(LogLevel.Info, "==============================================================");
                }
                catch (Exception ex)
                {
                    UpdaterLog.Log(LogLevel.Info, ex);
                    throw;
                }
            }
        }

        /// <summary>
        /// Checks for updates, sets property UpdateInfo to null if no updates found. Returns true is update is available.
        /// </summary>
        /// <returns></returns>
        public async Task<bool> CheckForUpdates()
        {
            try
            {
                using (UpdateManager = await UpdateManager.GitHubUpdateManager(Settings.Default.UpdateUrl, prerelease: true))
                {
                    UpdaterLog.Log(LogLevel.Info, "Update Check");

                    UpdateInfo update = await UpdateManager.CheckForUpdate();

                    CurrentVersion = update.CurrentlyInstalledVersion?.Version.Version;
                    LatestVersion = update.FutureReleaseEntry?.Version.Version;

                    UpdaterLog.Log(LogLevel.Info, $"Current Version: {CurrentVersionString}");
                    UpdaterLog.Log(LogLevel.Info, $"Latest Version: {LatestVersionString}");

                    if (CurrentVersion != null && CurrentVersion < LatestVersion)
                    {
                        UpdateInfo = update;
                        return true;
                    }
                    else
                    {
                        UpdateInfo = null;
                        return false;
                    }
{'fraction_non_alphanumeric': 0.09798945930119071, 'fraction_numerical': 0.0, 'mean_word_length': 1.8228650137741047, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '2547260', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2308, 'n_words': 481}