From a033a3f945e070da734563057bfee04e196b1658 Mon Sep 17 00:00:00 2001 From: "Martin Hinshelwood nkdAgility.com" Date: Fri, 12 Jul 2024 13:34:47 +0100 Subject: [PATCH] Make Winget name Dynamic and better version detection (#2145) - Moved to version detection through Informational Version with full path - Used FullSemVer regex to split out if we run on a branch or local. - Write out really version number - use version number to detect if we are preview or local Inlcudes fix for #2144 --- .github/workflows/main.yml | 8 +- .../MigrationTools.ConsoleFull.csproj | 18 +++++ src/MigrationTools.Host/MigrationToolHost.cs | 4 +- .../Services/DetectVersionService2.cs | 73 +++++++------------ src/MigrationTools.Host/StartupService.cs | 11 ++- src/MigrationTools/StaticVariables.cs | 7 ++ 6 files changed, 67 insertions(+), 54 deletions(-) create mode 100644 src/MigrationTools/StaticVariables.cs diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0d5747605..1d8a12501 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -65,6 +65,7 @@ jobs: GitVersion_AssemblySemVer: ${{ needs.Setup.outputs.GitVersion_AssemblySemVer }} GitVersion_InformationalVersion: ${{ needs.Setup.outputs.GitVersion_InformationalVersion }} GitVersion_NuGetVersion: ${{ needs.Setup.outputs.GitVersion_NuGetVersion }} + GitVersion_PreReleaseLabel: ${{ needs.Setup.outputs.GitVersion_PreReleaseLabel }} steps: # - name: Setup NuGet # uses: NuGet/setup-nuget@v1.0.2 @@ -79,6 +80,11 @@ jobs: distribution: 'zulu' - name: Checkout uses: actions/checkout@v2 + - uses: cschleiden/replace-tokens@v1 + with: + files: '["**/StaticVariables.cs"]' + tokenPrefix: "${" + tokenSuffix: "}" - name: Setup .NET uses: actions/setup-dotnet@v1 with: @@ -95,7 +101,7 @@ jobs: /k:"vsts-sync-migrator:master" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" - - run: dotnet build MigrationTools.sln /p:Version=${{ env.GitVersion_AssemblySemVer }} /p:FileVersion=${{ env.GitVersion_AssemblySemVer }} /p:InformationalVersion=${{ env.GitVersion_InformationalVersion }} + - run: dotnet build MigrationTools.sln /p:Version=${{ env.GitVersion_SemVer }} /p:FileVersion=${{ env.GitVersion_AssemblySemVer }} /p:InformationalVersion=${{ env.GitVersion_InformationalVersion }} /p:GitVersionTag=${{ env.GitVersion_PreReleaseLabel }} name: Build MigrationTools.sln id: Build - run: dotnet test "MigrationTools.sln" --results-directory ".\test-results\" --logger trx --collect "Code coverage" --no-build --filter "(TestCategory=L0|TestCategory=L1)" diff --git a/src/MigrationTools.ConsoleFull/MigrationTools.ConsoleFull.csproj b/src/MigrationTools.ConsoleFull/MigrationTools.ConsoleFull.csproj index 9f5fad818..6ed94ba0f 100644 --- a/src/MigrationTools.ConsoleFull/MigrationTools.ConsoleFull.csproj +++ b/src/MigrationTools.ConsoleFull/MigrationTools.ConsoleFull.csproj @@ -6,6 +6,13 @@ Azure DevOps Migration Tools [Object Model] devopsmigration AnyCPU + 0.0.0.0 + 0.0.0.0 + 0.0.0-local + README.md + https://github.com/nkdAgility/azure-devops-migration-tools + https://github.com/nkdAgility/azure-devops-migration-tools + LICENSE @@ -20,6 +27,17 @@ + + + True + \ + + + True + \ + + + diff --git a/src/MigrationTools.Host/MigrationToolHost.cs b/src/MigrationTools.Host/MigrationToolHost.cs index 5c38946c2..acbc9869f 100644 --- a/src/MigrationTools.Host/MigrationToolHost.cs +++ b/src/MigrationTools.Host/MigrationToolHost.cs @@ -159,8 +159,8 @@ public static IHostBuilder CreateDefaultBuilder(string[] args) private static string GetVersionTextForLog() { - Version runningVersion = DetectVersionService2.GetRunningVersion(); - string textVersion = ((runningVersion.Major > 1) ? "v" + runningVersion : ThisAssembly.Git.BaseTag + "-" + ThisAssembly.Git.Commits + "-local"); + Version runningVersion = DetectVersionService2.GetRunningVersion().version; + string textVersion = "v" + DetectVersionService2.GetRunningVersion().version + "-" + DetectVersionService2.GetRunningVersion().PreReleaseLabel; return textVersion; } diff --git a/src/MigrationTools.Host/Services/DetectVersionService2.cs b/src/MigrationTools.Host/Services/DetectVersionService2.cs index 6e4940048..e2d1937c4 100644 --- a/src/MigrationTools.Host/Services/DetectVersionService2.cs +++ b/src/MigrationTools.Host/Services/DetectVersionService2.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Linq; using System.Reflection; +using System.Text.RegularExpressions; using System.Threading; using Microsoft.ApplicationInsights.DataContracts; using Microsoft.Extensions.Logging; @@ -44,7 +45,7 @@ public Version RunningVersion { get { - return GetRunningVersion(); + return GetRunningVersion().version; } } public Version AvailableVersion @@ -107,6 +108,14 @@ private bool GetIsPackageManagerInstalled() return winget.IsInstalled; } + public bool IsPreviewVersion + { + get + { + return !string.IsNullOrEmpty( GetRunningVersion().PreReleaseLabel); + } + } + public bool IsUpdateAvailable { get @@ -119,7 +128,7 @@ public bool IsRunningInDebug { get { - return RunningVersion == new Version("0.0.0"); + return GetRunningVersion().PreReleaseLabel.ToLower() == "local"; } } @@ -127,7 +136,7 @@ public bool IsNewLocalVersionAvailable { get { - return (IsPackageInstalled) ? !(RunningVersion >= InstalledVersion) : false; + return (IsRunningInDebug) ? false : (IsPackageInstalled) ? !(RunningVersion >= InstalledVersion) : false; } } @@ -135,7 +144,14 @@ public DetectVersionService2(ITelemetryLogger telemetry, ILogger p.Id == PackageId); Log.Debug("Found package with id {PackageId}", PackageId); } _packageChecked = true; @@ -154,46 +170,13 @@ private WinGetPackage GetPackage() return _package; } - //private void InitialiseService() - //{ - // _logger.LogDebug("DetectVersionService2::InitialiseService"); - // DateTime startTime = DateTime.Now; - // using (var bench = new Benchmark("DetectVersionService2::InitialiseService")) - // { - // ////////////////////////////////// - - - // try - // { - // if (IsPackageManagerInstalled) - // { - - // if (package != null) - // { - - - // IsPackageInstalled = true; - // } - // _Telemetry.TrackDependency(new DependencyTelemetry("PackageRepository", "winget", PackageId, AvailableVersion == null ? "nullVersion" : AvailableVersion.ToString(), startTime, bench.Elapsed, "200", IsPackageInstalled)); - // } - // } - // catch (Exception ex) - // { - // Log.Error(ex, "DetectVersionService"); - // IsPackageInstalled = false; - // _Telemetry.TrackDependency(new DependencyTelemetry("PackageRepository", "winget", PackageId, AvailableVersion == null ? "nullVersion" : AvailableVersion.ToString(), startTime, bench.Elapsed, "500", IsPackageInstalled)); - // } - // } - //} - - public static Version GetRunningVersion() - { - Version assver = Assembly.GetEntryAssembly()?.GetName().Version; - if (assver == null) - { - return new Version("0.0.0"); - } - return new Version(assver.Major, assver.Minor, assver.Build); + public static (Version version, string PreReleaseLabel, string versionString) GetRunningVersion() + { + FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly()?.Location); + var matches = Regex.Matches(myFileVersionInfo.ProductVersion, @"^(?0|[1-9]\d*)\.(?0|[1-9]\d*)\.(?0|[1-9]\d*)(?:-((?