Search Unity

Unity and StyleCop.Analyzers

Discussion in 'Scripting' started by Afropenguinn, Mar 6, 2019.

  1. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    294
    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?
     
    kthrose likes this.
  2. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    294
    I realize this is probably a pretty niche topic, but any help would be greatly appreciated!
     
  3. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    436
    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:
    1. Open Visual Studio
    2. Open the Solution for your Unity Project
    3. Navigate to Tools -> NuGet Package manager -> Package Manager Settings
    4. 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)
    5. Close that window and navigate to Tools -> NuGet Package Manager -> Package Manager Console
    6. 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
    7. 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.
    1. 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).
    2. Navigate to the Solution Explorer window and right click on the Project you just Created
    3. After that, you should go to Code Analysis -> Rule Set -> Run this rule set -> Click on the dropdown and select browse
    4. 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).
    5. 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.
    1. Turn on Unity and open any .cs file
    2. Once again navigate to the Solution Explorer Window
    3. Right click on the Solution and select Properties
    4. 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)
    5. 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.
    1. Open any .cs file in your Unity project
    2. Place your cursor at the top of the file and press Ctrl + .
    3. A new menu will appear. Select "Add StyleCop Settings file to your project"
    4. Set it to "Additional Files" in the window right under the Solution Explorer
      (Select stylecop.json if you don't see it).
    5. 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.
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,068
    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):
    1.             foreach (var file in relPaths)
    2.             {
    3.                 var extension = new FileInfo(file).Extension;
    4.  
    5.                 switch (extension)
    6.                 {
    7.                     case ".dll":
    8.                     {
    9.                         var reference = new XElement(xmlns + "Analyzer");
    10.                         reference.Add(new XAttribute("Include", file));
    11.                         itemGroup.Add(reference);
    12.                         break;
    13.                     }
    14.  
    15.                     case ".json":
    16.                     {
    17.                         var reference = new XElement(xmlns + "AdditionalFiles");
    18.                         reference.Add(new XAttribute("Include", file));
    19.                         itemGroup.Add(reference);
    20.                         break;
    21.                     }
    22.  
    23.                     case ".ruleset":
    24.                     {
    25.                         SetOrUpdateProperty(projectContentElement, xmlns, "CodeAnalysisRuleSet", existing => file);
    26.                         break;
    27.                     }
    28.                 }
    29.             }
     
    Last edited: Mar 14, 2019
    AKocourek and SolidAlloy like this.
  5. freb97

    freb97

    Joined:
    Mar 19, 2020
    Posts:
    2
unityunity