0

找出 TFS 集合中总代码行的最佳方法是什么?我是否需要以某种方式使用 SDK 来执行此操作?或者有没有报告工具?

4

3 回答 3

1

我不知道最好的方法,但是您几乎可以肯定必须将源代码放入工作区,然后运行您选择的工具来计算“行”(取决于您认为是“代码行”的内容) .

计算源文件中的行数的工具没有尽头,而且自己编写一个也很简单,所以我不会尝试详细介绍这部分问题。

因此,另一部分是手动将源代码获取到您的 PC,或使用tf.exe从批处理文件或类似文件中自动执行获取过程。棘手的一点是找出相当不友好的 tf 命令行,但如果您仔细阅读文档,这是一个非常容易实现的任务。

于 2012-12-14T19:47:57.307 回答
0

我承认我在试图找到一个很好的理由时不知所措,但如果你得到了整个集合,那么你可以计算每个文件中包含代码的类型的行数( *.cs、vb、aspx 等)

许多工具可以计算行数,但如果您需要自己滚动,您可以尝试计算诸如“.+\n”之类的正则表达式的出现次数。

于 2012-12-15T13:29:24.320 回答
-1

I had written this many years ago. It can work on local folders though, so if you have a local copy of the TFS code, it can still work. Not the best quality code, but just a quick and dirty way to get a report in the grid that you can copy to Excel (again, not the best automation, but gets the job done)-

[Add a few controls that are needed in the forms app]

    private static int totalLinesCount = 0;
    private static int totalLinesOfCode = 0;
    private static int totalComments = 0;

    private void btnCount_Click(object sender, EventArgs e)
    {
        try
        {
            totalLinesCount = 0;
            totalLinesOfCode = 0;
            totalComments = 0;
            lblTotalCount.Text = "";
            DirectoryInfo di = new DirectoryInfo(txtFileName.Text);
            if (di.Exists)
            {
                FileInfo[] fis = di.GetFiles(txtSearchPattern.Text, SearchOption.AllDirectories);
                rtbReport.Text = "";
                Dictionary<string, int> dictionary = new Dictionary<string, int>();
                DataSet ds = new DataSet("Report");
                DataTable dt = new DataTable();
                dt.Columns.Add("FileName", typeof(string));
                dt.Columns.Add("TotalCount", typeof(string));
                dt.Columns.Add("Code", typeof(string));
                dt.Columns.Add("Commented", typeof(string));
                dt.Columns.Add("Summary", typeof(string));
                ds.Tables.Add(dt);
                foreach (FileInfo fi in fis)
                {
                    if (fi.Exists)
                    {
                        int fileLinesCount = File.ReadAllLines(fi.FullName).Length;
                        int commentedCode = 0;

                        foreach (string line in File.ReadLines(fi.FullName))
                        {
                            if (line.TrimStart().StartsWith("/") || (line.TrimStart().StartsWith("*")))
                            {
                                commentedCode++;
                            }
                        }

                        rtbReport.Text += string.Format("{0}: {1}; Actual Code: {2}; Commented lines: {3};{4}",
                            fi.Name, fileLinesCount.ToString(), fileLinesCount - commentedCode, commentedCode,"\n");
                        totalLinesCount += fileLinesCount;
                        totalComments += commentedCode;

                        DataRow dr = ds.Tables[0].NewRow();
                        dr["FileName"] = fi.Name;
                        dr["TotalCount"] = fileLinesCount;
                        dr["Code"] = fileLinesCount - commentedCode;
                        dr["Commented"] = commentedCode;
                        dr["Summary"] = string.Format("Code: {0}, Commented: {1}, Total: {2}",
                            fileLinesCount-commentedCode, commentedCode, fileLinesCount);

                        ds.Tables[0].Rows.Add(dr);
                    }
                }
                if (ds.Tables.Count > 0)
                {
                    dataGridView1.DataSource = ds.Tables[0].DefaultView;

                    dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
                    dataGridView1.Columns[0].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
                    dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
                }

                totalLinesOfCode = totalLinesCount - totalComments;

                lblTotalCount.Text = string.Format("{0}: {1}; Code: {2}; Comments: {3}",
                    "Total Number of lines in all files", totalLinesCount.ToString(),
                    totalLinesOfCode.ToString(), totalComments.ToString());
                rtbReport.Text += lblTotalCount.Text;
            }
            else
                MessageBox.Show("Folder does not exist. Select a valid folder",
                    "Folder not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
于 2014-12-16T03:01:27.710 回答