From 4a1d910c8ef5de75ab9cc24b0453fc26d918c0ba Mon Sep 17 00:00:00 2001 From: OrnarasUS <85742833+OrnarasUS@users.noreply.github.com> Date: Fri, 17 Jan 2025 10:50:07 +0400 Subject: [PATCH] 1.0.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Исправлена работа обновлений * Добавлены настройки интервала сохранения записей журнала в файл и показ их в консоли * Сценарий на авто-публикацию обновления --- .github/workflows/publish.yml | 40 +++++++++++++++++++++++++++++++++++ Managers/Logger.cs | 19 +++++++++++++---- Managers/Updater.cs | 19 +++++++++++------ OrnaLibs.Utils.csproj | 19 ++++++----------- 4 files changed, 74 insertions(+), 23 deletions(-) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..4164fd2 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,40 @@ +name: Publish Application + +on: + pull_request: + branches: [ "main" ] + +jobs: + publish: + runs-on: ubuntu-22.04 + steps: + - name: Checkout project + uses: actions/checkout@v4 + - name: Update version record + run: sed -i -e "s/0.0.0/${{ github.event.pull_request.title }}/g" OrnaLibs.Utils.csproj + - name: Install dotnet 8 + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 8.x + - name: Auth GitHub NuGet + run: dotnet nuget add source --username RetailWay --password ${{ secrets.TOKEN_GITHUB }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/RetailWay/index.json" + - name: Build project + run: dotnet build + - name: Set variables + id: vars + run: | + echo "nupkg=bin/Debug/$(ls bin/Debug | grep '\.nupkg')" >> $GITHUB_OUTPUT + echo "dll=bin/Debug/net8.0/$(ls bin/Debug/net8.0 | grep '\.dll')" >> $GITHUB_OUTPUT + - name: Publish to NuGet + run: | + dotnet nuget push ${{ steps.vars.outputs.nupkg }} --source "github" + dotnet nuget push ${{ steps.vars.outputs.nupkg }} --api-key ${{ secrets.NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json + - name: Publish to GitHub Release + uses: softprops/action-gh-release@v2 + with: + repository: RetailWay/RetailCorrector + token: ${{ secrets.PUBLISH_GITHUB_TOKEN }} + files: ${{ steps.vars.outputs.dll }} + name: ${{ github.event.pull_request.title }} + tag_name: ${{ github.event.pull_request.title }} + \ No newline at end of file diff --git a/Managers/Logger.cs b/Managers/Logger.cs index 04850fa..59bc897 100644 --- a/Managers/Logger.cs +++ b/Managers/Logger.cs @@ -5,10 +5,17 @@ public static class Logger private static Queue Messages = new(); private static Thread? thread; private static string _directory = null!; + private static int _interval; - public static void Init(string directory) + public static bool SetShowInConsole(bool showInConsole) => + _showInConsole = showInConsole; + + private static bool _showInConsole = false; + + public static void Init(string directory, int interval = 100, bool showInConsole = false) { _directory = directory; + _showInConsole = showInConsole; thread = new Thread(WriteToFile) { IsBackground = true }; thread.Start(); } @@ -24,7 +31,7 @@ private static void WriteToFile() { try { - Thread.Sleep(100); + Thread.Sleep(_interval); while (Messages.TryDequeue(out var msg)) File.AppendAllText($"{_directory}\\{DateTime.Today:yyyyMMdd}.log", $"{msg}\n"); } @@ -37,8 +44,12 @@ private static void WriteToFile() } } - private static void AddRecord(string text, string level, Exception? ex = null) => - Messages.Enqueue($"[{DateTime.Now:yyyy'-'MM'-'dd HH':'mm':'ss}] ({level}) {text}{(ex is null ? "" : $"\n{ex}")}"); + private static void AddRecord(string text, string level, Exception? ex = null) + { + var msg = $"[{DateTime.Now:yyyy'-'MM'-'dd HH':'mm':'ss}] ({level}) {text}{(ex is null ? "" : $"\n\t{ex}")}"; + if (_showInConsole) Console.WriteLine(msg); + Messages.Enqueue(msg); + } public static void Information(string text) => AddRecord(text, "INFO"); diff --git a/Managers/Updater.cs b/Managers/Updater.cs index db798bc..ad9deaf 100644 --- a/Managers/Updater.cs +++ b/Managers/Updater.cs @@ -20,10 +20,11 @@ public static void Init(string account, string repo, Version version, int interv _repo = repo; _version = version; _interval = interval; - thread = new Thread(Checker().Start) { IsBackground = true }; - thread.Start(); + Task.Run(Checker); } + public static event Action OnUpdated; + public static void Dispose() => thread?.Interrupt(); private static async Task Checker() @@ -37,6 +38,7 @@ private static async Task Checker() { var path = await DownloadFile(info.Item2, info.Item3); Process.Start(path, _execArgs); + OnUpdated.Invoke(); } Thread.Sleep(_interval * 1000); } @@ -55,8 +57,9 @@ private static async Task Checker() var json = JsonNode.Parse(body); var projInfo = json?[_repo]; if (projInfo is null) return (false, "", ""); - if (!Version.TryParse(projInfo["version"]?.GetValue(), out var lastVersion) || lastVersion > _version) - return (false, "", ""); + if (!Version.TryParse(projInfo["version"]?.GetValue(), out var lastVersion) || + lastVersion.CompareTo(_version) <= 0) + return (false, "", ""); var id = projInfo["id"]?.GetValue()!; var token = projInfo["token"]?.GetValue()!; return (true, id, token); @@ -64,17 +67,21 @@ private static async Task Checker() internal static async Task DownloadFile(string id, string token) { + var path = $"{Path.GetTempPath()}{_repo}.exe"; var url = new StringBuilder(UpdaterConstants.UpdaterUrl); url.Replace("%org%", _account); url.Replace("%repo%", _repo); url.Replace("%id%", id); + var req = new HttpRequestMessage(HttpMethod.Get, url.ToString()); - req.Headers.Add("Authorization", $"token {token}"); + if(!string.IsNullOrWhiteSpace(token)) + req.Headers.Add("Authorization", $"token {token}"); + req.Headers.Add("Accept", "application/octet-stream"); + req.Headers.Add("User-Agent", "curl/7.64.1"); using var http = new HttpClient(); var resp = await http.SendAsync(req); - var path = $"{Path.GetTempPath()}.exe"; using var fs = new FileStream(path!, FileMode.Create); await resp.Content.CopyToAsync(fs); return path; diff --git a/OrnaLibs.Utils.csproj b/OrnaLibs.Utils.csproj index 0f79d7b..b583432 100644 --- a/OrnaLibs.Utils.csproj +++ b/OrnaLibs.Utils.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -9,18 +9,11 @@ True $(AssemblyName) OrnarasUS - 2025.01.17 - 2025.01.17 - 2025.01.17 - https://github.com/OrnarasUS/OrnaLibs.Utils - - - - True - - - - True + 0.0.0 + 0.0.0 + 0.0.0 + True + https://github.com/OrnarasUS/OrnaLibs.Utils