diff --git a/docker/Instances/1/state.json b/docker/Instances/1/state.json index 81ade68..ade1da7 100644 --- a/docker/Instances/1/state.json +++ b/docker/Instances/1/state.json @@ -3,7 +3,7 @@ "installationName": "VisualStudio/public.d15rel/15.0.26116.0", "installationVersion": "15.0.26116", "installationPath": "C:\\VS\\Community", - "installDate": "2017-01-17T03:00:00Z", + "installDate": "2017-01-17T03:45:00Z", "product": { "id": "Microsoft.VisualStudio.Product.Community", "version": "15.0.26116.0", diff --git a/src/VSSetup.PowerShell/Instance.cs b/src/VSSetup.PowerShell/Instance.cs index ac0f238..604ee84 100644 --- a/src/VSSetup.PowerShell/Instance.cs +++ b/src/VSSetup.PowerShell/Instance.cs @@ -67,7 +67,7 @@ internal Instance(ISetupInstance2 instance) () => { var versionString = instance.GetInstallationVersion(); - if (Utilities.TryParseVersion(versionString, out Version version)) + if (Utilities.TryParseVersion(versionString, out var version)) { return version.Normalize(); } @@ -94,19 +94,13 @@ internal Instance(ISetupInstance2 instance) Utilities.TrySet( ref displayName, nameof(DisplayName), - () => - { - return instance.GetDisplayName(lcid); - }, + () => instance.GetDisplayName(lcid), OnError); Utilities.TrySet( ref description, nameof(Description), - () => - { - return instance.GetDescription(lcid); - }, + () => instance.GetDescription(lcid), OnError); Utilities.TrySet( diff --git a/src/VSSetup.PowerShell/InstanceComparer.cs b/src/VSSetup.PowerShell/InstanceComparer.cs new file mode 100644 index 0000000..d18b81f --- /dev/null +++ b/src/VSSetup.PowerShell/InstanceComparer.cs @@ -0,0 +1,65 @@ +// +// Copyright (C) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE.txt in the project root for license information. +// + +namespace Microsoft.VisualStudio.Setup +{ + using System; + using System.Collections.Generic; + + /// + /// Compares instances of . + /// + internal class InstanceComparer : IComparer + { + /// + /// Gets an instance of the that compares only the + /// and properties. + /// + public static readonly IComparer VersionAndDate = new InstanceComparer(); + + /// + public int Compare(Instance x, Instance y) + { + if (ReferenceEquals(x, y)) + { + return 0; + } + else if (x is null) + { + return -1; + } + else if (y is null) + { + return 1; + } + + var result = Compare(x.InstallationVersion, y.InstallationVersion); + if (result != 0) + { + return result; + } + + return DateTime.Compare(x.InstallDate, y.InstallDate); + } + + private static int Compare(Version x, Version y) + { + if (ReferenceEquals(x, y)) + { + return 0; + } + else if (x is null) + { + return -1; + } + else if (y is null) + { + return 1; + } + + return x.CompareTo(y); + } + } +} diff --git a/src/VSSetup.PowerShell/PowerShell/SelectInstanceCommand.cs b/src/VSSetup.PowerShell/PowerShell/SelectInstanceCommand.cs index 273c8ae..90f9d6e 100644 --- a/src/VSSetup.PowerShell/PowerShell/SelectInstanceCommand.cs +++ b/src/VSSetup.PowerShell/PowerShell/SelectInstanceCommand.cs @@ -139,6 +139,12 @@ bool Contains(IEnumerable packages, Func instance, InstanceComparer.VersionAndDate); + } + foreach (var instance in instances) { if (Latest) diff --git a/src/VSSetup.PowerShell/VSSetup.PowerShell.csproj b/src/VSSetup.PowerShell/VSSetup.PowerShell.csproj index be1cdcb..d88bc83 100644 --- a/src/VSSetup.PowerShell/VSSetup.PowerShell.csproj +++ b/src/VSSetup.PowerShell/VSSetup.PowerShell.csproj @@ -51,6 +51,7 @@ + @@ -112,4 +113,4 @@ - + \ No newline at end of file diff --git a/test/VSSetup.PowerShell.Test/InstanceComparerTests.cs b/test/VSSetup.PowerShell.Test/InstanceComparerTests.cs new file mode 100644 index 0000000..9e08036 --- /dev/null +++ b/test/VSSetup.PowerShell.Test/InstanceComparerTests.cs @@ -0,0 +1,61 @@ +// +// Copyright (C) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE.txt in the project root for license information. +// + +namespace Microsoft.VisualStudio.Setup +{ + using System; + using System.Collections.Generic; + using System.Runtime.InteropServices.ComTypes; + using Microsoft.VisualStudio.Setup.Configuration; + using Moq; + using Xunit; + + public class InstanceComparerTests + { + public static IEnumerable GetCompareVersionAndDateData() + { + var empty = new string[0]; + + var v1 = new Version(1, 0); + var v2 = new Version(2, 0); + + var ft1 = new FILETIME { dwHighDateTime = 1 }; + var ft2 = new FILETIME { dwHighDateTime = 2 }; + + Instance Mock(Version v = null, FILETIME ft = default(FILETIME)) + { + var mock = new Mock(); + mock.As().Setup(x => x.GetNames()).Returns(empty); + + mock.Setup(x => x.GetInstallationVersion()).Returns(v?.ToString()); + mock.Setup(x => x.GetInstallDate()).Returns(ft); + + return new Instance(mock.Object); + } + + return new[] + { + new object[] { null, null, 0 }, + new object[] { null, Mock(), -1 }, + new object[] { Mock(), null, 1 }, + new object[] { Mock(), Mock(v1), -1 }, + new object[] { Mock(v1), Mock(), 1 }, + new object[] { Mock(v1), Mock(v1), 0 }, + new object[] { Mock(v1), Mock(v2), -1 }, + new object[] { Mock(v2), Mock(v1), 1 }, + new object[] { Mock(v2, ft1), Mock(v2, ft2), -1 }, + new object[] { Mock(v2, ft2), Mock(v2, ft1), 1 }, + }; + } + + [Theory] + [MemberData(nameof(GetCompareVersionAndDateData))] + public void CompareVersionAndDate(Instance x, Instance y, int expected) + { + var actual = InstanceComparer.VersionAndDate.Compare(x, y); + Assert.Equal(expected, actual); + } + } +} diff --git a/test/VSSetup.PowerShell.Test/VSSetup.PowerShell.Test.csproj b/test/VSSetup.PowerShell.Test/VSSetup.PowerShell.Test.csproj index 562af93..8cfb823 100644 --- a/test/VSSetup.PowerShell.Test/VSSetup.PowerShell.Test.csproj +++ b/test/VSSetup.PowerShell.Test/VSSetup.PowerShell.Test.csproj @@ -90,6 +90,7 @@ +