Skip to content

Commit

Permalink
Make Winget name Dynamic and better version detection (#2145)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
MrHinsh authored Jul 12, 2024
1 parent 63bc769 commit a033a3f
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 54 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
Expand All @@ -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:
Expand All @@ -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)"
Expand Down
18 changes: 18 additions & 0 deletions src/MigrationTools.ConsoleFull/MigrationTools.ConsoleFull.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
<Product>Azure DevOps Migration Tools [Object Model]</Product>
<AssemblyName>devopsmigration</AssemblyName>
<PlatformTarget>AnyCPU</PlatformTarget>
<Version>0.0.0.0</Version>
<FileVersion>0.0.0.0</FileVersion>
<InformationalVersion>0.0.0-local</InformationalVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageProjectUrl>https://github.com/nkdAgility/azure-devops-migration-tools</PackageProjectUrl>
<RepositoryUrl>https://github.com/nkdAgility/azure-devops-migration-tools</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>

<ItemGroup>
Expand All @@ -20,6 +27,17 @@
</Content>
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MigrationTools.Clients.AzureDevops.Rest\MigrationTools.Clients.AzureDevops.Rest.csproj" />
<ProjectReference Include="..\MigrationTools.Clients.FileSystem\MigrationTools.Clients.FileSystem.csproj" />
Expand Down
4 changes: 2 additions & 2 deletions src/MigrationTools.Host/MigrationToolHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
73 changes: 28 additions & 45 deletions src/MigrationTools.Host/Services/DetectVersionService2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,7 +45,7 @@ public Version RunningVersion
{
get
{
return GetRunningVersion();
return GetRunningVersion().version;
}
}
public Version AvailableVersion
Expand Down Expand Up @@ -107,6 +108,14 @@ private bool GetIsPackageManagerInstalled()
return winget.IsInstalled;
}

public bool IsPreviewVersion
{
get
{
return !string.IsNullOrEmpty( GetRunningVersion().PreReleaseLabel);
}
}

public bool IsUpdateAvailable
{
get
Expand All @@ -119,23 +128,30 @@ public bool IsRunningInDebug
{
get
{
return RunningVersion == new Version("0.0.0");
return GetRunningVersion().PreReleaseLabel.ToLower() == "local";
}
}

public bool IsNewLocalVersionAvailable
{
get
{
return (IsPackageInstalled) ? !(RunningVersion >= InstalledVersion) : false;
return (IsRunningInDebug) ? false : (IsPackageInstalled) ? !(RunningVersion >= InstalledVersion) : false;
}
}

public DetectVersionService2(ITelemetryLogger telemetry, ILogger<IDetectVersionService2> logger)
{
_Telemetry = telemetry;
_logger = logger;
PackageId = "nkdAgility.AzureDevOpsMigrationTools";
if (IsPreviewVersion)
{
PackageId = "nkdAgility.AzureDevOpsMigrationTools.Preview";
} else
{
PackageId = "nkdAgility.AzureDevOpsMigrationTools";
}

}

private WinGetPackage GetPackage()
Expand All @@ -146,54 +162,21 @@ private WinGetPackage GetPackage()
{
_packageManager = new WinGetPackageManager();
Log.Debug("Searching for package!");
_package = _packageManager.GetInstalledPackages(PackageId, true).FirstOrDefault();
_package = _packageManager.GetInstalledPackages(PackageId).Find(p => p.Id == PackageId);
Log.Debug("Found package with id {PackageId}", PackageId);
}
_packageChecked = true;
}
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, @"^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<build>0|[1-9]\d*)(?:-((?<label>:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<fullEnd>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$");
Version version = new Version(myFileVersionInfo.FileVersion);
string textVersion = "v" + version.Major + "." + version.Minor + "." + version.Build + "-" + matches[0].Groups[1].Value;
return (version, matches[0].Groups[1].Value, textVersion);
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/MigrationTools.Host/StartupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,14 @@ public void RunExitLogic()
private void ApplicationStartup(string[] args)
{
_mainTimer.Start();
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
var version = Assembly.GetEntryAssembly().GetName().Version;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
_logger.LogInformation("Application Starting");
AsciiLogo(version);
AsciiLogo(DetectVersionService2.GetRunningVersion().versionString);
TelemetryNote();
_logger.LogInformation("Start Time: {StartTime}", DateTime.Now.ToUniversalTime().ToLocalTime());
_logger.LogInformation("Running with args: {@Args}", args);
_logger.LogInformation("OSVersion: {OSVersion}", Environment.OSVersion.ToString());
_logger.LogInformation("Version (Assembly): {Version}", version);
_logger.LogInformation("Version (Assembly): {Version}", DetectVersionService2.GetRunningVersion().versionString);
}

protected void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
Expand All @@ -166,7 +165,7 @@ private void TelemetryNote()
_logger.LogInformation("--------------------------------------");
}

private void AsciiLogo(Version thisVersion)
private void AsciiLogo(string thisVersion)
{
_logger.LogInformation(" &@& ");
_logger.LogInformation(" @@(((((@ ");
Expand Down Expand Up @@ -209,7 +208,7 @@ private void AsciiLogo(Version thisVersion)
var productName = ((AssemblyProductAttribute)Assembly.GetEntryAssembly()
.GetCustomAttributes(typeof(AssemblyProductAttribute), true)[0]).Product;
_logger.LogInformation("{productName} ", productName);
_logger.LogInformation("v{thisVersion}", thisVersion);
_logger.LogInformation("{thisVersion}", thisVersion);
var companyName = ((AssemblyCompanyAttribute)Assembly.GetEntryAssembly()
.GetCustomAttributes(typeof(AssemblyCompanyAttribute), true)[0]).Company;
_logger.LogInformation("{companyName} ", companyName);
Expand Down
7 changes: 7 additions & 0 deletions src/MigrationTools/StaticVariables.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace MigrationTools
{
}

0 comments on commit a033a3f

Please sign in to comment.