From 2258000a89ee8aa565f71f7d076622b3ffbdf3b2 Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Tue, 27 Jun 2023 23:57:57 +0100 Subject: [PATCH] Add build metadata for diagnostics, update checks is this actually gonna work? uhhhh maybe idk --- .github/workflows/ci.yml | 15 +++++--- Bloxstrap/App.xaml.cs | 7 ++++ Bloxstrap/Bloxstrap.csproj | 9 +++++ Bloxstrap/Bootstrapper.cs | 19 +++++++--- Bloxstrap/Extensions/DateTimeEx.cs | 12 +++++++ Bloxstrap/Logger.cs | 4 +-- .../Attributes/CompileTimeInfoAttribute.cs | 21 +++++++++++ .../UI/Menu/ViewModels/AboutViewModel.cs | 16 ++++++++- .../Menu/ViewModels/InstallationViewModel.cs | 3 +- Bloxstrap/UI/Menu/Views/Pages/AboutPage.xaml | 35 +++++++++++++++++++ Bloxstrap/Updater.cs | 4 +-- Bloxstrap/Utilities.cs | 7 ++++ 12 files changed, 136 insertions(+), 16 deletions(-) create mode 100644 Bloxstrap/Extensions/DateTimeEx.cs create mode 100644 Bloxstrap/Models/Attributes/CompileTimeInfoAttribute.cs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1921310c..294c81f3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,28 +6,33 @@ jobs: strategy: matrix: configuration: [Debug, Release] - platform: [x64] + runs-on: windows-latest steps: - uses: actions/checkout@v3 with: submodules: true + - uses: actions/setup-dotnet@v3 with: dotnet-version: '6.x' + - name: Restore dependencies run: dotnet restore + - name: Build run: dotnet build --no-restore + - name: Publish - run: dotnet publish -p:PublishSingleFile=true -r win-${{ matrix.platform }} -c ${{ matrix.configuration }} --self-contained false .\Bloxstrap\Bloxstrap.csproj + run: dotnet publish -p:PublishSingleFile=true -p:CommitHash=${{ github.sha }} -p:CommitRef=${{ github.ref_type }}/${{ github.ref_name }} -r win-x64 -c ${{ matrix.configuration }} --self-contained false .\Bloxstrap\Bloxstrap.csproj + - name: Upload Artifact uses: actions/upload-artifact@v3 with: - name: Bloxstrap (${{ matrix.configuration }}, ${{ matrix.platform }}) + name: Bloxstrap (${{ matrix.configuration }}, x64) path: | - .\Bloxstrap\bin\${{ matrix.configuration }}\net6.0-windows\win-${{ matrix.platform }}\publish\* + .\Bloxstrap\bin\${{ matrix.configuration }}\net6.0-windows\win-x64\publish\* release: needs: build @@ -40,9 +45,11 @@ jobs: with: name: Bloxstrap (Release, x64) path: x64 + - name: Rename binaries run: | mv x64/Bloxstrap.exe Bloxstrap-${{ github.ref_name }}-x64.exe + - name: Release uses: softprops/action-gh-release@v1 with: diff --git a/Bloxstrap/App.xaml.cs b/Bloxstrap/App.xaml.cs index d2c1cd0e..1b5a504b 100644 --- a/Bloxstrap/App.xaml.cs +++ b/Bloxstrap/App.xaml.cs @@ -15,6 +15,7 @@ using Bloxstrap.Extensions; using Bloxstrap.Models; +using Bloxstrap.Models.Attributes; using Bloxstrap.UI.BootstrapperDialogs; using Bloxstrap.UI.Menu.Views; using Bloxstrap.Utility; @@ -44,6 +45,7 @@ public partial class App : Application public static bool IsMenuLaunch { get; private set; } = false; public static string[] LaunchArgs { get; private set; } = null!; + public static BuildMetadataAttribute BuildMetadata => Assembly.GetExecutingAssembly().GetCustomAttribute()!; public static string Version = Assembly.GetExecutingAssembly().GetName().Version!.ToString()[..^2]; // singletons @@ -118,6 +120,11 @@ protected override void OnStartup(StartupEventArgs e) Logger.WriteLine($"[App::OnStartup] Starting {ProjectName} v{Version}"); + if (String.IsNullOrEmpty(BuildMetadata.CommitHash)) + Logger.WriteLine($"[App::OnStartup] Compiled {BuildMetadata.Timestamp.ToFriendlyString()} from {BuildMetadata.Machine}"); + else + Logger.WriteLine($"[App::OnStartup] Compiled {BuildMetadata.Timestamp.ToFriendlyString()} from commit {BuildMetadata.CommitHash} ({BuildMetadata.CommitRef})"); + // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); diff --git a/Bloxstrap/Bloxstrap.csproj b/Bloxstrap/Bloxstrap.csproj index 24f4203b..c4cccd69 100644 --- a/Bloxstrap/Bloxstrap.csproj +++ b/Bloxstrap/Bloxstrap.csproj @@ -39,4 +39,13 @@ + + + <_Parameter1>$([System.DateTime]::UtcNow.ToString("s"))Z + <_Parameter2>$(COMPUTERNAME)\$(USERNAME) + <_Parameter3>$(CommitHash) + <_Parameter4>$(CommitRef) + + + diff --git a/Bloxstrap/Bootstrapper.cs b/Bloxstrap/Bootstrapper.cs index 5c17080c..2676e9cc 100644 --- a/Bloxstrap/Bootstrapper.cs +++ b/Bloxstrap/Bootstrapper.cs @@ -576,25 +576,34 @@ private async Task CheckForUpdates() return; } - string currentVersion = $"{App.ProjectName} v{App.Version}"; - App.Logger.WriteLine($"[Bootstrapper::CheckForUpdates] Checking for {App.ProjectName} updates..."); var releaseInfo = await Utilities.GetJson($"https://api.github.com/repos/{App.ProjectRepository}/releases/latest"); - if (releaseInfo?.Assets is null || currentVersion == releaseInfo.Name) + if (releaseInfo is null || releaseInfo.Assets is null) + { + App.Logger.WriteLine($"[Bootstrapper::CheckForUpdates] No updates found"); + return; + } + + int numCurrentVersion = Utilities.VersionToNumber(App.Version); + int numLatestVersion = Utilities.VersionToNumber(releaseInfo.TagName); + + // check if we aren't using a deployed build, so we can update to one if a new version comes out + if (numCurrentVersion == numLatestVersion && App.BuildMetadata.CommitRef.StartsWith("tag") || numCurrentVersion > numLatestVersion) { App.Logger.WriteLine($"[Bootstrapper::CheckForUpdates] No updates found"); return; } + SetStatus($"Getting the latest {App.ProjectName}..."); // 64-bit is always the first option GithubReleaseAsset asset = releaseInfo.Assets[0]; string downloadLocation = Path.Combine(Directories.LocalAppData, "Temp", asset.Name); - App.Logger.WriteLine($"[Bootstrapper::CheckForUpdates] Downloading {releaseInfo.Name}..."); + App.Logger.WriteLine($"[Bootstrapper::CheckForUpdates] Downloading {releaseInfo.TagName}..."); if (!File.Exists(downloadLocation)) { @@ -604,7 +613,7 @@ private async Task CheckForUpdates() await response.Content.CopyToAsync(fileStream); } - App.Logger.WriteLine($"[Bootstrapper::CheckForUpdates] Starting {releaseInfo.Name}..."); + App.Logger.WriteLine($"[Bootstrapper::CheckForUpdates] Starting {releaseInfo.TagName}..."); ProcessStartInfo startInfo = new() { diff --git a/Bloxstrap/Extensions/DateTimeEx.cs b/Bloxstrap/Extensions/DateTimeEx.cs new file mode 100644 index 00000000..f30d0115 --- /dev/null +++ b/Bloxstrap/Extensions/DateTimeEx.cs @@ -0,0 +1,12 @@ +using System; + +namespace Bloxstrap.Extensions +{ + static class DateTimeEx + { + public static string ToFriendlyString(this DateTime dateTime) + { + return dateTime.ToString("dddd, d MMMM yyyy 'at' h:mm:ss tt", App.CultureFormat); + } + } +} diff --git a/Bloxstrap/Logger.cs b/Bloxstrap/Logger.cs index ad4bc935..883dac25 100644 --- a/Bloxstrap/Logger.cs +++ b/Bloxstrap/Logger.cs @@ -39,9 +39,9 @@ public void Initialize(string filename) public void WriteLine(string message) { - string timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"); + string timestamp = DateTime.UtcNow.ToString("s") + "Z"; string outcon = $"{timestamp} {message}"; - string outlog = outcon.Replace(Directories.UserProfile, ""); + string outlog = outcon.Replace(Directories.UserProfile, "%UserProfile%"); Debug.WriteLine(outcon); WriteToLog(outlog); diff --git a/Bloxstrap/Models/Attributes/CompileTimeInfoAttribute.cs b/Bloxstrap/Models/Attributes/CompileTimeInfoAttribute.cs new file mode 100644 index 00000000..32423f38 --- /dev/null +++ b/Bloxstrap/Models/Attributes/CompileTimeInfoAttribute.cs @@ -0,0 +1,21 @@ +using System; + +namespace Bloxstrap.Models.Attributes +{ + [AttributeUsage(AttributeTargets.Assembly)] + public class BuildMetadataAttribute : Attribute + { + public DateTime Timestamp { get; set; } + public string Machine { get; set; } + public string CommitHash { get; set; } + public string CommitRef { get; set; } + + public BuildMetadataAttribute(string timestamp, string machine, string commitHash, string commitRef) + { + Timestamp = DateTime.Parse(timestamp).ToLocalTime(); + Machine = machine; + CommitHash = commitHash; + CommitRef = commitRef; + } + } +} diff --git a/Bloxstrap/UI/Menu/ViewModels/AboutViewModel.cs b/Bloxstrap/UI/Menu/ViewModels/AboutViewModel.cs index 89566dc5..cea0d437 100644 --- a/Bloxstrap/UI/Menu/ViewModels/AboutViewModel.cs +++ b/Bloxstrap/UI/Menu/ViewModels/AboutViewModel.cs @@ -1,7 +1,21 @@ -namespace Bloxstrap.UI.Menu.ViewModels +using System; +using System.Windows; + +using Bloxstrap.Extensions; +using Bloxstrap.Models.Attributes; + +namespace Bloxstrap.UI.Menu.ViewModels { public class AboutViewModel { public string Version => $"Version {App.Version}"; + + public BuildMetadataAttribute BuildMetadata => App.BuildMetadata; + + public string BuildTimestamp => BuildMetadata.Timestamp.ToFriendlyString(); + public string BuildCommitHashUrl => $"https://github.com/{App.ProjectRepository}/commit/{BuildMetadata.CommitHash}"; + + public Visibility BuildInformationVisibility => BuildMetadata.CommitRef.StartsWith("tag") ? Visibility.Collapsed : Visibility.Visible; + public Visibility BuildCommitVisibility => String.IsNullOrEmpty(BuildMetadata.CommitHash) ? Visibility.Collapsed : Visibility.Visible; } } diff --git a/Bloxstrap/UI/Menu/ViewModels/InstallationViewModel.cs b/Bloxstrap/UI/Menu/ViewModels/InstallationViewModel.cs index e3ca027c..cfd75b57 100644 --- a/Bloxstrap/UI/Menu/ViewModels/InstallationViewModel.cs +++ b/Bloxstrap/UI/Menu/ViewModels/InstallationViewModel.cs @@ -12,6 +12,7 @@ using CommunityToolkit.Mvvm.Input; using Bloxstrap.Enums; +using Bloxstrap.Extensions; using Bloxstrap.Models; namespace Bloxstrap.UI.Menu.ViewModels @@ -50,7 +51,7 @@ private async Task LoadChannelDeployInfo(string channel) { Version = info.Version, VersionGuid = info.VersionGuid, - Timestamp = info.Timestamp?.ToString("dddd, d MMMM yyyy 'at' h:mm:ss tt", App.CultureFormat)! + Timestamp = info.Timestamp?.ToFriendlyString()! }; OnPropertyChanged(nameof(ChannelDeployInfo)); diff --git a/Bloxstrap/UI/Menu/Views/Pages/AboutPage.xaml b/Bloxstrap/UI/Menu/Views/Pages/AboutPage.xaml index 2a6f0bc7..e2a3d096 100644 --- a/Bloxstrap/UI/Menu/Views/Pages/AboutPage.xaml +++ b/Bloxstrap/UI/Menu/Views/Pages/AboutPage.xaml @@ -41,6 +41,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Bloxstrap/Updater.cs b/Bloxstrap/Updater.cs index d229f1c7..e9d53a4c 100644 --- a/Bloxstrap/Updater.cs +++ b/Bloxstrap/Updater.cs @@ -19,11 +19,9 @@ public static void CheckInstalledVersion() // 2.0.0 downloads updates to /Updates so lol bool isAutoUpgrade = Environment.ProcessPath.StartsWith(Path.Combine(Directories.Base, "Updates")) || Environment.ProcessPath.StartsWith(Path.Combine(Directories.LocalAppData, "Temp")); - // if downloaded version doesn't match, replace installed version with downloaded version FileVersionInfo currentVersionInfo = FileVersionInfo.GetVersionInfo(Environment.ProcessPath); - FileVersionInfo installedVersionInfo = FileVersionInfo.GetVersionInfo(Directories.Application); - if (installedVersionInfo.ProductVersion == currentVersionInfo.ProductVersion) + if (Utility.MD5Hash.FromFile(Environment.ProcessPath) == Utility.MD5Hash.FromFile(Directories.Application)) return; MessageBoxResult result; diff --git a/Bloxstrap/Utilities.cs b/Bloxstrap/Utilities.cs index 53571d90..43711464 100644 --- a/Bloxstrap/Utilities.cs +++ b/Bloxstrap/Utilities.cs @@ -39,5 +39,12 @@ public static long GetFreeDiskSpace(string path) return default; } } + + public static int VersionToNumber(string version) + { + // yes this is kinda stupid lol + version = version.Replace("v", "").Replace(".", ""); + return Int32.Parse(version); + } } }