Question

I got a task which is need to check whether a specific class with Summary when it is checked-in on Team Foundation System.

I have found a way which is turn on the code analysis in the process of check-in, the problem is there is no Summary checking item in rules.

Is there any way to check each class whether with Summary during the check-in?

Is it possible to customize BuildprocessTemplate to make it?

can this checkin policy evaluate make it?

public override PolicyFailure[] Evaluate()
{

List<PolicyFailure> failures = new List<PolicyFailure>();
foreach(PendingChange pc in PendingCheckin.PendingChanges.CheckedPendingChanges)
{
    if(pc.LocalItem == null)
    {
        continue;
    }

    /* Open the file */
    using(FileStream fs = new FileStream(pc.FileName,FileMode.Open,FileAccess.Read))
    {
        StreamReader fs1 = new StreamReader(fs);

        string eachline= fs1.ReadLine();

        int PublicCount=0;
        int SummaryCount = 0;

        while(eachline !="")
        {
            if (eachline.IndexOf("/// <summary>")!=-1) 
            {
                SummaryCount++;
            }

            if (eachline.IndexOf("public")!=-1) 
            {
                PublicCount++;
            }

        }

        if(PublicCount != SummaryCount)
        {
            failures.Add(new PolicyFailure("Class Summary missing"));
        }


        fs.Close();
    }
}

return failures.ToArray();
}
Was it helpful?

Solution

I'd recommend against a custom TFS check in policy. They get evaluated on the client meaning a) they interfere with developer workflow b) they can get overridden on the client (and it can be difficult to get notifications when developers override the policy), and most importantly c) you need to manage getting the assembly with your custom policy on it onto your developer machines and keeping it up-to-date.

The best thing to do, I think, is to integrate StyleCop with MSBuild so that you get build warnings or errors if StyleCop detects issues. There's a handy nuget package to get you started. This gives you a lot of flexibility to enforce code style rules. You can use this alongside the last-build-successful policy, or better use Gated Checkins so that the evaluation happens on the server.

Bear in mind the following:-

  • If you want to fail the build because of StyleCop violations, you'll need to set <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings> in your project file.

  • From personal experience, I'd recommend only setting StyleCopTreatErrorsAsWarnings false on your release configuration(s). If your developers have to add xml comments before they can, say, check if something compiles, then you're going to have trouble!

  • You'll need to spend a little time setting up which StyleCop rules you want to enforce for your project. Make sure they get source-controlled along with the .sln - you don't want to have to mess around with them on individual developer machines.

  • Start with a small ruleset that's quite permissive and expand as you go.

  • Don't waste expensive programmer time manually reformatting code files to match the style guidelines. Get resharper and set it up so that the code cleanup function tidies things up correctly.

OTHER TIPS

  1. As Richard commented a way to check for this is writing a custom policy. A small tutorial can be found here.

  2. Another approach is to write a StyleCop-rule that checks classes if they have a summary. And use this stylecop-checkinpolicy with it.

You can NOT do this using Code Analysis because Code Analysis checks compiled code. During compilation the comments disappear and there is no way to check for them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top