Skip to content

Commit 4df8722

Browse files
authored
Merge 4779037 into af3e82f
2 parents af3e82f + 4779037 commit 4df8722

File tree

7 files changed

+139
-11
lines changed

7 files changed

+139
-11
lines changed

docker/Instances/1/state.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"installationName": "VisualStudio/public.d15rel/15.0.26116.0",
44
"installationVersion": "15.0.26116",
55
"installationPath": "C:\\VS\\Community",
6-
"installDate": "2017-01-17T03:00:00Z",
6+
"installDate": "2017-01-17T03:45:00Z",
77
"product": {
88
"id": "Microsoft.VisualStudio.Product.Community",
99
"version": "15.0.26116.0",

src/VSSetup.PowerShell/Instance.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ internal Instance(ISetupInstance2 instance)
6767
() =>
6868
{
6969
var versionString = instance.GetInstallationVersion();
70-
if (Utilities.TryParseVersion(versionString, out Version version))
70+
if (Utilities.TryParseVersion(versionString, out var version))
7171
{
7272
return version.Normalize();
7373
}
@@ -94,19 +94,13 @@ internal Instance(ISetupInstance2 instance)
9494
Utilities.TrySet(
9595
ref displayName,
9696
nameof(DisplayName),
97-
() =>
98-
{
99-
return instance.GetDisplayName(lcid);
100-
},
97+
() => instance.GetDisplayName(lcid),
10198
OnError);
10299

103100
Utilities.TrySet(
104101
ref description,
105102
nameof(Description),
106-
() =>
107-
{
108-
return instance.GetDescription(lcid);
109-
},
103+
() => instance.GetDescription(lcid),
110104
OnError);
111105

112106
Utilities.TrySet(
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// <copyright file="InstanceComparer.cs" company="Microsoft Corporation">
2+
// Copyright (C) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt in the project root for license information.
4+
// </copyright>
5+
6+
namespace Microsoft.VisualStudio.Setup
7+
{
8+
using System;
9+
using System.Collections.Generic;
10+
11+
/// <summary>
12+
/// Compares instances of <see cref="Instance"/>.
13+
/// </summary>
14+
internal class InstanceComparer : IComparer<Instance>
15+
{
16+
/// <summary>
17+
/// Gets an instance of the <see cref="InstanceComparer"/> that compares only the
18+
/// <see cref="Instance.InstallationVersion"/> and <see cref="Instance.InstallDate"/> properties.
19+
/// </summary>
20+
public static readonly IComparer<Instance> VersionAndDate = new InstanceComparer();
21+
22+
/// <inheritdoc/>
23+
public int Compare(Instance x, Instance y)
24+
{
25+
if (ReferenceEquals(x, y))
26+
{
27+
return 0;
28+
}
29+
else if (x is null)
30+
{
31+
return -1;
32+
}
33+
else if (y is null)
34+
{
35+
return 1;
36+
}
37+
38+
var result = Compare(x.InstallationVersion, y.InstallationVersion);
39+
if (result != 0)
40+
{
41+
return result;
42+
}
43+
44+
return DateTime.Compare(x.InstallDate, y.InstallDate);
45+
}
46+
47+
private static int Compare(Version x, Version y)
48+
{
49+
if (ReferenceEquals(x, y))
50+
{
51+
return 0;
52+
}
53+
else if (x is null)
54+
{
55+
return -1;
56+
}
57+
else if (y is null)
58+
{
59+
return 1;
60+
}
61+
62+
return x.CompareTo(y);
63+
}
64+
}
65+
}

src/VSSetup.PowerShell/PowerShell/SelectInstanceCommand.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ bool Contains(IEnumerable<PackageReference> packages, Func<PackageReference, str
139139
select instance;
140140
}
141141

142+
if (Latest)
143+
{
144+
// Sort by the InstallationVersion then InstallDate so we can easily find the latest.
145+
instances = instances.OrderBy(instance => instance, InstanceComparer.VersionAndDate);
146+
}
147+
142148
foreach (var instance in instances)
143149
{
144150
if (Latest)

src/VSSetup.PowerShell/VSSetup.PowerShell.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<Compile Include="Extensions.cs" />
5252
<Compile Include="FailedPackageReference.cs" />
5353
<Compile Include="Instance.cs" />
54+
<Compile Include="InstanceComparer.cs" />
5455
<Compile Include="Module.cs" />
5556
<Compile Include="NativeMethods.cs" />
5657
<Compile Include="PackageReference.cs" />
@@ -112,4 +113,4 @@
112113
</PropertyGroup>
113114
<Error Condition="!Exists('..\..\packages\Nerdbank.GitVersioning.1.5.97\build\portable-net+win+wpa+wp+sl+netmf+MonoAndroid+MonoTouch+Xamarin.iOS\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Nerdbank.GitVersioning.1.5.97\build\portable-net+win+wpa+wp+sl+netmf+MonoAndroid+MonoTouch+Xamarin.iOS\Nerdbank.GitVersioning.targets'))" />
114115
</Target>
115-
</Project>
116+
</Project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// <copyright file="InstanceComparerTests.cs" company="Microsoft Corporation">
2+
// Copyright (C) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt in the project root for license information.
4+
// </copyright>
5+
6+
namespace Microsoft.VisualStudio.Setup
7+
{
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Runtime.InteropServices.ComTypes;
11+
using Microsoft.VisualStudio.Setup.Configuration;
12+
using Moq;
13+
using Xunit;
14+
15+
public class InstanceComparerTests
16+
{
17+
public static IEnumerable<object[]> GetCompareVersionAndDateData()
18+
{
19+
var empty = new string[0];
20+
21+
var v1 = new Version(1, 0);
22+
var v2 = new Version(2, 0);
23+
24+
var ft1 = new FILETIME { dwHighDateTime = 1 };
25+
var ft2 = new FILETIME { dwHighDateTime = 2 };
26+
27+
Instance Mock(Version v = null, FILETIME ft = default(FILETIME))
28+
{
29+
var mock = new Mock<ISetupInstance2>();
30+
mock.As<ISetupPropertyStore>().Setup(x => x.GetNames()).Returns(empty);
31+
32+
mock.Setup(x => x.GetInstallationVersion()).Returns(v?.ToString());
33+
mock.Setup(x => x.GetInstallDate()).Returns(ft);
34+
35+
return new Instance(mock.Object);
36+
}
37+
38+
return new[]
39+
{
40+
new object[] { null, null, 0 },
41+
new object[] { null, Mock(), -1 },
42+
new object[] { Mock(), null, 1 },
43+
new object[] { Mock(), Mock(v1), -1 },
44+
new object[] { Mock(v1), Mock(), 1 },
45+
new object[] { Mock(v1), Mock(v1), 0 },
46+
new object[] { Mock(v1), Mock(v2), -1 },
47+
new object[] { Mock(v2), Mock(v1), 1 },
48+
new object[] { Mock(v2, ft1), Mock(v2, ft2), -1 },
49+
new object[] { Mock(v2, ft2), Mock(v2, ft1), 1 },
50+
};
51+
}
52+
53+
[Theory]
54+
[MemberData(nameof(GetCompareVersionAndDateData))]
55+
public void CompareVersionAndDate(Instance x, Instance y, int expected)
56+
{
57+
var actual = InstanceComparer.VersionAndDate.Compare(x, y);
58+
Assert.Equal(expected, actual);
59+
}
60+
}
61+
}

test/VSSetup.PowerShell.Test/VSSetup.PowerShell.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<Compile Include="PowerShell\SelectInstanceCommandTests.cs" />
9191
<Compile Include="Properties\AssemblyInfo.cs" />
9292
<Compile Include="ReadOnlyDictionaryTests.cs" />
93+
<Compile Include="InstanceComparerTests.cs" />
9394
<Compile Include="UtilitiesTests.cs" />
9495
<Compile Include="ValidateTests.cs" />
9596
</ItemGroup>

0 commit comments

Comments
 (0)