[小工具]代码统计小工具编写

  1. 云栖社区>
  2. 博客>
  3. 正文

[小工具]代码统计小工具编写

蓬莱仙羽 2017-11-01 18:54:53 浏览740
展开阅读全文

代码小工具是有一定工作经验并且有更高追求的程序的必备技能,今天加班到早晨五六点,到家都七点了,回到家倒头就睡,下午来公司感觉头还有点晕乎,工作的话怕只会产生更多的代码,就想起来写个工程代码统计工具。

效果图

这里写图片描述

上图是平时积累的小工具,积累我认识也是程序员必备的素养!

主要代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CSProjectCodeLine
{
    class Program
    {
        static int Main(string[] args)
        {
            int result = 0;
            try
            {
                Console.WriteLine("===============Aladdin Tool================");
                Console.WriteLine("参数:打表配置文件名或打表配置文件所在的目录");
                Console.WriteLine("===========================================");
                if (args.Length == 0)
                {
                    Console.WriteLine("Error:请填写路径参数");
                    Console.ReadKey();
                    result = 1;
                }
                else if (args.Length == 1)
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(args[0]);
                    if (dirInfo == null)
                    {
                        Console.WriteLine("Error:路径有误");
                        Console.ReadKey();
                        result = 1;
                    }
                    else
                    {
                        CodeCounter counter = new CodeCounter();
                        counter.GetProjectResult(dirInfo.FullName);
                        int codenumber = counter.CodeLines;
                        int filenumber = counter.FileNumber;
                        Console.WriteLine("项目中共有cs代码文件" + filenumber + "个");
                        Console.WriteLine("项目中共有代码" + codenumber + "行");
                        Console.ReadKey();
                        result = 0;
                    }
                }
                else
                {
                    Console.WriteLine("Error:参数只支持一个工程路径");
                    Console.ReadKey();
                    result = 1;
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ((Console.ForegroundColor == ConsoleColor.Red) ? ConsoleColor.Green : ConsoleColor.Red);
                Console.WriteLine(ex.ToString());
                Console.ResetColor();
                result = 1;
            }
            return result;
        }
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CSProjectCodeLine
{
    class CodeCounter
    {
        private int codeLines;

        /// <summary>
        /// 代码行数
        /// </summary>
        public int CodeLines
        {
            get
            {
                return codeLines;
            }

            set
            {
                codeLines = value;
            }
        }

        private int commentLines;

        /// <summary>
        /// 注释行数
        /// </summary>
        public int CommentLines
        {
            get
            {
                return commentLines;
            }

            set
            {
                commentLines = value;
            }
        }

        private int fileNumber;

        /// <summary>
        /// 文件个数
        /// </summary>
        public int FileNumber
        {
            get
            {
                return fileNumber;
            }

            set
            {
                fileNumber = value;
            }
        }

        public CodeCounter()
        {
            codeLines = 0;
            commentLines = 0;
            fileNumber = 0;
        }

        /// <summary>
        /// 获取整个项目的代码统计情况
        /// </summary>
        /// <param name="folderPath"></param>
        public void GetProjectResult(string folderPath)
        {
            DirectoryInfo folder = new DirectoryInfo(folderPath);
            try
            {
                var folders = folder.GetDirectories();
                for (int i = 0; i < folders.Length; i++)
                {
                    //递归调用 有时候会碰到目录权限问题
                    GetProjectResult(folders[i].FullName);
                }

                var csFiles = folder.GetFiles("*.cs");
                for (int i = 0; i < csFiles.Length; i++)
                {
                    fileNumber++;
                    string fieleName = folderPath + "\\" + csFiles[i].Name;
                    GetCodeFileResult(fieleName);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 单个文件代码统计情况
        /// </summary>
        /// <param name="fileName"></param>
        public void GetCodeFileResult(string fileName)
        {
            string line;
            StreamReader sr = new StreamReader(fileName);
            while ((line = sr.ReadLine()) != null)
            {
                codeLines++;
            }
        }
    }
}

批处理

@echo off
Set BatDir=%~dp0
set/p path=请输入你的工程路径:

Set ToolPath=%BatDir%..\CodeCounter\CSProjectCodeLine.exe
call %ToolPath% %path%
echo.=====检查完毕====

使用方法看上面效果图

工程下载

https://gitee.com/dingxiaowei/CodeCounter

网友评论

登录后评论
0/500
评论
蓬莱仙羽
+ 关注