How to uninstall dll from nuget package?
If you’ve used the default template to create your analyzer project then you don’t need to worry because it is already configured to produce a nuget package upon building. If you have followed The #Pro way then you have to do some work to create a nuget package that will work properly for distributing your analyzer.
See analyzers conventions for official documentation about packaging your analyzers.
All that is explained below was discovered from looking at how the default template generates the nuget package. In short by analyzing the nuspec file and the scripts inside the tools folder.
Using a nuspec file¶
If you are using a .NET Core or .NET Standard project for your analyzers consider using the support for creating NuGet packages present in the csproj config.
There’s lot of metadata that you can configure on the nuspec file but to create nuget packages for analyzers you have to at least set the below metadata and files:
As explained in the section analyzers path format you can use more complex folder structures to target different languages. The line from the above config that is setting the framework to dotnet and the language to C# is:
The second file line:
Makes sure the nuget package will contain the custom install and uninstall scripts that are required to add/remove the analyzer dll in the correct way when the nuget package is installed/removed.
Nuget Powershell Scripts¶
You will need to create a folder in the root directory of your analyzer project and add two scripts to it:
The scripts that I’ve used were a copy of the ones produced by the default template to create an analyzer project. However you can use the ones from the official documentation.
They both do the same which is add all the dll files from your nuget package into the project’s analyzer references:
The uninstall script does the reverse by removing the analyzer dlls from the project’s analyzer references.
There is a small difference between the scripts from the default template and the ones in the official documentation. The scripts from the default template don’t just get all the dlls from the nuget package. It gets all the dlls except the resource ones as you shown by:
I have not tested but this line of the powershell script should only impact projects that are localized . Altough the official documentation in not excluding the resource files it is probably the correct thing to do but I have not tested it.
Using support from the csproj¶
If you are using .NET Core or .NET Standard project for your analyzers then you can make use of the csproj support to create NuGet packages. Here is an example of a csproj file configured to create a NuGet package for a .NET Standard analyzer project:
For more information on the csproj configuration see additions to the csproj format and more specifically NuGet metadata properties.
The parts that are worth calling out on this csproj config are:
The GeneratePackageOnBuild property will determine if a NuGet package is created as part of building the project:
The NoPackageAnalysis will ignore warnings from building the NuGet package:
The PackageReference lines will make sure that the NuGet package created has no dependencies. Otherwise when consuming the NuGet you would be required to install Microsoft.CodeAnalysis.CSharp.Workspaces and NETStandard.Library:
You also need to make sure the NuGet package will contain the custom install and uninstall scripts that are required to add/remove the analyzer dll in the correct way when the nuget package is installed/removed. You will need to create a tools folder with the required scripts for this to work. The line responsible for this is:
By default a NuGet package will contain the assembly inside a lib folder. However NuGet for analyzers follow a different convention as explained in the section analyzers path format. In summary for a C# analyzer the assembly packaged in the NuGet should be in the folder analyzers/dotnet/cs. The line responsible for this is:
I have not tested packaging a localized analyzer so there might be an extra step necessary to make the localization work with the NuGet package. See this note about the install scripts .
© Copyright 2017, Eduardo Serrano. Revision 3c7edb40 .