Try to find vswhere.exe on the system if the tool is not registered. · dreamsql/cake@4f2b636 · GitHub
Skip to content

Commit 4f2b636

Browse files
paulomorgadodevlead
authored andcommitted
Try to find vswhere.exe on the system if the tool is not registered.
1 parent 0ce4532 commit 4f2b636

9 files changed

Lines changed: 157 additions & 7 deletions

File tree

‎src/Cake.Common.Tests/Fixtures/Tools/VSWhere/VSWhereFixture.cs‎

Lines changed: 27 additions & 0 deletions
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Cake.Common.Tools.VSWhere;
6+
using Cake.Core.IO;
7+
using Cake.Core.Tooling;
8+
9+
namespace Cake.Common.Tests.Fixtures.Tools.VSWhere
10+
{
11+
using System.Linq;
12+
using Cake.Core;
13+
14+
internal class VSWhereToolFixture : VSWhereFixture<ToolSettings>
15+
{
16+
internal VSWhereToolFixture(bool is64BitOperativeSystem)
17+
: base(is64BitOperativeSystem)
18+
{
19+
}
20+
21+
protected override void RunTool()
22+
{
23+
var tool = new VSWhereTool(FileSystem, Environment, ProcessRunner, Tools);
24+
tool.Run(Settings);
25+
}
26+
27+
private sealed class VSWhereTool : VSWhereTool<ToolSettings>
28+
{
29+
public VSWhereTool(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner, IToolLocator toolLocator)
30+
: base(fileSystem, environment, processRunner, toolLocator)
31+
{
32+
}
33+
public DirectoryPath Run(ToolSettings settings)
34+
{
35+
return RunVSWhere(settings, new ProcessArgumentBuilder()).FirstOrDefault();
36+
}
37+
}
38+
}
39+
}

‎src/Cake.Common.Tests/Unit/Tools/VSWhere/All/VSWhereAllTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void Should_Find_VSWhere_Executable_If_Tool_Path_Not_Provided()
112112
var result = fixture.Run();
113113

114114
// Then
115-
Assert.Equal("/Working/tools/vswhere.exe", result.Path.FullPath);
115+
Assert.Equal("/Program86/Microsoft Visual Studio/Installer/vswhere.exe", result.Path.FullPath);
116116
}
117117

118118
[Fact]

‎src/Cake.Common.Tests/Unit/Tools/VSWhere/Latest/VSWhereLatestTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void Should_Find_VSWhere_Executable_If_Tool_Path_Not_Provided()
112112
var result = fixture.Run();
113113

114114
// Then
115-
Assert.Equal("/Working/tools/vswhere.exe", result.Path.FullPath);
115+
Assert.Equal("/Program86/Microsoft Visual Studio/Installer/vswhere.exe", result.Path.FullPath);
116116
}
117117

118118
[Fact]

‎src/Cake.Common.Tests/Unit/Tools/VSWhere/Legacy/VSWhereLegacyTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void Should_Find_VSWhere_Executable_If_Tool_Path_Not_Provided()
112112
var result = fixture.Run();
113113

114114
// Then
115-
Assert.Equal("/Working/tools/vswhere.exe", result.Path.FullPath);
115+
Assert.Equal("/Program86/Microsoft Visual Studio/Installer/vswhere.exe", result.Path.FullPath);
116116
}
117117

118118
[Fact]

‎src/Cake.Common.Tests/Unit/Tools/VSWhere/Product/VSWhereProductTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void Should_Find_VSWhere_Executable_If_Tool_Path_Not_Provided()
112112
var result = fixture.Run();
113113

114114
// Then
115-
Assert.Equal("/Working/tools/vswhere.exe", result.Path.FullPath);
115+
Assert.Equal("/Program86/Microsoft Visual Studio/Installer/vswhere.exe", result.Path.FullPath);
116116
}
117117

118118
[Fact]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Cake.Common.Tests.Fixtures.Tools.VSWhere;
6+
using Cake.Testing;
7+
using Xunit;
8+
9+
namespace Cake.Common.Tests.Unit.Tools.VSWhere
10+
{
11+
public sealed class VSWhereToolTests
12+
{
13+
public sealed class ToolResolution
14+
{
15+
[Fact]
16+
public void Should_Return_The_Registered_VSWhere_If_VSWhere_Is_Registered()
17+
{
18+
// Given
19+
var fixture = new VSWhereToolFixture(is64BitOperativeSystem: true);
20+
21+
var registeredVSWhere = fixture.FileSystem.CreateFile("/Registered/vswhere.exe");
22+
fixture.Tools.RegisterFile(registeredVSWhere.Path);
23+
24+
// When
25+
var result = fixture.Run();
26+
27+
// Then
28+
Assert.Equal(registeredVSWhere.Path.FullPath, result.Path.FullPath);
29+
}
30+
31+
[Fact]
32+
public void Should_Return_The_VSWhere_Default_Path_64Bit()
33+
{
34+
// Given
35+
var fixture = new VSWhereToolFixture(is64BitOperativeSystem: true);
36+
37+
// When
38+
var result = fixture.Run();
39+
40+
// Then
41+
Assert.Equal("/Program86/Microsoft Visual Studio/Installer/vswhere.exe", result.Path.FullPath);
42+
}
43+
44+
#pragma warning disable SA1005 // Single line comments should begin with single space
45+
//[Fact]
46+
//public void Should_Return_The_VSWhere_Default_Path_32Bit()
47+
//{
48+
// // Given
49+
// var fixture = new VSWhereToolFixture(is64BitOperativeSystem: false);
50+
51+
// // When
52+
// var result = fixture.Run();
53+
54+
// // Then
55+
// Assert.Equal("/Program/Microsoft Visual Studio/Installer/vswhere.exe", result.Path.FullPath);
56+
//}
57+
#pragma warning restore SA1005 // Single line comments should begin with single space
58+
}
59+
}
60+
}

‎src/Cake.Common/Tools/VSWhere/VSWhereTool.cs‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@ namespace Cake.Common.Tools.VSWhere
1818
public abstract class VSWhereTool<TSettings> : Tool<TSettings>
1919
where TSettings : ToolSettings
2020
{
21+
private const string VSWhereExecutableName = "vswhere.exe";
22+
private readonly ICakeEnvironment _environment;
23+
2124
/// <summary>
2225
/// Initializes a new instance of the <see cref="VSWhereTool{TSettings}"/> class.
2326
/// </summary>
2427
/// <param name="fileSystem">The file system.</param>
2528
/// <param name="environment">The environment.</param>
2629
/// <param name="processRunner">The process runner.</param>
27-
/// <param name="toolLocator">The tool servce.</param>
30+
/// <param name="toolLocator">The tool locator service.</param>
2831
protected VSWhereTool(IFileSystem fileSystem,
2932
ICakeEnvironment environment,
3033
IProcessRunner processRunner,
3134
IToolLocator toolLocator)
3235
: base(fileSystem, environment, processRunner, toolLocator)
3336
{
37+
_environment = environment;
3438
}
3539

3640
/// <summary>
@@ -48,7 +52,27 @@ protected override string GetToolName()
4852
/// <returns>The tool executable name.</returns>
4953
protected override IEnumerable<string> GetToolExecutableNames()
5054
{
51-
return new[] { "vswhere.exe" };
55+
return new[] { VSWhereExecutableName };
56+
}
57+
58+
/// <summary>
59+
/// Gets alternative file paths which the tool may exist in
60+
/// </summary>
61+
/// <param name="settings">The settings.</param>
62+
/// <returns>The default tool path.</returns>
63+
protected override IEnumerable<FilePath> GetAlternativeToolPaths(TSettings settings)
64+
{
65+
/*
66+
* According to https://blogs.msdn.microsoft.com/heaths/2017/04/21/vswhere-is-now-installed-with-visual-studio-2017/,
67+
* Starting in the latest preview release of Visual Studio version 15.2 (26418.1-Preview), you can now find vswhere installed in
68+
* “%ProgramFiles(x86)%\Microsoft Visual Studio\Installer” (on 32-bit operating systems before Windows 10, you should use
69+
* “%ProgramFiles%\Microsoft Visual Studio\Installer”).
70+
*/
71+
72+
return new FilePath[]
73+
{
74+
_environment.GetSpecialPath(_environment.Platform.Is64Bit ? SpecialPath.ProgramFilesX86 : SpecialPath.ProgramFiles).CombineWithFilePath("Microsoft Visual Studio/Installer/" + VSWhereExecutableName),
75+
};
5276
}
5377

5478
/// <summary>

‎src/Cake.Testing/Fixtures/ToolFixture`2.cs‎

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)