conan-roslyn / Conan

Lightweight fork of the .NET Compiler Platform ("Roslyn") by adding a compiler plugin infrastructure
97 stars 3 forks source link
compiler-plugin dotnet roslyn roslyn-analyzer

Conan Build status

Conan is a lightweight fork of the .NET Compiler Platform ("Roslyn") by adding a compiler plugin infrastructure. These plugins can be deployed and installed as regular Diagnostic Analyzers.

Notice

This repository is under construction, a documentation will follow in the coming weeks

Usage

How to develop a Conan compiler plugin?

  1. Create a netstandard1.3 library
  2. Add the latest Conan.CodeAnalysis package (alpha5+)
  3. Create a new class an inherit from CompilationRewriter. See HelloWorld plugin example

       [DiagnosticAnalyzer(LanguageNames.CSharp)]
       public class MyCompilationRewriter : CompilationRewriter
       {
           public override Compilation Rewrite(CompilationRewriterContext context)
           {
               var compilation = context.Compilation;
    
               // Transform compilation
               ...
    
               return compilation;
           }
       }
  4. If you want to create a NuGet package for this plugin, you can add a reference to the NuGet package AnalyzerPack and it will transform automatically your package into a Diagnostic Analyzer NuGet package (when doing a dotnet/msbuild Pack)

How to use this plugin in your project?

  1. Add the package Conan.Net.Compilers to your project: This will make the Conan compiler as the default CSharp/VB compiler and replace the default Roslyn compiler (This package works for both Full framework and Core framework unlike the Roslyn packages)
  2. Add your plugin you developed either by:
    • Adding directly a reference to it into your csproj. This is what is used by the HelloWorld package:
      <ItemGroup>
         <Analyzer Include="..\Conan.Plugin.HelloWorld\bin\$(Configuration)\netstandard1.3\Conan.Plugin.HelloWorld.dll" />
      </ItemGroup>
    • Adding a reference to the NuGet package of your plugin (that has been through AnalyzerPack)
  3. If you compile your project, the plugin will be automatically loaded and executed, check the logs!

NuGet Packages

Their are 2 fundamental root packages in Conan:

Roslyn Conan NuGet
Microsoft.Net.Compilers
Microsoft.NETCore.Compilers
Conan.Net.Compilers NuGet
Microsoft.CodeAnalysis Conan.CodeAnalysis NuGet
• Microsoft.CodeAnalysis.Common Conan.CodeAnalysis.Common NuGet
• Microsoft.CodeAnalysis.CSharp Conan.CodeAnalysis.CSharp NuGet
• Microsoft.CodeAnalysis.CSharp.Workspaces Conan.CodeAnalysis.CSharp.Workspaces NuGet
• Microsoft.CodeAnalysis.VisualBasic Conan.CodeAnalysis.VisualBasic NuGet
• Microsoft.CodeAnalysis.VisualBasic.Workspaces Conan.CodeAnalysis.VisualBasic.Workspaces NuGet
• Microsoft.CodeAnalysis.Workspaces.Common Conan.CodeAnalysis.Workspaces.Common NuGet

Credits

All the people involved behind the .NET Compiler Platform ("Roslyn"). Conan is a very little addition to the huge work already done there.

The logo is called Lion created by Jennifer Keana from the Noun Project

Licensing

Same license than Roslyn: Apache-2.0

Author

Alexandre MUTEL aka xoofx