2

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();
}
Kun-Yao Wang
  • 192
  • 1
  • 3
  • 16
  • Your best bet is likely to be a custom TFS Check In Policy. A quick [MSDN search](http://social.msdn.microsoft.com/Search/en-US?query=tfs%20check%20in%20policy&emptyWatermark=true&ac=2) has lots of useful links, including to existing custom ones. – Richard May 08 '14 at 08:42
  • A non-technical solution: Make this something that you check during code reviews. – Daniel Mann May 08 '14 at 14:14
  • @DanielMann hehe that is a good idea, but you know, we are a team work, my boss can not ensure everyone can make it before check-in. – Kun-Yao Wang May 08 '14 at 14:30
  • Do you use branching? Allow the developers to check code without summaries into the Development branch, but don't merge to the Main branch until the code has passed code review. – John Saunders May 09 '14 at 02:54
  • 1
    I've written a policy which is able to do this: http://cdcp.codeplex.com – Scordo May 09 '14 at 07:26

2 Answers2

5

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.

Community
  • 1
  • 1
Iain Galloway
  • 18,669
  • 6
  • 52
  • 73
  • Thanks for your replay, so if I use StyleCop to make a validation which means I will need to install that on my server. Is that possible to write a code to check it? (But not create a new check-in Policy). as you list those disadvantage, it won't be a good idea. – Kun-Yao Wang May 08 '14 at 14:29
  • If you use the NuGet package, you don't need to install StyleCop on the server. You just need to either check in the directory under "packages", or enable NuGet package restore. – Iain Galloway May 08 '14 at 15:22
  • Is it possible to customize DefaultTemplate.11.1.xaml in BuildProcessTemplate folder to make it? – Kun-Yao Wang May 09 '14 at 01:54
  • I found this question and response, do you think this is a way to make it? http://stackoverflow.com/questions/8821614/custom-checkin-policy-access-to-filecontent-from-changeset-files I found how many class and summary in each file and compare each number whether the same – Kun-Yao Wang May 09 '14 at 05:59
  • Do you know whether there is Stylecop checkin policy for 2012 TFS? – Kun-Yao Wang May 12 '14 at 06:41
  • I'm sure there is a StyleCop checkin policy, but I haven't used it. Did you read the links in my second paragraph:- http://blogs.msdn.com/b/sourceanalysis/archive/2008/05/24/source-analysis-msbuild-integration.aspx and http://www.nuget.org/packages/StyleCop.MSBuild/ – Iain Galloway May 12 '14 at 07:36
  • The NuGet package is the easiest way to do it. You don't need to customize your Build Process Template. – Iain Galloway May 12 '14 at 07:38
  • Yes,I did! Thanks. But I am confused why StyleCop for TFS is only for 2010 and 2013, it has install file, but I can not find 2012 and as u shared, it can be only done on 2012 by plus extra code – Kun-Yao Wang May 12 '14 at 08:00
  • If you use the NuGet package, you don't need to install StyleCop on the server. – Iain Galloway May 12 '14 at 08:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52500/discussion-between-kun-yao-wang-and-iain-galloway) – Kun-Yao Wang May 12 '14 at 08:13
1
  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.

Matthijs
  • 3,162
  • 4
  • 25
  • 46
  • I think I have to choice the first one you said, and I read the tutorial you provide, but I found out it writes "The policy is evaluated on the client when a developer checks in a file", but I would like to file be checked by the TFS Server, is it possible? – Kun-Yao Wang May 08 '14 at 09:26
  • A check-in policy is a mechanism that stops you from checking-in code if your code doesn't comply to the policy. So, in your case, if a class does not have a summary, you can not check in. Bear in mind that you can ignore the policy so it is not entirely failsafe. – Matthijs May 08 '14 at 09:51