Buildalyzer.Workspaces
7.1.0
dotnet add package Buildalyzer.Workspaces --version 7.1.0
NuGet\Install-Package Buildalyzer.Workspaces -Version 7.1.0
<PackageReference Include="Buildalyzer.Workspaces" Version="7.1.0" />
paket add Buildalyzer.Workspaces --version 7.1.0
#r "nuget: Buildalyzer.Workspaces, 7.1.0"
// Install Buildalyzer.Workspaces as a Cake Addin #addin nuget:?package=Buildalyzer.Workspaces&version=7.1.0 // Install Buildalyzer.Workspaces as a Cake Tool #tool nuget:?package=Buildalyzer.Workspaces&version=7.1.0
A utility to perform design-time builds of .NET projects without having to think too hard about it.
NuGet
GitHub
Donations
If you found this library useful, consider sponsoring more of it on GitHub. I promise to use it on something totally frivolous and unrelated.
Sponsors
Amazon Web Services (AWS) generously sponsors this project. Go check out what they have to offer for .NET developers (it's probably more than you think).
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();
IProjectAnalyzer analyzer = manager.GetProject(@"C:\MyCode\MyProject.csproj");
IAnalyzerResults 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();
IProjectAnalyzer 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)
andAnalyzerManager.RemoveGlobalProperty(string key)
. This will set the global properties for all projects loaded by thisAnalyzerManager
.ProjectAnalyzer.SetGlobalProperty(string key, string value)
andProjectAnalyzer.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.
Publish SingleFile
If your application's output is a single file, you will need to provide the path to the following DLLs:
-MsBuildPipeLogger.Logger.dll -Buildalyzer.logger.dll
Variable name: LoggerPathDll
See related issue 224 msbuild needs the physical address of the logger, for this reason it is not possible to use single file publish without informing this route.
Remembering that if the files are in the root where the project is running, it is not necessary to inform the path.
Binary Log Files
Buildalyzer can also read MSBuild binary log files:
AnalyzerManager manager = new AnalyzerManager();
IAnalyzerResults 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;
using Microsoft.CodeAnalysis;
// ...
AnalyzerManager manager = new AnalyzerManager();
IProjectAnalyzer 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;
using Microsoft.CodeAnalysis;
// ...
AnalyzerManager manager = new AnalyzerManager();
IProjectAnalyzer 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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Buildalyzer (>= 7.1.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.10.0)
- Microsoft.CodeAnalysis.VisualBasic.Workspaces (>= 4.10.0)
-
net8.0
- Buildalyzer (>= 7.1.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.10.0)
- Microsoft.CodeAnalysis.VisualBasic.Workspaces (>= 4.10.0)
NuGet packages (13)
Showing the top 5 NuGet packages that depend on Buildalyzer.Workspaces:
Package | Downloads |
---|---|
MongoDBMigrations
MongoDbMigrations uses the official MongoDB C# Driver to migrate your documents in your mongo database via useful fluent API. Supports up and down migrations with cancelation and progress handling. Also, this library is able to check a schema of collections in your database during the migration run. This version supports on-premise Mongo database either Azure CosmosDB (with Mongo-like API) or even AWS DocumentDB. In addition you can use TLS and/or SHH tunnels in your migrations. PS1 script for integration with CI/CD pipelines provides inside of the repository |
|
Wyam.CodeAnalysis
Wyam is a simple to use, highly modular, and extremely configurable static content generator. This library provides support for performing code analysis. |
|
Statiq.Web
Statiq Web is a flexible static site generator. |
|
Statiq.CodeAnalysis
Statiq is a configurable static content generation framework. This library provides support for performing code analysis. |
|
Stankins.AnalyzeSolution
Package Description |
GitHub repositories (7)
Showing the top 5 popular GitHub repositories that depend on Buildalyzer.Workspaces:
Repository | Stars |
---|---|
statiqdev/Statiq.Web
Statiq Web is a flexible static site generator written in .NET.
|
|
riganti/dotvvm
Open source MVVM framework for Web Apps
|
|
BrightstarDB/BrightstarDB
This is the core development repository for BrightstarDB.
|
|
statiqdev/Statiq.Framework
A flexible and extensible static content generation framework for .NET.
|
|
byme8/ZeroQL
C# GraphQL client with Linq-like syntax
|
Version | Downloads | Last updated |
---|---|---|
7.1.0 | 889 | 11/16/2024 |
7.0.2 | 27,405 | 6/18/2024 |
7.0.1 | 4,306 | 5/14/2024 |
6.0.4 | 42,747 | 1/6/2024 |
6.0.3 | 421 | 12/22/2023 |
6.0.2 | 731 | 12/20/2023 |
6.0.1 | 144 | 12/20/2023 |
6.0.0 | 836 | 12/19/2023 |
5.0.1 | 24,857 | 9/1/2023 |
5.0.0 | 17,572 | 12/5/2022 |
4.1.7 | 2,059 | 11/29/2022 |
4.1.6 | 1,001 | 10/31/2022 |
4.1.5 | 2,588 | 9/22/2022 |
4.1.4 | 85,653 | 5/5/2022 |
4.1.3 | 34,894 | 3/18/2022 |
4.1.2 | 104,858 | 3/2/2022 |
4.1.1 | 2,190 | 2/15/2022 |
4.1.0 | 5,100 | 2/15/2022 |
3.2.8 | 7,188 | 1/20/2022 |
3.2.7 | 3,374 | 1/12/2022 |
3.2.6 | 15,024 | 12/23/2021 |
3.2.5 | 1,185 | 12/14/2021 |
3.2.3 | 15,793 | 8/17/2021 |
3.2.2 | 21,328 | 5/26/2021 |
3.2.1 | 2,392 | 5/5/2021 |
3.2.0 | 35,612 | 1/12/2021 |
3.1.1 | 1,201 | 12/20/2020 |
3.1.0 | 3,492 | 10/30/2020 |
3.0.1 | 104,243 | 6/24/2020 |
3.0.0 | 3,444 | 5/21/2020 |
2.6.0 | 960,918 | 4/15/2020 |
2.5.1 | 5,686 | 2/13/2020 |
2.5.0 | 6,720 | 1/12/2020 |
2.4.0 | 122,089 | 10/11/2019 |
2.3.0 | 29,397 | 5/16/2019 |
2.2.0 | 85,435 | 11/8/2018 |
2.1.0 | 16,412 | 10/2/2018 |
2.0.1 | 1,153 | 9/27/2018 |
2.0.0 | 1,104 | 9/26/2018 |
1.0.0 | 255,296 | 7/27/2018 |
0.5.0 | 1,568 | 6/15/2018 |
0.4.0 | 9,985 | 4/9/2018 |
0.3.0 | 3,083 | 3/8/2018 |
0.2.3 | 1,858 | 2/14/2018 |
0.2.2 | 2,650 | 1/8/2018 |
0.2.1 | 1,640 | 10/26/2017 |
0.2.0 | 1,127 | 10/25/2017 |
0.1.6 | 1,169 | 10/19/2017 |
0.1.5 | 1,140 | 10/18/2017 |
0.1.4 | 1,173 | 10/12/2017 |
0.1.3 | 1,147 | 10/11/2017 |
0.1.1 | 1,332 | 10/7/2017 |
ToBeReleased
- Target .NET 8.0. #252