From 06ddddb852a3926ae7588d50af8949468f16372f Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Thu, 26 Sep 2024 19:37:53 -0400 Subject: [PATCH] Try setting up installer/updater --- .github/workflows/main.yml | 28 ++++++++++++++++++++ Program.cs | 27 +++++++++++++++++++ WindowSource.cs | 50 ++++++++++++++++++++++++++++++++++- WorldClockStatusWindow.csproj | 9 +++++++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/main.yml create mode 100644 Program.cs diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..adaa721 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,28 @@ +name: Deploy to GitHub Releases + +on: + push: + branches: + - main + +jobs: + deploy-to-github-releases: + runs-on: windows-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Install .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 8.0.x + + - name: Publish Application + run: dotnet publish WorldClockStatusWindow/WorldClockStatusWindow.csproj -c Release -o publish -r win-x64 --self-contained true + + - name: Create Velopack Release + run: | + dotnet tool install -g vpk + vpk download github --repoUrl https://github.com/ckaczor/WorldClockStatusWindow + vpk pack -u MyUniqueIdentifier -v ${{ steps.get-version.outputs.version }} -p publish + vpk upload github --repoUrl https://github.com/ckaczor/WorldClockStatusWindow --publish --releaseName "WorldClockStatusWindow ${{ steps.get-version.outputs.version }}" --tag v${{ steps.get-version.outputs.version }} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..3abdce2 --- /dev/null +++ b/Program.cs @@ -0,0 +1,27 @@ +using Microsoft.Extensions.Logging; +using Serilog; +using System; +using Velopack; + +namespace WorldClockStatusWindow; + +internal class Program +{ + [STAThread] + public static void Main(string[] args) + { + Log.Logger = new LoggerConfiguration().WriteTo.File("log.txt").CreateLogger(); + + Log.Logger.Information("Start"); + + var loggerFactory = new LoggerFactory().AddSerilog(Log.Logger); + + VelopackApp.Build().Run(loggerFactory.CreateLogger("Install")); + + var app = new App(); + app.InitializeComponent(); + app.Run(); + + Log.Logger.Information("End"); + } +} \ No newline at end of file diff --git a/WindowSource.cs b/WindowSource.cs index a7490da..c9ab967 100644 --- a/WindowSource.cs +++ b/WindowSource.cs @@ -5,8 +5,14 @@ using System.Linq; using System.Reflection; using System.Text; using System.Text.Json; +using System.Threading.Tasks; using System.Timers; using System.Windows.Threading; +using Serilog; +using Serilog.Core; +using Velopack; +using Velopack.Sources; +using WorldClockStatusWindow.Properties; namespace WorldClockStatusWindow; @@ -25,9 +31,47 @@ internal class WindowSource : IWindowSource, IDisposable _dispatcher = Dispatcher.CurrentDispatcher; + _timer = new Timer(1000); + + Task.Factory.StartNew(UpdateApp).ContinueWith(_ => Start()); + } + + private async Task UpdateApp() + { + try + { + var updateManager = new UpdateManager(new GithubSource("https://github.com/ckaczor/WorldClockStatusWindow", null, false)); + + if (!updateManager.IsInstalled) + return false; + + _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText("Checking for update...")); + + var newVersion = await updateManager.CheckForUpdatesAsync(); + + if (newVersion == null) + return false; + + _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText("Downloading update...")); + + await updateManager.DownloadUpdatesAsync(newVersion); + + _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText("Installing update...")); + + updateManager.ApplyUpdatesAndRestart(newVersion); + } + catch (Exception e) + { + Log.Logger.Error(e, nameof(UpdateApp)); + } + + return true; + } + + private void Start() + { Load(); - _timer = new Timer(1000); _timer.Elapsed += HandleTimerElapsed; _timer.Enabled = true; } @@ -71,6 +115,10 @@ internal class WindowSource : IWindowSource, IDisposable text.Append($"{timeZoneEntry.Label.PadLeft(labelLength)}: {TimeZoneInfo.ConvertTime(now, timeZone).ToString(Properties.Settings.Default.TimeFormat)}"); } + text.AppendLine(); + text.AppendLine(); + text.Append($"Version: {Assembly.GetEntryAssembly()!.GetName().Version!.ToString()}"); + _dispatcher.InvokeAsync(() => _floatingStatusWindow.SetText(text.ToString())); } diff --git a/WorldClockStatusWindow.csproj b/WorldClockStatusWindow.csproj index 12132b0..adfc56e 100644 --- a/WorldClockStatusWindow.csproj +++ b/WorldClockStatusWindow.csproj @@ -5,6 +5,7 @@ false true true + WorldClockStatusWindow.Program @@ -13,7 +14,12 @@ + + + + + @@ -28,4 +34,7 @@ Settings.Designer.cs + + + \ No newline at end of file