-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add support for user-supplied project file detection #2684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
39791f7
Add support for user-supplied project file detection
Genbox 94aa84c
Make ProjectLocator fall back to other locators if enabled
Genbox b7b2130
Implement most of the suggestion
Genbox fd4bb75
Use fallback logic instead
Genbox 4d84092
Add a few docs and use default code style
Genbox 33ab1dd
Include file locator paths in exception message
Genbox 4479071
Update src/BenchmarkDotNet/Toolchains/CsProj/CsProjGenerator.cs
Genbox fcd632e
Create integration test
Genbox 4802d24
Support .NET Framework csproj tool chain as well
Genbox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace BenchmarkDotNet.Locators; | ||
|
|
||
| public interface ILocator | ||
| { | ||
| LocatorType LocatorType { get; } | ||
| FileInfo Locate(DirectoryInfo rootDirectory, Type type); | ||
Genbox marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace BenchmarkDotNet.Locators; | ||
|
|
||
| public enum LocatorType | ||
| { | ||
| ProjectFile | ||
| } | ||
Genbox marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using BenchmarkDotNet.Exporters; | ||
|
|
||
| namespace BenchmarkDotNet.Locators; | ||
|
|
||
| public class ProjectLocator : ILocator | ||
| { | ||
| public static ProjectLocator Default { get; } = new ProjectLocator(); | ||
|
|
||
| public LocatorType LocatorType => LocatorType.ProjectFile; | ||
|
|
||
| public FileInfo Locate(DirectoryInfo rootDirectory, Type type) | ||
| { | ||
| // important assumption! project's file name === output dll name | ||
| string projectName = type.GetTypeInfo().Assembly.GetName().Name; | ||
|
|
||
| var possibleNames = new HashSet<string> { $"{projectName}.csproj", $"{projectName}.fsproj", $"{projectName}.vbproj" }; | ||
| var projectFiles = rootDirectory | ||
| .EnumerateFiles("*proj", SearchOption.AllDirectories) | ||
| .Where(file => possibleNames.Contains(file.Name)) | ||
| .ToArray(); | ||
|
|
||
| if (projectFiles.Length == 0) | ||
| { | ||
| throw new NotSupportedException( | ||
Genbox marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $"Unable to find {projectName} in {rootDirectory.FullName} and its subfolders. Most probably the name of output exe is different than the name of the .(c/f)sproj"); | ||
| } | ||
| else if (projectFiles.Length > 1) | ||
| { | ||
| throw new NotSupportedException( | ||
| $"Found more than one matching project file for {projectName} in {rootDirectory.FullName} and its subfolders: {string.Join(",", projectFiles.Select(pf => $"'{pf.FullName}'"))}. Benchmark project names needs to be unique."); | ||
| } | ||
|
|
||
| return projectFiles[0]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| using System; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Collections.Immutable; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
@@ -11,6 +11,7 @@ | |
| using BenchmarkDotNet.Extensions; | ||
| using BenchmarkDotNet.Helpers; | ||
| using BenchmarkDotNet.Jobs; | ||
| using BenchmarkDotNet.Locators; | ||
| using BenchmarkDotNet.Loggers; | ||
| using BenchmarkDotNet.Running; | ||
| using BenchmarkDotNet.Toolchains.DotNetCli; | ||
|
|
@@ -71,7 +72,7 @@ protected override string GetIntermediateDirectoryPath(string buildArtifactsDire | |
| protected override void GenerateProject(BuildPartition buildPartition, ArtifactsPaths artifactsPaths, ILogger logger) | ||
| { | ||
| var benchmark = buildPartition.RepresentativeBenchmarkCase; | ||
| var projectFile = GetProjectFilePath(benchmark.Descriptor.Type, logger); | ||
| var projectFile = GetProjectFilePath(benchmark, logger); | ||
|
|
||
| var xmlDoc = new XmlDocument(); | ||
| xmlDoc.Load(projectFile.FullName); | ||
|
|
@@ -246,36 +247,32 @@ private static string GetIndentedXmlString(XmlDocument doc) | |
| /// returns a path to the project file which defines the benchmarks | ||
| /// </summary> | ||
| [PublicAPI] | ||
| protected virtual FileInfo GetProjectFilePath(Type benchmarkTarget, ILogger logger) | ||
| protected virtual FileInfo GetProjectFilePath(BenchmarkCase benchmark, ILogger logger) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noting the breaking change to a public API. |
||
| { | ||
| var benchmarkTarget = benchmark.Descriptor.Type; | ||
|
|
||
| if (!GetSolutionRootDirectory(out var rootDirectory) && !GetProjectRootDirectory(out rootDirectory)) | ||
| { | ||
| logger.WriteLineError( | ||
| $"Unable to find .sln or .csproj file. Will use current directory {Directory.GetCurrentDirectory()} to search for project file. If you don't use .sln file on purpose it should not be a problem."); | ||
| rootDirectory = new DirectoryInfo(Directory.GetCurrentDirectory()); | ||
| } | ||
Genbox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // important assumption! project's file name === output dll name | ||
| string projectName = benchmarkTarget.GetTypeInfo().Assembly.GetName().Name; | ||
| List<string> notFound = new List<string>(); | ||
| foreach (ILocator locator in benchmark.Config.GetLocators()) | ||
Genbox marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| if (locator.LocatorType != LocatorType.ProjectFile) | ||
| continue; | ||
|
|
||
| var possibleNames = new HashSet<string> { $"{projectName}.csproj", $"{projectName}.fsproj", $"{projectName}.vbproj" }; | ||
| var projectFiles = rootDirectory | ||
| .EnumerateFiles("*proj", SearchOption.AllDirectories) | ||
| .Where(file => possibleNames.Contains(file.Name)) | ||
| .ToArray(); | ||
| var path = locator.Locate(rootDirectory, benchmarkTarget); | ||
|
|
||
| if (projectFiles.Length == 0) | ||
| { | ||
| throw new NotSupportedException( | ||
| $"Unable to find {projectName} in {rootDirectory.FullName} and its subfolders. Most probably the name of output exe is different than the name of the .(c/f)sproj"); | ||
| } | ||
| else if (projectFiles.Length > 1) | ||
| { | ||
| throw new NotSupportedException( | ||
| $"Found more than one matching project file for {projectName} in {rootDirectory.FullName} and its subfolders: {string.Join(",", projectFiles.Select(pf => $"'{pf.FullName}'"))}. Benchmark project names needs to be unique."); | ||
| if (path.Exists) | ||
| return path; | ||
|
|
||
| notFound.Add(path.FullName); | ||
| } | ||
|
|
||
| return projectFiles[0]; | ||
| throw new NotSupportedException($"Unable to find project file in {rootDirectory}. Attempted location(s): " + string.Join(", ", notFound)); | ||
Genbox marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public override bool Equals(object obj) => obj is CsProjGenerator other && Equals(other); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.