buildalyzer - Buildalyzer 2.3.0-build-20190516-8

A little utility to perform design-time builds of .NET projects without having to think too hard about it. Should work with any project type on any .NET runtime.

PM> Install-Package Buildalyzer -Version 2.3.0-build-20190516-8 -Source https://www.myget.org/F/buildalyzer/api/v3/index.json

Copy to clipboard

> nuget.exe install Buildalyzer -Version 2.3.0-build-20190516-8 -Source https://www.myget.org/F/buildalyzer/api/v3/index.json

Copy to clipboard

> dotnet add package Buildalyzer --version 2.3.0-build-20190516-8 --source https://www.myget.org/F/buildalyzer/api/v3/index.json

Copy to clipboard
<PackageReference Include="Buildalyzer" Version="2.3.0-build-20190516-8" />
Copy to clipboard
source https://www.myget.org/F/buildalyzer/api/v3/index.json

nuget Buildalyzer  ~> 2.3.0-build-20190516-8
Copy to clipboard

> choco install Buildalyzer --version 2.3.0-build-20190516-8 --source https://www.myget.org/F/buildalyzer/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "buildalyzer" -SourceLocation "https://www.myget.org/F/buildalyzer/api/v2"
Install-Module -Name "Buildalyzer" -RequiredVersion "2.3.0-build-20190516-8" -Repository "buildalyzer" -AllowPreRelease
Copy to clipboard

A utility to perform design-time builds of .NET projects without having to think too hard about it.

Logo

NuGet

MyGet

GitHub

Donations

If you found this library useful, consider kicking me a few bucks. I promise to use it on something totally frivolous and unrelated.

Buy Me A Coffee


What Is It?

Buildalyzer lets you run MSBuild from your own code and returns information about the project. By default, it runs a design-time build which is higher performance than a normal build because it doesn't actually try to compile the project. You can use it to perform analysis of MSBuild projects, get project properties, or create a Roslyn Workspace using Buildalyzer.Workspaces. It runs MSBuild out-of-process and therefore should work anywhere, anytime, and on any platform you can build the project yourself manually on the command line.

AnalyzerManager manager = new AnalyzerManager();
ProjectAnalyzer analyzer = manager.GetProject(@"C:\MyCode\MyProject.csproj");
AnalyzerResults results = analyzer.Build();
string[] sourceFiles = results.First().SourceFiles;

These blog posts might also help explain the motivation behind the project and how it works:

Installation

Buildalyzer is available on NuGet and can be installed via the commands below:

$ Install-Package Buildalyzer

or via the .NET Core CLI:

$ dotnet add package Buildalyzer

Buildalyzer.Workspaces is available on NuGet and can be installed via the commands below:

$ Install-Package Buildalyzer.Workspaces

or via the .NET Core CLI:

$ dotnet add package Buildalyzer.Workspaces

Both packages target .NET Standard 2.0.

Usage

There are two main classes in Buildalyzer: AnalyzerManager and ProjectAnalyzer.

The AnalyzerManager class coordinates loading each individual project and consolidates information from a solution file if provided.

The ProjectAnalyzer class figures out how to configure MSBuild and uses it to load and compile the project in design-time mode. Using a design-time build lets us get information about the project such as resolved references and source files without actually having to call the compiler.

To get a ProjectAnalyzer you first create an AnalyzerManager and then call GetProject():

AnalyzerManager manager = new AnalyzerManager();
ProjectAnalyzer analyzer = manager.GetProject(@"C:\MyCode\MyProject.csproj");

You can add all projects in a solution to the AnalyzerManager by passing the solution path as the first argument of the AnalyzerManager constructor. This will parse the solution file and execute GetProject() for each of the projects that it finds.

Calling GetProject() again for the same project path will return the existing ProjectAnalyzer. You can iterate all the existing project analyzers with the IReadOnlyDictionary<string, ProjectAnalyzer> property AnalyzerManager.Projects.

To build the project, which triggers evaluation of the specified MSBuild tasks and targets but stops short of invoking the compiler by default in Buildalyzer, call Build(). This method has a number of overloads that lets you customize the build process by specifying target frameworks, build targets, and more.

Results

Calling ProjectAnalyzer.Build() (or an overload) will return an AnalyzerResults object, which is a collection of AnalyzerResult objects for each of the target frameworks that were built. It will usually only contain a single AnalyzerResult unless the project is multi-targeted.

AnalyzerResult contains several properties and methods with the results from the build:

AnalyzerResult.TargetFramework - The target framework of this particular result (each result consists of data from a particular target framework build).

AnalyzerResult.SourceFiles - The full path of all resolved source files in the project.

AnalyzerResult.References - The full path of all resolved references in the project.

AnalyzerResult.ProjectReferences - The full path of the project file for all resolved project references in the project.

AnalyzerResult.Properties - A IReadOnlyDictionary<string, string> containing all MSBuild properties from the project.

AnalyzerResult.GetProperty(string) - Gets the value of the specified MSBuild property.

AnalyzerResult.Items - A IReadOnlyDictionary<string, ProjectItem[]> containing all MSBuild items from the project (the ProjectItem class contains the item name/specification as ProjectItem.ItemSpec and all it's metadata in a IReadOnlyDictionary<string, string> as ProjectItem.Metadata).

Adjusting MSBuild Properties

Buildalyzer sets some MSBuild properties to make loading and compilation work the way it needs to (for example, to trigger a design-time build). You can view these properties with the IReadOnlyDictionary<string, string> property ProjectAnalyzer.GlobalProperties.

If you want to change the configured properties before loading or compiling the project, there are two options:

  • AnalyzerManager.SetGlobalProperty(string key, string value) and AnalyzerManager.RemoveGlobalProperty(string key). This will set the global properties for all projects loaded by this AnalyzerManager.

  • ProjectAnalyzer.SetGlobalProperty(string key, string value) and ProjectAnalyzer.RemoveGlobalProperty(string key). This will set the global properties for just this project.

Be careful though, you may break the ability to load, compile, or interpret the project if you change the MSBuild properties.

Binary Log Files

Buildalyzer can also read MSBuild binary log files:

AnalyzerManager manager = new AnalyzerManager();
AnalyzerResults results = manager.Analyze(@"C:\MyCode\MyProject.binlog");
string[] sourceFiles = results.First().SourceFiles;

This is useful if you already have a binary log file and want to analyze it with Buildalyzer the same way you would build results.

Logging

Buildalyzer uses the Microsoft.Extensions.Logging framework for logging MSBuild output. When you create an AnayzerManager you can specify an ILoggerFactory that Buildalyzer should use to create loggers. By default, the ProjectAnalyzer will log MSBuild output to the provided logger.

You can also log to a StringWriter using AnalyzerManagerOptions:

StringWriter log = new StringWriter();
AnalyzerManagerOptions options = new AnalyzerManagerOptions
{
    LogWriter = log
};
AnalyzerManager manager = new AnalyzerManager(path, options);
// ...
// check log.ToString() after build for any error messages

Roslyn Workspaces

The extension library Buildalyzer.Workspaces adds extension methods to the Buildalyzer ProjectAnalyzer that make it easier to take Buildalyzer output and create a Roslyn AdhocWorkspace from it:

using Buildalyzer.Workspaces;
// ...

AnalyzerManager manager = new AnalyzerManager();
ProjectAnalyzer analyzer = manager.GetProject(@"C:\MyCode\MyProject.csproj");
AdhocWorkspace workspace = analyzer.GetWorkspace();

You can also create your own workspace and add Buildalyzer projects to it:

using Buildalyzer.Workspaces;
// ...

AnalyzerManager manager = new AnalyzerManager();
ProjectAnalyzer analyzer = manager.GetProject(@"C:\MyCode\MyProject.csproj");
AdhocWorkspace workspace = new AdhocWorkspace();
Project roslynProject = analyzer.AddToWorkspace(workspace);

In both cases, Buildalyzer will attempt to resolve project references within the Roslyn workspace so the Roslyn projects will correctly reference each other.

  • .NETStandard 2.0
    • Buildalyzer.Logger (>= 2.3.0-build-20190516-8)
    • Microsoft.Build (>= 15.8.166)
    • Microsoft.Build.Framework (>= 15.8.166)
    • Microsoft.Build.Tasks.Core (>= 15.8.166)
    • Microsoft.Build.Utilities.Core (>= 15.8.166)
    • Microsoft.Extensions.DependencyModel (>= 2.1.0)
    • Microsoft.Extensions.Logging (>= 2.1.1)
    • Microsoft.Extensions.Logging.Abstractions (>= 2.1.1)
    • MSBuild.StructuredLogger (>= 2.0.11)
    • MsBuildPipeLogger.Server (>= 1.1.2)
  • .NETStandard 2.0: 2.0.0.0

Owners

daveaglick

Authors

Dave Glick and contributors

Project URL

https://github.com/daveaglick/Buildalyzer

License

MIT

Info

0 total downloads
0 downloads for version 2.3.0-build-20190516-8
Download (29.37 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
2.3.0-build-20190516-8 29.37 KB Thu, 16 May 2019 14:42:56 GMT 0
2.2.0-build-20190515-1 28.31 KB Wed, 15 May 2019 13:21:21 GMT 0
2.2.0-build-20181226-5 28.28 KB Wed, 26 Dec 2018 17:33:44 GMT 0
2.2.0-build-127 24.66 KB Thu, 08 Nov 2018 17:14:47 GMT 0
2.2.0-build-126 24.67 KB Thu, 08 Nov 2018 16:28:01 GMT 0
2.1.1-build-125 24.64 KB Thu, 08 Nov 2018 15:39:09 GMT 0
2.1.1-build-124 24.63 KB Mon, 05 Nov 2018 19:02:41 GMT 0
2.1.1-build-122 24.46 KB Mon, 05 Nov 2018 17:55:37 GMT 0
2.1.1-build-120 24.33 KB Fri, 02 Nov 2018 20:27:09 GMT 0
2.1.1-build-119 24.33 KB Fri, 02 Nov 2018 19:48:19 GMT 0
2.1.1-build-118 24.01 KB Fri, 02 Nov 2018 15:08:20 GMT 0
2.1.1-build-117 23.81 KB Fri, 02 Nov 2018 14:10:40 GMT 0
2.1.1-build-116 23.53 KB Fri, 02 Nov 2018 13:33:44 GMT 0
2.1.1-build-115 23.54 KB Thu, 01 Nov 2018 20:44:45 GMT 0
2.1.1-build-114 23.54 KB Thu, 01 Nov 2018 17:53:04 GMT 0
2.1.1-build-113 23.52 KB Thu, 01 Nov 2018 14:33:02 GMT 0
2.1.1-build-106 23.52 KB Fri, 05 Oct 2018 15:52:25 GMT 0
2.1.1-build-104 23.15 KB Tue, 02 Oct 2018 19:04:43 GMT 0
2.1.0-build-103 23.5 KB Tue, 02 Oct 2018 18:02:57 GMT 0
2.1.0-build-102 23.16 KB Tue, 02 Oct 2018 17:23:04 GMT 0
2.0.2-build-99 22.47 KB Tue, 02 Oct 2018 00:51:26 GMT 0
2.0.2-build-74 22.83 KB Thu, 27 Sep 2018 21:35:43 GMT 0
2.0.2-build-101 23.51 KB Tue, 02 Oct 2018 15:55:47 GMT 0
2.0.2-build-100 22.82 KB Tue, 02 Oct 2018 12:53:50 GMT 0
2.0.1-build-73 22.83 KB Thu, 27 Sep 2018 20:56:59 GMT 0
2.0.1-build-72 22.83 KB Thu, 27 Sep 2018 20:03:47 GMT 0
2.0.0-build-71 22.53 KB Wed, 26 Sep 2018 20:20:35 GMT 0
2.0.0-build-69 22.54 KB Wed, 26 Sep 2018 19:28:14 GMT 0
2.0.0-build-68 22.54 KB Wed, 26 Sep 2018 18:32:29 GMT 0
2.0.0-build-66 22.53 KB Wed, 26 Sep 2018 16:42:14 GMT 0
2.0.0-build-65 22.54 KB Wed, 26 Sep 2018 16:01:40 GMT 0
1.0.1-build-64 22.53 KB Wed, 26 Sep 2018 01:32:43 GMT 0
1.0.1-build-62 22.24 KB Tue, 25 Sep 2018 19:18:20 GMT 0
1.0.1-build-61 22.24 KB Mon, 24 Sep 2018 20:31:29 GMT 0
1.0.1-build-60 22.2 KB Mon, 24 Sep 2018 19:21:43 GMT 0
1.0.1-build-59 22.05 KB Thu, 20 Sep 2018 17:35:30 GMT 0
1.0.1-build-58 22.6 KB Wed, 19 Sep 2018 17:07:48 GMT 0
1.0.1-build-56 22.79 KB Tue, 18 Sep 2018 18:21:15 GMT 0
1.0.1-build-51 22.59 KB Fri, 14 Sep 2018 19:25:08 GMT 0
1.0.1-build-50 22.55 KB Fri, 14 Sep 2018 15:23:55 GMT 0
1.0.1-build-45 22.52 KB Thu, 13 Sep 2018 23:03:41 GMT 0
1.0.0-build-8 21.75 KB Fri, 03 Aug 2018 13:32:58 GMT 0
1.0.0-build-7 21.75 KB Fri, 03 Aug 2018 13:02:15 GMT 0
1.0.0-build-19 21.75 KB Thu, 16 Aug 2018 14:48:38 GMT 0
1.0.0-build-18 21.75 KB Wed, 15 Aug 2018 19:27:38 GMT 0
1.0.0-build-16 21.75 KB Fri, 03 Aug 2018 15:25:30 GMT 0
1.0.0-build-12 21.76 KB Fri, 03 Aug 2018 14:43:27 GMT 0
0.5.1 14.93 KB Mon, 25 Jun 2018 19:48:36 GMT 0