Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
* Исправлена работа обновлений
* Добавлены настройки интервала сохранения записей журнала в файл и показ их в консоли
* Сценарий на авто-публикацию обновления
  • Loading branch information
OrnarasUS authored Jan 17, 2025
1 parent 8edd847 commit 4a1d910
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 23 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -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 }}

19 changes: 15 additions & 4 deletions Managers/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ public static class Logger
private static Queue<string> 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();
}
Expand All @@ -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");
}
Expand All @@ -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");

Expand Down
19 changes: 13 additions & 6 deletions Managers/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
Expand All @@ -55,26 +57,31 @@ 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<string>(), out var lastVersion) || lastVersion > _version)
return (false, "", "");
if (!Version.TryParse(projInfo["version"]?.GetValue<string>(), out var lastVersion) ||
lastVersion.CompareTo(_version) <= 0)
return (false, "", "");
var id = projInfo["id"]?.GetValue<string>()!;
var token = projInfo["token"]?.GetValue<string>()!;
return (true, id, token);
}

internal static async Task<string> 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;
Expand Down
19 changes: 6 additions & 13 deletions OrnaLibs.Utils.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -9,18 +9,11 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>$(AssemblyName)</Title>
<Authors>OrnarasUS</Authors>
<Version>2025.01.17</Version>
<FileVersion>2025.01.17</FileVersion>
<AssemblyVersion>2025.01.17</AssemblyVersion>
<RepositoryUrl>https://github.com/OrnarasUS/OrnaLibs.Utils</RepositoryUrl>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<Version>0.0.0</Version>
<FileVersion>0.0.0</FileVersion>
<AssemblyVersion>0.0.0</AssemblyVersion>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<RepositoryUrl>https://github.com/OrnarasUS/OrnaLibs.Utils</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 4a1d910

Please sign in to comment.