您的位置:首页 > 编程语言

精确计算TFS中新增以及更改的代码行数

2013-10-25 17:38 375 查看
<configuration>
<configSections>
<sectionname="LOCTargets"
type="ConsoleApplication2.TFSConfigSection,TFSChangeLOC"/>
</configSections>
<appSettings>
<addkey="OutputFilePath"value="C:\Users\xingy\Desktop\LOCdemo.html"/>
</appSettings>
<LOCTargets>
<addLocalPath="C:\SPA_5150\Apps\IntelligentRewards\dev\source"
StartChangeSetId="373171"EndChangeSetId="373173"/>
<addLocalPath="C:\SPA_5150\Apps\Framework\source"
StartChangeSetId="373165"EndChangeSetId="373170"/>
</LOCTargets>
<startup>
<supportedRuntimeversion="v4.0"sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>


classProgram
{
staticintaddNumber=0;
staticintdelNumber=0;
staticinteditNumber=0;
staticintotherNumber=0;
staticintrenameNumber=0;
staticDictionary<int,int>changeSetDic=newDictionary<int,int>();
staticList<string>changedFiles=newList<string>();

staticvoidMain(string[]args)
{
Configurationconfig=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
TFSConfigSectionsection=config.Sections["LOCTargets"]asTFSConfigSection;
foreach(TargetlocTargetinsection.Instances)
{
TFS(locTarget.LocalPath,locTarget.StartChangeSetId,locTarget.EndChangeSetId);
}

ScanFiles();
OutputHtmlFile(changeSetDic);

Console.WriteLine("PressAnyKeytoExit......");
Console.Read();
}

privatestaticvoidTFS(stringlocalPath,stringstartId,stringendId)
{
UritfsUri=newUri("http://rnotfsat:8080/tfs");
stringtfsPath=null;

try
{
TfsConfigurationServerconfigurationServer=
TfsConfigurationServerFactory.GetConfigurationServer(tfsUri);

//Getthecatalogofteamprojectcollections
ReadOnlyCollection<CatalogNode>collectionNodes=configurationServer.CatalogNode.QueryChildren(
new[]{CatalogResourceTypes.ProjectCollection},
false,CatalogQueryOptions.None);

//Listtheteamprojectcollections
foreach(CatalogNodecollectionNodeincollectionNodes)
{
//UsetheInstanceIdpropertytogettheteamprojectcollection
GuidcollectionId=newGuid(collectionNode.Resource.Properties["InstanceId"]);
TfsTeamProjectCollectionteamProjectCollection=configurationServer.GetTeamProjectCollection(collectionId);

if(!teamProjectCollection.Name.Equals(@"rnotfsat\rnotfsat"))
continue;

varvcs=teamProjectCollection.GetService<VersionControlServer>();
varworkSpace=vcs.GetWorkspace(localPath);
tfsPath=workSpace.GetServerItemForLocalItem(localPath);

varchangesetList=vcs.QueryHistory(
tfsPath,
VersionSpec.Latest,
0,
RecursionType.Full,
null,
VersionSpec.ParseSingleSpec(startId,null),
VersionSpec.ParseSingleSpec(endId,null),
Int32.MaxValue,
true,
false).Cast<Changeset>();

foreach(varcsinchangesetList)
{
changeSetDic.Add(cs.ChangesetId,0);

varchangeList=cs.Changes;
foreach(varchangeinchangeList)
{
if(change.Item!=null)
{
varlocalFile=workSpace.GetLocalItemForServerItem(change.Item.ServerItem);
if(change.ChangeType.HasFlag(ChangeType.Edit))
{
if(!changedFiles.Contains(localFile))
{
changedFiles.Add(localFile);
}
editNumber++;
}

elseif(change.ChangeType.HasFlag(ChangeType.Add))
{
if(File.Exists(localFile))
{
changeSetDic[cs.ChangesetId]+=File.ReadLines(localFile).Count();
addNumber++;
}
}

elseif(change.ChangeType.HasFlag(ChangeType.Rename)||
change.ChangeType.HasFlag(ChangeType.SourceRename))
{
renameNumber++;
}

elseif(change.ChangeType.HasFlag(ChangeType.Delete))
{
delNumber++;
}

else
{
otherNumber++;
}
}
}
}
}
}
catch(ChangesetNotFoundException)
{
Console.WriteLine("!!Pleasecheckthechangesetidinsideyourconfigfile!!");
}
catch(Exceptione)
{
Console.WriteLine(e);
}
}

privatestaticvoidScanFiles()
{
Console.WriteLine("Intotal{0}fileswillbescanned!",changedFiles.Count);
Console.WriteLine();
Console.WriteLine();

intleft=changedFiles.Count;

foreach(varcsFileinchangedFiles)
{
GetContent(changeSetDic,csFile);
Console.WriteLine(csFile+"isscannedandanalyzed!");
left--;
Console.WriteLine("{0}filesleft.",left);
}
}

privatestaticvoidGetContent(Dictionary<int,int>csDic,stringfilePath)
{
Processproc=newProcess();
proc.StartInfo.FileName="tfpt";
proc.StartInfo.UseShellExecute=false;
proc.StartInfo.RedirectStandardOutput=true;
proc.StartInfo.Arguments="annotate\""+filePath+"\"/noprompt";
proc.Start();

StringBuildersb=newStringBuilder();
while(!proc.HasExited)
{
sb.Append(proc.StandardOutput.ReadToEnd());
}

stringallResult=sb.ToString();
String[]lines=allResult.Split(new[]{Environment.NewLine},Int32.MaxValue,StringSplitOptions.RemoveEmptyEntries);

foreach(varlineinlines)
{
varcsIdInStr=line.Split(default(string[]),StringSplitOptions.RemoveEmptyEntries);
intcsId=Convert.ToInt32(csIdInStr[0]);

if(csDic.ContainsKey(csId))
{
csDic[csId]++;
}
}
}

privatestaticvoidOutputHtmlFile(Dictionary<int,int>csDic)
{
stringhtmlSource=File.ReadAllText("HTMLTemplate.html");
inttotalCount=0;

StringBuildersb=newStringBuilder();
foreach(KeyValuePair<int,int>kvincsDic)
{
sb.Append("<tr><td>").Append(kv.Key).Append("</td><td>").Append(kv.Value).Append("</td></tr>");
totalCount+=kv.Value;
}

htmlSource=Regex.Replace(htmlSource,"yukunContent",sb.ToString());
htmlSource=Regex.Replace(htmlSource,"yukunTotal",totalCount.ToString());
htmlSource=Regex.Replace(htmlSource,"AddNumber",addNumber.ToString());
htmlSource=Regex.Replace(htmlSource,"EditNumber",editNumber.ToString());
htmlSource=Regex.Replace(htmlSource,"DeleteNumber",delNumber.ToString());
htmlSource=Regex.Replace(htmlSource,"OtherNumber",otherNumber.ToString());
htmlSource=Regex.Replace(htmlSource,"RenameNumber",renameNumber.ToString());
File.WriteAllText(ConfigurationManager.AppSettings["OutputFilePath"],htmlSource);
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐