All Projects → daveaglick → Buildalyzer

daveaglick / Buildalyzer

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

Projects that are alternatives of or similar to Buildalyzer

Natasha
基于 Roslyn 的 C# 动态程序集构建库,该库允许开发者在运行时使用 C# 代码构建域 / 程序集 / 类 / 结构体 / 枚举 / 接口 / 方法等,使得程序在运行的时候可以增加新的模块及功能。Natasha 集成了域管理/插件管理,可以实现域隔离,域卸载,热拔插等功能。 该库遵循完整的编译流程,提供完整的错误提示, 可自动添加引用,完善的数据结构构建模板让开发者只专注于程序集脚本的编写,兼容 stanadard2.0 / netcoreapp3.0+, 跨平台,统一、简便的链式 API。 且我们会尽快修复您的问题及回复您的 issue.
Stars: ✭ 705 (+99.15%)
Mutual labels:  hacktoberfest, roslyn
Uno
Build Mobile, Desktop and WebAssembly apps with C# and XAML. Today. Open source and professionally supported.
Stars: ✭ 6,029 (+1603.11%)
Mutual labels:  hacktoberfest, roslyn
Awesome Roslyn
Curated list of awesome Roslyn books, tutorials, open-source projects, analyzers, code fixes, refactorings, and source generators
Stars: ✭ 395 (+11.58%)
Mutual labels:  hacktoberfest, roslyn
Roslyn
The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
Stars: ✭ 15,296 (+4220.9%)
Mutual labels:  hacktoberfest, roslyn
Devops Guide
DevOps Guide - Development to Production all configurations with basic notes to debug efficiently.
Stars: ✭ 4,119 (+1063.56%)
Mutual labels:  hacktoberfest
Baystation12
The code for Baystation12's branch of SS13
Stars: ✭ 350 (-1.13%)
Mutual labels:  hacktoberfest
Exodus
Platform to audit trackers used by Android application
Stars: ✭ 349 (-1.41%)
Mutual labels:  hacktoberfest
Awesome Hacktoberfest 2020
A curated list of awesome Hacktoberfest 2020 repositories, guides and resources
Stars: ✭ 349 (-1.41%)
Mutual labels:  hacktoberfest
Open Sauced
🍕 This is a project to identify your next open source contribution.
Stars: ✭ 352 (-0.56%)
Mutual labels:  hacktoberfest
Rotten Scripts
Scripts that will make you go WOW 😍💻
Stars: ✭ 344 (-2.82%)
Mutual labels:  hacktoberfest
Itop
A simple, web based IT Service Management tool
Stars: ✭ 349 (-1.41%)
Mutual labels:  hacktoberfest
Core.js
Extendable client for GitHub's REST & GraphQL APIs
Stars: ✭ 344 (-2.82%)
Mutual labels:  hacktoberfest
Meteor Autocomplete
Client/server autocompletion designed for Meteor's collections and reactivity.
Stars: ✭ 352 (-0.56%)
Mutual labels:  hacktoberfest
Rethinkdb.driver
🎧 A NoSQL C#/.NET RethinkDB database driver with 100% ReQL API coverage.
Stars: ✭ 350 (-1.13%)
Mutual labels:  hacktoberfest
Node Html To Image
A Node.js module that generates images from HTML
Stars: ✭ 351 (-0.85%)
Mutual labels:  hacktoberfest
Larasail
LaraSail - Set Sail with your Laravel app on DigitalOcean
Stars: ✭ 348 (-1.69%)
Mutual labels:  hacktoberfest
Zwave2mqtt
Fully configurable Zwave to MQTT gateway and Control Panel using NodeJS and Vue
Stars: ✭ 352 (-0.56%)
Mutual labels:  hacktoberfest
Animatify Ios
Animation, Effects & Transitions for iOS
Stars: ✭ 350 (-1.13%)
Mutual labels:  hacktoberfest
Askql
AskQL is a query language that can express any data request
Stars: ✭ 352 (-0.56%)
Mutual labels:  hacktoberfest
Sharpen
Visual Studio extension that intelligently introduces new C# features into your existing codebase
Stars: ✭ 351 (-0.85%)
Mutual labels:  hacktoberfest

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].