This plugin shows how to develop a very basic HelloWorld Conan Plugin.
When used with Conan Compiler and in a project that has a main method, it will automatically insert at the beginning of the method a Console.WriteLine(""Hello World from Conan!");
- Checkout this repository
- Open and build the solution
src\TestHelloWorld.sln - Run the project
TestHelloWorldwhich contains the followingProgram.cs:using System; namespace TestHelloWorld { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); } } }
- You should see the following output:
CSC : warning HW0001: Main method successfuly modified by the HelloWorld Conan Plugin (See modified file at: ...) - If you run the program, you will see the following output:

The project Conan.Plugin.HelloWorld is using the package Conan.CodeAnalysis and is providing a HelloWorldCompilationRewriter which is implementing a CompilationRewriter (provided by the Conan compiler)
Note like for diagnostic analyzers, the project must be targeting
netstandard1.3
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class HelloWorldCompilationRewriter : CompilationRewriter
{
public override Compilation Rewrite(CompilationRewriterContext context)
{
var compilation = context.Compilation;
// Transform compilation
...
return compilation;
}
}A CompilationRewriter inherits from DiagnosticAnalyzer so it will show up in your project as an analyzer (while it is a compilation rewriter):
The project TestHelloWorld is using the package Conan.Net.Compilers that allows to load the compilation rewriter plugin Conan.Plugin.HelloWorld, and modify the ongoing compilation.
The Conan.Plugin.HelloWorld is manually added to the TestHelloWorld.csproj project to be able to work in a local scenario:
<ItemGroup>
<Analyzer Include="..\Conan.Plugin.HelloWorld\bin\$(Configuration)\netstandard1.3\Conan.Plugin.HelloWorld.dll" />
</ItemGroup>In a scenario where the Conan.Plugin.HelloWorld would be deployed on NuGet, you would not have to use this but simply reference the plugin with Private Asset:
<ItemGroup>
<PackageReference Include="Conan.Plugin.HelloWorld" Version="1.0.0" PrivateAssets="all" />
</ItemGroup>This software is released under the BSD-Clause 2 license.
Alexandre MUTEL aka xoofx
