diff --git a/src/MigrationTools.Host.Tests/Commands/VersionCommandTests.cs b/src/MigrationTools.Host.Tests/Commands/VersionCommandTests.cs new file mode 100644 index 000000000..43aa6bb06 --- /dev/null +++ b/src/MigrationTools.Host.Tests/Commands/VersionCommandTests.cs @@ -0,0 +1,43 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using MigrationTools.Services; + +namespace MigrationTools.Host.Tests.Commands +{ + [TestClass()] + public class VersionCommandTests + { + private IHost host; + + [TestInitialize] + public void Setup() + { + host = MigrationToolHost.CreateDefaultBuilder(new string[] { "version", "--skipVersionCheck" }).Build(); + } + + [TestCleanup] + public void Cleanup() + { + host?.Dispose(); + } + + [TestMethod, TestCategory("L0")] + public void VersionCommand_ServiceResolution_ShouldSucceed() + { + // Verify that all required services can be resolved + var migrationToolVersion = host.Services.GetRequiredService(); + Assert.IsNotNull(migrationToolVersion); + } + + [TestMethod, TestCategory("L0")] + public void VersionCommand_GetRunningVersion_ShouldReturnVersionInfo() + { + var migrationToolVersion = host.Services.GetRequiredService(); + var versionInfo = migrationToolVersion.GetRunningVersion(); + + Assert.IsNotNull(versionInfo); + Assert.IsNotNull(versionInfo.versionString); + } + } +} diff --git a/src/MigrationTools.Host.Tests/MigrationTools.Host.Tests.csproj b/src/MigrationTools.Host.Tests/MigrationTools.Host.Tests.csproj index de59101b0..a89ebf66e 100644 --- a/src/MigrationTools.Host.Tests/MigrationTools.Host.Tests.csproj +++ b/src/MigrationTools.Host.Tests/MigrationTools.Host.Tests.csproj @@ -9,6 +9,9 @@ Always + + Always + diff --git a/src/MigrationTools.Host/Commands/CommandBase.cs b/src/MigrationTools.Host/Commands/CommandBase.cs index c7f52db8f..e385b8c64 100644 --- a/src/MigrationTools.Host/Commands/CommandBase.cs +++ b/src/MigrationTools.Host/Commands/CommandBase.cs @@ -120,7 +120,7 @@ public void RunStartupLogic(TSettings settings) Log.Debug(" IsRunningInDebug: {IsRunningInDebug}", _detectVersionService.IsRunningInDebug); Log.Verbose("Full version data: ${_detectVersionService}", _detectVersionService); - Log.Information("Verion Info:"); + Log.Information("Version Info:"); Log.Information(" Running: {RunningVersion}", _detectVersionService.RunningVersion); Log.Information(" Installed: {InstalledVersion}", _detectVersionService.InstalledVersion); Log.Information(" Available: {AvailableVersion}", _detectVersionService.AvailableVersion); diff --git a/src/MigrationTools.Host/Commands/VersionCommand.cs b/src/MigrationTools.Host/Commands/VersionCommand.cs new file mode 100644 index 000000000..d450fbb3b --- /dev/null +++ b/src/MigrationTools.Host/Commands/VersionCommand.cs @@ -0,0 +1,50 @@ +using System; +using System.Diagnostics; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using MigrationTools.Host.Services; +using MigrationTools.Services; +using Spectre.Console; +using Spectre.Console.Cli; + +namespace MigrationTools.Host.Commands +{ + internal class VersionCommand : CommandBase + { + private readonly IMigrationToolVersion _migrationToolVersion; + + public VersionCommand( + IHostApplicationLifetime appLifetime, + IServiceProvider services, + IDetectOnlineService detectOnlineService, + IDetectVersionService2 detectVersionService, + ILogger> logger, + ITelemetryLogger telemetryLogger, + IMigrationToolVersion migrationToolVersion, + IConfiguration configuration, + ActivitySource activitySource) + : base(appLifetime, services, detectOnlineService, detectVersionService, logger, telemetryLogger, + migrationToolVersion, configuration, activitySource) + { + _migrationToolVersion = migrationToolVersion; + } + + internal override Task ExecuteInternalAsync(CommandContext context, VersionCommandSettings settings) + { + var versionInfo = _migrationToolVersion.GetRunningVersion(); + + AnsiConsole.MarkupLine($"[bold cyan]Version:[/] {Markup.Escape(versionInfo.versionString)}"); + + if (versionInfo.version.Major == 0) + { + AnsiConsole.MarkupLine($"[dim]Git Tag:[/] {Markup.Escape(ThisAssembly.Git.Tag)}"); + AnsiConsole.MarkupLine($"[dim]Git Branch:[/] {Markup.Escape(ThisAssembly.Git.Branch)}"); + AnsiConsole.MarkupLine($"[dim]Git Commits:[/] {Markup.Escape(ThisAssembly.Git.Commits)}"); + } + + return Task.FromResult(0); + } + } +} diff --git a/src/MigrationTools.Host/Commands/VersionCommandSettings.cs b/src/MigrationTools.Host/Commands/VersionCommandSettings.cs new file mode 100644 index 000000000..ff774deb4 --- /dev/null +++ b/src/MigrationTools.Host/Commands/VersionCommandSettings.cs @@ -0,0 +1,9 @@ +using System.ComponentModel; +using Spectre.Console.Cli; + +namespace MigrationTools.Host.Commands +{ + internal class VersionCommandSettings : CommandSettingsBase + { + } +} diff --git a/src/MigrationTools.Host/MigrationToolHost.cs b/src/MigrationTools.Host/MigrationToolHost.cs index 61fa0a045..d6dc0b314 100644 --- a/src/MigrationTools.Host/MigrationToolHost.cs +++ b/src/MigrationTools.Host/MigrationToolHost.cs @@ -125,6 +125,10 @@ public static IHostBuilder CreateDefaultBuilder(string[] args, Action("version") + .WithDescription("Displays the version of the tool") + .WithExample("version"); + extraCommands?.Invoke(config); config.PropagateExceptions(); });