StyleCop.Analyzers is a handy NuGet package for keeping your code neat and tidy. However, Unity removes it every time it compiles. Does anyone know a good way to get these two to play nice?
Hi, I've managed to integrate StyleCop with Unity but only partially. The problem is that I still don't know how to make Visual Studio remember my rulesets or keep the stylecop.json file in the project. Please, if you have an idea on how to solve that, any help would mean a lot. Here's what I did: Installation: Open Visual Studio Open the Solution for your Unity Project Navigate to Tools -> NuGet Package manager -> Package Manager Settings Inside of that window look for General -> Package Management -> Default Package Management Format -> Set it's value to PackageReference (This makes sure that you don't create the Settings file that Unity deletes) Close that window and navigate to Tools -> NuGet Package Manager -> Package Manager Console Type in the following command: "Install-Package StyleCop.Analyzers" (if for some reason it doesn't work, check the following link to see if it has any help on it: https://github.com/DotNetAnalyzers/StyleCopAnalyzers Installation has been completed. Configuration is the next step Configuration: This is where things get tricky. I'll split the configuration into two major parts. One for the Rulesets and the other for the stylecop.json IMPORTANT: Since Unity regenerates your project files every time it starts, part of this process will have to be repeated every time you start Unity. Fear not, it's not as bad as it sounds. Rulesets Configuration (First time only) If this is NOT the first time you are doing this you can go ahead and skip this section. Create a new project from Visual Studio (It might also be possible to do this part from a solution that Unity uses but it's tricky at times so I recommend doing it this way for the first time). Navigate to the Solution Explorer window and right click on the Project you just Created After that, you should go to Code Analysis -> Rule Set -> Run this rule set -> Click on the dropdown and select browse Now this is the part where you tell Visual Studio where to look for .ruleset file. You should download one from https://github.com/DotNetAnalyzers/StyleCopAnalyzers and place it wherever on your pc. (Just download the .zip and look for .ruleset inside. DO NOT use the .internal one). Confirm and now you should have a reference to the .ruleset file that uses StyleCop rules Note: Sorry if this could have been pulled of a bit cleaner. If you figure out a cleaner way please let me know. Now that you are done with that you can go ahead and delete the temporary project you just created. We needed only a link to the .ruleset file that we will be using from now on. If you completed this process successfully you shouldn't have to repeat it ever again. Rulesets Configuration (Per Unity Start) We are almost done. Stick with me here. Keep in mind that this section WILL have to be repeated every time you run Unity. Turn on Unity and open any .cs file Once again navigate to the Solution Explorer Window Right click on the Solution and select Properties Navigate to Code Analysis Settings -> Rule Set for the [Your project assembly] -> Change it to Rules for StyleCop.Analyzers. (If you don't see that option, you missed something in the First Time Configuration) That's it. Your code should now support StyleCop for the most part. Next time you start Unity just make sure you tell it to use the proper ruleset again. Detailed Configuration (Stylecop.json) This part is a bit tedious and I wouldn't blame you if you skipped it. Most people won't need this. Open any .cs file in your Unity project Place your cursor at the top of the file and press Ctrl + . A new menu will appear. Select "Add StyleCop Settings file to your project" Set it to "Additional Files" in the window right under the Solution Explorer (Select stylecop.json if you don't see it). The settings file has been generated and you should be able to use it properly. There is a huge issue here. Since I haven't discovered how to tell StyleCop to place the generated file elsewhere Unity WILL DELETE IT. There's nothing I found that prevents this. If you really need this file, copy it's contents every time you make a change and keep a backup on your PC somewhere. Next time you create the file just paste the contents from the backup into the main file. More info on adding stylecop.json can be found on the GitHub webpage. That is all that I've learned. Please, and I can't stress this enough, if you find a more efficient way to handle any of these things please let me know. I think I've pretty much tried everything at this point.
I have a package for adding any rosyln analyzer to Unity, including stylecop. https://github.com/tertle/com.bovinelabs.analyzers/ I just automatically update the project file references every time they are regenerated https://github.com/tertle/com.bovinelabs.analyzers/blob/master/ProjectFilesGeneration.cs Just drop any .dll, .json or .ruleset file into the RoslynAnalyzers folder and it'll reference them automatically for every project in unity. Pretty much it just runs this loop Code (CSharp): foreach (var file in relPaths) { var extension = new FileInfo(file).Extension; switch (extension) { case ".dll": { var reference = new XElement(xmlns + "Analyzer"); reference.Add(new XAttribute("Include", file)); itemGroup.Add(reference); break; } case ".json": { var reference = new XElement(xmlns + "AdditionalFiles"); reference.Add(new XAttribute("Include", file)); itemGroup.Add(reference); break; } case ".ruleset": { SetOrUpdateProperty(projectContentElement, xmlns, "CodeAnalysisRuleSet", existing => file); break; } } }
The answer @tertle provided is what worked for me. I've since written a tutorial for setting up stylecop using an asset post processor with complete explanation of the different steps and the code you need to set it up. https://www.bussmann.io/blog/using-stylecop-analyzers-with-unity Hope this helps someone in the future