Skip to content

Commit

Permalink
apply code style rules + update headers
Browse files Browse the repository at this point in the history
  • Loading branch information
YuliiaKovalova committed Aug 25, 2023
1 parent 7aabcd9 commit 77a68d9
Show file tree
Hide file tree
Showing 17 changed files with 606 additions and 228 deletions.
400 changes: 400 additions & 0 deletions .editorconfig

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions MSBuildLocator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
version.json = version.json
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
Expand Down
45 changes: 23 additions & 22 deletions samples/BuilderApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,45 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Framework;
using Microsoft.Build.Locator;

namespace BuilderApp
{
internal class Program
internal sealed class Program
{
private static void Main(string[] args)
{
Header();
var projectFilePath = Args(args);
string projectFilePath = Args(args);

// Before we can build we need to resolve MSBuild assemblies. We could:
// 1) Use defaults and call: MSBuildLocator.RegisterDefaults();
// 2) Do something fancier and ask the user. As an example we'll do that.
var instances = MSBuildLocator.QueryVisualStudioInstances().ToList();
var msbuildDeploymentToUse = AskWhichMSBuildToUse(instances);
(VisualStudioInstance VSInstance, string MSBuildPath) = AskWhichMSBuildToUse(instances);

// Calling Register methods will subscribe to AssemblyResolve event. After this we can
// safely call code that use MSBuild types (in the Builder class).
if (msbuildDeploymentToUse.VSInstance != null)
if (VSInstance != null)
{
Console.WriteLine($"Using MSBuild from VS Instance: {msbuildDeploymentToUse.VSInstance.Name} - {msbuildDeploymentToUse.VSInstance.Version}");
Console.WriteLine($"Using MSBuild from VS Instance: {VSInstance.Name} - {VSInstance.Version}");
Console.WriteLine();

MSBuildLocator.RegisterInstance(msbuildDeploymentToUse.VSInstance);
MSBuildLocator.RegisterInstance(VSInstance);
}
else
{
Console.WriteLine($"Using MSBuild from path: {msbuildDeploymentToUse.MSBuildPath}");
Console.WriteLine($"Using MSBuild from path: {MSBuildPath}");
Console.WriteLine();

MSBuildLocator.RegisterMSBuildPath(msbuildDeploymentToUse.MSBuildPath);
MSBuildLocator.RegisterMSBuildPath(MSBuildPath);
}

var result = new Builder().Build(projectFilePath);
bool result = Builder.Build(projectFilePath);
Console.WriteLine();

Console.ForegroundColor = result ? ConsoleColor.Green : ConsoleColor.Red;
Expand All @@ -59,10 +60,10 @@ private static (VisualStudioInstance VSInstance, string MSBuildPath) AskWhichMSB
}

Console.WriteLine($"0) Custom path");
for (var i = 1; i <= instances.Count; i++)
for (int i = 1; i <= instances.Count; i++)
{
var instance = instances[i - 1];
var recommended = string.Empty;
VisualStudioInstance instance = instances[i - 1];
string recommended = string.Empty;

// The dev console is probably always the right choice because the user explicitly opened
// one associated with a Visual Studio install. It will always be first in the list.
Expand All @@ -74,27 +75,27 @@ private static (VisualStudioInstance VSInstance, string MSBuildPath) AskWhichMSB

Console.WriteLine();
Console.WriteLine("Select an instance of MSBuild: ");
var answer = Console.ReadLine();
string answer = Console.ReadLine();

if (int.TryParse(answer, out int instanceChoice) && instanceChoice >= 0 && instanceChoice <= instances.Count)
{
if (instanceChoice == 0)
{
Console.WriteLine("Input path to MSBuild deployment:");
var msbuildPath = Console.ReadLine();
string msBuildPath = Console.ReadLine();

if (!Directory.Exists(msbuildPath))
if (!Directory.Exists(msBuildPath))
{
Console.WriteLine($"Directory does not exist: {msbuildPath}");
Console.WriteLine($"Directory does not exist: {msBuildPath}");
Environment.Exit(-1);
}

return (null, msbuildPath);
return (null, msBuildPath);

}
else
{
var instanceUsed = instances[instanceChoice - 1];
VisualStudioInstance instanceUsed = instances[instanceChoice - 1];
return (instanceUsed, null);
}
}
Expand All @@ -116,7 +117,7 @@ private static void Header()
private static string Args(string[] args)
{
if (args.Length < 1 || !File.Exists(args[0])) Usage();
var projectFilePath = args[0];
string projectFilePath = args[0];
return projectFilePath;
}

Expand All @@ -139,9 +140,9 @@ private static void Usage()
/// </remarks>
public class Builder
{
public bool Build(string projectFile)
public static bool Build(string projectFile)
{
var assembly = typeof(Project).Assembly;
Assembly assembly = typeof(Project).Assembly;
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);

Console.WriteLine();
Expand All @@ -154,7 +155,7 @@ public bool Build(string projectFile)
return project.Build(new Logger());
}

private class Logger : ILogger
private sealed class Logger : ILogger
{
public void Initialize(IEventSource eventSource)
{
Expand Down
4 changes: 2 additions & 2 deletions src/MSBuildLocator.Tests/QueryInstanceTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Shouldly;
using System.Linq;
Expand Down
8 changes: 4 additions & 4 deletions src/MSBuildLocator.Tests/QueryOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Shouldly;
using System;
Expand Down Expand Up @@ -55,7 +55,7 @@ public void NoResultsTest()
VerifyQueryResults(instances, DiscoveryType.DotNetSdk);
}

private void VerifyQueryResults(IEnumerable<VisualStudioInstance> instances, DiscoveryType discoveryTypes, params string[] expectedInstanceNames)
private static void VerifyQueryResults(IEnumerable<VisualStudioInstance> instances, DiscoveryType discoveryTypes, params string[] expectedInstanceNames)
{
IEnumerable<VisualStudioInstance> actual = MSBuildLocator.QueryVisualStudioInstances(instances, new VisualStudioInstanceQueryOptions
{
Expand All @@ -76,4 +76,4 @@ private void VerifyQueryResults(IEnumerable<VisualStudioInstance> instances, Dis
}
}
}
}
}
63 changes: 32 additions & 31 deletions src/MSBuildLocator.Tests/SemanticVersionParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if NETCOREAPP

using Microsoft.Build.Locator.Utils;
using Shouldly;
using System.Linq;
using Xunit;
Expand All @@ -14,94 +15,94 @@ public class SemanticVersionParserTests
[Fact]
public void TryParseTest_ReleaseVersion()
{
var version = "7.0.333";
string version = "7.0.333";

var isParsed = SemanticVersionParser.TryParse(version, out var parsedVerion);
bool isParsed = SemanticVersionParser.TryParse(version, out SemanticVersion parsedVersion);

parsedVerion.ShouldNotBeNull();
_ = parsedVersion.ShouldNotBeNull();
isParsed.ShouldBeTrue();
parsedVerion.Major.ShouldBe(7);
parsedVerion.Minor.ShouldBe(0);
parsedVerion.Patch.ShouldBe(333);
parsedVerion.ReleaseLabels.ShouldBeEmpty();
parsedVersion.Major.ShouldBe(7);
parsedVersion.Minor.ShouldBe(0);
parsedVersion.Patch.ShouldBe(333);
parsedVersion.ReleaseLabels.ShouldBeEmpty();
}

[Fact]
public void TryParseTest_PreviewVersion()
{
var version = "8.0.0-preview.6.23329.7";
string version = "8.0.0-preview.6.23329.7";

var isParsed = SemanticVersionParser.TryParse(version, out var parsedVerion);
bool isParsed = SemanticVersionParser.TryParse(version, out SemanticVersion parsedVersion);

parsedVerion.ShouldNotBeNull();
_ = parsedVersion.ShouldNotBeNull();
isParsed.ShouldBeTrue();
parsedVerion.Major.ShouldBe(8);
parsedVerion.Minor.ShouldBe(0);
parsedVerion.Patch.ShouldBe(0);
parsedVerion.ReleaseLabels.ShouldBe(new[] { "preview", "6", "23329", "7" });
parsedVersion.Major.ShouldBe(8);
parsedVersion.Minor.ShouldBe(0);
parsedVersion.Patch.ShouldBe(0);
parsedVersion.ReleaseLabels.ShouldBe(new[] { "preview", "6", "23329", "7" });
}

[Fact]
public void TryParseTest_InvalidInput_LeadingZero()
{
var version = "0.0-preview.6";
string version = "0.0-preview.6";

var isParsed = SemanticVersionParser.TryParse(version, out var parsedVerion);
bool isParsed = SemanticVersionParser.TryParse(version, out SemanticVersion parsedVersion);

Assert.Null(parsedVerion);
Assert.Null(parsedVersion);
isParsed.ShouldBeFalse();
}

[Fact]
public void TryParseTest_InvalidInput_FourPartsVersion()
{
var version = "5.0.3.4";
string version = "5.0.3.4";

var isParsed = SemanticVersionParser.TryParse(version, out var parsedVerion);
bool isParsed = SemanticVersionParser.TryParse(version, out SemanticVersion parsedVersion);

Assert.Null(parsedVerion);
Assert.Null(parsedVersion);
isParsed.ShouldBeFalse();
}

[Fact]
public void VersionSortingTest_WithPreview()
{
var versions = new[] { "7.0.7", "8.0.0-preview.6.23329.7", "8.0.0-preview.3.23174.8" };
string[] versions = new[] { "7.0.7", "8.0.0-preview.6.23329.7", "8.0.0-preview.3.23174.8" };

var maxVersion = versions.Select(v => SemanticVersionParser.TryParse(v, out var parsedVerion) ? parsedVerion : null).Max();
SemanticVersion maxVersion = versions.Select(v => SemanticVersionParser.TryParse(v, out SemanticVersion parsedVersion) ? parsedVersion : null).Max();

maxVersion.OriginalValue.ShouldBe("8.0.0-preview.6.23329.7");
}

[Fact]
public void VersionSortingTest_ReleaseOnly()
{
var versions = new[] { "7.0.7", "3.7.2", "10.0.0" };
string[] versions = new[] { "7.0.7", "3.7.2", "10.0.0" };

var maxVersion = versions.Max(v => SemanticVersionParser.TryParse(v, out var parsedVerion) ? parsedVerion : null);
SemanticVersion maxVersion = versions.Max(v => SemanticVersionParser.TryParse(v, out SemanticVersion parsedVersion) ? parsedVersion : null);

maxVersion.OriginalValue.ShouldBe("10.0.0");
}

[Fact]
public void VersionSortingTest_WithInvalidFolderNames()
{
var versions = new[] { "7.0.7", "3.7.2", "dummy", "5.7.8.9" };
string[] versions = new[] { "7.0.7", "3.7.2", "dummy", "5.7.8.9" };

var maxVersion = versions.Max(v => SemanticVersionParser.TryParse(v, out var parsedVerion) ? parsedVerion : null);
SemanticVersion maxVersion = versions.Max(v => SemanticVersionParser.TryParse(v, out SemanticVersion parsedVersion) ? parsedVersion : null);

maxVersion.OriginalValue.ShouldBe("7.0.7");
}

[Fact]
public void VersionSortingTest_WithAllInvalidFolderNames()
{
var versions = new[] { "dummy", "5.7.8.9" };
string[] versions = new[] { "dummy", "5.7.8.9" };

var maxVersion = versions.Max(v => SemanticVersionParser.TryParse(v, out var parsedVerion) ? parsedVerion : null);
SemanticVersion maxVersion = versions.Max(v => SemanticVersionParser.TryParse(v, out SemanticVersion parsedVersion) ? parsedVersion : null);

maxVersion.ShouldBeNull();
}
}
}
#endif
#endif
6 changes: 3 additions & 3 deletions src/MSBuildLocator/DiscoveryType.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

Expand Down Expand Up @@ -27,4 +27,4 @@ public enum DiscoveryType
/// </summary>
DotNetSdk = 4
}
}
}
Loading

0 comments on commit 77a68d9

Please sign in to comment.